libxcoder  5.2.0
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 
997 // open netint p2p driver and fill the pcie address to p_ctx
998 static ni_retcode_t p2p_fill_pcie_address(ni_session_context_t *p_ctx)
999 {
1000 #ifdef _WIN32
1001  (void)p_ctx;
1002  return NI_RETCODE_FAILURE;
1003 #else
1004  int ret = 0;
1005  char line[256];
1006  char syspath[256];
1007  struct stat bstat;
1008  char *p_dev;
1009  char *dom, *bus, *dev, *fnc;
1010  FILE *fp;
1011 
1012  if(!p_ctx)
1013  {
1014  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() parameter is NULL\n",__func__);
1015  return NI_RETCODE_FAILURE;
1016  }
1017 
1018  p_ctx->netint_fd = open("/dev/netint", O_RDWR);
1019  if (p_ctx->netint_fd < 0)
1020  {
1021  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: Can't open device /dev/netint\n");
1022  return NI_RETCODE_FAILURE;
1023  }
1024 
1025  p_dev = &p_ctx->dev_xcoder_name[0];
1026  if (stat(p_dev, &bstat) < 0)
1027  {
1028  ni_log2(p_ctx, NI_LOG_ERROR, "failed to get stat of file %s\n", p_dev);
1029  return NI_RETCODE_FAILURE;
1030  }
1031 
1032  if ((bstat.st_mode & S_IFMT) != S_IFBLK)
1033  {
1034  ni_log2(p_ctx, NI_LOG_ERROR, "%s is not a block device\n", p_dev);
1035  return NI_RETCODE_FAILURE;
1036  }
1037 
1038 #ifdef _ANDROID
1039  ret = snprintf(syspath, sizeof(syspath) - 1,
1040  "/sys/block/%s/device/address",
1041  p_dev + 5);
1042  syspath[ret] = '\0';
1043 
1044  fp = fopen(syspath, "r");
1045 #else
1046  ret = snprintf(syspath, sizeof(syspath) - 1,
1047  "udevadm info -q path -n %s | perl -nle'print $& "
1048  "while m{(?<=/)[0-9a-f]{4}:[0-9a-f]{2}:[0-9a-f]{2}\\.[0-9a-f]}g' | tail -n 1",
1049  p_dev + 5);
1050  syspath[ret] = '\0';
1051 
1052  fp = popen(syspath, "r");
1053 #endif
1054 
1055  if (fp == NULL)
1056  {
1057  ni_log2(p_ctx, NI_LOG_ERROR, "Failed to read address\n");
1058  return NI_RETCODE_FAILURE;
1059  }
1060 
1061  if (fgets(line, 256, fp) == NULL)
1062  {
1063  ni_log2(p_ctx, NI_LOG_ERROR, "Failed to read line from address\n");
1064 #ifdef _ANDROID
1065  fclose(fp);
1066 #else
1067  pclose(fp);
1068 #endif
1069  return NI_RETCODE_FAILURE;
1070  }
1071 
1072 #ifdef _ANDROID
1073  fclose(fp);
1074 #else
1075  pclose(fp);
1076 #endif
1077 
1078  errno = 0;
1079  p_ctx->domain = strtoul(line, &dom, 16);
1080  if (errno < 0)
1081  {
1082  ni_log2(p_ctx, NI_LOG_ERROR, "Failed to read PCI domain\n");
1083  return NI_RETCODE_FAILURE;
1084  }
1085 
1086  errno = 0;
1087  p_ctx->bus = strtoul(dom + 1, &bus, 16);
1088  if (errno < 0)
1089  {
1090  ni_log2(p_ctx, NI_LOG_ERROR, "Failed to read PCI bus\n");
1091  return NI_RETCODE_FAILURE;
1092  }
1093 
1094  errno = 0;
1095  p_ctx->dev = strtoul(bus + 1, &dev, 16);
1096 
1097  if (errno < 0)
1098  {
1099  ni_log2(p_ctx, NI_LOG_ERROR, "Failed to read PCI device\n");
1100  return NI_RETCODE_FAILURE;
1101  }
1102 
1103  errno = 0;
1104  p_ctx->fn = strtoul(dev + 1, &fnc, 16);
1105 
1106  if (errno < 0)
1107  {
1108  ni_log2(p_ctx, NI_LOG_ERROR, "Falied to read PCI function\n");
1109  return NI_RETCODE_FAILURE;
1110  }
1111 
1112  ni_log2(p_ctx, NI_LOG_DEBUG, "PCI slot = %d:%d:%d:%d\n", p_ctx->domain,
1113  p_ctx->bus, p_ctx->dev, p_ctx->fn);
1114 
1115  return NI_RETCODE_SUCCESS;
1116 
1117 #endif
1118 }
1119 
1120 #if __linux__ || __APPLE__
1121 #if !defined(_ANDROID) && !defined(__OPENHARMONY__)
1122 #ifndef DISABLE_BACKTRACE_PRINT
1123 void ni_print_backtrace() {
1124  void* callstack[128];
1125  int frames = backtrace(callstack, 128);
1126  char** strs = backtrace_symbols(callstack, frames);
1127 
1128  ni_log(NI_LOG_ERROR, "Call stack:\n");
1129  for (int i = 0; i < frames; ++i) {
1130  ni_log(NI_LOG_ERROR, "%s\n", strs[i]);
1131  }
1132 
1133  free(strs);
1134 }
1135 #endif
1136 #endif
1137 #endif
1138 
1139 /*!******************************************************************************
1140  * \brief Open a xcoder decoder instance
1141  *
1142  * \param
1143  *
1144  * \return
1145 *******************************************************************************/
1147 {
1149  ni_xcoder_params_t *p_param = NULL;
1150  void* p_buffer = NULL;
1151  uint32_t ui32LBA = 0;
1152  char fmt_fw_api_ver1[5], fmt_fw_api_ver2[5];
1153 
1154  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
1155 
1156  if (!p_ctx)
1157  {
1158  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): passed parameters are null!, return\n",
1159  __func__);
1160  retval = NI_RETCODE_INVALID_PARAM;
1161  LRETURN;
1162  }
1163 
1164  //Create the session if the create session flag is set
1165  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
1166  {
1168  p_ctx->pts_table = NULL;
1169  p_ctx->dts_queue = NULL;
1170  p_ctx->p_leftover = NULL;
1171  p_ctx->buffer_pool = NULL;
1172  p_ctx->dec_fme_buf_pool = NULL;
1173  p_ctx->prev_size = 0;
1174  p_ctx->sent_size = 0;
1175  p_ctx->status = 0;
1176  p_ctx->key_frame_type = 0;
1177  p_ctx->ready_to_close = 0;
1178  p_ctx->max_retry_fail_count[0] = p_ctx->max_retry_fail_count[1] = 0;
1179  p_ctx->rc_error_count = 0;
1180  p_ctx->frame_num = 0;
1181  p_ctx->pkt_num = 0;
1182  p_ctx->pkt_index = 0;
1183  p_ctx->session_timestamp = 0;
1184  p_ctx->is_dec_pkt_512_aligned = 0;
1185  p_ctx->p_all_zero_buf = NULL;
1186  p_ctx->last_pkt_pos = 0;
1187  p_ctx->last_frame_offset = 0;
1188  memset(p_ctx->pkt_custom_sei_set, 0, NI_FIFO_SZ * sizeof(ni_custom_sei_set_t *));
1189 
1190  //malloc zero data buffer
1191  if (ni_posix_memalign(&p_ctx->p_all_zero_buf, sysconf(_SC_PAGESIZE),
1193  {
1194  ni_log2(p_ctx, NI_LOG_ERROR,
1195  "ERROR %d: %s() alloc decoder all zero buffer failed\n",
1196  NI_ERRNO, __func__);
1197  retval = NI_RETCODE_ERROR_MEM_ALOC;
1198  LRETURN;
1199  }
1200  memset(p_ctx->p_all_zero_buf, 0, NI_DATA_BUFFER_LEN);
1201 
1202  //malloc data buffer
1203  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), NI_DATA_BUFFER_LEN))
1204  {
1205  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() alloc data buffer failed\n",
1206  NI_ERRNO, __func__);
1208  retval = NI_RETCODE_ERROR_MEM_ALOC;
1209  LRETURN;
1210  }
1211  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
1212 
1213  //Set session ID to be invalid. In case we cannot open session, the session id wold remain invalid.
1214  //In case we can open sesison, the session id would become valid.
1215  ((ni_session_stats_t *)p_buffer)->ui16SessionId =
1216  (uint16_t)NI_INVALID_SESSION_ID;
1217 
1218  // First uint32_t is either an invaild session ID or a valid session ID, depending on if session could be opened
1219  ui32LBA = OPEN_SESSION_CODEC(NI_DEVICE_TYPE_DECODER, ni_htonl(p_ctx->codec_format), p_ctx->hw_action);
1220  retval = ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
1221  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
1222  if (retval != NI_RETCODE_SUCCESS)
1223  {
1224  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR ni_nvme_send_read_cmd\n");
1225  LRETURN;
1226  }
1227  //Open will return a session status structure with a valid session id if it worked.
1228  //Otherwise the invalid session id set before the open command will stay
1229  p_ctx->session_id = ni_ntohs(((ni_session_stats_t *)p_buffer)->ui16SessionId);
1230  p_ctx->session_timestamp = ni_htonl(((ni_session_stats_t *)p_buffer)->ui32Session_timestamp_high);
1231  p_ctx->session_timestamp = (p_ctx->session_timestamp << 32) |
1232  ni_htonl(((ni_session_stats_t *)p_buffer)->ui32Session_timestamp_low);
1233  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
1234  {
1235  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): p_ctx->device_handle=0x%" PRIx64 ", "
1236  "p_ctx->hw_id=%d, p_ctx->session_id=%d\n", __func__,
1237  (int64_t)p_ctx->device_handle, p_ctx->hw_id, p_ctx->session_id);
1239  LRETURN;
1240  }
1241  ni_log2(p_ctx, NI_LOG_DEBUG, "Decoder open session ID:0x%x, timestamp:%" PRIu64 "\n",
1242  p_ctx->session_id, p_ctx->session_timestamp);
1243 
1244  //Send keep alive timeout Info
1245  uint64_t keep_alive_timeout =
1246  p_ctx->keep_alive_timeout * 1000000; //send us to FW
1247  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
1248  memcpy(p_buffer, &keep_alive_timeout, sizeof(keep_alive_timeout));
1249  ni_log2(p_ctx, NI_LOG_DEBUG, "%s keep_alive_timeout %" PRIx64 "\n", __func__,
1250  keep_alive_timeout);
1252  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
1253  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
1254  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
1255  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
1256  CHECK_VPU_RECOVERY(retval);
1257 
1258  if (NI_RETCODE_SUCCESS != retval)
1259  {
1260  ni_log2(p_ctx, NI_LOG_ERROR,
1261  "ERROR %s(): nvme write keep_alive_timeout command "
1262  "failed, blk_io_handle: %" PRIx64 ", hw_id, %d\n",
1263  __func__, (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id);
1265  LRETURN;
1266  }
1267 
1268  // Send SW version to FW if FW API version is >= 6.2
1270  "62") >= 0)
1271  {
1272  // Send SW version to session manager
1273  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
1274  memcpy(p_buffer, NI_XCODER_REVISION, sizeof(uint64_t));
1276  ni_fmt_fw_api_ver_str((char*) &p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], &fmt_fw_api_ver2[0]);
1277  ni_log2(p_ctx, NI_LOG_DEBUG, "%s libxcoder FW API ver %s, FW FW API ver %s\n",
1278  __func__, fmt_fw_api_ver1, fmt_fw_api_ver2);
1279  ui32LBA = CONFIG_SESSION_SWVersion_W(p_ctx->session_id);
1280  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
1281  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
1282  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config, p_ctx->device_type,
1283  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
1284  CHECK_VPU_RECOVERY(retval);
1285 
1286  if (NI_RETCODE_SUCCESS != retval)
1287  {
1288  ni_log2(p_ctx, NI_LOG_ERROR,
1289  "ERROR %s(): nvme write sw_version command "
1290  "failed, blk_io_handle: %" PRIx64 ", hw_id, %d\n",
1291  __func__, (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id);
1293  LRETURN;
1294  }
1295  }
1296 
1297  //VP9 requires a scaler session to be opened internally and attached as
1298  //well
1299  if(p_ctx->codec_format == NI_CODEC_FORMAT_VP9)
1300  {
1301  ni_log2(p_ctx, NI_LOG_DEBUG, "Adding scaling session to Vp9 decoder\n");
1302  ui32LBA = OPEN_ADD_CODEC(NI_DEVICE_TYPE_SCALER, ni_htonl(NI_SCALER_OPCODE_SCALE), ni_htons(p_ctx->session_id));
1303  retval = ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
1304  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
1305 
1306  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_open,
1307  p_ctx->device_type, p_ctx->hw_id,
1308  &(p_ctx->session_id), OPT_1);
1309  if (NI_RETCODE_SUCCESS != retval)
1310  {
1311  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR couldn't add vp9 scaler to decoding session\n");
1312  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s():p_ctx->device_handle=0x%" PRIx64 ", "
1313  "p_ctx->hw_id=%d, p_ctx->session_id=%d\n", __func__,
1314  (int64_t)p_ctx->device_handle,p_ctx->hw_id, p_ctx->session_id);
1315  ni_decoder_session_close(p_ctx, 0);
1316  LRETURN;
1317  }
1318  }
1319 
1320  ni_log2(p_ctx, NI_LOG_DEBUG,
1321  "%s(): p_ctx->device_handle=0x%" PRIx64 ", p_ctx->hw_id=%d, "
1322  "p_ctx->session_id=%d\n",
1323  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
1324  p_ctx->session_id);
1325  }
1326 
1327  //start dec config
1328  retval = ni_config_instance_set_decoder_params(p_ctx, 0);
1329  if (NI_RETCODE_SUCCESS != retval)
1330  {
1331  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);
1332  //ni_decoder_session_close(p_ctx, 0); //close happens on above
1334  LRETURN;
1335  }
1336  //end dec config
1337 
1338  // init for frame pts calculation
1339  p_ctx->is_first_frame = 1;
1340  p_ctx->last_pts = NI_NOPTS_VALUE;
1341  p_ctx->last_dts = NI_NOPTS_VALUE;
1342  p_ctx->last_dts_interval = 0;
1343  p_ctx->last_pts_interval = 0;
1344  p_ctx->pts_correction_last_dts = INT64_MIN;
1345  p_ctx->pts_correction_last_pts = INT64_MIN;
1346 
1347  //p_ctx->p_leftover = malloc(NI_MAX_PACKET_SZ * 2);
1348  p_ctx->p_leftover = malloc(p_ctx->max_nvme_io_size * 2);
1349  if (!p_ctx->p_leftover)
1350  {
1351  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Cannot allocate leftover buffer.\n",
1352  __func__);
1353  retval = NI_RETCODE_ERROR_MEM_ALOC;
1354  //ni_decoder_session_close(p_ctx, 0);
1355  LRETURN;
1356  }
1357 
1358  ni_timestamp_init(p_ctx, &p_ctx->pts_table, "dec_pts");
1359  ni_timestamp_init(p_ctx, &p_ctx->dts_queue, "dec_dts");
1360 
1361  if (p_ctx->p_session_config)
1362  {
1363  p_param = (ni_xcoder_params_t *)p_ctx->p_session_config;
1364  ni_params_print(p_param);
1365 
1366  if (p_param->ddr_priority_mode > NI_DDR_PRIORITY_NONE)
1367  {
1368  retval = ni_device_set_ddr_configuration(p_ctx, p_param->ddr_priority_mode);
1369  if (NI_RETCODE_SUCCESS != retval)
1370  {
1371  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): ddr priority setting failure for %s\n",
1372  __func__, strerror(NI_ERRNO));
1373  LRETURN;
1374  }
1375  }
1376  }
1377 
1378  if (p_ctx->force_low_delay)
1379  {
1380  if (ni_cmp_fw_api_ver(
1382  "6r3") < 0)
1383  {
1384  p_ctx->force_low_delay = false; // forceLowDelay not available for fw < 6r3
1385  ni_log2(p_ctx, NI_LOG_INFO, "Warn %s(): forceLowDelay is not available for fw < 6r3\n",
1386  __func__);
1387  }
1388  }
1389 
1390  p_ctx->active_video_width = 0;
1391  p_ctx->active_video_height = 0;
1392  p_ctx->actual_video_width = 0;
1393  p_ctx->pixel_format_changed = 0;
1394 
1395  ni_log2(p_ctx, NI_LOG_DEBUG,
1396  "%s(): p_ctx->device_handle=%" PRIx64 ", p_ctx->hw_id=%d, "
1397  "p_ctx->session_id=%d\n",
1398  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
1399  p_ctx->session_id);
1400 
1401  if (p_param && p_param->dec_input_params.nb_save_pkt)
1402  {
1403  decoder_dump_dir_open(p_ctx);
1404  }
1405 
1406 #ifdef XCODER_DUMP_DATA
1407  char dir_name[256] = {0};
1408 
1409  snprintf(dir_name, sizeof(dir_name), "%ld-%u-dec-fme", (long)getpid(),
1410  p_ctx->session_id);
1411  DIR *dir = opendir(dir_name);
1412  if (!dir && ENOENT == NI_ERRNO)
1413  {
1414  mkdir(dir_name, S_IRWXU | S_IRWXG | S_IRWXO);
1415  ni_log2(p_ctx, NI_LOG_DEBUG, "Decoder frame dump dir created: %s\n", dir_name);
1416  }
1417  if(dir){
1418  closedir(dir);
1419  }
1420 #endif
1421 
1422 END:
1423 
1424  ni_aligned_free(p_buffer);
1425  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
1426 
1427  return retval;
1428 }
1429 
1430 /*!******************************************************************************
1431  * \brief send a keep alive message to firmware
1432  *
1433  * \param
1434  *
1435  * \return
1436  *******************************************************************************/
1437 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)
1438 {
1439  ni_retcode_t retval;
1440  uint32_t ui32LBA = 0;
1441 
1442  ni_log(NI_LOG_TRACE, "%s(): enter\n", __func__);
1443  if (NI_INVALID_SESSION_ID == session_id)
1444  {
1445  ni_log(NI_LOG_ERROR, "ERROR: %s(): Invalid session ID!, return\n",
1446  __func__);
1448  LRETURN;
1449  }
1450 
1451  if (NI_INVALID_DEVICE_HANDLE == device_handle)
1452  {
1453  ni_log(NI_LOG_ERROR, "ERROR: %s(): xcoder instance id < 0, return\n",
1454  __func__);
1455  retval = NI_RETCODE_INVALID_PARAM;
1456  LRETURN;
1457  }
1458 
1459  ui32LBA = CONFIG_SESSION_KeepAlive_W(session_id);
1460  if (ni_nvme_send_write_cmd(device_handle, event_handle, p_data,
1461  NI_DATA_BUFFER_LEN, ui32LBA) < 0)
1462  {
1463  ni_log(NI_LOG_ERROR, "ERROR: %s(): device_handle=%" PRIx64 " , "
1464  "session_id=%d\n", __func__, (int64_t)device_handle, session_id);
1465  retval = NI_RETCODE_FAILURE;
1466  }
1467  else
1468  {
1470  "SUCCESS %s(): device_handle=%" PRIx64 " , "
1471  "session_id=%d\n", __func__, (int64_t)device_handle, session_id);
1472  retval = NI_RETCODE_SUCCESS;
1473  }
1474 
1475 END:
1476 
1477  ni_log(NI_LOG_TRACE, "%s(): exit\n", __func__);
1478 
1479  return retval;
1480 }
1481 
1482 /*!******************************************************************************
1483  * \brief Send end of stream signal to the decoder
1484  *
1485  * \param
1486  *
1487  * \return
1488  *******************************************************************************/
1490 {
1491  ni_retcode_t retval;
1492  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
1493  if (!p_ctx)
1494  {
1495  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
1496  __func__);
1497  retval = NI_RETCODE_INVALID_PARAM;
1498  LRETURN;
1499  }
1500 
1501  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
1502  {
1503  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
1504  __func__);
1506  LRETURN;
1507  }
1508 
1510  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
1511  p_ctx->device_type, p_ctx->hw_id,
1512  &(p_ctx->session_id), OPT_1);
1513  CHECK_VPU_RECOVERY(retval);
1514 
1515 END:
1516 
1517  if (NI_RETCODE_SUCCESS != retval)
1518  {
1519  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s(): %d, return\n", __func__, retval);
1520  }
1521 
1522  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
1523 
1524  return retval;
1525 }
1526 
1527 /*!******************************************************************************
1528  * \brief Flush decoder output
1529  *
1530  * \param
1531  *
1532  * \return
1533  *******************************************************************************/
1535 {
1536  ni_retcode_t retval;
1537  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
1538 
1539  if (!p_ctx)
1540  {
1541  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s(): passed parameters are null!, return\n",
1542  __func__);
1543  retval = NI_RETCODE_INVALID_PARAM;
1544  LRETURN;
1545  }
1546 
1547  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
1548  {
1549  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s(): xcoder instance id < 0, return\n",
1550  __func__);
1552  LRETURN;
1553  }
1554 
1556  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
1557  p_ctx->device_type, p_ctx->hw_id,
1558  &(p_ctx->session_id), OPT_1);
1559 
1560 END:
1561 
1562  if (NI_RETCODE_SUCCESS != retval)
1563  {
1564  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s(): %d, return\n", __func__, retval);
1565  }
1566 
1567  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
1568 
1569  return retval;
1570 }
1571 
1572 /*!******************************************************************************
1573  * \brief Close a xcoder decoder instance
1574  *
1575  * \param
1576  *
1577  * \return
1578  *******************************************************************************/
1580 {
1582  void* p_buffer = NULL;
1583  uint32_t ui32LBA = 0;
1584  int i;
1585  ni_xcoder_params_t *p_param = NULL;
1586 
1587  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
1588 
1589  if (!p_ctx)
1590  {
1591  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
1592  __func__);
1593  return NI_RETCODE_INVALID_PARAM;
1594  }
1595 
1596  ni_pthread_mutex_lock(&p_ctx->mutex);
1597 
1598  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
1599  {
1600  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): Invalid session ID, return.\n", __func__);
1601  retval = NI_RETCODE_SUCCESS;
1602  LRETURN;
1603  }
1604 
1605  if (NI_CODEC_HW_ENABLE == p_ctx->hw_action)
1606  {
1607  ni_session_statistic_t sessionStatistic = {0};
1608  ni_query_session_statistic_info(p_ctx, NI_DEVICE_TYPE_DECODER, &sessionStatistic);
1609  }
1610 
1611  if (ni_cmp_fw_api_ver(
1612  (char *) &p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6rg") >= 0)
1613  {
1614  ni_log2(p_ctx, NI_LOG_INFO,
1615  "Decoder_complete_info:session_id 0x%x, total frames input:%u "
1616  "buffered: %u completed: %u output: %u dropped: %u error: %u corrupted: %u\n",
1624  } else
1625  {
1626  ni_log2(p_ctx, NI_LOG_INFO,
1627  "Decoder_complete_info:session_id 0x%x, total frames input:%u "
1628  "buffered: %u completed: %u output: %u dropped: %u error: %u\n",
1635  }
1636 
1637  if (p_ctx->session_id == NI_INVALID_SESSION_ID)
1638  {
1639  ni_log2(p_ctx, NI_LOG_ERROR, "%s: Invalid session ID, return.\n", __func__);
1640  retval = NI_RETCODE_SUCCESS;
1641  LRETURN;
1642  }
1643 
1644  //malloc data buffer
1645  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), NI_DATA_BUFFER_LEN))
1646  {
1647  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: malloc decoder close data buffer failed\n", NI_ERRNO);
1648  retval = NI_RETCODE_ERROR_MEM_ALOC;
1649  LRETURN;
1650  }
1651 
1652  if (p_ctx->p_session_config)
1653  {
1654  p_param = (ni_xcoder_params_t *)p_ctx->p_session_config;
1655  if (p_param->ddr_priority_mode > NI_DDR_PRIORITY_NONE)
1656  {
1658  if (NI_RETCODE_SUCCESS != retval)
1659  {
1660  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): ddr priority setting failure for %s\n",
1661  __func__, strerror(NI_ERRNO));
1662  LRETURN;
1663  }
1664  }
1665  }
1666 
1667  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
1668 
1670 
1671  int retry = 0;
1672  while (retry < NI_SESSION_CLOSE_RETRY_MAX)
1673  {
1674  ni_log2(p_ctx, NI_LOG_DEBUG,
1675  "%s(): p_ctx->blk_io_handle=%" PRIx64 ", p_ctx->hw_id=%d, "
1676  "p_ctx->session_id=%d, close_mode=1\n",
1677  __func__, (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id,
1678  p_ctx->session_id);
1679 
1681  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA) < 0)
1682  {
1683  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): command failed!\n", __func__);
1686  break;
1687  } else
1688  {
1689  //Close should always succeed
1690  retval = NI_RETCODE_SUCCESS;
1692  break;
1693  }
1694  /*
1695  else if (*((ni_retcode_t *)p_buffer) == RETCODE_SUCCESS)
1696  {
1697  retval = NI_RETCODE_SUCCESS;
1698  p_ctx->session_id = NI_INVALID_SESSION_ID;
1699  break;
1700  }
1701  else
1702  {
1703  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): wait for close\n", __func__);
1704  ni_usleep(NI_SESSION_CLOSE_RETRY_INTERVAL_US);
1705  retval = NI_RETCODE_ERROR_NVME_CMD_FAILED;
1706  }
1707  */
1708  retry++;
1709  }
1710 
1711 END:
1712 
1713  ni_aligned_free(p_buffer);
1715  ni_memfree(p_ctx->p_leftover);
1716 
1717  if (p_ctx->pts_table)
1718  {
1719  ni_queue_free(&p_ctx->pts_table->list, p_ctx->buffer_pool);
1720  ni_memfree(p_ctx->pts_table);
1721  ni_log2(p_ctx, NI_LOG_DEBUG, "ni_timestamp_done: success\n");
1722  }
1723 
1724  if (p_ctx->dts_queue)
1725  {
1726  ni_queue_free(&p_ctx->dts_queue->list, p_ctx->buffer_pool);
1727  ni_memfree(p_ctx->dts_queue);
1728  ni_log2(p_ctx, NI_LOG_DEBUG, "ni_timestamp_done: success\n");
1729  }
1730 
1733  p_ctx->buffer_pool = NULL;
1734  p_ctx->dec_fme_buf_pool = NULL;
1735 
1736  for (i = 0; i < NI_FIFO_SZ; i++)
1737  {
1738  ni_memfree(p_ctx->pkt_custom_sei_set[i]);
1739  }
1740 
1741  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): CTX[Card:%" PRIx64 " / HW:%d / INST:%d]\n",
1742  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
1743  p_ctx->session_id);
1744 
1745  low_delay_signal(p_ctx);
1746  ni_pthread_mutex_unlock(&p_ctx->mutex);
1747 
1748  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
1749 
1750  return retval;
1751 }
1752 
1753 /*!******************************************************************************
1754  * \brief Send a video p_packet to decoder
1755  *
1756  * \param
1757  *
1758  * \return
1759  *******************************************************************************/
1761 {
1762  uint32_t sent_size = 0;
1763  uint32_t packet_size = 0;
1764  int current_pkt_size;
1765  int retval = NI_RETCODE_SUCCESS;
1766  ni_xcoder_params_t *p_param;
1767  ni_instance_buf_info_t buf_info = { 0 };
1768  ni_session_statistic_t sessionStatistic = {0};
1769  int query_retry = 0;
1770  uint32_t ui32LBA = 0;
1771 
1772  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
1773 
1774  if ((!p_ctx) || (!p_packet))
1775  {
1776  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
1777  __func__);
1778  return NI_RETCODE_INVALID_PARAM;
1779  }
1780 
1781  ni_pthread_mutex_lock(&p_ctx->mutex);
1782 
1783  if ((NI_INVALID_SESSION_ID == p_ctx->session_id))
1784  {
1785  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
1786  __func__);
1788  LRETURN;
1789  }
1790 
1791  low_delay_wait(p_ctx);
1792 
1793 #ifdef MEASURE_LATENCY
1794  if ((p_packet->dts != NI_NOPTS_VALUE) && (p_ctx->frame_time_q != NULL))
1795  {
1796  uint64_t abs_time_ns = ni_gettime_ns();
1798  abs_time_ns, p_packet->dts);
1799  }
1800 #endif
1801 
1802  p_param = (ni_xcoder_params_t *)p_ctx->p_session_config;
1803  packet_size = p_packet->data_len;
1804  current_pkt_size = packet_size;
1805 
1806 #ifdef XCODER_311
1807  int max_retry = 200; // 20ms
1808  if (p_packet->video_width * p_packet->video_height >= NI_NUM_OF_PIXELS_1440P)
1809  {
1810  max_retry = NI_MAX_TX_RETRIES; // 100ms
1811  }
1812 #endif
1813 
1814  for (;;)
1815  {
1816  query_sleep(p_ctx);
1817 
1818  query_retry++;
1819 
1820 
1822  "65") >= 0)
1823  {
1825  &sessionStatistic);
1826  CHECK_ERR_RC(p_ctx, retval, &sessionStatistic,
1828  p_ctx->hw_id, &(p_ctx->session_id), OPT_2);
1829  CHECK_VPU_RECOVERY(retval);
1830 
1831  buf_info.buf_avail_size = sessionStatistic.ui32WrBufAvailSize;
1832  } else
1833  {
1835  NI_DEVICE_TYPE_DECODER, &buf_info);
1836  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
1837  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
1838  CHECK_VPU_RECOVERY(retval);
1839  }
1840 
1841  if (buf_info.buf_avail_size == DP_IPC_PASSTHRU)
1842  {
1843  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): Bad available buffer size %u\n", __FUNCTION__, buf_info.buf_avail_size);
1844  retval = NI_RETCODE_FAILURE;
1845  LRETURN;
1846  }
1847 
1848  if (p_ctx->biggest_bitstream_buffer_allocated < buf_info.buf_avail_size)
1849  {
1851  }
1852  if (p_ctx->biggest_bitstream_buffer_allocated < packet_size &&
1854  {
1855  // Reallocate decoder bitstream buffers to accomodate
1857  "66") >= 0)
1858  {
1859  retval = ni_config_instance_set_write_len(p_ctx,
1861  // packet buffer aligned to NI_MAX_PACKET_SZ(128k)
1862  (packet_size / NI_MAX_PACKET_SZ + 1) * NI_MAX_PACKET_SZ);
1864  p_ctx->device_type, p_ctx->hw_id,
1865  &(p_ctx->session_id), OPT_3);
1866  CHECK_VPU_RECOVERY(retval);
1867  }
1868  else
1869  {
1870  retval = ni_config_instance_set_decoder_params(p_ctx, packet_size);
1871  }
1872  if (NI_RETCODE_SUCCESS != retval)
1873  {
1874  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): failed to reallocate bitstream\n", __FUNCTION__);
1875  LRETURN;
1876  }
1877  query_retry--;
1878  continue;
1879  }
1880 
1881  if (NI_RETCODE_SUCCESS != retval ||
1882  buf_info.buf_avail_size < packet_size)
1883  {
1884  ni_log2(p_ctx, NI_LOG_TRACE,
1885  "Warning: dec write query fail rc %d or available buf size %u < "
1886  "pkt size %u , retry: %d max_retry_fail_count %d\n",
1887  retval, buf_info.buf_avail_size, packet_size, query_retry, p_ctx->max_retry_fail_count[0]);
1888 #ifdef XCODER_311
1889  if (query_retry > max_retry ||
1890  sessionStatistic.ui32RdBufAvailSize > 0)
1891 #else
1892  if (query_retry > NI_MAX_TX_RETRIES ||
1893  sessionStatistic.ui32RdBufAvailSize > 0)
1894 #endif
1895  {
1897 #ifdef XCODER_311
1898  if (query_retry > max_retry)
1899 #else
1900  if (query_retry > NI_MAX_TX_RETRIES)
1901 #endif
1902  {
1903  p_ctx->max_retry_fail_count[0]++;
1904  }
1905  p_ctx->required_buf_size = packet_size;
1907  LRETURN;
1908  }
1909  ni_pthread_mutex_unlock(&p_ctx->mutex);
1911  ni_pthread_mutex_lock(&p_ctx->mutex);
1912  }
1913  else
1914  {
1915  p_ctx->max_retry_fail_count[0] = 0;
1916  ni_log2(p_ctx, NI_LOG_DEBUG, "Info dec write query success, available buf "
1917  "size %u >= pkt size %u !\n",
1918  buf_info.buf_avail_size, packet_size);
1919  break;
1920  }
1921  }
1922 
1923  //Configure write size for the buffer
1925  packet_size);
1927  p_ctx->device_type, p_ctx->hw_id,
1928  &(p_ctx->session_id), OPT_1);
1929  CHECK_VPU_RECOVERY(retval);
1930  if (retval < 0)
1931  {
1932  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): config pkt size command failed\n",
1933  __func__);
1935  LRETURN;
1936  }
1937 
1939 
1940  //check for start of stream flag
1941  if (p_packet->start_of_stream)
1942  {
1944  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
1945  p_ctx->device_type, p_ctx->hw_id,
1946  &(p_ctx->session_id), OPT_1);
1947  CHECK_VPU_RECOVERY(retval);
1948  if (NI_RETCODE_SUCCESS != retval)
1949  {
1950  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Failed to send SOS.\n", __func__);
1951  LRETURN;
1952  }
1953 
1954  p_packet->start_of_stream = 0;
1955  }
1956 
1957  if (p_packet->p_data)
1958  {
1959  ni_log2(p_ctx, NI_LOG_DEBUG,
1960  "%s() had data to send: packet_size=%u, "
1961  "p_packet->sent_size=%d, p_packet->data_len=%u, "
1962  "p_packet->start_of_stream=%u, p_packet->end_of_stream=%u, "
1963  "p_packet->video_width=%u, p_packet->video_height=%u\n",
1964  __func__, packet_size, p_packet->sent_size, p_packet->data_len,
1965  p_packet->start_of_stream, p_packet->end_of_stream,
1966  p_packet->video_width, p_packet->video_height);
1967 
1968  uint8_t *p_data = (uint8_t *)p_packet->p_data;
1969  // Note: session status is NOT reset but tracked between send
1970  // and recv to catch and recover from a loop condition
1971  // p_ctx->status = 0;
1972 
1973  if (packet_size % NI_MEM_PAGE_ALIGNMENT) //packet size, already aligned
1974  {
1975  packet_size = ( (packet_size / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT) + NI_MEM_PAGE_ALIGNMENT;
1976  }
1977 
1978  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
1979  p_data, packet_size, ui32LBA);
1980  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_write,
1981  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
1982  CHECK_VPU_RECOVERY(retval);
1983  if (retval < 0)
1984  {
1985  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
1987  LRETURN;
1988  }
1989 
1990  // reset session status after successful send
1991  p_ctx->status = 0;
1992  p_ctx->required_buf_size = 0;
1993 
1994  sent_size = p_packet->data_len;
1995  p_packet->data_len = 0;
1996 
1997  if (p_param->dec_input_params.nb_save_pkt)
1998  {
1999  char dump_file[512] = {0};
2000  long curr_pkt_num =
2001  ((long)p_ctx->pkt_num % p_param->dec_input_params.nb_save_pkt) + 1;
2002  snprintf(dump_file, sizeof(dump_file), "%s/pkt-%04ld.bin",
2003  p_ctx->stream_dir_name, curr_pkt_num);
2004  FILE *f = fopen(dump_file, "wb");
2005  if (f)
2006  {
2007  fwrite(p_packet->p_data, sent_size, 1, f);
2008  fflush(f);
2009  fclose(f);
2010  }
2011  }
2012 
2013  p_ctx->pkt_num++;
2014  p_ctx->low_delay_sync_flag = 1;
2015  }
2016 
2017  //Handle end of stream flag
2018  if (p_packet->end_of_stream)
2019  {
2021  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
2022  p_ctx->device_type, p_ctx->hw_id,
2023  &(p_ctx->session_id), OPT_1);
2024  CHECK_VPU_RECOVERY(retval);
2025 
2026  if (NI_RETCODE_SUCCESS != retval)
2027  {
2028  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Failed to send EOS.\n", __func__);
2029  LRETURN;
2030  }
2031 
2032  p_packet->end_of_stream = 0;
2033  p_ctx->ready_to_close = 1;
2034  }
2035 
2036  {
2037  p_ctx->pts_offsets[p_ctx->pkt_index % NI_FIFO_SZ] = p_packet->pts;
2038  p_ctx->flags_array[p_ctx->pkt_index % NI_FIFO_SZ] = p_packet->flags;
2039  p_ctx->pkt_pos[p_ctx->pkt_index % NI_FIFO_SZ] = p_packet->pkt_pos;
2040  if (p_ctx->pkt_index == 0)
2041  {
2042  p_ctx->pkt_offsets_index_min[p_ctx->pkt_index % NI_FIFO_SZ] = 0;
2043  /* minus 1 here. ffmpeg parses the msb 0 of long start code as the last packet's payload for hevc bitstream (hevc_parse).
2044  * move 1 byte forward on all the pkt_offset so that frame_offset coming from fw can fall into the correct range. */
2045  //p_ctx->pkt_offsets_index[p_ctx->pkt_index % NI_FIFO_SZ] = current_pkt_size - 1;
2046  p_ctx->pkt_offsets_index[p_ctx->pkt_index % NI_FIFO_SZ] = current_pkt_size;
2047  ni_log2(p_ctx, NI_LOG_DEBUG,
2048  "%s: (first packet) pkt_index %d i %u "
2049  "pkt_offsets_index_min %" PRIu64 " pkt_offsets_index %" PRIu64
2050  " pts_offsets %" PRId64 "\n",
2051  __func__, p_ctx->pkt_index, p_ctx->pkt_index % NI_FIFO_SZ,
2052  p_ctx->pkt_offsets_index_min[p_ctx->pkt_index % NI_FIFO_SZ],
2053  p_ctx->pkt_offsets_index[p_ctx->pkt_index % NI_FIFO_SZ],
2054  p_ctx->pts_offsets[p_ctx->pkt_index % NI_FIFO_SZ]);
2055  }
2056  else
2057  {
2058  // cumulate sizes to correspond to FW offsets
2059  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];
2060  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;
2061  ni_log2(p_ctx, NI_LOG_DEBUG,
2062  "%s: pkt_index %d i %u pkt_offsets_index_min "
2063  "%" PRIu64 " pkt_offsets_index %" PRIu64 " pts_offsets %" PRId64
2064  "\n",
2065  __func__, p_ctx->pkt_index, p_ctx->pkt_index % NI_FIFO_SZ,
2066  p_ctx->pkt_offsets_index_min[p_ctx->pkt_index % NI_FIFO_SZ],
2067  p_ctx->pkt_offsets_index[p_ctx->pkt_index % NI_FIFO_SZ],
2068  p_ctx->pts_offsets[p_ctx->pkt_index % NI_FIFO_SZ]);
2069 
2070  //Wrapping 32 bits since FW send u32 wrapped values
2071  if (p_ctx->pkt_offsets_index_min[p_ctx->pkt_index % NI_FIFO_SZ] > 0xFFFFFFFF)
2072  {
2073  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);
2074  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;
2075  ni_log2(p_ctx, NI_LOG_DEBUG,
2076  "%s: (wrap) pkt_index %d i %u "
2077  "pkt_offsets_index_min %" PRIu64 " pkt_offsets_index %" PRIu64
2078  " pts_offsets %" PRId64 "\n",
2079  __func__, p_ctx->pkt_index, p_ctx->pkt_index % NI_FIFO_SZ,
2080  p_ctx->pkt_offsets_index_min[p_ctx->pkt_index % NI_FIFO_SZ],
2081  p_ctx->pkt_offsets_index[p_ctx->pkt_index % NI_FIFO_SZ],
2082  p_ctx->pts_offsets[p_ctx->pkt_index % NI_FIFO_SZ]);
2083  }
2084  }
2085 
2086  /* if this wrap-around pkt_offset_index spot is about to be overwritten, free the previous one. */
2087  free(p_ctx->pkt_custom_sei_set[p_ctx->pkt_index % NI_FIFO_SZ]);
2088 
2089  if (p_packet->p_custom_sei_set)
2090  {
2091  p_ctx->pkt_custom_sei_set[p_ctx->pkt_index % NI_FIFO_SZ] = malloc(sizeof(ni_custom_sei_set_t));
2092  if (p_ctx->pkt_custom_sei_set[p_ctx->pkt_index % NI_FIFO_SZ])
2093  {
2094  ni_custom_sei_set_t *p_custom_sei_set = p_ctx->pkt_custom_sei_set[p_ctx->pkt_index % NI_FIFO_SZ];
2095  memcpy(p_custom_sei_set, p_packet->p_custom_sei_set, sizeof(ni_custom_sei_set_t));
2096  }
2097  else
2098  {
2099  /* warn and lose the sei data. */
2100  ni_log2(p_ctx, NI_LOG_ERROR,
2101  "Error %s: failed to allocate custom SEI buffer for pkt.\n",
2102  __func__);
2103  }
2104  }
2105  else
2106  {
2107  p_ctx->pkt_custom_sei_set[p_ctx->pkt_index % NI_FIFO_SZ] = NULL;
2108  }
2109 
2110  p_ctx->pkt_index++;
2111  }
2112 
2113  retval = ni_timestamp_register(p_ctx->buffer_pool, p_ctx->dts_queue, p_packet->dts, 0);
2114  if (NI_RETCODE_SUCCESS != retval)
2115  {
2116  ni_log2(p_ctx, NI_LOG_ERROR,
2117  "ERROR %s(): ni_timestamp_register() for dts returned %d\n",
2118  __func__, retval);
2119  }
2120 END:
2121 
2122  ni_pthread_mutex_unlock(&p_ctx->mutex);
2123 
2124  if (NI_RETCODE_SUCCESS == retval)
2125  {
2126  ni_log2(p_ctx, NI_LOG_TRACE,
2127  "%s(): exit: packets: %" PRIu64 " offset %" PRIx64 ""
2128  " sent_size = %u, status=%d\n",
2129  __func__, p_ctx->pkt_num, (uint64_t)p_packet->pos, sent_size,
2130  p_ctx->status);
2131  return sent_size;
2132  } else
2133  {
2134  ni_log2(p_ctx, NI_LOG_ERROR,
2135  "ERROR %s(): exit: returnErr: %d, p_ctx->status: %d\n", __func__,
2136  retval, p_ctx->status);
2137  return retval;
2138  }
2139 }
2140 
2141 static int64_t guess_correct_pts(ni_session_context_t* p_ctx, int64_t reordered_pts, int64_t dts)
2142 {
2143  int64_t pts = NI_NOPTS_VALUE;
2144  if (dts != NI_NOPTS_VALUE)
2145  {
2147  p_ctx->pts_correction_last_dts = dts;
2148  ni_log2(p_ctx, NI_LOG_DEBUG,
2149  "%s: pts_correction_last_dts %" PRId64 " "
2150  "pts_correction_num_faulty_dts %d\n",
2151  __func__, p_ctx->pts_correction_last_dts,
2153  }
2154  else if (reordered_pts != NI_NOPTS_VALUE)
2155  {
2156  p_ctx->pts_correction_last_dts = reordered_pts;
2157  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: pts_correction_last_dts %" PRId64 "\n", __func__,
2158  p_ctx->pts_correction_last_dts);
2159  }
2160  if (reordered_pts != NI_NOPTS_VALUE)
2161  {
2162  p_ctx->pts_correction_num_faulty_pts += reordered_pts <= p_ctx->pts_correction_last_pts;
2163  p_ctx->pts_correction_last_pts = reordered_pts;
2164  ni_log2(p_ctx, NI_LOG_DEBUG,
2165  "%s: pts_correction_last_pts %" PRId64 " "
2166  "pts_correction_num_faulty_pts %d\n",
2167  __func__, p_ctx->pts_correction_last_pts,
2169  }
2170  else if (dts != NI_NOPTS_VALUE)
2171  {
2172  p_ctx->pts_correction_last_pts = dts;
2173  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: pts_correction_last_pts %" PRId64 "\n", __func__,
2174  p_ctx->pts_correction_last_pts);
2175  }
2177  && reordered_pts != NI_NOPTS_VALUE)
2178  {
2179  pts = reordered_pts;
2180  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: (reordered_pts) pts %" PRId64 "\n", __func__,
2181  pts);
2182  }
2183  else
2184  {
2185  if ((NI_NOPTS_VALUE == reordered_pts) ||
2186  (NI_NOPTS_VALUE == p_ctx->last_pts) || (dts >= p_ctx->last_pts))
2187  {
2188  pts = dts;
2189  } else
2190  {
2191  pts = reordered_pts;
2192  }
2193 
2194  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: (dts) pts %" PRId64 "\n", __func__, pts);
2195  }
2196  return pts;
2197 }
2198 
2199 static int rotated_array_binary_search(uint64_t *lefts, uint64_t *rights,
2200  int32_t size, uint64_t target)
2201 {
2202  int lo = 0;
2203  int hi = size - 1;
2204  while (lo <= hi)
2205  {
2206  int mid = lo + (hi - lo) / 2;
2207  if (lefts[mid] <= target && target < rights[mid])
2208  {
2209  return mid;
2210  }
2211 
2212  if (rights[mid] == 0)
2213  {
2214  // empty in (mid, hi)
2215  hi = mid - 1;
2216  continue;
2217  }
2218 
2219  if (rights[lo] <= rights[mid])
2220  {
2221  if (lefts[lo] <= target && target < lefts[mid])
2222  {
2223  // Elements are all monotonous in (lo, mid)
2224  hi = mid - 1;
2225  } else
2226  {
2227  // Rotation in (mid, hi)
2228  lo = mid + 1;
2229  }
2230  } else
2231  {
2232  if (rights[mid] <= target && target < rights[hi])
2233  {
2234  // Elements are all monotonous in (mid, hi)
2235  lo = mid + 1;
2236  } else
2237  {
2238  // Rotation in (lo, mid)
2239  hi = mid - 1;
2240  }
2241  }
2242  }
2243 
2244  return -1;
2245 }
2246 
2247 /*!******************************************************************************
2248  * \brief Retrieve a YUV p_frame from decoder
2249  *
2250  * \param
2251  *
2252  * \return
2253  *******************************************************************************/
2255 {
2256  ni_instance_mgr_stream_info_t data = { 0 };
2257  int rx_size = 0;
2258  uint64_t frame_offset = 0;
2259  uint8_t *p_data_buffer = NULL;
2260  int i = 0;
2261  int is_planar;
2262  int retval = NI_RETCODE_SUCCESS;
2263  int metadata_hdr_size = NI_FW_META_DATA_SZ - NI_MAX_NUM_OF_DECODER_OUTPUTS * sizeof(niFrameSurface1_t);
2264  int sei_size = 0;
2265  uint32_t total_bytes_to_read = 0;
2266  uint32_t read_size_bytes = 0;
2267  ni_instance_buf_info_t buf_info = { 0 };
2268  ni_session_statistic_t sessionStatistic = {0};
2269  ni_xcoder_params_t *p_param;
2270 
2271  int query_retry = 0;
2272  uint32_t ui32LBA = 0;
2273  unsigned int bytes_read_so_far = 0;
2274  int query_type = INST_BUF_INFO_RW_READ;
2275  int low_delay_notify = 0;
2276  uint32_t frames_dropped = 0;
2277  uint8_t get_first_metadata = 0;
2278  uint8_t sequence_change = 0;
2279 
2280  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
2281 
2282  if ((!p_ctx) || (!p_frame))
2283  {
2284  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: passed parameters are null!, return\n");
2285  return NI_RETCODE_INVALID_PARAM;
2286  }
2287 
2288  ni_pthread_mutex_lock(&p_ctx->mutex);
2289 
2290 start:
2291  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
2292  {
2293  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): xcoder instance id < 0, return\n",
2294  __func__);
2296  LRETURN;
2297  }
2298 
2299  p_param = (ni_xcoder_params_t *)p_ctx->p_session_config;
2300  p_data_buffer = (uint8_t *)p_frame->p_buffer;
2301 
2302  // p_frame->p_data[] can be NULL before actual resolution is returned by
2303  // decoder and buffer pool is allocated, so no checking here.
2304  total_bytes_to_read = p_frame->data_len[0] + p_frame->data_len[1] +
2305  p_frame->data_len[2] + metadata_hdr_size;
2306  ni_log2(p_ctx, NI_LOG_DEBUG, "Total bytes to read %u, low_delay %u\n",
2307  total_bytes_to_read, p_ctx->decoder_low_delay);
2308 
2309  if (p_ctx->decoder_low_delay > 0 && !p_ctx->ready_to_close)
2310  {
2311  ni_log2(p_ctx, NI_LOG_DEBUG, "frame_num = %" PRIu64 ", pkt_num = %" PRIu64 "\n",
2312  p_ctx->frame_num, p_ctx->pkt_num);
2313  frames_dropped = p_ctx->session_statistic.ui32FramesDropped;
2314  if (p_ctx->force_low_delay && (p_ctx->force_low_delay_cnt < frames_dropped)) {
2315  p_ctx->force_low_delay_cnt = frames_dropped;
2316  }
2317  if (p_ctx->frame_num + p_ctx->force_low_delay_cnt >= p_ctx->pkt_num)
2318  {
2319  //nothing to query, leave
2320  retval = NI_RETCODE_SUCCESS;
2321  LRETURN;
2322  }
2323  query_type = INST_BUF_INFO_RW_READ_BUSY;
2324  }
2325  for (;;)
2326  {
2327  query_sleep(p_ctx);
2328 
2329  query_retry++;
2330 
2332  "65") >= 0)
2333  {
2335  &sessionStatistic);
2336  CHECK_ERR_RC(p_ctx, retval, &sessionStatistic,
2338  p_ctx->hw_id, &(p_ctx->session_id), OPT_2);
2339  CHECK_VPU_RECOVERY(retval);
2340 
2341  buf_info.buf_avail_size = sessionStatistic.ui32RdBufAvailSize;
2342  } else
2343  {
2344  retval = ni_query_instance_buf_info(p_ctx, query_type,
2345  NI_DEVICE_TYPE_DECODER, &buf_info);
2346  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
2347  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
2348  CHECK_VPU_RECOVERY(retval);
2349  }
2350 
2351  ni_log2(p_ctx, NI_LOG_TRACE, "Info query buf_info.size = %u\n",
2352  buf_info.buf_avail_size);
2353 
2354  if (NI_RETCODE_SUCCESS != retval)
2355  {
2356  ni_log2(p_ctx, NI_LOG_TRACE, "Warning: dec read query fail rc %d retry %d max_retry_fail_count %d\n",
2357  retval, query_retry, p_ctx->max_retry_fail_count[1]);
2358 
2359  if (query_retry >= 1000)
2360  {
2361  p_ctx->max_retry_fail_count[1]++;
2362  low_delay_notify = 1;
2364  LRETURN;
2365  }
2366  ni_pthread_mutex_unlock(&p_ctx->mutex);
2368  ni_pthread_mutex_lock(&p_ctx->mutex);
2369  }
2370  else if (buf_info.buf_avail_size == DP_IPC_PASSTHRU)
2371  {
2372  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): Bad available buffer size %u\n", __FUNCTION__, buf_info.buf_avail_size);
2373  retval = NI_RETCODE_FAILURE;
2374  LRETURN;
2375  }
2376  else if (buf_info.buf_avail_size == metadata_hdr_size)
2377  {
2378  ni_log2(p_ctx, NI_LOG_DEBUG, "Info only metadata hdr is available, seq change?\n");
2379  total_bytes_to_read = metadata_hdr_size;
2380  sequence_change = 1;
2381  break;
2382  }
2383  else if (0 == buf_info.buf_avail_size)
2384  {
2385  // query to see if it is eos now, if we have sent it
2386  if (p_ctx->ready_to_close)
2387  {
2388  ni_log2(p_ctx, NI_LOG_TRACE, "Info dec query, ready_to_close %u, query eos\n",
2389  p_ctx->ready_to_close);
2390  retval = ni_query_stream_info(p_ctx, NI_DEVICE_TYPE_DECODER, &data);
2391  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
2392  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
2393  CHECK_VPU_RECOVERY(retval);
2394 
2395  if (data.is_flushed ||
2396  query_retry >=
2398  {
2399  if (query_retry >=
2401  {
2402  ni_log2(p_ctx, NI_LOG_ERROR,
2403  "WARNING: Dec eos reached but also exceeded max dec read query "
2404  "retries. is_flushed=%u try=%d.\n",
2405  data.is_flushed, query_retry);
2406  } else
2407  {
2408  ni_log2(p_ctx, NI_LOG_DEBUG,
2409  "Dec eos reached. is_flushed=%u try=%d.\n",
2410  data.is_flushed, query_retry);
2411  }
2412  p_frame->end_of_stream = 1;
2413  low_delay_notify = 1;
2414  retval = NI_RETCODE_SUCCESS;
2415  LRETURN;
2416  } else
2417  {
2418  ni_log2(p_ctx, NI_LOG_TRACE, "Dec read available buf size == 0, query try %d,"
2419  " retrying ..\n", query_retry);
2420  ni_pthread_mutex_unlock(&p_ctx->mutex);
2422  ni_pthread_mutex_lock(&p_ctx->mutex);
2423  continue;
2424  }
2425  }
2426 
2427  ni_log2(p_ctx, NI_LOG_TRACE, "Dec read available buf size == 0. retry=%d, eos=%d"
2428  "\n", query_retry, p_frame->end_of_stream);
2430  (p_ctx->decoder_low_delay > 0 &&
2431  ((p_ctx->frame_num + p_ctx->force_low_delay_cnt)
2432  < p_ctx->pkt_num))) &&
2433  query_retry < 1000 / 2)
2434  {
2435  if (p_ctx->decoder_low_delay && p_ctx->force_low_delay) {
2436  if (p_ctx->session_statistic.ui32FramesDropped > frames_dropped) {
2437  // last pkt sent to decoder marked as dropped, no output,
2438  // so just stop query and return
2439  p_ctx->force_low_delay_cnt++;
2440  low_delay_signal(p_ctx);
2441  retval = NI_RETCODE_SUCCESS;
2442  LRETURN;
2443  }
2444  } else if (NI_RETCODE_NVME_SC_WRITE_BUFFER_FULL == p_ctx->status &&
2445  sessionStatistic.ui32WrBufAvailSize > p_ctx->required_buf_size)
2446  {
2447  ni_log2(p_ctx, NI_LOG_TRACE, "Info dec write buffer is enough, available buf "
2448  "size %u >= required size %u !\n",
2449  sessionStatistic.ui32WrBufAvailSize, p_ctx->required_buf_size);
2450  p_ctx->status = 0;
2451  p_ctx->required_buf_size = 0;
2452  retval = NI_RETCODE_SUCCESS;
2453  LRETURN;
2454  }
2455  ni_pthread_mutex_unlock(&p_ctx->mutex);
2456  ni_usleep(25);
2457  ni_pthread_mutex_lock(&p_ctx->mutex);
2458  continue;
2459  } else
2460  {
2461  if(p_ctx->decoder_low_delay > 0)
2462  {
2463  if (p_ctx->force_low_delay) {
2464  p_ctx->force_low_delay_cnt++;
2465  low_delay_signal(p_ctx);
2466  } else {
2467  ni_log2(p_ctx, NI_LOG_ERROR,
2468  "Warning: ceased using low delay decoding mode after "
2469  "excessively long decoder read query.\n");
2470  // Here it should be the last signal to release the send thread
2471  // holding the low delay mutex.
2472  low_delay_signal(p_ctx);
2473  p_ctx->decoder_low_delay = 0;
2474  }
2475  }
2476 
2477  if ((p_param->dec_input_params.min_packets_delay && p_ctx->pkt_delay_cnt))
2478  {
2479  if(p_ctx->pkt_num >= (p_ctx->frame_num + p_ctx->pkt_delay_cnt +
2481  {
2482  if(query_retry <= 2000)
2483  {
2484  ni_pthread_mutex_unlock(&p_ctx->mutex);
2485  ni_usleep(25);
2486  ni_pthread_mutex_lock(&p_ctx->mutex);
2487  continue;
2488  } else {
2489  p_ctx->pkt_delay_cnt++;
2490  ni_log2(p_ctx, NI_LOG_ERROR,
2491  "Warning: decoder pkt_num %u frame_num %u "
2492  "timeout, increasing pkt_delay_cnt to %u\n",
2493  p_ctx->pkt_num, p_ctx->frame_num,
2494  p_ctx->pkt_delay_cnt);
2495  }
2496  }
2497  }
2498 
2499  ni_log2(p_ctx, NI_LOG_DEBUG, "Warning: dec read failed %d retries. rc=%d; eos=%d\n",
2500  query_retry, p_ctx->status, p_frame->end_of_stream);
2501  }
2502  retval = NI_RETCODE_SUCCESS;
2503  LRETURN;
2504  }
2505  else
2506  {
2507  // We have to ensure there are adequate number of DTS for picture
2508  // reorder delay otherwise wait for more packets to be sent to decoder.
2509  ni_timestamp_table_t *p_dts_queue = p_ctx->dts_queue;
2510  if ((int)p_dts_queue->list.count < p_ctx->pic_reorder_delay + 1 &&
2511  !p_ctx->ready_to_close &&
2513  {
2514  retval = NI_RETCODE_SUCCESS;
2515  ni_log2(p_ctx, NI_LOG_DEBUG,
2516  "At least %d packets should be sent before reading the "
2517  "first frame!\n",
2518  p_ctx->pic_reorder_delay + 1);
2519  LRETURN;
2520  }
2521  p_ctx->max_retry_fail_count[1] = 0;
2522 
2523  // get actual YUV transfer size if this is the stream's very first read
2524  if (0 == p_ctx->active_video_width || 0 == p_ctx->active_video_height)
2525  {
2526  retval = ni_query_stream_info(p_ctx, NI_DEVICE_TYPE_DECODER, &data);
2527  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
2528  p_ctx->device_type, p_ctx->hw_id,
2529  &(p_ctx->session_id), OPT_1);
2530  CHECK_VPU_RECOVERY(retval);
2531 
2532  ni_log2(p_ctx, NI_LOG_DEBUG, "Info dec YUV query, pic size %ux%u xfer frame size "
2533  "%ux%u frame-rate %u is_flushed %u\n",
2534  data.picture_width, data.picture_height,
2536  data.frame_rate, data.is_flushed);
2539  p_ctx->actual_video_width = data.picture_width;
2540  p_ctx->pixel_format = data.pix_format;
2541  is_planar = (p_ctx->pixel_format == NI_PIX_FMT_YUV420P) ||
2544  //p_ctx->bit_depth_factor = data.transfer_frame_stride / data.picture_width;
2545  p_ctx->is_first_frame = 1;
2546  p_ctx->pixel_format_changed = 0;
2547 
2548  ni_log2(p_ctx, NI_LOG_DEBUG, "Info dec YUV, adjust frame size from %ux%u to "
2549  "%ux%u format = %d\n", p_frame->video_width, p_frame->video_height,
2550  p_ctx->active_video_width, p_ctx->active_video_height, p_ctx->pixel_format);
2551 
2553 
2554  // set up decoder YUV frame buffer pool
2557  p_ctx->active_video_height,
2559  p_ctx->bit_depth_factor))
2560  {
2561  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Cannot allocate fme buf pool.\n",
2562  __func__);
2563  retval = NI_RETCODE_ERROR_MEM_ALOC;
2564 
2565  ni_pthread_mutex_unlock(&p_ctx->mutex);
2566  ni_decoder_session_close(p_ctx, 0);
2567 #ifdef XCODER_SELF_KILL_ERR
2568  // if need to terminate at such occasion when continuing is not
2569  // possible, trigger a codec closure
2570  ni_log2(p_ctx, NI_LOG_ERROR, "Terminating due to unable to allocate fme buf "
2571  "pool.\n");
2572  kill(getpid(), SIGTERM);
2573 #endif
2574  ni_pthread_mutex_lock(&p_ctx->mutex);
2575  LRETURN;
2576  }
2577 
2579  p_ctx->dec_fme_buf_pool, p_frame, 1, // get mem buffer
2580  p_ctx->actual_video_width, p_ctx->active_video_height,
2582  p_ctx->bit_depth_factor, is_planar);
2583 
2584  if (NI_RETCODE_SUCCESS != retval)
2585  {
2586  LRETURN;
2587  }
2588  total_bytes_to_read = p_frame->data_len[0] + p_frame->data_len[1] +
2589  p_frame->data_len[2] + metadata_hdr_size;
2590  p_data_buffer = (uint8_t*) p_frame->p_buffer;
2591 
2592  // make sure we don't read more than available
2593  ni_log2(p_ctx, NI_LOG_DEBUG, "Info dec buf size: %u YUV frame + meta-hdr size: %u "
2594  "available: %u\n", p_frame->buffer_size,
2595  total_bytes_to_read, buf_info.buf_avail_size);
2596  }
2597  break;
2598  }
2599  }
2600 
2601  ni_log2(p_ctx, NI_LOG_DEBUG, "total_bytes_to_read %u max_nvme_io_size %u ylen %u cr len "
2602  "%u cb len %u hdr %d\n",
2603  total_bytes_to_read, p_ctx->max_nvme_io_size,
2604  p_frame->data_len[0], p_frame->data_len[1],
2605  p_frame->data_len[2], metadata_hdr_size);
2606 
2607  if (buf_info.buf_avail_size < total_bytes_to_read)
2608  {
2609  ni_pthread_mutex_unlock(&p_ctx->mutex);
2610  ni_log2(p_ctx, NI_LOG_ERROR,
2611  "ERROR %s() avaliable size(%u)"
2612  "less than needed (%u)\n",
2613  __func__, buf_info.buf_avail_size, total_bytes_to_read);
2614  abort();
2615  }
2616 
2617  if (buf_info.buf_avail_size == metadata_hdr_size && (!p_ctx->frame_num || !p_data_buffer))
2618  {
2619  if (ni_cmp_fw_api_ver(
2621  "6rE") >= 0)
2622  {
2623  // allocate p_data_buffer to read the first metadata
2624  void *p_metadata_buffer = NULL;
2625  int buffer_size = ((metadata_hdr_size + (NI_MEM_PAGE_ALIGNMENT - 1)) /
2627  if (ni_posix_memalign(&p_metadata_buffer, sysconf(_SC_PAGESIZE), buffer_size))
2628  {
2629  ni_log2(p_ctx, NI_LOG_ERROR,
2630  "ERROR %d: %s() Cannot allocate metadata buffer.\n",
2631  NI_ERRNO, __func__);
2632  retval = NI_RETCODE_ERROR_MEM_ALOC;
2633  LRETURN;
2634  }
2635  p_data_buffer = (uint8_t *)p_metadata_buffer;
2636  get_first_metadata = 1;
2637  if (!p_ctx->frame_num)
2638  sequence_change = 0;
2639  }
2640  }
2641 
2642  if (!p_data_buffer)
2643  {
2644  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: data buffer is null!, return\n");
2645  retval = NI_RETCODE_INVALID_PARAM;
2646  LRETURN;
2647  }
2648 
2649  read_size_bytes = buf_info.buf_avail_size;
2651  if (read_size_bytes % NI_MEM_PAGE_ALIGNMENT)
2652  {
2653  read_size_bytes = ((read_size_bytes / NI_MEM_PAGE_ALIGNMENT) *
2656  }
2657 
2658  retval = ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
2659  p_data_buffer, read_size_bytes, ui32LBA);
2660  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_read, p_ctx->device_type,
2661  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
2662  CHECK_VPU_RECOVERY(retval);
2663  if (retval < 0)
2664  {
2665  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
2667  LRETURN;
2668  } else if (get_first_metadata) {
2669  // got first metadata alone
2670  ni_metadata_dec_frame_t *p_meta =
2671  (ni_metadata_dec_frame_t *)((uint8_t *)p_data_buffer);
2672  ni_log2(p_ctx, NI_LOG_DEBUG, "Got first pkt_delay_cnt %u\n",
2673  p_meta->metadata_common.pkt_delay_cnt);
2674  if (p_ctx->pkt_delay_cnt < p_meta->metadata_common.pkt_delay_cnt)
2675  p_ctx->pkt_delay_cnt = p_meta->metadata_common.pkt_delay_cnt;
2676  get_first_metadata = 0;
2677  ni_aligned_free(p_data_buffer);
2678  goto start;
2679  } else {
2680  // command issued successfully, now exit
2681  ni_metadata_dec_frame_t *p_meta;
2682  p_meta =
2683  (ni_metadata_dec_frame_t *)((uint8_t *)p_frame->p_buffer +
2684  p_frame->data_len[0] +
2685  p_frame->data_len[1] +
2686  p_frame->data_len[2]);
2687 
2688  if (buf_info.buf_avail_size != metadata_hdr_size)
2689  {
2690  low_delay_notify = 1;
2691  sei_size = p_meta->sei_size;
2692  } else if (ni_cmp_fw_api_ver(
2694  "6rE") >= 0)
2695  {
2696  p_meta =
2697  (ni_metadata_dec_frame_t *)((uint8_t *)p_frame->p_buffer);
2698  ni_log2(p_ctx, NI_LOG_DEBUG, "Got pkt_delay_cnt %u\n",
2699  p_meta->metadata_common.pkt_delay_cnt);
2700  if (p_ctx->pkt_delay_cnt < p_meta->metadata_common.pkt_delay_cnt)
2701  p_ctx->pkt_delay_cnt = p_meta->metadata_common.pkt_delay_cnt;
2702  }
2703  total_bytes_to_read = total_bytes_to_read + sei_size;
2704  ni_log2(p_ctx, NI_LOG_DEBUG, "decoder read success, size %d total_bytes_to_read "
2705  "include sei %u sei_size %d\n",
2706  retval, total_bytes_to_read, sei_size);
2707  }
2708 
2709  bytes_read_so_far = total_bytes_to_read ;
2710  // Note: session status is NOT reset but tracked between send
2711  // and recv to catch and recover from a loop condition
2712 
2713  rx_size = ni_create_frame(p_frame, bytes_read_so_far, &frame_offset, false);
2714  p_ctx->frame_pkt_offset = frame_offset;
2715  if (p_ctx->decoder_low_delay > 0 && buf_info.buf_avail_size == metadata_hdr_size &&
2716  p_ctx->enable_low_delay_check)
2717  {
2718  ni_log2(p_ctx, NI_LOG_TRACE, "Low delay mode amd check header if has b frame\n");
2719 
2720  ni_metadata_dec_frame_t *p_meta =
2721  (ni_metadata_dec_frame_t *)((uint8_t *)p_frame->p_buffer +
2722  p_frame->data_len[0] +
2723  p_frame->data_len[1] +
2724  p_frame->data_len[2]);
2725  if (p_meta->metadata_common.has_b_frame == 1)
2726  {
2727  ni_log2(p_ctx, NI_LOG_ERROR,"Warning: session 0x%x decoder lowDelay mode "
2728  "is cancelled due to has_b_frames, frame_num %u\n",
2729  p_ctx->session_id, p_ctx->frame_num);
2730  p_ctx->decoder_low_delay = 0;
2731  }
2732  }
2733 
2734  if (rx_size > 0)
2735  {
2736  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): s-state %d first_frame %d\n", __func__,
2737  p_ctx->session_run_state, p_ctx->is_first_frame);
2738 
2739  int64_t tmp_dts, prev_dts = INT64_MIN, ts_diff = 0;
2740  int nb_diff = 0;
2741  // at stream start, if there are already frames dropped at decoding, drop
2742  // equal number of dts from dts queue to try to align with the first
2743  // returned frame, and calculate the average of dts difference to be used
2744  // for locating the first frame's dts
2745  if (p_ctx->is_first_frame)
2746  {
2747  ni_log2(p_ctx, NI_LOG_DEBUG,
2748  "%s(): First frame : session_id 0x%x, pic_reorder_delay: %d "
2749  "total frames input:%u buffered: %u completed: %u output: %u "
2750  "dropped: %u error: %u\n",
2751  __func__, p_ctx->session_id, p_ctx->pic_reorder_delay,
2758 
2759  if (p_ctx->session_statistic.ui32FramesDropped > 0)
2760  {
2761  for (i = 0; i < p_ctx->session_statistic.ui32FramesDropped; i++)
2762  {
2764  p_ctx->dts_queue, 0, &tmp_dts,
2766  p_ctx->buffer_pool) != NI_RETCODE_SUCCESS)
2767  {
2768  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): FramesDropped pop "
2769  "decoder dts queue %d %ld failed !\n",
2770  __func__, i, tmp_dts);
2771  break;
2772  } else
2773  {
2774  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): FramesDropped pop "
2775  "decoder dts queue %d %ld success !\n",
2776  __func__, i, tmp_dts);
2777  if (prev_dts != INT64_MIN) {
2778  ts_diff += labs(tmp_dts - prev_dts);
2779  nb_diff++;
2780  }
2781  prev_dts = tmp_dts;
2782  }
2783  }
2784  if (nb_diff > 1)
2785  {
2786  ts_diff = ts_diff / nb_diff;
2787  }
2789  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): FramesDropped pop dts "
2790  "average diff: %ld\n", __func__, ts_diff);
2791  }
2792  }
2793  else
2794  {
2796  {
2797  for(i = p_ctx->decoder_last_drop_frame_num; i < p_ctx->session_statistic.ui32FramesDropped; i++)
2798  {
2800  p_ctx->dts_queue, 0, &tmp_dts,
2802  p_ctx->buffer_pool) != NI_RETCODE_SUCCESS)
2803  {
2804  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): FramesDropped pop "
2805  "decoder dts queue %d %ld failed !\n",
2806  __func__, i, tmp_dts);
2807  break;
2808  }
2809  }
2811  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): FramesDropped pop dts %d\n", __func__, p_ctx->decoder_last_drop_frame_num);
2812  }
2813  }
2814 
2816  p_ctx->dts_queue, 0, (int64_t *)&p_frame->dts,
2818  p_ctx->buffer_pool) != NI_RETCODE_SUCCESS)
2819  {
2820  if (p_ctx->last_dts != NI_NOPTS_VALUE && !p_ctx->ready_to_close)
2821  {
2822  p_ctx->pic_reorder_delay++;
2823  p_frame->dts = p_ctx->last_dts + p_ctx->last_dts_interval;
2824  ni_log2(p_ctx, NI_LOG_DEBUG, "Padding DTS: %" PRId64 "\n", p_frame->dts);
2825  } else
2826  {
2827  p_frame->dts = NI_NOPTS_VALUE;
2828  }
2829  }
2830 
2831  if (p_ctx->is_first_frame)
2832  {
2833  for (i = 0; i < p_ctx->pic_reorder_delay; i++)
2834  {
2835  if (p_ctx->last_pts == NI_NOPTS_VALUE &&
2836  p_ctx->last_dts == NI_NOPTS_VALUE)
2837  {
2838  // If the p_frame->pts is unknown in the very beginning we assume
2839  // p_frame->pts == 0 as well as DTS less than PTS by 1000 * 1/timebase
2840  if (p_frame->pts >= p_frame->dts &&
2841  p_frame->pts - p_frame->dts < 1000)
2842  {
2843  break;
2844  }
2845  }
2846 
2848  p_ctx->dts_queue, 0, (int64_t *)&p_frame->dts,
2850  p_ctx->frame_num % 500 == 0,
2851  p_ctx->buffer_pool) != NI_RETCODE_SUCCESS)
2852  {
2853  p_frame->dts = NI_NOPTS_VALUE;
2854  } else
2855  {
2856  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): pic_reorder_delay pop "
2857  "decoder dts queue %d %ld success !\n",
2858  __func__, i, p_frame->dts);
2859  }
2860  }
2861  // Reset for DTS padding counting
2862  p_ctx->pic_reorder_delay = 0;
2863  }
2864  if (p_ctx->codec_format == NI_CODEC_FORMAT_JPEG)//fw won't save frameoffset when decoding jpeg.
2865  {
2866  if (p_ctx->is_first_frame)
2867  {
2868  p_ctx->is_first_frame = 0;
2869  }
2870  p_frame->pts = p_ctx->pts_offsets[p_ctx->frame_num % NI_FIFO_SZ];
2871  p_frame->flags = p_ctx->flags_array[p_ctx->frame_num % NI_FIFO_SZ];
2872  p_frame->pkt_pos = p_ctx->pkt_pos[p_ctx->frame_num % NI_FIFO_SZ];
2873  ni_log2(p_ctx, NI_LOG_DEBUG, "p_frame->pts = %u, frame_num = %d, p_frame->dts = %u\n",
2874  p_frame->pts, p_ctx->frame_num, p_frame->dts);
2875  }
2876  else
2877  {
2878  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: frame_offset %" PRIu64 "\n", __func__,
2879  frame_offset);
2880 
2881  // search for the pkt_offsets of received frame according to frame_offset.
2882  // 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)
2883  // i = -1 if not found
2884  i = rotated_array_binary_search(p_ctx->pkt_offsets_index_min,
2885  p_ctx->pkt_offsets_index, NI_FIFO_SZ,
2886  frame_offset);
2887  if (i >= 0)
2888  {
2889  p_frame->pts = p_ctx->pts_offsets[i];
2890  p_frame->flags = p_ctx->flags_array[i];
2891  p_frame->pkt_pos = p_ctx->pkt_pos[i];
2892  ni_log2(p_ctx, NI_LOG_DEBUG,
2893  "%s: (found pts) dts %" PRId64 " pts "
2894  "%" PRId64 " frame_offset %" PRIu64 " i %d "
2895  "pkt_offsets_index_min %" PRIu64 " pkt_offsets_index "
2896  "%" PRIu64 " pkt_pos %" PRIu64 " \n",
2897  __func__, p_frame->dts, p_frame->pts, frame_offset, i,
2898  p_ctx->pkt_offsets_index_min[i],
2899  p_ctx->pkt_offsets_index[i],
2900  p_ctx->pkt_pos[i]);
2901 
2902  if (p_ctx->is_first_frame)
2903  {
2904  // if the first frame is I frame and there are dropped frames,
2905  // find the dts closest to its pts using the average of dts diff
2906  if (p_frame->dts != NI_NOPTS_VALUE &&
2907  p_frame->pts != NI_NOPTS_VALUE &&
2908  PIC_TYPE_I == p_frame->ni_pict_type &&
2909  p_ctx->session_statistic.ui32FramesDropped > 0 &&
2910  ts_diff > 0)
2911  {
2912  while (p_frame->dts < p_frame->pts &&
2913  labs(p_frame->pts - p_frame->dts) > ts_diff)
2914  {
2915  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: First I frame pts %ld "
2916  "dts %ld diff. %ld > ts_diff %ld\n",
2917  __func__, p_frame->pts, p_frame->dts,
2918  labs(p_frame->pts - p_frame->dts), ts_diff);
2919 
2921  p_ctx->dts_queue, 0, (int64_t *)&p_frame->dts,
2923  {
2924  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): First I frame "
2925  "pop decoder dts queue error.\n", __func__);
2926  break;
2927  }
2928  }
2929  }
2930  }
2931 
2932  p_frame->p_custom_sei_set = p_ctx->pkt_custom_sei_set[i];
2933  p_ctx->pkt_custom_sei_set[i] = NULL;
2934  } else
2935  {
2936  // backup solution pts
2937  if (p_param->dec_input_params.skip_pts_guess && p_ctx->last_pts != NI_NOPTS_VALUE)
2938  {
2939  // if skip guess_correct_pts, use pts interval to get the correct pts
2940  p_frame->pts = p_ctx->last_pts + (p_ctx->last_pts_interval > 0 ? p_ctx->last_pts_interval : 1);
2941  }
2942  else
2943  {
2944  p_frame->pts = p_ctx->last_pts + (p_frame->dts - p_ctx->last_dts);
2945  }
2946  p_frame->pkt_pos = p_ctx->last_pkt_pos + (frame_offset - p_ctx->last_frame_offset);
2947  ni_log2(p_ctx, NI_LOG_ERROR,
2948  "ERROR: Frame pts %" PRId64 " not found for offset "
2949  "%" PRIu64 "\n", p_frame->pts, frame_offset);
2950  ni_log2(p_ctx, NI_LOG_DEBUG,
2951  "%s: (not found use default) dts %" PRId64 " pts %" PRId64
2952  "\n",
2953  __func__, p_frame->dts, p_frame->pts);
2954  }
2955 
2956  if (p_ctx->is_first_frame)
2957  {
2958  p_ctx->is_first_frame = 0;
2959  }
2960  }
2961  p_frame->orignal_pts = p_frame->pts;
2962  p_ctx->last_pkt_pos = p_frame->pkt_pos;
2963  p_ctx->last_frame_offset = frame_offset;
2964  if (!p_param->dec_input_params.skip_pts_guess)
2965  p_frame->pts = guess_correct_pts(p_ctx, p_frame->pts, p_frame->dts);
2966  if (p_frame->pts != NI_NOPTS_VALUE && p_ctx->last_pts != NI_NOPTS_VALUE)
2967  p_ctx->last_pts_interval = p_frame->pts - p_ctx->last_pts;
2968  p_ctx->last_pts = p_frame->pts;
2969  if (p_frame->dts != NI_NOPTS_VALUE && p_ctx->last_dts != NI_NOPTS_VALUE)
2970  p_ctx->last_dts_interval = p_frame->dts - p_ctx->last_dts;
2971  p_ctx->last_dts = p_frame->dts;
2972  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: (best_effort_timestamp) pts %" PRId64 "\n",
2973  __func__, p_frame->pts);
2974  p_ctx->frame_num++;
2975 
2977  if (p_frame->error_ratio > 0)
2978  {
2979  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: frame number = %d, error_ratio = %u\n",
2980  __func__, p_ctx->frame_num, p_frame->error_ratio);
2981  }
2982 
2983 #ifdef MEASURE_LATENCY
2984 #ifndef XCODER_311
2985  ni_log2(p_ctx, NI_LOG_INFO, "DEC pkt_num %d, fme_num %d, latecy is %d\n",
2986  p_ctx->pkt_num, p_ctx->frame_num, p_ctx->pkt_num - p_ctx->frame_num);
2987 #endif
2988 #endif
2989 
2990 #ifdef XCODER_DUMP_DATA
2991  char dump_file[256];
2992  snprintf(dump_file, sizeof(dump_file), "%ld-%u-dec-fme/fme-%04ld.yuv",
2993  (long)getpid(), p_ctx->session_id, (long)p_ctx->frame_num);
2994  FILE *f = fopen(dump_file, "wb");
2995  fwrite(p_frame->p_buffer,
2996  p_frame->data_len[0] + p_frame->data_len[1] + p_frame->data_len[2],
2997  1, f);
2998  fflush(f);
2999  fclose(f);
3000 #endif
3001  }
3002 
3003  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): received data: [0x%08x]\n", __func__, rx_size);
3004  ni_log2(p_ctx, NI_LOG_DEBUG,
3005  "%s(): p_frame->start_of_stream=%u, "
3006  "p_frame->end_of_stream=%u, p_frame->video_width=%u, "
3007  "p_frame->video_height=%u\n",
3008  __func__, p_frame->start_of_stream, p_frame->end_of_stream,
3009  p_frame->video_width, p_frame->video_height);
3010  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): p_frame->data_len[0/1/2]=%u/%u/%u\n", __func__,
3011  p_frame->data_len[0], p_frame->data_len[1], p_frame->data_len[2]);
3012 
3013  if (p_ctx->frame_num % 500 == 0)
3014  {
3015  ni_log2(p_ctx, NI_LOG_DEBUG,
3016  "Decoder pts queue size = %u dts queue size = %u\n\n",
3017  p_ctx->pts_table->list.count, p_ctx->dts_queue->list.count);
3018  // scan and clean up
3020  p_ctx->buffer_pool);
3021  }
3022 
3023 #ifdef MEASURE_LATENCY
3024  if ((p_frame->dts != NI_NOPTS_VALUE) && (p_ctx->frame_time_q != NULL))
3025  {
3026  uint64_t abs_time_ns = ni_gettime_ns();
3028  ni_log2(p_ctx, NI_LOG_INFO, "DTS:%" PRId64 ",DELTA:%" PRId64 ",dLAT:%" PRIu64 ";\n",
3029  p_frame->dts, abs_time_ns - q->last_benchmark_time,
3030  ni_lat_meas_q_check_latency(q, abs_time_ns, p_frame->dts));
3031  q->last_benchmark_time = abs_time_ns;
3032  }
3033 #endif
3034 
3035 END:
3036 
3037  ni_pthread_mutex_unlock(&p_ctx->mutex);
3038 
3039  if (get_first_metadata && p_data_buffer)
3040  ni_aligned_free(p_data_buffer);
3041  if (sequence_change && p_ctx->frame_num)
3042  {
3043  if (p_ctx->actual_video_width == p_frame->video_width &&
3044  p_ctx->active_video_height == p_frame->video_height)
3045  {
3046  p_ctx->pixel_format_changed = 1;
3047  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): format changed\n", __func__);
3048  }
3049  }
3050 
3051  if (NI_RETCODE_SUCCESS != retval)
3052  {
3053  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): bad exit, retval = %d\n", __func__, retval);
3054  if (retval == NI_RETCODE_ERROR_VPU_RECOVERY)
3055  {
3056  low_delay_signal(p_ctx);
3057  }
3058  return retval;
3059  } else
3060  {
3061  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit, rx_size = %d\n", __func__, rx_size);
3062  if (low_delay_notify)
3063  {
3064  low_delay_signal(p_ctx);
3065  }
3066  return rx_size;
3067  }
3068 }
3069 
3070 /*!******************************************************************************
3071  * \brief Query current xcoder status
3072  *
3073  * \param
3074  *
3075  * \return
3076  *******************************************************************************/
3078  ni_device_type_t device_type)
3079 {
3081  int retval = NI_RETCODE_SUCCESS;
3082 
3083  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): device_type %d:%s; enter\n", __func__,
3084  device_type, g_device_type_str[device_type]);
3085 
3086  if (!p_ctx)
3087  {
3088  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
3089  __func__);
3090  retval = NI_RETCODE_INVALID_PARAM;
3091  LRETURN;
3092  }
3093 
3094  retval = ni_query_general_status(p_ctx, device_type, &data);
3095  if (NI_RETCODE_SUCCESS == retval)
3096  {
3100  p_ctx->overall_load_query.admin_queried = (uint32_t)data.admin_nsid;
3101  p_ctx->load_query.current_load =
3102  (uint32_t)data.process_load_percent | (uint32_t)(data.process_load_percent_upper << 8);
3103  p_ctx->load_query.fw_model_load = (uint32_t)data.fw_model_load;
3104  p_ctx->load_query.fw_load = (uint32_t)data.fw_load;
3106 
3107  switch (device_type)
3108  {
3111  case NI_DEVICE_TYPE_UPLOAD:
3112  if (p_ctx->load_query.fw_load == 1 &&
3114  {
3115  // ignore negligible non-video traffic
3116  p_ctx->load_query.fw_load -= 1;
3117  }
3118  break;
3119  default:
3120  break;
3121  }
3122  p_ctx->load_query.total_contexts = (uint32_t)data.active_sub_instances_cnt;
3123  p_ctx->load_query.fw_video_mem_usage = (uint32_t)data.fw_video_mem_usage;
3125  p_ctx->load_query.fw_share_mem_usage = (uint32_t)data.fw_share_mem_usage;
3126  p_ctx->load_query.fw_p2p_mem_usage = (uint32_t)data.fw_p2p_mem_usage;
3128  (uint32_t)data.active_hwupload_sub_inst_cnt;
3129  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 "
3130  "total_contexts:%u fw_video_mem_usage:%u "
3131  "fw_video_shared_mem_usage:%u fw_share_mem_usage:%u "
3132  "fw_p2p_mem_usage:%u active_hwuploaders:%u\n", __func__,
3133  p_ctx->blk_dev_name,
3134  p_ctx->blk_xcoder_name,
3135  p_ctx->dev_xcoder_name,
3136  p_ctx->load_query.current_load,
3137  p_ctx->load_query.fw_model_load,
3138  p_ctx->load_query.fw_load,
3139  p_ctx->load_query.total_contexts,
3145  if (p_ctx->overall_load_query.admin_queried)
3146  {
3147  ni_log2(p_ctx, NI_LOG_DEBUG,
3148  "Overall load %u, model load %u, instance count %u\n",
3152  }
3153  }
3154 
3155 END:
3156 
3157  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
3158 
3159  return retval;
3160 }
3161 
3162 /*!******************************************************************************
3163  * \brief Query current xcoder status
3164  *
3165  * \param
3166  *
3167  * \return
3168  *******************************************************************************/
3170  ni_device_type_t device_type, void *detail_data, int ver)
3171 {
3172  int retval = NI_RETCODE_SUCCESS;
3173 
3174  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): device_type %d:%s; enter\n", __func__,
3175  device_type, g_device_type_str[device_type]);
3176 
3177  if (!p_ctx)
3178  {
3179  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
3180  __func__);
3181  retval = NI_RETCODE_INVALID_PARAM;
3182  LRETURN;
3183  }
3184 
3185  retval = ni_query_detail_status(p_ctx, device_type, detail_data, ver);
3186 
3187 END:
3188 
3189  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
3190 
3191  return retval;
3192 }
3193 
3194 /*!******************************************************************************
3195  * \brief Open a xcoder encoder instance
3196  *
3197  * \param
3198  *
3199  * \return
3200  *******************************************************************************/
3201 
3203 {
3205  ni_xcoder_params_t *p_param;
3206  void *p_buffer = NULL;
3207  ni_instance_buf_info_t buf_info = {0};
3208  uint32_t ui32LBA = 0;
3209  uint32_t max_cu_size;
3210  uint32_t block_size;
3211  int32_t width, height;
3212  int m_threshold = 0;
3213  char fmt_fw_api_ver1[5], fmt_fw_api_ver2[5];
3214 
3215  if (!p_ctx || !p_ctx->p_session_config)
3216  {
3217  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
3218  __func__);
3219  retval = NI_RETCODE_INVALID_PARAM;
3220  LRETURN;
3221  }
3222 
3223  p_param = (ni_xcoder_params_t *)p_ctx->p_session_config;
3224  bool isrgba = (p_ctx->pixel_format == NI_PIX_FMT_ABGR || p_ctx->pixel_format == NI_PIX_FMT_ARGB
3225  || p_ctx->pixel_format == NI_PIX_FMT_RGBA || p_ctx->pixel_format == NI_PIX_FMT_BGRA);
3226  if (isrgba)
3227  {
3229  || p_param->source_width < NI_MIN_WIDTH || p_param->source_height < NI_MIN_HEIGHT)
3230  {
3231  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() RGBA / BGRA / ARGB / AGBR resolution invalid, return\n",
3232  __func__);
3233  retval = NI_RETCODE_INVALID_PARAM;
3234  LRETURN;
3235  }
3236  }
3237 
3238  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter hwframes = %d\n", __func__,
3239  p_param->hwframes);
3240 
3241  // calculate encoder ROI map size: each QP info takes 8-bit, represent 8 x 8
3242  // pixel block
3243  max_cu_size = (NI_CODEC_FORMAT_H264 == p_ctx->codec_format) ? 16 : 64;
3244 
3245  width = p_param->source_width;
3246  height = p_param->source_height;
3247  // AV1 non-8x8-aligned resolution is implicitly cropped due to Quadra HW limitation
3248  if (NI_CODEC_FORMAT_AV1 == p_ctx->codec_format)
3249  {
3250  width = (width / 8) * 8;
3251  height = (height / 8) * 8;
3252  }
3253 
3254  block_size =
3255  ((width + max_cu_size - 1) & (~(max_cu_size - 1))) *
3256  ((height + max_cu_size - 1) & (~(max_cu_size - 1))) /
3257  (8 * 8);
3258  p_ctx->roi_len = ((block_size + 63) & (~63)); // align to 64 bytes
3259 
3260  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): finish init\n", __func__);
3261 
3262  //Check if there is an instance or we need a new one
3263  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
3264  {
3266  p_ctx->pts_table = NULL;
3267  p_ctx->dts_queue = NULL;
3268  p_ctx->buffer_pool = NULL;
3269  p_ctx->status = 0;
3270  p_ctx->key_frame_type = 0;
3271  p_ctx->keyframe_factor = 1;
3272  p_ctx->frame_num = 0;
3273  p_ctx->pkt_num = 0;
3274  p_ctx->av1_pkt_num = 0;
3275  p_ctx->rc_error_count = 0;
3276  p_ctx->force_frame_type = 0;
3277  p_ctx->ready_to_close = 0;
3278  p_ctx->auto_dl_handle = 0;
3279  //Sequence change tracking related stuff
3280  p_ctx->active_video_width = 0;
3281  p_ctx->active_video_height = 0;
3282  p_ctx->p_all_zero_buf = NULL;
3283  p_ctx->actual_video_width = 0;
3284  p_ctx->enc_pts_w_idx = 0;
3285  p_ctx->enc_pts_r_idx = 0;
3286  p_ctx->session_timestamp = 0;
3287  memset(p_ctx->pkt_custom_sei_set, 0, NI_FIFO_SZ * sizeof(ni_custom_sei_set_t *));
3288  memset(&(p_ctx->param_err_msg[0]), 0, sizeof(p_ctx->param_err_msg));
3290  {
3292  }
3293 
3294  //malloc zero data buffer
3295  if (ni_posix_memalign(&p_ctx->p_all_zero_buf, sysconf(_SC_PAGESIZE),
3297  {
3298  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() alloc all zero buffer failed\n",
3299  NI_ERRNO, __func__);
3300  retval = NI_RETCODE_ERROR_MEM_ALOC;
3301  LRETURN;
3302  }
3303  memset(p_ctx->p_all_zero_buf, 0, NI_DATA_BUFFER_LEN);
3304 
3305  //malloc data buffer
3306  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), NI_DATA_BUFFER_LEN))
3307  {
3308  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() alloc data buffer failed\n",
3309  NI_ERRNO, __func__);
3311  retval = NI_RETCODE_ERROR_MEM_ALOC;
3312  LRETURN;
3313  }
3314  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
3315 
3316  //Set session ID to be invalid. In case we cannot open session, the session id wold remain invalid.
3317  //In case we can open sesison, the session id would become valid.
3318  ((ni_session_stats_t *)p_buffer)->ui16SessionId =
3319  (uint16_t)NI_INVALID_SESSION_ID;
3320 
3321  // First uint32_t is either an invaild session ID or a valid session ID, depending on if session could be opened
3323  ni_htonl(p_ctx->codec_format), 0);
3324  ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_buffer,
3325  NI_DATA_BUFFER_LEN, ui32LBA);
3326  //Open will return a session status structure with a valid session id if it worked.
3327  //Otherwise the invalid session id set before the open command will stay
3328  p_ctx->session_id =
3329  ni_ntohs(((ni_session_stats_t *)p_buffer)->ui16SessionId);
3330  p_ctx->session_timestamp = ni_htonl(((ni_session_stats_t *)p_buffer)->ui32Session_timestamp_high);
3331  p_ctx->session_timestamp = (p_ctx->session_timestamp << 32) |
3332  ni_htonl(((ni_session_stats_t *)p_buffer)->ui32Session_timestamp_low);
3333  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
3334  {
3335  ni_log2(p_ctx, NI_LOG_ERROR,
3336  "ERROR %s(): p_ctx->device_handle=%" PRIx64 ", "
3337  "p_ctx->hw_id=%d, p_ctx->session_id=%d\n",
3338  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
3339  p_ctx->session_id);
3340  ni_encoder_session_close(p_ctx, 0);
3342  LRETURN;
3343  }
3344  ni_log2(p_ctx, NI_LOG_DEBUG, "Encoder open session ID:0x%x timestamp:%" PRIu64 "\n",
3345  p_ctx->session_id, p_ctx->session_timestamp);
3346 
3347  //Send keep alive timeout Info
3348  uint64_t keep_alive_timeout =
3349  p_ctx->keep_alive_timeout * 1000000; //send us to FW
3350  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
3351  memcpy(p_buffer, &keep_alive_timeout, sizeof(keep_alive_timeout));
3352  ni_log2(p_ctx, NI_LOG_DEBUG, "%s keep_alive_timeout %" PRIx64 "\n", __func__,
3353  keep_alive_timeout);
3355  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
3356  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
3357  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config, p_ctx->device_type,
3358  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
3359  CHECK_VPU_RECOVERY(retval);
3360 
3361  if (NI_RETCODE_SUCCESS != retval)
3362  {
3363  ni_log2(p_ctx, NI_LOG_ERROR,
3364  "ERROR %s(): nvme write keep_alive_timeout command "
3365  "failed, blk_io_handle: %" PRIx64 ", hw_id, %d\n",
3366  __func__, (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id);
3368  LRETURN;
3369  }
3370 
3371  // Send SW version to FW if FW API version is >= 6.2
3373  "62") >= 0)
3374  {
3375  // Send SW version to session manager
3376  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
3377  memcpy(p_buffer, NI_XCODER_REVISION, sizeof(uint64_t));
3379  ni_fmt_fw_api_ver_str((char*) &p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], &fmt_fw_api_ver2[0]);
3380  ni_log2(p_ctx, NI_LOG_DEBUG, "%s libxcoder FW API ver %s, FW FW API ver %s\n",
3381  __func__, fmt_fw_api_ver1, fmt_fw_api_ver2);
3382  ui32LBA = CONFIG_SESSION_SWVersion_W(p_ctx->session_id);
3383  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
3384  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
3385  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config, p_ctx->device_type,
3386  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
3387  CHECK_VPU_RECOVERY(retval);
3388 
3389  if (NI_RETCODE_SUCCESS != retval)
3390  {
3391  ni_log2(p_ctx, NI_LOG_ERROR,
3392  "ERROR %s(): nvme write sw_version command "
3393  "failed, blk_io_handle: %" PRIx64 ", hw_id, %d\n",
3394  __func__, (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id);
3396  LRETURN;
3397  }
3398 
3399  if (ni_cmp_fw_api_ver((char*) &p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6rc") >= 0)
3400  {
3401  // For FW API ver 6rc or newer, initialize with the most current size
3402  p_ctx->meta_size = sizeof(ni_metadata_enc_bstream_t);
3403  }
3405  "6p") >= 0)
3406  // For FW API ver 6.p or newer, initialize with the most current size
3408  else
3409  // For FW API ver 6.2 or newer, initialize with the most current size
3411  }
3412  else
3413  {
3414  // For FW API ver 6.1 or older, initialize with metadata size 32
3416  }
3417 
3418  // Open AI session for AI Enhance
3419  if(p_param->enable_ai_enhance)
3420  {
3421  // Check if the bit depth & resoultion
3422  if((p_ctx->bit_depth_factor != 1) ||
3423  ((p_param->source_width != 1280 && p_param->source_height != 720) &&
3424  (p_param->source_width != 1920 && p_param->source_height != 1080) &&
3425  (p_param->source_width != 3840 && p_param->source_height != 2160) &&
3426  (p_param->source_width != 720 && p_param->source_height != 1280) &&
3427  (p_param->source_width != 768 && p_param->source_height != 1280) &&
3428  (p_param->source_width != 1080 && p_param->source_height != 1920) &&
3429  (p_param->source_width != 2496 && p_param->source_height != 1080)))
3430  {
3431  p_param->enable_ai_enhance = 0;
3432  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);
3433  ni_log2(p_ctx, NI_LOG_ERROR, "The bit depth or resoultion is not supported on ai enhance mode\n");
3434  }else{
3435  ni_log2(p_ctx, NI_LOG_DEBUG, "Adding ai enhance session to encoder\n");
3436  ui32LBA = OPEN_ADD_CODEC(NI_DEVICE_TYPE_AI, 0, ni_htons(p_ctx->session_id));
3437  retval = ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
3438  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
3439 
3440  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_open,
3441  p_ctx->device_type, p_ctx->hw_id,
3442  &(p_ctx->session_id), OPT_1);
3443  if (NI_RETCODE_SUCCESS != retval)
3444  {
3445  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR couldn't add ai to encoder session\n");
3446  ni_log2(p_ctx, NI_LOG_ERROR,
3447  "ERROR %s(): p_ctx->device_handle=%" PRIx64 ", "
3448  "p_ctx->hw_id=%d, p_ctx->session_id=%d\n",
3449  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
3450  p_ctx->session_id);
3451  ni_encoder_session_close(p_ctx, 0);
3452  LRETURN;
3453  }
3454  }
3455  }
3456 
3457  if (p_param->ddr_priority_mode > NI_DDR_PRIORITY_NONE)
3458  {
3459  retval = ni_device_set_ddr_configuration(p_ctx, p_param->ddr_priority_mode);
3460  if (NI_RETCODE_SUCCESS != retval)
3461  {
3462  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): ddr priority setting failure for %s\n",
3463  __func__, strerror(NI_ERRNO));
3464  LRETURN;
3465  }
3466  }
3467 
3468  ni_log2(p_ctx, NI_LOG_DEBUG, "Open session completed\n");
3469  }
3470 
3471  /* Modify mmap_threshold and trim_threshold of malloc */
3472  // calculate the default threshold which equals to the resolution + padding size.
3473  // 64 is AV_INPUT_BUFFER_PADDING_SIZE
3474  m_threshold = p_param->source_width * p_param->source_height * 3 / 2 *
3475  p_ctx->bit_depth_factor + 64;
3476  m_threshold = ((m_threshold + (NI_MEM_PAGE_ALIGNMENT - 1)) /
3478 
3479  // The upper limit is DEFAULT_MMAP_THRESHOLD_MAX: 512*1024 on 32-bit systems,
3480  // or 4*1024*1024*sizeof(long) on 64-bit systems.
3481  // This macro is not defined in header file.
3482  // So, assume that is running on 64-bit systems
3483  if (m_threshold > 4 * 1024 * 1024 * sizeof(long))
3484  {
3485  ni_log2(p_ctx, NI_LOG_INFO, "Warning: m_threshold (%d) is bigger than "
3486  "DEFAULT_MMAP_THRESHOLD_MAX, use default value (%d)\n",
3487  m_threshold, 4 * 1024 * 1024 * sizeof(long));
3488  m_threshold = 4 * 1024 * 1024 * sizeof(long);
3489  }
3490 
3491  if (p_param->staticMmapThreshold) // Set Static Mmap Threshold
3492  {
3493 #if defined(__linux__) && !defined(_ANDROID) && !defined(__OPENHARMONY__)
3494  // If the malloc buffer is larger than the threshold,
3495  // glibc will use mmap to malloc the memory, which is a low speed method.
3496  // So, set the M_MMAP_THRESHOLD >= 1 yuv buffer size here.
3497  if (mallopt(M_MMAP_THRESHOLD, m_threshold) == 0)
3498  {
3499  ni_log2(p_ctx, NI_LOG_ERROR, "Error: could not set M_MMAP_THRESHOLD to %d\n",
3500  m_threshold);
3501  retval = NI_RETCODE_ERROR_MEM_ALOC;
3502  LRETURN;
3503  }
3504 
3505  // When the amount of contiguous free memory at the top of the heap
3506  // grows larger than M_TRIM_THRESHOLD,
3507  // free(3) employs sbrk(2) to release this memory back to the system.
3508  // So, set the M_TRIM_THRESHOLD to 3*m_threshold,
3509  // to avoid an inefficient case, frequently releasing and requesting physical memory.
3510  if (mallopt(M_TRIM_THRESHOLD, 3 * m_threshold) == 0)
3511  {
3512  ni_log2(p_ctx, NI_LOG_ERROR, "Error: could not set M_TRIM_THRESHOLD to %ds\n",
3513  3 * m_threshold);
3514  retval = NI_RETCODE_ERROR_MEM_ALOC;
3515  LRETURN;
3516  }
3517 #else
3518  ni_log2(p_ctx, NI_LOG_INFO,
3519  "Warning: this platform does not support staticMmapThreshold\n");
3520 #endif
3521  }
3522  else // default
3523  {
3524  // a way to dynamically trigger a pre-mature increase in mmap&trim threshold
3525  // make the mmap_threshold larger than frame size to avoid frequent page faults
3526  m_threshold = (int) (m_threshold * 1.25); // An experience value
3527  void *temp_buf = malloc((size_t) m_threshold);
3528  ni_log2(p_ctx, NI_LOG_TRACE, "trigger a pre-mature increase in mmap&trim threshold: 0x%p = malloc(%d)\n",
3529  temp_buf, m_threshold);
3530  free(temp_buf);
3531  }
3532 
3533  if (p_ctx->hw_action == NI_CODEC_HW_ENABLE)
3534  {
3535  ni_device_capability_t sender_cap, receiver_cap;
3536  bool device_in_ctxt = true;
3537  retval = ni_device_capability_query2(p_ctx->sender_handle, &sender_cap, device_in_ctxt);
3538  if (retval != NI_RETCODE_SUCCESS)
3539  {
3540  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: ni_device_capability_query2 returned %d\n",
3541  retval);
3542  LRETURN;
3543  }
3544  retval = ni_device_capability_query2(p_ctx->blk_io_handle, &receiver_cap, device_in_ctxt);
3545  if (retval != NI_RETCODE_SUCCESS)
3546  {
3547  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: ni_device_capability_query2 returned %d\n",
3548  retval);
3549  LRETURN;
3550  }
3551 
3552  for (uint8_t ui8Index = 0; ui8Index < 20; ui8Index++)
3553  {
3554  if (sender_cap.serial_number[ui8Index] !=
3555  receiver_cap.serial_number[ui8Index])
3556  {
3557  // QDFW-315 Autodownload
3558  p_ctx->auto_dl_handle = p_ctx->sender_handle;
3559  ni_log2(p_ctx, NI_LOG_DEBUG, "Autodownload device handle set %p!\n",
3560  p_ctx->auto_dl_handle);
3561  p_ctx->hw_action = NI_CODEC_HW_NONE;
3562  break;
3563  }
3564  else
3565  {
3567  "6m") >= 0)
3568  {
3569  ni_device_vf_ns_id_t sender_vf_ns_id = {0};
3570  ni_device_vf_ns_id_t curr_vf_ns_id = {0};
3571  retval = ni_query_vf_ns_id(p_ctx->sender_handle, &sender_vf_ns_id, p_ctx->fw_rev);
3572  if (retval != NI_RETCODE_SUCCESS)
3573  {
3574  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: calling ni_query_vf_ns_id(): "
3575  "p_ctx->sender_handle=%" PRIx64 ", p_ctx->hw_id=%d, p_ctx->session_id=%u\n",
3576  (int64_t)p_ctx->sender_handle, p_ctx->hw_id, p_ctx->session_id);
3577  ni_encoder_session_close(p_ctx, 0);
3578  LRETURN;
3579  }
3580  retval = ni_query_vf_ns_id(p_ctx->blk_io_handle, &curr_vf_ns_id, p_ctx->fw_rev);
3581  if (retval != NI_RETCODE_SUCCESS)
3582  {
3583  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: calling ni_query_vf_ns_id(): "
3584  "p_ctx->blk_io_handle=%" PRIx64 ", p_ctx->hw_id=%d, p_ctx->session_id=%u\n",
3585  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
3586  ni_encoder_session_close(p_ctx, 0);
3587  LRETURN;
3588  }
3589 
3590  if ((sender_vf_ns_id.ns_id != curr_vf_ns_id.ns_id) ||
3591  (sender_vf_ns_id.vf_id != curr_vf_ns_id.vf_id))
3592  {
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  }
3600  }
3601  }
3602  }
3603 
3604  retval = ni_config_instance_set_encoder_params(p_ctx);
3605  if (NI_RETCODE_SUCCESS != retval)
3606  {
3607  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: calling ni_config_instance_set_encoder_params(): "
3608  "p_ctx->device_handle=%" PRIx64 ", p_ctx->hw_id=%d, p_ctx->session_id=%d\n",
3609  (int64_t)p_ctx->device_handle, p_ctx->hw_id, p_ctx->session_id);
3610  ni_encoder_session_close(p_ctx, 0);
3611  LRETURN;
3612  }
3613  if(p_param->enable_ai_enhance)
3614  {
3615  retval = ni_ai_query_network_ready(p_ctx);
3616  if(retval != NI_RETCODE_SUCCESS)
3617  {
3618  ni_log2(p_ctx, NI_LOG_ERROR,
3619  "ERROR: ni_ai_query_network_ready returned %d\n", retval);
3620  LRETURN;
3621  }
3622  }
3623 
3624  ni_timestamp_init(p_ctx, &p_ctx->pts_table, "enc_pts");
3625  ni_timestamp_init(p_ctx, &p_ctx->dts_queue, "enc_dts");
3626 
3627  // init close caption SEI header and trailer
3628  memcpy(p_ctx->itu_t_t35_cc_sei_hdr_hevc, g_itu_t_t35_cc_sei_hdr_hevc,
3630  memcpy(p_ctx->itu_t_t35_cc_sei_hdr_h264, g_itu_t_t35_cc_sei_hdr_h264,
3632  memcpy(p_ctx->sei_trailer, g_sei_trailer, NI_CC_SEI_TRAILER_LEN);
3633  // init hdr10+ SEI header
3634  memcpy(p_ctx->itu_t_t35_hdr10p_sei_hdr_hevc, g_itu_t_t35_hdr10p_sei_hdr_hevc,
3636  memcpy(p_ctx->itu_t_t35_hdr10p_sei_hdr_h264, g_itu_t_t35_hdr10p_sei_hdr_h264,
3638 
3639  // query to check the final encoder config status
3640  for (;;)
3641  {
3643  NI_DEVICE_TYPE_ENCODER, &buf_info);
3644 
3645  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
3646  p_ctx->device_type, p_ctx->hw_id,
3647  &(p_ctx->session_id), OPT_1);
3648 
3650  || NI_RETCODE_FAILURE == retval)
3651  {
3652  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() line-%d return %d\n", __func__, __LINE__, retval);
3653  LRETURN;
3654  }
3655  else if (buf_info.buf_avail_size > 0)
3656  {
3657  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): buf_avail_size %u\n", __func__,
3658  buf_info.buf_avail_size);
3659  break;
3660  }
3661  else
3662  {
3663  ni_usleep(1000);
3664  }
3665  }
3666 
3667  ni_log2(p_ctx, NI_LOG_DEBUG,
3668  "%s(): p_ctx->device_handle=%" PRIx64 ", p_ctx->hw_id=%d, "
3669  "p_ctx->session_id=%d\n",
3670  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
3671  p_ctx->session_id);
3672 
3673 #ifdef XCODER_DUMP_DATA
3674  char dir_name[256] = {0};
3675  snprintf(dir_name, sizeof(dir_name), "%ld-%u-enc-pkt", (long)getpid(),
3676  p_ctx->session_id);
3677  DIR *dir = opendir(dir_name);
3678  if (!dir && ENOENT == NI_ERRNO)
3679  {
3680  mkdir(dir_name, S_IRWXU | S_IRWXG | S_IRWXO);
3681  ni_log2(p_ctx, NI_LOG_DEBUG, "Encoder pkt dump dir created: %s\n", dir_name);
3682  }
3683 
3684  if(dir){
3685  closedir(dir);
3686  }
3687 
3688  snprintf(dir_name, sizeof(dir_name), "%ld-%u-enc-fme", (long)getpid(),
3689  p_ctx->session_id);
3690  dir = opendir(dir_name);
3691  if (!dir && ENOENT == NI_ERRNO)
3692  {
3693  mkdir(dir_name, S_IRWXU | S_IRWXG | S_IRWXO);
3694  ni_log2(p_ctx, NI_LOG_DEBUG, "Encoder frame dump dir created: %s\n", dir_name);
3695  }
3696  if(dir){
3697  closedir(dir);
3698  }
3699 #endif
3700 
3701 END:
3702 
3703  ni_aligned_free(p_buffer);
3704  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
3705 
3706  return retval;
3707 }
3708 
3709 /*!******************************************************************************
3710  * \brief Flush encoder output
3711  *
3712  * \param
3713  *
3714  * \return
3715  *******************************************************************************/
3717 {
3718  ni_retcode_t retval;
3719  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
3720  if (!p_ctx)
3721  {
3722  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
3723  __func__);
3724  retval = NI_RETCODE_INVALID_PARAM;
3725  LRETURN;
3726  }
3727 
3728  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
3729  {
3730  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
3731  __func__);
3733  LRETURN;
3734  }
3735 
3737  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
3738  p_ctx->device_type, p_ctx->hw_id,
3739  &(p_ctx->session_id), OPT_1);
3740  CHECK_VPU_RECOVERY(retval);
3741 
3742 END:
3743 
3744  if (NI_RETCODE_SUCCESS != retval)
3745  {
3746  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s(): %d, return\n", __func__, retval);
3747  }
3748 
3749  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
3750 
3751  return retval;
3752 }
3753 
3754 /*!******************************************************************************
3755  * \brief Close a xcoder encoder instance
3756  *
3757  * \param
3758  *
3759  * \return
3760  *******************************************************************************/
3762 {
3764  void* p_buffer = NULL;
3765  uint32_t ui32LBA = 0;
3766  int i = 0;
3767  ni_xcoder_params_t *p_param = NULL;
3768 
3769  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
3770 
3771  if (!p_ctx)
3772  {
3773  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
3774  __func__);
3775  return NI_RETCODE_INVALID_PARAM;
3776  }
3777 
3778  ni_pthread_mutex_lock(&p_ctx->mutex);
3779 
3780  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
3781  {
3782  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): Invalid session ID, return.\n", __func__);
3783  retval = NI_RETCODE_SUCCESS;
3784  LRETURN;
3785  }
3786 
3787  if (p_ctx->device_type == NI_DEVICE_TYPE_ENCODER)
3788  {
3789  ni_log2(p_ctx, NI_LOG_INFO,
3790  "Encoder_complete_info:session_id 0x%x, total frames input:%u "
3791  "buffered: %u completed: %u output: %u dropped: %u error: %u\n",
3798  }
3799 
3800  //malloc data buffer
3801  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), NI_DATA_BUFFER_LEN))
3802  {
3803  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() alloc data buffer failed\n",
3804  NI_ERRNO, __func__);
3805  retval = NI_RETCODE_ERROR_MEM_ALOC;
3806  LRETURN;
3807  }
3808  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
3809 
3810  if (p_ctx->p_session_config)
3811  {
3812  p_param = (ni_xcoder_params_t *)p_ctx->p_session_config;
3813  if (p_param->ddr_priority_mode > NI_DDR_PRIORITY_NONE)
3814  {
3816  if (NI_RETCODE_SUCCESS != retval)
3817  {
3818  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): ddr priority setting failure for %s\n",
3819  __func__, strerror(NI_ERRNO));
3820  LRETURN;
3821  }
3822  }
3823  }
3824 
3826 
3827  int retry = 0;
3828  while (retry < NI_SESSION_CLOSE_RETRY_MAX)
3829  {
3830  ni_log2(p_ctx, NI_LOG_DEBUG,
3831  "%s(): p_ctx->blk_io_handle=%" PRIx64 ", p_ctx->hw_id=%d, "
3832  "p_ctx->session_id=%d, close_mode=1\n",
3833  __func__, (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id,
3834  p_ctx->session_id);
3835 
3837  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA) < 0)
3838  {
3839  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): command failed\n", __func__);
3842  break;
3843  } else
3844  {
3845  //Close should always succeed
3846  retval = NI_RETCODE_SUCCESS;
3848  break;
3849  }
3850  /*
3851  else if(((ni_session_closed_status_t *)p_buffer)->session_closed)
3852  {
3853  retval = NI_RETCODE_SUCCESS;
3854  p_ctx->session_id = NI_INVALID_SESSION_ID;
3855  break;
3856  }
3857  else
3858  {
3859  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): wait for close\n");
3860  ni_usleep(NI_SESSION_CLOSE_RETRY_INTERVAL_US);
3861  retval = NI_RETCODE_ERROR_NVME_CMD_FAILED;
3862  }
3863  */
3864  retry++;
3865  }
3866 
3867 END:
3868 
3869  ni_aligned_free(p_buffer);
3871 
3872  //Sequence change related stuff cleanup here
3873  p_ctx->active_video_width = 0;
3874  p_ctx->active_video_height = 0;
3875  p_ctx->actual_video_width = 0;
3876  //End of sequence change related stuff cleanup
3877 
3878  if (p_ctx->pts_table)
3879  {
3880  ni_queue_free(&p_ctx->pts_table->list, p_ctx->buffer_pool);
3881  ni_memfree(p_ctx->pts_table);
3882  ni_log2(p_ctx, NI_LOG_DEBUG, "ni_timestamp_done: success\n");
3883  }
3884 
3885  if (p_ctx->dts_queue)
3886  {
3887  ni_queue_free(&p_ctx->dts_queue->list, p_ctx->buffer_pool);
3888  ni_memfree(p_ctx->dts_queue);
3889  ni_log2(p_ctx, NI_LOG_DEBUG, "ni_timestamp_done: success\n");
3890  }
3891 
3893  p_ctx->buffer_pool = NULL;
3894 
3895  for (i = 0; i < NI_FIFO_SZ; i++)
3896  {
3897  ni_memfree(p_ctx->pkt_custom_sei_set[i]);
3898  }
3899 
3900  for (i = 0; i < 120 ; i++)
3901  {
3902  if (p_ctx->input_frame_fifo[i].p_input_buffer != NULL)
3903  {
3904  free(p_ctx->input_frame_fifo[i].p_input_buffer);
3905  p_ctx->input_frame_fifo[i].p_input_buffer = NULL;
3906  }
3907  }
3908 
3909  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): CTX[Card:%" PRIx64 " / HW:%d / INST:%d]\n",
3910  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
3911  p_ctx->session_id);
3912  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
3913 
3914  low_delay_signal(p_ctx);
3915  ni_pthread_mutex_unlock(&p_ctx->mutex);
3916 
3917  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
3918 
3919  return retval;
3920 }
3921 
3922 
3923 void enqueue_ni_frame(ni_session_context_t *p_ctx, ni_frame_t *ni_frame, int32_t source_width, int32_t source_height)
3924 {
3925  int32_t width_stride[4] = {0};
3926  int32_t height_stride[4] = {0};
3927 
3928  if (((ni_xcoder_params_t *)(p_ctx->p_session_config))->zerocopy_mode &&
3929  ((ni_xcoder_params_t *)(p_ctx->p_session_config))->luma_linesize != 0 &&
3930  ((ni_xcoder_params_t *)(p_ctx->p_session_config))->chroma_linesize != 0)
3931  {
3932  //zero copy happend
3933  width_stride[0] = ((ni_xcoder_params_t *)(p_ctx->p_session_config))->luma_linesize;
3934  width_stride[1] = width_stride[2] = ((ni_xcoder_params_t *)(p_ctx->p_session_config))->chroma_linesize;
3935  }
3936  else
3937  {
3938  int is_semiplanar = !ni_get_planar_from_pixfmt(p_ctx->pixel_format);
3939  ni_get_hw_yuv420p_dim(ni_frame->video_width, ni_frame->video_height,
3940  p_ctx->bit_depth_factor, is_semiplanar,
3941  width_stride, height_stride);
3942  }
3943 
3944  for (int i = 0; i < 120; i++)
3945  {
3946  // 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",
3947  // __FUNCTION__, __LINE__, i, p_ctx->frame_num, p_ctx->input_frame_fifo[i].p_input_buffer,
3948  // ni_frame->data_len[0], ni_frame->data_len[1], ni_frame->data_len[2],
3949  // ni_frame->p_data[0], ni_frame->p_data[1], ni_frame->p_data[2],
3950  // ni_frame->video_width, ni_frame->video_height);
3951  if ((source_width != p_ctx->input_frame_fifo[i].video_width ||
3952  source_height != p_ctx->input_frame_fifo[i].video_height) &&
3953  p_ctx->input_frame_fifo[i].p_input_buffer != NULL &&
3954  p_ctx->input_frame_fifo[i].usable == 1)
3955  {
3956  free(p_ctx->input_frame_fifo[i].p_input_buffer);
3957  p_ctx->input_frame_fifo[i].p_input_buffer = NULL;
3958  p_ctx->input_frame_fifo[i].usable = -1;
3959  }
3960 
3961  if (p_ctx->input_frame_fifo[i].p_input_buffer == NULL)
3962  {
3963  p_ctx->input_frame_fifo[i].p_input_buffer = (uint8_t *)malloc(source_width * p_ctx->bit_depth_factor * source_height * 3 / 2);
3964 
3965  for (int j = 0; j < source_height; j++)
3966  {
3967  memcpy(p_ctx->input_frame_fifo[i].p_input_buffer + source_width * p_ctx->bit_depth_factor * j,
3968  ni_frame->p_data[0] + j * width_stride[0],
3969  source_width * p_ctx->bit_depth_factor);
3970  }
3971  for (int j = 0; j < (source_height + 1) / 2; j++)
3972  {
3973  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,
3974  ni_frame->p_data[1] + j * width_stride[1],
3975  source_width / 2 * p_ctx->bit_depth_factor);
3976  }
3977  for (int j = 0; j < (source_height + 1) / 2; j++)
3978  {
3979  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,
3980  ni_frame->p_data[2] + j * width_stride[2],
3981  source_width / 2 * p_ctx->bit_depth_factor);
3982  }
3983 
3984  p_ctx->input_frame_fifo[i].video_width = source_width;
3985  p_ctx->input_frame_fifo[i].video_height = source_height;
3986 
3987  p_ctx->input_frame_fifo[i].usable = -1;
3988  p_ctx->input_frame_fifo[i].pts = ni_frame->pts;
3989  // 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",
3990  // __FUNCTION__, __LINE__, i, p_ctx->frame_num, ni_frame->buffer_size, ni_frame->video_width, ni_frame->video_height,
3991  // p_ctx->input_frame_fifo[i].pts);
3992  break;
3993  }
3994  else if (p_ctx->input_frame_fifo[i].p_input_buffer != NULL && p_ctx->input_frame_fifo[i].usable == 1)
3995  {
3996  for (int j = 0; j < source_height; j++)
3997  {
3998  memcpy(p_ctx->input_frame_fifo[i].p_input_buffer + source_width * p_ctx->bit_depth_factor * j,
3999  ni_frame->p_data[0] + j * width_stride[0],
4000  source_width * p_ctx->bit_depth_factor);
4001  }
4002  for (int j = 0; j < (source_height + 1) / 2; j++)
4003  {
4004  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,
4005  ni_frame->p_data[1] + j * width_stride[1],
4006  source_width / 2 * p_ctx->bit_depth_factor);
4007  }
4008  for (int j = 0; j < (source_height + 1) / 2; j++)
4009  {
4010  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,
4011  ni_frame->p_data[2] + j * width_stride[2],
4012  source_width / 2 * p_ctx->bit_depth_factor);
4013  }
4014 
4015  p_ctx->input_frame_fifo[i].video_width = source_width;
4016  p_ctx->input_frame_fifo[i].video_height = source_height;
4017 
4018  p_ctx->input_frame_fifo[i].usable = -1;
4019  p_ctx->input_frame_fifo[i].pts = ni_frame->pts;
4020  break;
4021  }
4022  }
4023 }
4024 
4025 /*!******************************************************************************
4026  * \brief Send a YUV p_frame to encoder
4027  *
4028  * \param
4029  *
4030  * \return
4031  *******************************************************************************/
4033 {
4034  bool ishwframe = false;
4035  uint32_t size = 0;
4036  uint32_t send_count = 0;
4037  uint32_t i = 0;
4038  uint32_t sent_size = 0;
4039  uint32_t frame_size_bytes = 0;
4040  int retval = 0;
4041  ni_instance_buf_info_t buf_info = { 0 };
4042  ni_session_statistic_t sessionStatistic = {0};
4043 
4044  if (!p_ctx || !p_frame)
4045  {
4046  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
4047  __func__);
4048  return NI_RETCODE_INVALID_PARAM;
4049  }
4050  ishwframe = p_ctx->hw_action & NI_CODEC_HW_ENABLE;
4051  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter hw=%d\n", __func__, ishwframe);
4052 
4053  if (ishwframe && !p_frame->end_of_stream)
4054  {
4055  // check if the hw input frame is valid
4056  if (!p_frame->p_data[3])
4057  {
4058  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() hw input frame is null!, return\n",
4059  __func__);
4060  return NI_RETCODE_INVALID_PARAM;
4061  }
4062  uint16_t input_frame_idx = ((niFrameSurface1_t*)(p_frame->p_data[3]))->ui16FrameIdx;
4063  if (!((input_frame_idx > 0 &&
4064  input_frame_idx < NI_GET_MAX_HWDESC_FRAME_INDEX(p_ctx->ddr_config)) ||
4065  (input_frame_idx > NI_GET_MIN_HWDESC_P2P_BUF_ID(p_ctx->ddr_config) &&
4066  input_frame_idx <= NI_GET_MAX_HWDESC_P2P_BUF_ID(p_ctx->ddr_config))))
4067  {
4068  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): got invalid frameIdx [%u]\n",
4069  __func__, input_frame_idx);
4070  return NI_RETCODE_INVALID_PARAM;
4071  }
4072  }
4073 
4074  ni_pthread_mutex_lock(&p_ctx->mutex);
4075 
4076  uint8_t separate_metadata = p_frame->separate_metadata;
4077 
4078  uint8_t separate_start = (p_frame->separate_start && p_frame->total_start_len) ? 1 : 0;
4079 
4080  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
4081  {
4082  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
4083  __func__);
4085  LRETURN;
4086  }
4087 
4088  low_delay_wait(p_ctx);
4089 
4090 #ifdef MEASURE_LATENCY
4091  if ((p_frame->dts != NI_NOPTS_VALUE) && (p_ctx->frame_time_q != NULL))
4092  {
4093  uint64_t abs_time_ns = ni_gettime_ns();
4094  ni_lat_meas_q_t *frame_time_q = (ni_lat_meas_q_t *)p_ctx->frame_time_q;
4095  ni_lat_meas_q_add_entry(frame_time_q, abs_time_ns, p_frame->dts);
4096  }
4097 #endif
4098 
4099  /*!********************************************************************/
4100  /*!************ Sequence Change related stuff *************************/
4101  //First check squence changed related stuff.
4102  //We need to record the current hight/width params if we didn't do it before:
4103 
4104  if( p_frame->video_height)
4105  {
4106  p_ctx->active_video_width = p_frame->data_len[0] / p_frame->video_height;
4107  p_ctx->active_video_height = p_frame->video_height;
4108  }
4109  else if (p_frame->video_width)
4110  {
4111  ni_log2(p_ctx, NI_LOG_DEBUG, "WARNING: passed video_height is not valid!, return\n");
4112  p_ctx->active_video_height = p_frame->data_len[0] / p_frame->video_width;
4113  p_ctx->active_video_width = p_frame->video_width;
4114  }
4115  else
4116  {
4117  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: passed video_height and video_width are not valid!, return\n");
4119  LRETURN;
4120  }
4121  // record actual width (in pixels / without padding) for sequnce change detection
4122  p_ctx->actual_video_width = p_frame->video_width;
4123 
4124  /*!************ Sequence Change related stuff end*************************/
4125  /*!********************************************************************/
4126 
4127  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;
4128  ni_log2(p_ctx, NI_LOG_DEBUG,
4129  "%s: data_len[0] %u data_len[1] %u "
4130  "data_len[2] %u extra_data_len %u frame_size_bytes %u\n",
4131  __func__, p_frame->data_len[0], p_frame->data_len[1],
4132  p_frame->data_len[2], p_frame->extra_data_len, frame_size_bytes);
4133 
4134  // skip query write buffer because we just send EOS
4135  if (!p_frame->end_of_stream)
4136  {
4137  for (;;)
4138  {
4139  query_sleep(p_ctx);
4140 
4142  "65") >= 0)
4143  {
4145  p_ctx, NI_DEVICE_TYPE_ENCODER, &sessionStatistic);
4146  CHECK_ERR_RC(p_ctx, retval, &sessionStatistic,
4148  p_ctx->hw_id, &(p_ctx->session_id), OPT_2);
4149  CHECK_VPU_RECOVERY(retval);
4150  buf_info.buf_avail_size = sessionStatistic.ui32WrBufAvailSize;
4151  } else
4152  {
4153  retval =
4155  NI_DEVICE_TYPE_ENCODER, &buf_info);
4156  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
4157  p_ctx->device_type, p_ctx->hw_id,
4158  &(p_ctx->session_id), OPT_1);
4159  CHECK_VPU_RECOVERY(retval);
4160  }
4161 
4162  if (NI_RETCODE_FAILURE == retval)
4163  {
4164  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() line-%d retrun %d\n", __func__, __LINE__, retval);
4165  LRETURN;
4166  }
4167  else if (NI_RETCODE_SUCCESS != retval ||
4168  buf_info.buf_avail_size < frame_size_bytes)
4169  {
4170  ni_log2(p_ctx, NI_LOG_TRACE,
4171  "Enc write query retry %d. rc=%d. Available buf size %u < "
4172  "frame size %u\n", retval, send_count,
4173  buf_info.buf_avail_size, frame_size_bytes);
4174  if (send_count >= NI_MAX_ENCODER_QUERY_RETRIES)
4175  {
4176  int retval_backup = retval;
4177  retval = ni_query_instance_buf_info(
4180  {
4181  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
4182  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
4183  CHECK_VPU_RECOVERY(retval);
4184  }
4185  else
4186  retval = NI_RETCODE_SUCCESS;
4187 
4188  ni_log2(p_ctx, NI_LOG_DEBUG,
4189  "Enc write query buf info exceeded max retries: "
4190  "%d, rc=%d. Available buf size %u < frame size %u\n",
4191  NI_MAX_ENCODER_QUERY_RETRIES, retval_backup,
4192  buf_info.buf_avail_size, frame_size_bytes);
4194 
4195  LRETURN;
4196  }
4197  send_count++;
4198  ni_pthread_mutex_unlock(&p_ctx->mutex);
4200  ni_pthread_mutex_lock(&p_ctx->mutex);
4201  } else
4202  {
4203  ni_log2(p_ctx, NI_LOG_DEBUG,
4204  "Info enc write query success, available buf "
4205  "size %u >= frame size %u !\n",
4206  buf_info.buf_avail_size, frame_size_bytes);
4207  break;
4208  }
4209  }
4210  }
4211 
4212  // fill in metadata such as timestamp
4213  ni_metadata_enc_frame_t *p_meta;
4214  if (separate_metadata)
4215  {
4216  p_meta = (ni_metadata_enc_frame_t *)p_frame->p_metadata_buffer;
4217  } else
4218  {
4219  p_meta = (ni_metadata_enc_frame_t *)((uint8_t *)
4220  p_frame->p_data[2 + ishwframe] +
4221  p_frame->data_len[2 + ishwframe]);
4222  }
4223 
4224  if (p_meta) //When hwframe xcoding reaches eos, frame looks like swframe but no allocation for p_meta
4225  {
4226  p_meta->metadata_common.ui64_data.frame_tstamp = (uint64_t)p_frame->pts;
4227 
4228  p_meta->force_headers = 0; // p_frame->force_headers not implemented/used
4231  p_meta->use_long_term_ref = p_frame->use_long_term_ref;
4232 
4233  ni_log2(p_ctx, NI_LOG_DEBUG,
4234  "%s: p_meta "
4235  "use_cur_src_as_long_term_pic %d use_long_term_ref %d\n",
4236  __func__, p_meta->use_cur_src_as_long_term_pic,
4237  p_meta->use_long_term_ref);
4238 
4239  p_meta->frame_force_type_enable = p_meta->frame_force_type = 0;
4240  // frame type to be forced to is supposed to be set correctly
4241  // in p_frame->ni_pict_type
4242  if (1 == p_ctx->force_frame_type || p_frame->force_key_frame)
4243  {
4244  if (p_frame->ni_pict_type)
4245  {
4246  p_meta->frame_force_type_enable = 1;
4247  p_meta->frame_force_type = p_frame->ni_pict_type;
4248  }
4249  ni_log2(p_ctx, NI_LOG_DEBUG,
4250  "%s(): ctx->force_frame_type"
4251  " %d frame->force_key_frame %d force frame_num %" PRIu64 ""
4252  " type to %d\n",
4253  __func__, p_ctx->force_frame_type, p_frame->force_key_frame,
4254  p_ctx->frame_num, p_frame->ni_pict_type);
4255  }
4256 
4257  // force pic qp if specified
4258  p_meta->force_pic_qp_enable = p_meta->force_pic_qp_i =
4259  p_meta->force_pic_qp_p = p_meta->force_pic_qp_b = 0;
4260  if (p_frame->force_pic_qp)
4261  {
4262  p_meta->force_pic_qp_enable = 1;
4263  p_meta->force_pic_qp_i = p_meta->force_pic_qp_p =
4264  p_meta->force_pic_qp_b = p_frame->force_pic_qp;
4265  }
4266  p_meta->frame_sei_data_size = p_frame->sei_total_len;
4267  p_meta->frame_roi_map_size = p_frame->roi_len;
4268  p_meta->frame_roi_avg_qp = p_ctx->roi_avg_qp;
4269  p_meta->enc_reconfig_data_size = p_frame->reconf_len;
4270 
4272  "6Q") >= 0)
4273  {
4274  if (separate_start)
4275  {
4276  for (i = 0; i < NI_MAX_NUM_SW_FRAME_DATA_POINTERS; i++)
4277  p_meta->start_len[i] = p_frame->start_len[i];
4278  }
4279  else
4280  {
4281  memset(p_meta->start_len, 0, sizeof(p_meta->start_len));
4282  }
4284  }
4285 
4286  if (p_ctx->frame_num % ((ni_xcoder_params_t *)(p_ctx->p_session_config))->interval_of_psnr == 0)
4287  p_meta->get_recon_frame_mode = ((ni_xcoder_params_t *)(p_ctx->p_session_config))->cfg_enc_params.get_psnr_mode;
4288  else
4289  p_meta->get_recon_frame_mode = 3;
4290 
4291  ni_log2(p_ctx,
4292  NI_LOG_DEBUG,
4293  "%s(): %d.%u p_ctx->frame_num=%" PRIu64 ", "
4294  "p_frame->start_of_stream=%u, p_frame->end_of_stream=%u, "
4295  "p_frame->video_width=%u, p_frame->video_height=%u, pts=0x%08x 0x%08x, "
4296  "dts=0x%08x 0x%08x, sei_len=%u, roi size=%u avg_qp=%u reconf_len=%u "
4297  "force_pic_qp=%u use_cur_src_as_long_term_pic %u use_long_term_ref "
4298  "%u start_len [%u,%u,%u] inconsecutive_transfer %u "
4299  "get_recon_frame_mode %u\n",
4300  __func__, p_ctx->hw_id, p_ctx->session_id, p_ctx->frame_num,
4301  p_frame->start_of_stream, p_frame->end_of_stream, p_frame->video_width,
4302  p_frame->video_height, (uint32_t)((p_frame->pts >> 32) & 0xFFFFFFFF),
4303  (uint32_t)(p_frame->pts & 0xFFFFFFFF),
4304  (uint32_t)((p_frame->dts >> 32) & 0xFFFFFFFF),
4305  (uint32_t)(p_frame->dts & 0xFFFFFFFF), p_meta->frame_sei_data_size,
4306  p_meta->frame_roi_map_size, p_meta->frame_roi_avg_qp,
4307  p_meta->enc_reconfig_data_size, p_meta->force_pic_qp_i,
4309  p_meta->start_len[0], p_meta->start_len[1], p_meta->start_len[2],
4310  p_meta->inconsecutive_transfer,
4311  p_meta->get_recon_frame_mode);
4312  }
4313  if (p_frame->start_of_stream)
4314  {
4315  //Send Start of stream p_config command here
4317  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
4318  p_ctx->device_type, p_ctx->hw_id,
4319  &(p_ctx->session_id), OPT_1);
4320  CHECK_VPU_RECOVERY(retval);
4321  if (NI_RETCODE_SUCCESS != retval)
4322  {
4323  LRETURN;
4324  }
4325 
4326  p_frame->start_of_stream = 0;
4327  }
4328 
4329  // skip direct to send eos without sending the passed in p_frame as it's been sent already
4330  if (p_frame->end_of_stream)
4331  {
4333  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
4334  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
4335  CHECK_VPU_RECOVERY(retval);
4336  if (NI_RETCODE_SUCCESS != retval)
4337  {
4338  LRETURN;
4339  }
4340 
4341  p_frame->end_of_stream = 0;
4342  p_ctx->ready_to_close = 1;
4343  } else //handle regular frame sending
4344  {
4345  retval = ni_timestamp_register(p_ctx->buffer_pool, p_ctx->dts_queue,
4346  p_frame->dts, 0);
4347  if (NI_RETCODE_SUCCESS != retval)
4348  {
4349  ni_log2(p_ctx, NI_LOG_ERROR,
4350  "ERROR %s(): "
4351  "ni_timestamp_register() for dts returned: %d\n",
4352  __func__, retval);
4353  }
4354 
4355  if (separate_metadata)
4356  {
4357  uint32_t ui32LBA_metadata =
4359  ni_log2(p_ctx, NI_LOG_DEBUG,
4360  "%s: p_metadata_buffer = %p, metadata_buffer_size "
4361  "= %u, p_ctx->frame_num = %" PRIu64 ", LBA = 0x%x\n",
4362  __func__, p_frame->p_metadata_buffer,
4363  p_frame->metadata_buffer_size, p_ctx->frame_num,
4364  ui32LBA_metadata);
4365 
4366  sent_size =
4368 
4369  retval = ni_nvme_send_write_cmd(
4370  p_ctx->blk_io_handle, p_ctx->event_handle,
4371  p_frame->p_metadata_buffer, sent_size,
4372  ui32LBA_metadata);
4373  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_write, p_ctx->device_type,
4374  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
4375  CHECK_VPU_RECOVERY(retval);
4376  if (retval < 0)
4377  {
4378  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n",
4379  __func__);
4381  LRETURN;
4382  }
4383  }
4384 
4385  if (separate_start)
4386  {
4387  uint32_t ui32LBA =
4389  ni_log2(p_ctx, NI_LOG_DEBUG,
4390  "%s: p_start_buffer = %p, p_frame->start_buffer_size "
4391  "= %u, p_ctx->frame_num = %" PRIu64 ", LBA = 0x%x\n",
4392  __func__, p_frame->p_start_buffer, p_frame->start_buffer_size, p_ctx->frame_num,
4393  ui32LBA);
4394 
4395  sent_size =
4397 
4398  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
4399  p_frame->p_start_buffer, sent_size, ui32LBA);
4400  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_write, p_ctx->device_type,
4401  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
4402  CHECK_VPU_RECOVERY(retval);
4403  if (retval < 0)
4404  {
4405  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
4407  LRETURN;
4408  }
4409  }
4410 
4411  if (p_frame->inconsecutive_transfer)
4412  {
4413  uint32_t ui32LBA =
4415 
4416  for (i = 0; i < NI_MAX_NUM_SW_FRAME_DATA_POINTERS; i++)
4417  {
4418  ni_log2(p_ctx, NI_LOG_DEBUG,
4419  "%s: p_data = %p, p_frame->buffer_size "
4420  "= %u, p_ctx->frame_num = %" PRIu64 ", LBA = 0x%x\n",
4421  __func__, p_frame->p_data, p_frame->buffer_size, p_ctx->frame_num,
4422  ui32LBA);
4423 
4424  if (p_frame->data_len[i])
4425  {
4426  sent_size = p_frame->data_len[i];
4427  if (separate_start)
4428  sent_size -= p_frame->start_len[i];
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_data[i]+p_frame->start_len[i], 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  }
4447  else
4448  {
4449  uint32_t ui32LBA =
4451  ni_log2(p_ctx, NI_LOG_DEBUG,
4452  "%s: p_data = %p, p_frame->buffer_size "
4453  "= %u, p_ctx->frame_num = %" PRIu64 ", LBA = 0x%x\n",
4454  __func__, p_frame->p_data, p_frame->buffer_size, p_ctx->frame_num,
4455  ui32LBA);
4456 
4457  sent_size = frame_size_bytes;
4458  if (separate_metadata)
4459  sent_size -= p_frame->extra_data_len;
4460  if (separate_start)
4461  sent_size -= p_frame->total_start_len;
4462 
4463  sent_size =
4465 
4466  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
4467  p_frame->p_buffer+p_frame->total_start_len, sent_size, ui32LBA);
4468  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_write, p_ctx->device_type,
4469  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
4470  CHECK_VPU_RECOVERY(retval);
4471  if (retval < 0)
4472  {
4473  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
4475  LRETURN;
4476  }
4477  }
4478 
4479  //Save input frame data used for calculate PSNR
4480  if ((ni_cmp_fw_api_ver((char*) &p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6rc") >= 0) &&
4481  (p_ctx->frame_num == 0 || ((p_ctx->frame_num % ((ni_xcoder_params_t *)(p_ctx->p_session_config))->interval_of_psnr) == 0)) &&
4482  (((ni_xcoder_params_t *)(p_ctx->p_session_config))->cfg_enc_params.get_psnr_mode < 3) &&
4483  (!(((ni_xcoder_params_t *)(p_ctx->p_session_config))->cfg_enc_params.get_psnr_mode == 2 && p_ctx->codec_format == NI_CODEC_FORMAT_H265)))
4484  {
4485  if (ishwframe && !p_frame->end_of_stream)
4486  {
4487  // uint16_t input_frame_idx = ((niFrameSurface1_t*)(p_frame->p_data[3]))->ui16FrameIdx;
4488  // use hwdownload to download hw frame, but donot recycle hwframe buffer
4489  ni_session_data_io_t hwdl_session_data = {0};
4490  // ni_log2(p_ctx, NI_LOG_ERROR, "pixel_format %d\n", p_ctx->pixel_format);
4491  int ret = hwdl_frame(p_ctx, &hwdl_session_data, p_frame,
4492  p_frame->pixel_format);
4493  if (ret <= 0)
4494  {
4495  ni_frame_buffer_free(&(hwdl_session_data.data.frame));
4496  return ret;
4497  }
4498 
4499  hwdl_session_data.data.frame.pts = p_frame->pts;
4500  enqueue_ni_frame(p_ctx, &hwdl_session_data.data.frame, p_frame->video_width, p_frame->video_height);
4501  ni_frame_buffer_free(&hwdl_session_data.data.frame);
4502  }
4503  else
4504  {
4505  enqueue_ni_frame(p_ctx, p_frame, p_frame->video_width, p_frame->video_height);
4506  }
4507  }
4508 
4509  if (ishwframe)
4510  {
4511  ni_log2(p_ctx, NI_LOG_DEBUG,
4512  "%s(): session=0x%x ui16FrameIdx=%u\n",
4513  __func__,
4514  p_ctx->session_id,
4515  ((niFrameSurface1_t*)(p_frame->p_data[3]))->ui16FrameIdx);
4516  }
4517 
4518  p_ctx->status = 0;
4519  p_ctx->frame_num++;
4520  size = frame_size_bytes;
4521  p_ctx->low_delay_sync_flag = 1;
4522 
4523 #ifdef XCODER_DUMP_DATA
4524  char dump_file[256];
4525  snprintf(dump_file, sizeof(dump_file), "%ld-%u-enc-fme/fme-%04ld.yuv",
4526  (long)getpid(), p_ctx->session_id, (long)p_ctx->frame_num);
4527 
4528  FILE *f = fopen(dump_file, "wb");
4529  fwrite(p_frame->p_buffer,
4530  p_frame->data_len[0] + p_frame->data_len[1] + p_frame->data_len[2],
4531  1, f);
4532  fflush(f);
4533  fclose(f);
4534 #endif
4535  }
4536 
4537  retval = size;
4538 
4539 END:
4540 
4541  ni_pthread_mutex_unlock(&p_ctx->mutex);
4542 
4543  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
4544  return retval;
4545 }
4546 
4547 
4549 {
4550  ni_instance_mgr_stream_info_t data = { 0 };
4551  uint32_t actual_read_size = 0;
4552  uint32_t to_read_size = 0;
4553  int size = 0;
4554  static long long encq_count = 0LL;
4555  int retval = NI_RETCODE_SUCCESS;
4556  int query_retry = 0;
4557  int query_type = INST_BUF_INFO_RW_READ;
4558  uint32_t ui32LBA = 0;
4559  ni_metadata_enc_bstream_t *p_meta = NULL;
4560  ni_metadata_enc_bstream_rev61_t *p_meta_rev61 = NULL;
4561  ni_instance_buf_info_t buf_info = { 0 };
4562  ni_session_statistic_t sessionStatistic = {0};
4563  int low_delay_notify = 0;
4564  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
4565 
4566  if (!p_ctx || !p_packet || !p_packet->p_data)
4567  {
4568  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
4569  __func__);
4570  retval = NI_RETCODE_INVALID_PARAM;
4571  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
4572 
4573  return retval;
4574  }
4575 
4576  ni_pthread_mutex_lock(&p_ctx->mutex);
4577 
4578  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
4579  {
4580  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
4581  __func__);
4583  LRETURN;
4584  }
4585 
4586  ni_log2(p_ctx, NI_LOG_DEBUG, "frame_num=%" PRIu64 ", pkt_num=%" PRIu64 ", av1_pkt_num=%" PRIu64 "\n",
4587  p_ctx->frame_num, p_ctx->pkt_num, p_ctx->av1_pkt_num);
4588  if (((ni_xcoder_params_t *)p_ctx->p_session_config)->low_delay_mode)
4589  {
4590  query_type = INST_BUF_INFO_RW_READ_BUSY;
4591  if (!p_packet->end_of_stream && p_ctx->frame_num < p_ctx->pkt_num)
4592  {
4593  if (p_ctx->ready_to_close)
4594  {
4595  query_type = INST_BUF_INFO_RW_READ;
4596  } else
4597  { //nothing to query, leave
4598  retval = NI_RETCODE_SUCCESS;
4599  low_delay_notify = 1;
4600  LRETURN;
4601  }
4602  }
4603  }
4604  for (;;)
4605  {
4606  query_sleep(p_ctx);
4607 
4608  query_retry++;
4609 
4611  "65") >= 0)
4612  {
4614  p_ctx, NI_DEVICE_TYPE_ENCODER, &sessionStatistic);
4615  CHECK_ERR_RC(p_ctx, retval, &sessionStatistic,
4617  p_ctx->hw_id, &(p_ctx->session_id), OPT_2);
4618  CHECK_VPU_RECOVERY(retval);
4619 
4620  buf_info.buf_avail_size = sessionStatistic.ui32RdBufAvailSize;
4621 
4622  if (((ni_xcoder_params_t *)p_ctx->p_session_config)->cfg_enc_params.lookAheadDepth)
4623  {
4625  "6rX") >= 0)
4626  {
4627  if (p_ctx->current_frame_delay < (int)sessionStatistic.ui8AdditionalFramesDelay + p_ctx->initial_frame_delay)
4628  {
4629  p_ctx->current_frame_delay = (int)sessionStatistic.ui8AdditionalFramesDelay + p_ctx->initial_frame_delay; // extend frames delay by FW estimated additional number of frames
4630  }
4631  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: initial_frame_delay %d max_frame_delay %d ui8AdditionalFramesDelay %u current_frame_delay %d\n",
4632  __FUNCTION__, p_ctx->initial_frame_delay, p_ctx->max_frame_delay, sessionStatistic.ui8AdditionalFramesDelay, p_ctx->current_frame_delay);
4633  }
4634  }
4635  } else
4636  {
4637  retval = ni_query_instance_buf_info(
4638  p_ctx, query_type, NI_DEVICE_TYPE_ENCODER, &buf_info);
4639  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
4640  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
4641  CHECK_VPU_RECOVERY(retval);
4642  }
4643 
4644  ni_log2(p_ctx, NI_LOG_TRACE,
4645  "Info enc read query rc %d, available buf size %u, "
4646  "frame_num=%" PRIu64 ", pkt_num=%" PRIu64 "\n",
4647  retval, buf_info.buf_avail_size, p_ctx->frame_num, p_ctx->pkt_num);
4648 
4649  if (NI_RETCODE_SUCCESS != retval)
4650  {
4651  ni_log2(p_ctx, NI_LOG_ERROR, "Buffer info query failed in encoder read!!!!\n");
4652  LRETURN;
4653  } else if (0 == buf_info.buf_avail_size)
4654  {
4655  // query to see if it is eos now, if we have sent it
4656  if (p_ctx->ready_to_close)
4657  {
4658  retval = ni_query_stream_info(p_ctx, NI_DEVICE_TYPE_ENCODER, &data);
4659  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
4660  p_ctx->device_type, p_ctx->hw_id,
4661  &(p_ctx->session_id), OPT_1);
4662  CHECK_VPU_RECOVERY(retval);
4663 
4664  if (NI_RETCODE_SUCCESS != retval)
4665  {
4666  ni_log2(p_ctx, NI_LOG_ERROR, "Stream info query failed in encoder read!!!!\n");
4667  LRETURN;
4668  }
4669 
4670  if (data.is_flushed)
4671  {
4672  p_packet->end_of_stream = 1;
4673  }
4674  }
4675  ni_log2(p_ctx, NI_LOG_DEBUG, "Info enc read available buf size %u, eos %u !\n",
4676  buf_info.buf_avail_size, p_packet->end_of_stream);
4677 
4678  if ((((ni_xcoder_params_t *)p_ctx->p_session_config)->low_delay_mode ||
4680  !p_packet->end_of_stream && p_ctx->frame_num >= p_ctx->pkt_num)
4681  {
4682  ni_log2(p_ctx, NI_LOG_DEBUG, "Encoder low latency mode, eos not sent, frame_num "
4683  "%" PRIu64 " >= %" PRIu64 " pkt_num, keep querying p_ctx->status %d\n",
4684  p_ctx->frame_num, p_ctx->pkt_num, p_ctx->status);
4685  ni_pthread_mutex_unlock(&p_ctx->mutex);
4687  ni_pthread_mutex_lock(&p_ctx->mutex);
4688  if (query_retry >= NI_MAX_ENCODER_QUERY_RETRIES &&
4690  {
4691  low_delay_notify = 1;
4693  LRETURN;
4694  }
4695  else
4696  {
4697  continue;
4698  }
4699  }
4700  else if (((ni_xcoder_params_t *)p_ctx->p_session_config)->minFramesDelay)
4701  {
4702  if (p_ctx->pkt_num) // do not busy read until header is received
4703  {
4704  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
4705  if (p_ctx->frame_num - pkt_num >= p_ctx->current_frame_delay &&
4706  !p_packet->end_of_stream)
4707  {
4708  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",
4709  __FUNCTION__, p_ctx->frame_num, pkt_num,
4710  p_ctx->last_gop_size, p_ctx->current_frame_delay);
4711  continue;
4712  }
4713  }
4714  }
4715  retval = NI_RETCODE_SUCCESS;
4716  LRETURN;
4717  } else
4718  {
4719  break;
4720  }
4721  }
4722  ni_log2(p_ctx, NI_LOG_DEBUG, "Encoder read buf_avail_size %u\n", buf_info.buf_avail_size);
4723 
4724  to_read_size = buf_info.buf_avail_size;
4725 
4726  p_packet->data_len = 0;
4727  p_packet->pts = NI_NOPTS_VALUE;
4728  p_packet->dts = 0;
4729 
4730  p_packet->psnr_y = 0;
4731  p_packet->psnr_u = 0;
4732  p_packet->psnr_v = 0;
4733  p_packet->average_psnr = 0;
4734  p_packet->ssim_y = 0;
4735  p_packet->ssim_u = 0;
4736  p_packet->ssim_v = 0;
4737 
4739  actual_read_size = to_read_size;
4740  if (actual_read_size % NI_MEM_PAGE_ALIGNMENT)
4741  {
4742  actual_read_size =
4743  ((actual_read_size / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT) +
4745  }
4746 
4747  if (p_packet->buffer_size < actual_read_size)
4748  {
4749  if (ni_packet_buffer_alloc(p_packet, actual_read_size))
4750  {
4751  ni_log2(p_ctx, NI_LOG_ERROR,
4752  "ERROR %s(): packet buffer size %u allocation "
4753  "failed\n",
4754  __func__, actual_read_size);
4755  retval = NI_RETCODE_ERROR_MEM_ALOC;
4756  LRETURN;
4757  }
4758  }
4759 
4760  retval = ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
4761  p_packet->p_data, actual_read_size, ui32LBA);
4762  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_read, p_ctx->device_type,
4763  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
4764  CHECK_VPU_RECOVERY(retval);
4765  if (retval < 0)
4766  {
4767  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
4769  LRETURN;
4770  }
4771 
4772  // SSIM is supported if fw_rev is >= 6.2
4774  "62") >= 0)
4775  {
4776  p_meta = (ni_metadata_enc_bstream_t *)p_packet->p_data;
4777  p_packet->pts = (int64_t)(p_meta->frame_tstamp);
4778  p_packet->frame_type = p_meta->frame_type;
4779  p_packet->avg_frame_qp = p_meta->avg_frame_qp;
4780  p_packet->recycle_index = p_meta->recycle_index;
4781  p_packet->av1_show_frame = p_meta->av1_show_frame;
4782  ni_log2(p_ctx, NI_LOG_DEBUG, "%s RECYCLE INDEX = %u!!!\n", __FUNCTION__, p_meta->recycle_index);
4783  ni_log2(p_ctx, NI_LOG_DEBUG, "%s MetaDataSize %d FrameType %d AvgFrameQp %d ssim %d %d %d\n",
4784  __FUNCTION__, p_meta->metadata_size, p_meta->frame_type, p_meta->avg_frame_qp, p_meta->ssimY, p_meta->ssimU, p_meta->ssimV);
4785 
4787  "6r2") >= 0)
4788  {
4789  if (((ni_xcoder_params_t *)p_ctx->p_session_config)->cfg_enc_params.lookAheadDepth)
4790  {
4792  "6rX") < 0)
4793  {
4794  if (p_meta->gop_size) // ignore frame 0 gop size 0 (other I-frame gop size 1)
4795  {
4796  if ((int)p_meta->gop_size < p_ctx->last_gop_size)
4797  {
4798  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
4799  }
4800  }
4801  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",
4802  __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);
4803  }
4804  p_ctx->last_gop_size = p_meta->gop_size;
4805  }
4806  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: current gop_size %u\n",
4807  __FUNCTION__, p_meta->gop_size);
4808  }
4809 
4811  "6p") >= 0)
4812  {
4813  if (ni_cmp_fw_api_ver((char*)&p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6rm") >= 0)
4814  {
4815  p_packet->still_image_detected = p_meta->ui8StillImage;
4816  p_packet->scene_change_detected = p_meta->ui8SceneChange;
4817  }
4818  else
4819  {
4820  p_packet->still_image_detected = 0;
4821  p_packet->scene_change_detected = 0;
4822  }
4823 
4824  if(p_meta->frame_size > 0)
4825  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",
4826  p_ctx->pkt_num,
4827  p_meta->max_mv_x[0], p_meta->max_mv_x[1], p_meta->max_mv_y[0], p_meta->max_mv_y[1],
4828  p_meta->min_mv_x[0], p_meta->min_mv_x[1], p_meta->min_mv_y[0], p_meta->min_mv_y[1],
4829  p_meta->frame_size, p_meta->inter_total_count, p_meta->intra_total_count,
4830  p_meta->ui8StillImage, p_meta->ui8SceneChange);
4831  }
4832 
4833  p_ctx->meta_size = p_meta->metadata_size;
4834 
4835  if (p_meta->ssimY != 0)
4836  {
4837  p_packet->ssim_y = (float)p_meta->ssimY/10000;
4838  p_packet->ssim_u = (float)p_meta->ssimU/10000;
4839  p_packet->ssim_v = (float)p_meta->ssimV/10000;
4840  // The SSIM Y, U, V values returned by FW are 4 decimal places multiplied by 10000.
4841  //Divide by 10000 to get the original value.
4842  ni_log2(p_ctx,
4843 #ifdef NI_LOG_SSIM_AT_INFO
4844  NI_LOG_INFO,
4845 #else
4846  NI_LOG_DEBUG,
4847 #endif
4848  "%s: pkt #%" PRId64 " pts %" PRId64 " ssim "
4849  "Y %.4f U %.4f V %.4f\n", __FUNCTION__, p_ctx->pkt_num,
4850  p_packet->pts, (float)p_meta->ssimY/10000,
4851  (float)p_meta->ssimU/10000, (float)p_meta->ssimV/10000);
4852  }
4853  }
4854  else
4855  {
4856  // Up to fw_rev major 6 and minor 1, use the old meta data structure
4857  p_meta_rev61 = (ni_metadata_enc_bstream_rev61_t *)p_packet->p_data;
4858  p_packet->pts = (int64_t)(p_meta_rev61->frame_tstamp);
4859  p_packet->frame_type = p_meta_rev61->frame_type;
4860  p_packet->avg_frame_qp = p_meta_rev61->avg_frame_qp;
4861  p_packet->recycle_index = p_meta_rev61->recycle_index;
4862  p_packet->av1_show_frame = p_meta_rev61->av1_show_frame;
4863  ni_log2(p_ctx, NI_LOG_DEBUG, "%s RECYCLE INDEX = %u!!!\n", __FUNCTION__, p_meta_rev61->recycle_index);
4864 
4865  }
4866 
4867  p_packet->data_len = to_read_size;
4868 
4869  size = p_packet->data_len;
4870 
4871  if (size > 0)
4872  {
4873  if (p_ctx->pkt_num >= 1 &&
4875  "6rc") >= 0)
4876  {
4877  calculate_psnr(p_ctx, p_packet);
4878  }
4879 
4880  if (p_ctx->pkt_num >= 1)
4881  {
4882  if (NI_CODEC_FORMAT_AV1 != p_ctx->codec_format
4883  || p_packet->av1_show_frame)
4884  {
4885  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)
4886  {
4887  p_packet->dts = NI_NOPTS_VALUE;
4888  }
4889  }
4890 
4891  p_ctx->pkt_num++;
4892  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
4893  {
4894  p_ctx->av1_pkt_num++;
4895  }
4896  low_delay_notify = 1;
4897  }
4898 
4899  encq_count++;
4900 
4901 #ifdef MEASURE_LATENCY
4902 #ifndef XCODER_311
4903  ni_log2(p_ctx, NI_LOG_INFO, "ENC fme_num %d, pkt_num %d, latency is %d\n",
4904  p_ctx->frame_num, p_ctx->pkt_num, p_ctx->frame_num - p_ctx->pkt_num);
4905 #endif
4906 #endif
4907 
4908 #ifdef XCODER_DUMP_DATA
4909  char dump_file[256];
4910  snprintf(dump_file, sizeof(dump_file), "%ld-%u-enc-pkt/pkt-%04ld.bin",
4911  (long)getpid(), p_ctx->session_id, (long)p_ctx->pkt_num);
4912 
4913  FILE *f = fopen(dump_file, "wb");
4914  fwrite((uint8_t *)p_packet->p_data + sizeof(ni_metadata_enc_bstream_t),
4915  p_packet->data_len - sizeof(ni_metadata_enc_bstream_t), 1, f);
4916  fflush(f);
4917  fclose(f);
4918 #endif
4919  }
4920 
4921  ni_log2(p_ctx,
4922  NI_LOG_DEBUG,
4923  "%s(): %d.%u p_packet->start_of_stream=%u, "
4924  "p_packet->end_of_stream=%u, p_packet->video_width=%u, "
4925  "p_packet->video_height=%u, p_packet->dts=0x%08x 0x%08x, "
4926  "p_packet->pts=0x%08x 0x%08x, type=%u, avg_frame_qp=%u, show_frame=%d\n",
4927  __func__, p_ctx->hw_id, p_ctx->session_id, p_packet->start_of_stream,
4928  p_packet->end_of_stream, p_packet->video_width, p_packet->video_height,
4929  (uint32_t)((p_packet->dts >> 32) & 0xFFFFFFFF),
4930  (uint32_t)(p_packet->dts & 0xFFFFFFFF),
4931  (uint32_t)((p_packet->pts >> 32) & 0xFFFFFFFF),
4932  (uint32_t)(p_packet->pts & 0xFFFFFFFF), p_packet->frame_type,
4933  p_packet->avg_frame_qp, p_packet->av1_show_frame);
4934 
4935  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): p_packet->data_len=%u, size=%d\n", __func__,
4936  p_packet->data_len, size);
4937 
4938  if (encq_count % 500 == 0)
4939  {
4940  ni_log2(p_ctx, NI_LOG_DEBUG,
4941  "Encoder pts queue size = %u dts queue size = %u\n\n",
4942  p_ctx->pts_table->list.count, p_ctx->dts_queue->list.count);
4943  }
4944 
4945  retval = size;
4946 
4947 #ifdef MEASURE_LATENCY
4948  if ((p_packet->dts != NI_NOPTS_VALUE) && (p_ctx->frame_time_q != NULL) && (p_ctx->pkt_num > 0))
4949  {
4950  if (NI_CODEC_FORMAT_AV1 != p_ctx->codec_format
4951  || p_packet->av1_show_frame)
4952  {
4953  uint64_t abs_time_ns = ni_gettime_ns();
4955  ni_log2(p_ctx, NI_LOG_INFO, "DTS:%" PRId64 ",DELTA:%" PRId64 ",eLAT:%" PRIu64 ";\n",
4956  p_packet->dts, abs_time_ns - q->last_benchmark_time,
4957  ni_lat_meas_q_check_latency(q, abs_time_ns, p_packet->dts));
4958  q->last_benchmark_time = abs_time_ns;
4959  }
4960  }
4961 #endif
4962 
4963 END:
4964 
4965  ni_pthread_mutex_unlock(&p_ctx->mutex);
4966 
4967  if (low_delay_notify)
4968  {
4969  low_delay_signal(p_ctx);
4970  }
4971 
4972  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
4973 
4974  return retval;
4975 }
4976 
4977 /*!******************************************************************************
4978  * \brief Send sequnce change to a xcoder encoder instance
4979  *
4980  * \param
4981  *
4982  * \return
4983  *******************************************************************************/
4985 {
4987 
4988  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
4989 
4990  // re-init last_gop_size and av1_pkt_num for minFramesDelay frame delay estimiation
4991  int lookAheadEnable = !!(((ni_xcoder_params_t *)p_ctx->p_session_config)->cfg_enc_params.lookAheadDepth);
4992  int gop_preset_index = ((ni_xcoder_params_t *)p_ctx->p_session_config)->cfg_enc_params.gop_preset_index;
4993  p_ctx->last_gop_size = g_map_preset_to_gopsize[lookAheadEnable][gop_preset_index + 1];
4994  p_ctx->av1_pkt_num = 0;
4995 
4996  //Configure encoder sequence change
4998  p_resolution);
5000  p_ctx->device_type, p_ctx->hw_id,
5001  &(p_ctx->session_id), OPT_1);
5002  CHECK_VPU_RECOVERY(retval);
5003  if (retval < 0)
5004  {
5005  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): config encoder sequence change command failed\n",
5006  __func__);
5008  }
5009 
5010 END:
5011 
5012  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
5013 
5014  return retval;
5015 }
5016 
5017 /*!******************************************************************************
5018  * \brief Open a xcoder scaler instance
5019  *
5020  * \param[in] p_ctx pointer to session context
5021  *
5022  * \return NI_RETCODE_INVALID_PARAM
5023  * NI_RETCODE_ERROR_NVME_CMD_FAILED
5024  * NI_RETCODE_ERROR_INVALID_SESSION
5025  * NI_RETCODE_ERROR_MEM_ALOC
5026  *******************************************************************************/
5028 {
5030  void* p_buffer = NULL;
5031  uint32_t ui32LBA = 0;
5032  char fmt_fw_api_ver1[5], fmt_fw_api_ver2[5];
5033 
5034  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
5035 
5036  if (!p_ctx)
5037  {
5038  retval = NI_RETCODE_INVALID_PARAM;
5039  LRETURN;
5040  }
5041 
5043  {
5045  "64") < 0)
5046  {
5047  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: Cannot use stack filter on device with FW API version < 6.4\n");
5049  }
5050  }
5051 
5053  {
5055  "67") < 0)
5056  {
5057  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: Cannot use rotate filter on device with FW API version < 6.7\n");
5059  }
5060  }
5061 
5063  {
5065  "6L") < 0)
5066  {
5067  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: Cannot use in-place overlay filter on device with FW API version < 6.L\n");
5069  }
5070  }
5071 
5072  if (p_ctx->session_id == NI_INVALID_SESSION_ID)
5073  {
5075  p_ctx->pts_table = NULL;
5076  p_ctx->dts_queue = NULL;
5077  p_ctx->p_leftover = NULL;
5078  p_ctx->buffer_pool = NULL;
5079  p_ctx->dec_fme_buf_pool = NULL;
5080  p_ctx->prev_size = 0;
5081  p_ctx->sent_size = 0;
5082  p_ctx->status = 0;
5083  p_ctx->key_frame_type = 0;
5084  p_ctx->ready_to_close = 0;
5085  p_ctx->rc_error_count = 0;
5086  p_ctx->frame_num = 0;
5087  p_ctx->pkt_num = 0;
5088  p_ctx->pkt_index = 0;
5089 
5090  //malloc zero data buffer
5091  if (ni_posix_memalign(&p_ctx->p_all_zero_buf, sysconf(_SC_PAGESIZE),
5093  {
5094  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() alloc all zero buffer failed\n",
5095  NI_ERRNO, __func__);
5096  retval = NI_RETCODE_ERROR_MEM_ALOC;
5097  LRETURN;
5098  }
5099  memset(p_ctx->p_all_zero_buf, 0, NI_DATA_BUFFER_LEN);
5100 
5101  //malloc data buffer
5102  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), NI_DATA_BUFFER_LEN))
5103  {
5104  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() alloc data buffer failed\n",
5105  NI_ERRNO, __func__);
5106  retval = NI_RETCODE_ERROR_MEM_ALOC;
5107  LRETURN;
5108  }
5109  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
5110 
5111  //Set session ID to be invalid. In case we cannot open session, the session id wold remain invalid.
5112  //In case we can open sesison, the session id would become valid.
5113  ((ni_session_stats_t *)p_buffer)->ui16SessionId =
5114  (uint16_t)NI_INVALID_SESSION_ID;
5115 
5116  // First uint32_t is either an invaild session ID or a valid session ID, depending on if session could be opened
5117  ui32LBA = OPEN_SESSION_CODEC(NI_DEVICE_TYPE_SCALER, ni_htonl(p_ctx->scaler_operation), 0);
5118  retval = ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
5119  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
5120  if (retval != NI_RETCODE_SUCCESS)
5121  {
5122  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR ni_nvme_send_read_cmd\n");
5123  LRETURN;
5124  }
5125  //Open will return a session status structure with a valid session id if it worked.
5126  //Otherwise the invalid session id set before the open command will stay
5127  p_ctx->session_id = ni_ntohs(((ni_session_stats_t *)p_buffer)->ui16SessionId);
5128  p_ctx->session_timestamp = ni_htonl(((ni_session_stats_t *)p_buffer)->ui32Session_timestamp_high);
5129  p_ctx->session_timestamp = (p_ctx->session_timestamp << 32) |
5130  ni_htonl(((ni_session_stats_t *)p_buffer)->ui32Session_timestamp_low);
5131  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
5132  {
5133  ni_log2(p_ctx, NI_LOG_ERROR,
5134  "ERROR %s(): p_ctx->device_handle=%" PRIx64
5135  ", p_ctx->hw_id=%d, p_ctx->session_id=%d\n",
5136  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
5137  p_ctx->session_id);
5139  LRETURN;
5140  }
5141  ni_log2(p_ctx, NI_LOG_DEBUG, "Scaler open session ID:0x%x\n",p_ctx->session_id);
5142  ni_log2(p_ctx, NI_LOG_DEBUG,
5143  "%s(): p_ctx->device_handle=%" PRIx64
5144  ", p_ctx->hw_id=%d, p_ctx->session_id=%d\n",
5145  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
5146  p_ctx->session_id);
5147 
5148  //Send keep alive timeout Info
5149  uint64_t keep_alive_timeout =
5150  p_ctx->keep_alive_timeout * 1000000; //send us to FW
5151  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
5152  memcpy(p_buffer, &keep_alive_timeout, sizeof(keep_alive_timeout));
5153  ni_log2(p_ctx, NI_LOG_DEBUG, "%s keep_alive_timeout %" PRIx64 "\n", __func__,
5154  keep_alive_timeout);
5156  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
5157  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
5158  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config, p_ctx->device_type,
5159  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
5160  CHECK_VPU_RECOVERY(retval);
5161 
5162  if (NI_RETCODE_SUCCESS != retval)
5163  {
5164  ni_log2(p_ctx, NI_LOG_ERROR,
5165  "ERROR %s(): nvme write keep_alive_timeout command "
5166  "failed, blk_io_handle: %" PRIx64 ", hw_id, %d\n",
5167  __func__, (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id);
5169  LRETURN;
5170  }
5171 
5172  // Send SW version to FW if FW API version is >= 6.2
5174  "62") >= 0)
5175  {
5176  // Send SW version to session manager
5177  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
5178  memcpy(p_buffer, NI_XCODER_REVISION, sizeof(uint64_t));
5180  ni_fmt_fw_api_ver_str((char*) &p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], &fmt_fw_api_ver2[0]);
5181  ni_log2(p_ctx, NI_LOG_DEBUG, "%s libxcoder FW API ver %s, FW FW API ver %s\n",
5182  __func__, fmt_fw_api_ver1, fmt_fw_api_ver2);
5183  ui32LBA = CONFIG_SESSION_SWVersion_W(p_ctx->session_id);
5184  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
5185  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
5186  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config, p_ctx->device_type,
5187  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
5188  CHECK_VPU_RECOVERY(retval);
5189 
5190  if (NI_RETCODE_SUCCESS != retval)
5191  {
5192  ni_log2(p_ctx, NI_LOG_ERROR,
5193  "ERROR %s(): nvme write sw_version command "
5194  "failed, blk_io_handle: %" PRIx64 ", hw_id, %d\n",
5195  __func__, (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id);
5197  LRETURN;
5198  }
5199  }
5200  }
5201 
5202  // init for frame pts calculation
5203  p_ctx->is_first_frame = 1;
5204  p_ctx->last_pts = 0;
5205  p_ctx->last_dts = 0;
5206  p_ctx->active_video_width = 0;
5207  p_ctx->active_video_height = 0;
5208  p_ctx->actual_video_width = 0;
5209 
5210 #ifndef _WIN32
5211  if (p_ctx->isP2P)
5212  {
5213  retval = p2p_fill_pcie_address(p_ctx);
5214  if(retval != NI_RETCODE_SUCCESS)
5215  {
5216  LRETURN;
5217  }
5218  }
5219 #endif
5220 
5221 END:
5222 
5223  ni_aligned_free(p_buffer);
5224  return retval;
5225 }
5226 
5227 /*!******************************************************************************
5228  * \brief close a scaler session
5229  *
5230  * \param[in] p_ctx pointer to session context
5231  * \param[in] eos_received (not used)
5232  *
5233  * \return NI_RETCODE_INVALID_PARAM
5234  * NI_RETCODE_ERROR_INVALID_SESSION
5235  * NI_RETCODE_ERROR_NVME_CMD_FAILED
5236  *******************************************************************************/
5238 {
5240  void * p_buffer = NULL;
5241  uint32_t ui32LBA = 0;
5242 
5243  if (!p_ctx)
5244  {
5245  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
5246  __func__);
5247  return NI_RETCODE_INVALID_PARAM;
5248  }
5249 
5250  ni_pthread_mutex_lock(&p_ctx->mutex);
5251 
5252  if (p_ctx->session_id == NI_INVALID_SESSION_ID)
5253  {
5254  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): Invalid session ID, return.\n", __func__);
5255  retval = NI_RETCODE_SUCCESS;
5256  LRETURN;
5257  }
5258 
5259  //malloc data buffer
5260  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), NI_DATA_BUFFER_LEN))
5261  {
5262  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() malloc data buffer failed\n",
5263  NI_ERRNO, __func__);
5264  retval = NI_RETCODE_ERROR_MEM_ALOC;
5265  LRETURN;
5266  }
5267  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
5268 
5270 
5271  int retry = 0;
5272  while (retry < NI_SESSION_CLOSE_RETRY_MAX)
5273  {
5274  ni_log2(p_ctx, NI_LOG_DEBUG,
5275  "%s(): p_ctx->blk_io_handle=%" PRIx64 ", p_ctx->hw_id=%d, "
5276  "p_ctx->session_id=%d, close_mode=1\n",
5277  __func__, (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id,
5278  p_ctx->session_id);
5279 
5281  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA) < 0)
5282  {
5283  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): command failed!\n", __func__);
5286  break;
5287  } else
5288  {
5289  //Close should always succeed
5290  retval = NI_RETCODE_SUCCESS;
5292  break;
5293  }
5294  retry++;
5295  }
5296 
5297 #ifndef _WIN32
5298  if (p_ctx->isP2P)
5299  {
5300  if (p_ctx->netint_fd)
5301  {
5302  close(p_ctx->netint_fd);
5303  }
5304  }
5305 #endif
5306 
5307 END:
5308 
5309  ni_aligned_free(p_buffer);
5311 
5312  ni_pthread_mutex_unlock(&p_ctx->mutex);
5313 
5314  return retval;
5315 }
5316 
5317 /*!******************************************************************************
5318  * \brief Send a p_config command to configure scaling parameters.
5319  *
5320  * \param ni_session_context_t p_ctx - xcoder Context
5321  * \param ni_scaler_params_t * params - pointer to the scaler ni_scaler_params_t struct
5322  *
5323  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION, NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
5324  *******************************************************************************/
5326  ni_scaler_params_t *p_params)
5327 {
5328  void *p_scaler_config = NULL;
5329  ni_scaler_config_t *p_cfg = NULL;
5330  uint32_t buffer_size = sizeof(ni_scaler_params_t);
5332  uint32_t ui32LBA = 0;
5333 
5334  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
5335 
5336  if (!p_ctx || !p_params)
5337  {
5338  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
5339  __func__);
5340  retval = NI_RETCODE_INVALID_PARAM;
5341  LRETURN;
5342  }
5343 
5344  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
5345  {
5346  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
5347  __func__);
5349  LRETURN;
5350  }
5351 
5352  buffer_size =
5353  ((buffer_size + (NI_MEM_PAGE_ALIGNMENT - 1)) / NI_MEM_PAGE_ALIGNMENT) *
5355  if (ni_posix_memalign(&p_scaler_config, sysconf(_SC_PAGESIZE), buffer_size))
5356  {
5357  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() malloc p_scaler_config buffer failed\n",
5358  NI_ERRNO, __func__);
5359  retval = NI_RETCODE_ERROR_MEM_ALOC;
5360  LRETURN;
5361  }
5362  memset(p_scaler_config, 0, buffer_size);
5363 
5364  //configure the session here
5367 
5368  //Flip the bytes!!
5369  p_cfg = (ni_scaler_config_t *)p_scaler_config;
5370  p_cfg->filterblit = p_params->filterblit;
5371  p_cfg->numInputs = p_params->nb_inputs;
5372  p_cfg->scaler_param_b = (uint16_t)(0);
5373  p_cfg->scaler_param_c = (uint16_t)(0.75 * 10000);
5374 
5375  if (p_params->enable_scaler_params)
5376  {
5377  // check fw revision
5378  if (ni_cmp_fw_api_ver(
5380  "6s2") < 0)
5381  {
5382  ni_log2(p_ctx, NI_LOG_ERROR, "%s: not supported config scaler params B and C "
5383  "on device with FW API version < 6s1\n", __func__);
5384  // Close the session since we can't configure it as per fw
5385  retval = ni_scaler_session_close(p_ctx, 0);
5386  if (NI_RETCODE_SUCCESS != retval)
5387  {
5388  ni_log2(p_ctx, NI_LOG_ERROR,
5389  "ERROR: %s failed: blk_io_handle: %" PRIx64 ","
5390  "hw_id, %d, xcoder_inst_id: %d\n",
5391  __func__,
5392  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id,
5393  p_ctx->session_id);
5394  }
5395 
5397  LRETURN;
5398  }
5399  if (p_params->scaler_param_b < 0 || p_params->scaler_param_c < 0 ||
5400  p_params->scaler_param_b > 1 || p_params->scaler_param_c > 1)
5401  {
5402  ni_log2(p_ctx, NI_LOG_ERROR, "%s: scaler_params_b and scaler_params_c must "
5403  "be in [0 , 1]. scaler_params_b is %lf, scaler_params_c is %lf\n",
5404  __func__, p_params->scaler_param_b, p_params->scaler_param_c);
5405  // Close the session since we can't configure it as per fw
5406  retval = ni_scaler_session_close(p_ctx, 0);
5407  if (NI_RETCODE_SUCCESS != retval)
5408  {
5409  ni_log2(p_ctx, NI_LOG_ERROR,
5410  "ERROR: %s failed: blk_io_handle: %" PRIx64 ","
5411  "hw_id, %d, xcoder_inst_id: %d\n",
5412  __func__,
5413  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id,
5414  p_ctx->session_id);
5415  }
5416 
5418  LRETURN;
5419  }
5420  p_cfg->scaler_param_b = (uint16_t)(p_params->scaler_param_b * 10000);
5421  p_cfg->scaler_param_c = (uint16_t)(p_params->scaler_param_c * 10000);
5422  }
5423 
5424  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
5425  p_scaler_config, buffer_size, ui32LBA);
5426  if ((int32_t)retval < 0)
5427  {
5428  ni_log2(p_ctx, NI_LOG_ERROR,
5429  "ERROR: ni_nvme_send_write_cmd failed: blk_io_handle: %" PRIx64
5430  ", hw_id, %d, xcoder_inst_id: %d\n",
5431  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
5432  // Close the session since we can't configure it as per fw
5433  retval = ni_scaler_session_close(p_ctx, 0);
5434  if (NI_RETCODE_SUCCESS != retval)
5435  {
5436  ni_log2(p_ctx, NI_LOG_ERROR,
5437  "ERROR: %s failed: blk_io_handle: %" PRIx64 ","
5438  "hw_id, %d, xcoder_inst_id: %d\n",
5439  __func__,
5440  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id,
5441  p_ctx->session_id);
5442  }
5443 
5445  LRETURN;
5446  }
5447  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
5448  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
5449 
5450 END:
5451 
5452  ni_aligned_free(p_scaler_config);
5453  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
5454 
5455  return retval;
5456 }
5457 
5458 /*!******************************************************************************
5459  * \brief allocate a frame in the scaler
5460  *
5461  * \param[in] p_ctx pointer to session context
5462  * \param[in] width width in pixels
5463  * \param[in] height height in pixels
5464  * \param[in] format pixel format
5465  * \param[in] options option flags
5466  * \param[in] rectangle_width clipping rectangle width in pixels
5467  * \param[in] rectangle_height clipping rectangle height in pixels
5468  * \param[in] rectangle_x clipping rectangle x position
5469  * \param[in] rectangle_y clipping rectangle y position
5470  * \param[in] rgba_color background colour (only used by pad filter)
5471  * \param[in] frame_index frame index (only for hardware frames)
5472  *
5473  * \return NI_RETCODE_INVALID_PARAM
5474  * NI_RETCODE_ERROR_INVALID_SESSION
5475  * NI_RETCODE_ERROR_NVME_CMD_FAILED
5476  * NI_RETCODE_ERROR_MEM_ALOC
5477  *******************************************************************************/
5479  int width,
5480  int height,
5481  int format,
5482  int options,
5483  int rectangle_width,
5484  int rectangle_height,
5485  int rectangle_x,
5486  int rectangle_y,
5487  int rgba_color,
5488  int frame_index)
5489 {
5492  uint32_t dataLen;
5493  uint32_t ui32LBA = 0;
5494  uint32_t query_retry = 0;
5495 
5496  /* Round up to nearest 4096 bytes */
5497  dataLen =
5499  dataLen = dataLen & 0xFFFFF000;
5500 
5501  if (!p_ctx)
5502  {
5503  return NI_RETCODE_INVALID_PARAM;
5504  }
5505 
5506  if (p_ctx->session_id == NI_INVALID_SESSION_ID)
5507  {
5508  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
5509  __func__);
5511  }
5512 
5513  if (((options & NI_SCALER_FLAG_IO) && (options & NI_SCALER_FLAG_PC)))
5514  {
5515  // this operation is to free/allocate scaler frame pool
5516  if (rgba_color == 0)
5517  {
5518  if (ni_cmp_fw_api_ver(
5520  "6r3") < 0)
5521  {
5522  ni_log2(p_ctx, NI_LOG_INFO,
5523  "WARNING: Allocate framepool size 0 for session 0x%x\n", p_ctx->session_id);
5524  return NI_RETCODE_SUCCESS;
5525  }
5526  else if (p_ctx->pool_type == NI_POOL_TYPE_NONE)
5527  {
5528  ni_log2(p_ctx, NI_LOG_ERROR,
5529  "ERROR: %s() try to free session 0x%x framepool while it's not allocated\n",
5530  __func__, p_ctx->session_id);
5531  return NI_RETCODE_INVALID_PARAM;
5532  }
5533  else if ((options & NI_SCALER_FLAG_P2) == p_ctx->pool_type)
5534  {
5535  ni_log2(p_ctx, NI_LOG_INFO, "Free framepool of scaler 0x%x\n", p_ctx->session_id);
5536  }
5537  else
5538  {
5539  ni_log2(p_ctx, NI_LOG_ERROR,
5540  "ERROR: %s() try to free session 0x%x framepool of type %u while "
5541  "passing type %u\n",
5542  __func__, p_ctx->session_id, p_ctx->pool_type,
5543  (options & NI_SCALER_FLAG_P2));
5544  }
5545  }
5546  else
5547  {
5548  if (p_ctx->pool_type != NI_POOL_TYPE_NONE)
5549  {
5550  // try to expand the framepool
5551  if (ni_cmp_fw_api_ver(
5553  "6r3") < 0)
5554  {
5555  ni_log2(p_ctx, NI_LOG_ERROR,
5556  "ERROR: allocate framepool multiple times for session 0x%x "
5557  "not supported in FW API version < 6r3\n",
5558  p_ctx->session_id);
5559  return NI_RETCODE_INVALID_PARAM;
5560  }
5561  if ((options & NI_SCALER_FLAG_P2) == p_ctx->pool_type)
5562  {
5563  ni_log2(p_ctx, NI_LOG_INFO,
5564  "Expand frame pool of scaler 0x%x with %u more frames\n",
5565  p_ctx->session_id, rgba_color);
5566  }
5567  else
5568  {
5569  ni_log2(p_ctx, NI_LOG_ERROR,
5570  "ERROR: try to expand session 0x%x framepool with type %u "
5571  "while pool type is %u\n",
5572  p_ctx->session_id, options & NI_SCALER_FLAG_P2, p_ctx->pool_type);
5573  return NI_RETCODE_INVALID_PARAM;
5574  }
5575  }
5576  }
5577  }
5578 
5579  if (ni_posix_memalign((void **)&p_data, sysconf(_SC_PAGESIZE), dataLen))
5580  {
5581  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
5582  NI_ERRNO, __func__);
5584  }
5585 
5586  memset(p_data, 0x00, dataLen);
5587 
5588  p_data->picture_width = width;
5589  p_data->picture_height = height;
5590  p_data->picture_format = format;
5591  p_data->options = options;
5592  p_data->rectangle_width = rectangle_width;
5593  p_data->rectangle_height = rectangle_height;
5594  p_data->rectangle_x = rectangle_x;
5595  p_data->rectangle_y = rectangle_y;
5596  p_data->rgba_color = rgba_color;
5597  p_data->frame_index = frame_index;
5598 
5599  ni_log2(p_ctx, NI_LOG_DEBUG, "%s() options %d frame_index %u buffered_frame_index %d\n",
5600  __func__, options, p_data->frame_index, p_ctx->buffered_frame_index);
5601 
5602  switch (p_ctx->scaler_operation)
5603  {
5606  if ((options & NI_SCALER_FLAG_IO) && !(options & NI_SCALER_FLAG_PC))
5607  {
5608  p_data->frame_index = frame_index; // background
5609  if (p_ctx->buffered_frame_index)
5610  {
5611  p_data->rgba_color = p_ctx->buffered_frame_index; // output frame
5612  }
5613  else
5614  {
5615  p_data->rgba_color = 0; // no output frame, fw must acquire
5616  }
5617  }
5618  break;
5619 
5620  default:
5621  // Send acquired buffer index in AdminWr SET_ALLOC_FRAME command to prevent FP
5622  // ConfigOutput from acquire buffer again
5623  if ((options & NI_SCALER_FLAG_IO) && p_ctx->buffered_frame_index)
5624  {
5625  p_data->frame_index = p_ctx->buffered_frame_index;
5626  }
5627  break;
5628  }
5629 
5630  bool isrgb = ((GC620_RGBA8888 == format) || (GC620_BGRX8888 == format) ||
5631  (GC620_ARGB8888 == format) || (GC620_ABGR8888 == format));
5632  if(width > NI_MAX_RESOLUTION_WIDTH || height > NI_MAX_RESOLUTION_HEIGHT ||
5634  ((width > NI_MAX_RESOLUTION_RGBA_WIDTH || height > NI_MAX_RESOLUTION_RGBA_HEIGHT) && isrgb))
5635  {
5636  if(width < NI_MIN_RESOLUTION_WIDTH_SCALER)
5637  ni_log2(p_ctx, NI_LOG_ERROR, "Invalid Picture Width: too small for scaler\n");
5638  if(height < NI_MIN_RESOLUTION_HEIGHT_SCALER)
5639  ni_log2(p_ctx, NI_LOG_ERROR, "Invalid Picture Height: too small for scaler\n");
5640  if(width > NI_MAX_RESOLUTION_WIDTH || ((width > NI_MAX_RESOLUTION_RGBA_WIDTH) && isrgb))
5641  ni_log2(p_ctx, NI_LOG_ERROR, "Invalid Picture Width: too big for scaler\n");
5642  if(height > NI_MAX_RESOLUTION_HEIGHT || ((height > NI_MAX_RESOLUTION_RGBA_HEIGHT) && isrgb))
5643  ni_log2(p_ctx, NI_LOG_ERROR, "Invalid Picture Height: too big for scaler\n");
5644 
5645  ni_log2(p_ctx, NI_LOG_ERROR, "Resolution %d x %d not supported for %d format!\n", width, height, format);
5646  ni_aligned_free(p_data);
5648  }
5649  ni_log2(p_ctx, NI_LOG_DEBUG,
5650  "Session=0x%x: Dev alloc frame: FrameIndex=%d; W=%d; H=%d; C=%d; RW=%d; RH=%d; RX=%d; RY=%d\n",
5651  p_ctx->session_id, p_data->frame_index,
5652  p_data->picture_width, p_data->picture_height,
5653  p_data->picture_format, p_data->rectangle_width,
5654  p_data->rectangle_height, p_data->rectangle_x, p_data->rectangle_y);
5655 
5658 
5659  for (;;)
5660  {
5661  query_retry++;
5662 
5663  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
5664  p_data, dataLen, ui32LBA);
5665 
5666  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
5667  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_3);
5668 
5670  {
5671  if (query_retry >= 1000 || (options & NI_SCALER_FLAG_PC))
5672  {
5673  ni_log2(p_ctx, NI_LOG_ERROR, "Error: 2D could not acquire frame\n");
5674  retval = NI_RETCODE_FAILURE;
5675  LRETURN;
5676  }
5678  continue;
5679  }
5680  else
5681  {
5682  p_ctx->pool_type = ((options & NI_SCALER_FLAG_IO) && (options & NI_SCALER_FLAG_PC)) ?
5683  (options & NI_SCALER_FLAG_P2) : (p_ctx->pool_type);
5684  break;
5685  }
5686  }
5687 
5688  if (NI_RETCODE_SUCCESS != retval)
5689  {
5690  ni_log2(p_ctx, NI_LOG_ERROR,
5691  "ERROR: ni_nvme_send_admin_cmd failed: "
5692  "blk_io_handle: %" PRIx64 ", hw_id, %u, xcoder_inst_id: %d\n",
5693  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
5694  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed!\n", __func__);
5696  }
5697 
5698 END:
5699 
5700  ni_aligned_free(p_data);
5701  return retval;
5702 }
5703 
5704 /*!******************************************************************************
5705  * \brief config a frame in the scaler
5706  *
5707  * \param[in] p_ctx pointer to session context
5708  * \param[in] p_cfg pointer to frame config
5709  *
5710  * \return NI_RETCODE_INVALID_PARAM
5711  * NI_RETCODE_ERROR_INVALID_SESSION
5712  * NI_RETCODE_ERROR_NVME_CMD_FAILED
5713  * NI_RETCODE_ERROR_MEM_ALOC
5714  *******************************************************************************/
5716  ni_frame_config_t *p_cfg)
5717 {
5720  uint32_t dataLen;
5721  uint32_t ui32LBA = 0;
5722 
5723  /* Round up to nearest 4096 bytes */
5724  dataLen =
5726  dataLen = dataLen & 0xFFFFF000;
5727 
5728  if (!p_ctx || !p_cfg)
5729  {
5730  return NI_RETCODE_INVALID_PARAM;
5731  }
5732 
5733  if (p_ctx->session_id == NI_INVALID_SESSION_ID)
5734  {
5735  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
5736  __func__);
5738  }
5739 
5740  if (ni_posix_memalign((void **)&p_data, sysconf(_SC_PAGESIZE), dataLen))
5741  {
5742  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
5743  NI_ERRNO, __func__);
5745  }
5746 
5747  memset(p_data, 0x00, dataLen);
5748 
5749  p_data->picture_width = p_cfg->picture_width;
5750  p_data->picture_height = p_cfg->picture_height;
5751  p_data->picture_format = p_cfg->picture_format;
5752  p_data->options = p_cfg->options;
5753 
5754  p_data->rectangle_width = p_cfg->rectangle_width;
5755  p_data->rectangle_height = p_cfg->rectangle_height;
5756  p_data->rectangle_x = p_cfg->rectangle_x;
5757  p_data->rectangle_y = p_cfg->rectangle_y;
5758  p_data->rgba_color = p_cfg->rgba_color;
5759  p_data->frame_index = p_cfg->frame_index;
5760  p_data->session_id = p_cfg->session_id;
5761  p_data->output_index = p_cfg->output_index;
5762  switch (p_cfg->orientation)
5763  {
5764  case 0:
5765  case 2:
5766  case 4:
5767  case 5:
5768  p_data->orientation = p_cfg->orientation;
5769  break;
5770  case 1:
5771  p_data->orientation = 3;
5772  break;
5773  case 3:
5774  p_data->orientation = 1;
5775  break;
5776  default:
5777  ni_log2(p_ctx, NI_LOG_ERROR, "Bad orientation: %u\n", p_cfg->orientation);
5779  }
5780  bool isrgb = ((GC620_RGBA8888 == p_data->picture_format) || (GC620_BGRX8888 == p_data->picture_format) ||
5781  (GC620_ARGB8888 == p_data->picture_format) || (GC620_ABGR8888 == p_data->picture_format));
5782  if(p_data->picture_width > NI_MAX_RESOLUTION_WIDTH || p_data->picture_height > NI_MAX_RESOLUTION_HEIGHT ||
5783  p_data->picture_width < NI_MIN_RESOLUTION_WIDTH_SCALER || p_data->picture_height < NI_MIN_RESOLUTION_HEIGHT_SCALER ||
5784  ((p_data->picture_width > NI_MAX_RESOLUTION_RGBA_WIDTH || p_data->picture_height > NI_MAX_RESOLUTION_RGBA_HEIGHT) && isrgb))
5785  {
5786  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);
5787  ni_aligned_free(p_data);
5789  }
5790 
5791  ni_log2(p_ctx, NI_LOG_DEBUG,
5792  "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",
5793  p_ctx->session_id, p_cfg->frame_index,
5794  p_data->picture_width, p_data->picture_height,
5795  p_data->picture_format, p_data->rectangle_width,
5796  p_data->rectangle_height, p_data->rectangle_x,
5797  p_data->rectangle_y, p_data->orientation);
5798 
5801 
5802  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
5803  p_data, dataLen, ui32LBA);
5804  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
5805  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
5806  if (NI_RETCODE_SUCCESS != retval)
5807  {
5808  ni_log2(p_ctx, NI_LOG_ERROR,
5809  "ERROR: ni_nvme_send_admin_cmd failed: blk_io_handle: %" PRIx64
5810  ", hw_id, %u, xcoder_inst_id: %d\n",
5811  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
5812 
5813  ni_log2(p_ctx, NI_LOG_ERROR,
5814  "ERROR ni_scaler_config(): nvme command failed!\n");
5816  }
5817 
5818 END:
5819 
5820  ni_aligned_free(p_data);
5821  return retval;
5822 }
5823 
5824 /*!******************************************************************************
5825  * \brief config multiple frames in the scaler
5826  *
5827  * \param[in] p_ctx pointer to session context
5828  * \param[in] p_cfg_in pointer to input frame config array
5829  * \param[in] numInCfgs number of input frame configs in the p_cfg array
5830  * \param[in] p_cfg_out pointer to output frame config
5831  *
5832  * \return NI_RETCODE_INVALID_PARAM
5833  * NI_RETCODE_ERROR_INVALID_SESSION
5834  * NI_RETCODE_ERROR_NVME_CMD_FAILED
5835  * NI_RETCODE_ERROR_MEM_ALOC
5836  *******************************************************************************/
5838  ni_frame_config_t p_cfg_in[],
5839  int numInCfgs,
5840  ni_frame_config_t *p_cfg_out)
5841 {
5843  ni_instance_mgr_allocation_info_t *p_data, *p_data_orig;
5844  uint32_t dataLen;
5845  uint32_t ui32LBA = 0;
5846  int i;
5847 
5848  /* Round up to nearest 4096 bytes */
5849  dataLen =
5850  sizeof(ni_instance_mgr_allocation_info_t) * (numInCfgs + 1) + NI_MEM_PAGE_ALIGNMENT - 1;
5851  dataLen = dataLen & 0xFFFFF000;
5852 
5853  if (!p_ctx || (!p_cfg_in && numInCfgs))
5854  {
5855  return NI_RETCODE_INVALID_PARAM;
5856  }
5857 
5858  if (p_ctx->session_id == NI_INVALID_SESSION_ID)
5859  {
5860  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
5861  __func__);
5863  }
5864 
5865  if (ni_posix_memalign((void **)&p_data, sysconf(_SC_PAGESIZE), dataLen))
5866  {
5867  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
5868  NI_ERRNO, __func__);
5870  }
5871 
5872  memset(p_data, 0x00, dataLen);
5873 
5874  p_data_orig = p_data;
5875 
5876  for (i = 0; i < numInCfgs; i++)
5877  {
5878  p_data->picture_width = p_cfg_in[i].picture_width;
5879  p_data->picture_height = p_cfg_in[i].picture_height;
5880  p_data->picture_format = p_cfg_in[i].picture_format;
5881  p_data->options = p_cfg_in[i].options & ~NI_SCALER_FLAG_IO;
5882 
5883  p_data->rectangle_width = p_cfg_in[i].rectangle_width;
5884  p_data->rectangle_height = p_cfg_in[i].rectangle_height;
5885  p_data->rectangle_x = p_cfg_in[i].rectangle_x;
5886  p_data->rectangle_y = p_cfg_in[i].rectangle_y;
5887  p_data->rgba_color = p_cfg_in[i].rgba_color;
5888  p_data->frame_index = p_cfg_in[i].frame_index;
5889  p_data->session_id = p_cfg_in[i].session_id;
5890  p_data->output_index = p_cfg_in[i].output_index;
5891 
5892  ni_log2(p_ctx, NI_LOG_DEBUG,
5893  "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",
5894  p_ctx->session_id, i,
5895  p_data->frame_index, p_data->session_id,
5896  p_data->picture_width, p_data->picture_height,
5897  p_data->picture_format, p_data->rectangle_width,
5898  p_data->rectangle_height, p_data->rectangle_x, p_data->rectangle_y);
5899 
5900  p_data++;
5901  }
5902 
5903  if (p_cfg_out)
5904  {
5905  p_data->picture_width = p_cfg_out->picture_width;
5906  p_data->picture_height = p_cfg_out->picture_height;
5907  p_data->picture_format = p_cfg_out->picture_format;
5908  p_data->options = p_cfg_out->options | NI_SCALER_FLAG_IO;
5909 
5910  p_data->rectangle_width = p_cfg_out->rectangle_width;
5911  p_data->rectangle_height = p_cfg_out->rectangle_height;
5912  p_data->rectangle_x = p_cfg_out->rectangle_x;
5913  p_data->rectangle_y = p_cfg_out->rectangle_y;
5914  p_data->rgba_color = p_cfg_out->rgba_color;
5915  p_data->frame_index = p_cfg_out->frame_index;
5916 
5917  ni_log2(p_ctx, NI_LOG_DEBUG,
5918  "Session=0x%x: Dev out config frame: FrameIndex=%u; W=%d; H=%d; C=%d; RW=%d; RH=%d; RX=%d; RY=%d\n",
5919  p_ctx->session_id, p_data->frame_index,
5920  p_data->picture_width, p_data->picture_height,
5921  p_data->picture_format, p_data->rectangle_width,
5922  p_data->rectangle_height, p_data->rectangle_x,
5923  p_data->rectangle_y);
5924  }
5925  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))))
5926  {
5927  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);
5928  ni_aligned_free(p_data);
5930  }
5931 
5934 
5935  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
5936  p_data_orig, dataLen, ui32LBA);
5937  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
5938  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
5939  if (NI_RETCODE_SUCCESS != retval)
5940  {
5941  ni_log2(p_ctx, NI_LOG_ERROR,
5942  "ERROR: ni_nvme_send_admin_cmd failed: blk_io_handle: %" PRIx64
5943  ", hw_id, %u, xcoder_inst_id: %d\n",
5944  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
5945 
5946  ni_log2(p_ctx, NI_LOG_ERROR,
5947  "ERROR ni_scaler_config(): nvme command failed!\n");
5949  }
5950 
5951 END:
5952 
5953  ni_aligned_free(p_data_orig);
5954  return retval;
5955 }
5956 
5957 /*!******************************************************************************
5958  * \brief Query a particular xcoder instance to get GeneralStatus data
5959  *
5960  * \param ni_session_context_t p_ctx - xcoder Context
5961  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
5962  * \param ni_instance_mgr_general_status_t *out - Struct preallocated from the caller where the
5963  * resulting data will be placed
5964  *
5965  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_MEM_ALOC or NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
5966  *******************************************************************************/
5968 {
5969  void* p_buffer = NULL;
5970  int retval = NI_RETCODE_SUCCESS;
5971  uint32_t ui32LBA = 0;
5973 
5974  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
5975 
5976  if ((!p_ctx) || (!p_gen_status))
5977  {
5978  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
5979  __func__);
5980  retval = NI_RETCODE_INVALID_PARAM;
5981  LRETURN;
5982  }
5983 
5984  if (!IS_XCODER_DEVICE_TYPE(device_type))
5985  {
5986  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown device type %d, return\n",
5987  __func__, device_type);
5988  retval = NI_RETCODE_INVALID_PARAM;
5989  LRETURN;
5990  }
5991 
5992  ui32LBA = QUERY_GENERAL_GET_STATUS_R(device_type);
5993 
5994  if (ni_posix_memalign((void **)&p_buffer, sysconf(_SC_PAGESIZE), dataLen))
5995  {
5996  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
5997  NI_ERRNO, __func__);
5998  retval = NI_RETCODE_ERROR_MEM_ALOC;
5999  LRETURN;
6000  }
6001 
6002  memset(p_buffer, 0, dataLen);
6003 
6004  if (ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_buffer, dataLen, ui32LBA) < 0)
6005  {
6006  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
6008  LRETURN;
6009  }
6010 
6011  //No need to flip the bytes since the datastruct has only uint8_t datatypes
6012  memcpy((void*)p_gen_status, p_buffer, sizeof(ni_instance_mgr_general_status_t));
6013 
6014  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): model_load:%u qc:%d percent:%d\n", __func__,
6015  p_gen_status->fw_model_load, p_gen_status->cmd_queue_count,
6016  p_gen_status->process_load_percent);
6017 END:
6018 
6019  ni_aligned_free(p_buffer);
6020  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
6021 
6022  return retval;
6023 }
6024 
6025 /*!******************************************************************************
6026  * \brief Query a particular xcoder instance to get DetailStatus data
6027  *
6028  * \param ni_session_context_t p_ctx - xcoder Context
6029  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
6030  * \param ni_instance_mgr_detail_status_t *out - Struct preallocated from the caller where the
6031  * resulting data will be placed
6032  *
6033  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_MEM_ALOC or NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
6034  *******************************************************************************/
6035 int ni_query_detail_status(ni_session_context_t* p_ctx, ni_device_type_t device_type, void* p_detail_status, int ver)
6036 {
6037  void* p_buffer = NULL;
6038  int retval = NI_RETCODE_SUCCESS;
6039  uint32_t ui32LBA = 0;
6040  uint32_t dataLen = 0;
6041  uint32_t copyLen = 0;
6042  if(ver == 0)
6043  {
6044  ui32LBA = QUERY_DETAIL_GET_STATUS_R(device_type);
6046  dataLen = ((copyLen + (NI_MEM_PAGE_ALIGNMENT - 1)) / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT;
6047  }
6048  else if(ver == 1)
6049  {
6050  ui32LBA = QUERY_DETAIL_GET_STATUS_V1_R(device_type);
6051  copyLen = sizeof(ni_instance_mgr_detail_status_v1_t);
6052  dataLen = (copyLen + (NI_MEM_PAGE_ALIGNMENT - 1)) / NI_MEM_PAGE_ALIGNMENT * NI_MEM_PAGE_ALIGNMENT;
6053  }
6054  else
6055  {
6056  retval = NI_RETCODE_INVALID_PARAM;
6057  LRETURN;
6058  }
6059 
6060  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
6061 
6062  if ((!p_ctx) || (!p_detail_status))
6063  {
6064  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
6065  __func__);
6066  retval = NI_RETCODE_INVALID_PARAM;
6067  LRETURN;
6068  }
6069 
6070  if (!IS_XCODER_DEVICE_TYPE(device_type))
6071  {
6072  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown device type %d, return\n",
6073  __func__, device_type);
6074  retval = NI_RETCODE_INVALID_PARAM;
6075  LRETURN;
6076  }
6077 
6078  if (ni_posix_memalign((void **)&p_buffer, sysconf(_SC_PAGESIZE), dataLen))
6079  {
6080  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
6081  NI_ERRNO, __func__);
6082  retval = NI_RETCODE_ERROR_MEM_ALOC;
6083  LRETURN;
6084  }
6085 
6086  memset(p_buffer, 0, dataLen);
6087 
6088  if (ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_buffer, dataLen, ui32LBA) < 0)
6089  {
6090  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
6092  LRETURN;
6093  }
6094 
6095  //No need to flip the bytes since the datastruct has only uint8_t datatypes
6096  memcpy(p_detail_status, p_buffer, copyLen);
6097 
6098 END:
6099 
6100  ni_aligned_free(p_buffer);
6101  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
6102 
6103  return retval;
6104 }
6105 
6106 /*!******************************************************************************
6107  * \brief Query a particular xcoder instance to get Stream Info data
6108  *
6109  * \param ni_session_context_t p_ctx - xcoder Context
6110  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
6111  * \param ni_instance_mgr_stream_info_t *out - Struct preallocated from the caller where the
6112  * resulting data will be placed
6113  *
6114  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION, NI_RETCODE_ERROR_MEM_ALOC
6115  * or NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
6116  *******************************************************************************/
6118 {
6119  void* p_buffer = NULL;
6121  uint32_t ui32LBA = 0;
6123 
6124  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
6125 
6126  if ((!p_ctx) || (!p_stream_info))
6127  {
6128  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
6129  __func__);
6130  retval = NI_RETCODE_INVALID_PARAM;
6131  LRETURN;
6132  }
6133 
6134  if (! (NI_DEVICE_TYPE_DECODER == device_type ||
6135  NI_DEVICE_TYPE_ENCODER == device_type))
6136  {
6137  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown device type %d, return\n",
6138  __func__, device_type);
6139  retval = NI_RETCODE_INVALID_PARAM;
6140  LRETURN;
6141  }
6142 
6143  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
6144  {
6145  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
6146  __func__);
6148  LRETURN;
6149  }
6150 
6151  ui32LBA = QUERY_INSTANCE_STREAM_INFO_R(p_ctx->session_id, device_type);
6152 
6153  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), dataLen))
6154  {
6155  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
6156  NI_ERRNO, __func__);
6157  retval = NI_RETCODE_ERROR_MEM_ALOC;
6158  LRETURN;
6159  }
6160  memset(p_buffer, 0, dataLen);
6161 
6162  if (ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_buffer, dataLen, ui32LBA) < 0)
6163  {
6164  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
6166  LRETURN;
6167  }
6168 
6169  memcpy((void*)p_stream_info, p_buffer, sizeof(ni_instance_mgr_stream_info_t));
6170 
6171  //flip the bytes to host order
6172  p_stream_info->picture_width = ni_htons(p_stream_info->picture_width);
6173  p_stream_info->picture_height = ni_htons(p_stream_info->picture_height);
6174  p_stream_info->frame_rate = ni_htons(p_stream_info->frame_rate);
6175  p_stream_info->is_flushed = ni_htons(p_stream_info->is_flushed);
6176  p_stream_info->transfer_frame_stride = ni_htons(p_stream_info->transfer_frame_stride);
6177  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): pix_format = %d\n", __func__,
6178  p_stream_info->pix_format); //temp
6179 
6180 END:
6181 
6182  ni_aligned_free(p_buffer);
6183  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
6184 
6185  return retval;
6186 }
6187 
6188 /*!*****************************************************************************
6189  * \brief Query a particular session to get the stats info
6190  *
6191  * \param ni_session_context_t p_ctx - xcoder Context
6192  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
6193  * \param ni_session_stats_t *out - Struct preallocated from the
6194  * caller where the resulting data will be placed
6195  *
6196  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION,
6197  * NI_RETCODE_ERROR_MEM_ALOC or NI_RETCODE_ERROR_NVME_CMD_FAILED
6198  * on failure
6199  ******************************************************************************/
6201  ni_device_type_t device_type,
6202  ni_session_stats_t *p_session_stats, int rc,
6203  int opcode)
6204 {
6206  void* p_buffer = NULL;
6207  uint64_t session_timestamp;
6208  uint32_t ui32LBA = 0;
6209  uint32_t dataLen = ((sizeof(ni_session_stats_t) + (NI_MEM_PAGE_ALIGNMENT - 1)) / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT;
6210 
6211  ni_device_type_t xc_device_type =
6212  (device_type != NI_DEVICE_TYPE_UPLOAD ? device_type :
6214 
6215  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
6216 
6217  if ((!p_ctx) || (!p_session_stats))
6218  {
6219  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
6220  __func__);
6221  retval = NI_RETCODE_INVALID_PARAM;
6222  LRETURN;
6223  }
6224 
6225  if (!IS_XCODER_DEVICE_TYPE(xc_device_type))
6226  {
6227  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown device type %d, return\n",
6228  __func__, device_type);
6229  retval = NI_RETCODE_INVALID_PARAM;
6230  LRETURN;
6231  }
6232 
6233  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
6234  {
6235  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
6236  __func__);
6238  LRETURN;
6239  }
6240 
6241  ui32LBA = QUERY_SESSION_STATS_R(p_ctx->session_id, xc_device_type);
6242 
6243  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), dataLen))
6244  {
6245  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
6246  NI_ERRNO, __func__);
6247  retval = NI_RETCODE_ERROR_MEM_ALOC;
6248  LRETURN;
6249  }
6250  memset(p_buffer, 0, dataLen);
6251 
6252  // Set session ID to be invalid. In case, the last command fails because the invalid session ID was submitted
6253  // with the command, the session id would remain invalid.
6254  // If the last command is processed successfully in session manager, the session id would become valid.
6255  ((ni_session_stats_t *)p_buffer)->ui16SessionId =
6256  (uint16_t)NI_INVALID_SESSION_ID;
6257 
6258  if (ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_buffer, dataLen, ui32LBA)
6259  < 0)
6260  {
6261  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): read command Failed\n", __func__);
6263  LRETURN;
6264  }
6265 
6266  memcpy((void*)p_session_stats, p_buffer, sizeof(ni_session_stats_t));
6267 
6268  // flip the bytes to host order
6269  // all query commands are guaranteed success once a session is opened
6270  p_session_stats->ui16SessionId = ni_htons(p_session_stats->ui16SessionId);
6271  p_session_stats->ui16ErrorCount = ni_htons(p_session_stats->ui16ErrorCount);
6272  p_session_stats->ui32LastTransactionId =
6273  ni_htonl(p_session_stats->ui32LastTransactionId);
6274  p_session_stats->ui32LastTransactionCompletionStatus =
6275  ni_htonl(p_session_stats->ui32LastTransactionCompletionStatus);
6276  p_session_stats->ui32LastErrorTransactionId = ni_htonl(p_session_stats->ui32LastErrorTransactionId);
6277  p_session_stats->ui32LastErrorStatus = ni_htonl(p_session_stats->ui32LastErrorStatus);
6278  p_session_stats->ui32Session_timestamp_high = ni_htonl(p_session_stats->ui32Session_timestamp_high);
6279  p_session_stats->ui32Session_timestamp_low = ni_htonl(p_session_stats->ui32Session_timestamp_low);
6280 
6281  session_timestamp = p_session_stats->ui32Session_timestamp_high;
6282  session_timestamp <<= 32;
6283  session_timestamp |= p_session_stats->ui32Session_timestamp_low;
6284 
6285  // get the session timestamp when open session
6286  // check the timestamp during transcoding
6287  if ((p_ctx->session_timestamp != session_timestamp) &&
6288  (ni_xcoder_resource_recovery != p_session_stats->ui32LastErrorStatus))
6289  // if VPU recovery, the session timestamp will be reset.
6290  {
6291  p_session_stats->ui32LastErrorStatus =
6293  ni_log2(p_ctx, NI_LOG_DEBUG, "instance id invalid:%u, timestamp:%" PRIu64 ", "
6294  "query timestamp:%" PRIu64 "\n", p_ctx->session_id,
6295  p_ctx->session_timestamp, session_timestamp);
6296  }
6297 
6298  // check rc here, if rc != NI_RETCODE_SUCCESS, it means that last read/write command failed
6299  // failures may be link layer errors, such as physical link errors or ERROR_WRITE_PROTECT in windows.
6300  if (NI_RETCODE_SUCCESS != rc)
6301  {
6302  ni_log2(p_ctx, NI_LOG_ERROR, "%s():last command Failed: rc %d\n", __func__, rc);
6303  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 "
6304  "ready_to_close %u session_run_state %d active_video_width %u active_video_height %u\n",
6305  __func__,
6306  p_ctx->session_id,
6307  p_ctx->session_timestamp,
6308  p_ctx->hw_id,
6309  p_ctx->device_type,
6310  p_ctx->codec_format,
6311  p_ctx->frame_num,
6312  p_ctx->pkt_num,
6313  p_ctx->ready_to_close,
6314  p_ctx->session_run_state,
6315  p_ctx->active_video_width,
6316  p_ctx->active_video_height);
6317 
6318 #if __linux__ || __APPLE__
6319 #if !defined(_ANDROID) && !defined(__OPENHARMONY__)
6320 #ifndef DISABLE_BACKTRACE_PRINT
6321  ni_print_backtrace(); // log backtrace
6322 #endif
6323 #endif
6324 #endif
6325 
6326  p_session_stats->ui32LastTransactionCompletionStatus =
6328  p_session_stats->ui32LastErrorStatus = NI_RETCODE_ERROR_NVME_CMD_FAILED;
6330  } else if (p_ctx->session_id != p_session_stats->ui16SessionId)
6331  {
6332  uint64_t ct = ni_gettime_ns();
6333  uint64_t dt = ct - p_ctx->last_access_time;
6334  ni_log2(p_ctx, NI_LOG_ERROR,
6335  "%s(): device 0x%" PRIx64 " last command Failed due to wrong "
6336  "session ID. Expected 0x%x, got 0x%x keep alive last access "
6337  "time %" PRIu64 ", current %" PRIu64 "\n", __func__,
6338  (int64_t)p_ctx->blk_io_handle, p_ctx->session_id,
6339  p_session_stats->ui16SessionId, p_ctx->last_access_time, ct);
6340  if (dt > 1000000000)
6341  {
6342  ni_log2(p_ctx, NI_LOG_ERROR,
6343  "%s():long delay between last command dt = %" PRId64 " ns, "
6344  "process was possibly blocked.\n", __func__, dt);
6345  }
6346  p_session_stats->ui32LastErrorStatus = NI_RETCODE_ERROR_INVALID_SESSION;
6347 
6348  // Mark session id to INVALID so that all commands afterward are blocked
6350  }
6351 
6352  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): error count %u last rc 0x%x inst_err_no 0x%x\n",
6353  __func__, p_session_stats->ui16ErrorCount,
6354  p_session_stats->ui32LastTransactionCompletionStatus,
6355  p_session_stats->ui32LastErrorStatus);
6356 
6357 END:
6358 
6359  ni_aligned_free(p_buffer);
6360  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
6361 
6362  return retval;
6363 }
6364 
6365 /*!******************************************************************************
6366  * \brief Query a particular xcoder instance to get End of Output data
6367  *
6368  * \param ni_session_context_t p_ctx - xcoder Context
6369  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
6370  * \param InstMgrStreamComp *out - Struct preallocated from the caller where the
6371  * resulting data will be placed
6372  *
6373  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION, NI_RETCODE_ERROR_MEM_ALOC
6374  * or NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
6375  *******************************************************************************/
6377 {
6378  void* p_buffer = NULL;
6380  uint32_t ui32LBA = 0;
6382 
6383  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
6384 
6385  if (!p_ctx || !p_stream_complete)
6386  {
6387  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
6388  __func__);
6389  retval = NI_RETCODE_INVALID_PARAM;
6390  LRETURN;
6391  }
6392 
6393  if (! (NI_DEVICE_TYPE_DECODER == device_type ||
6394  NI_DEVICE_TYPE_ENCODER == device_type))
6395  {
6396  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown device type %d, return\n",
6397  __func__, device_type);
6398  retval = NI_RETCODE_INVALID_PARAM;
6399  LRETURN;
6400  }
6401 
6402  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
6403  {
6404  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
6405  __func__);
6407  LRETURN;
6408  }
6409 
6410  ui32LBA = QUERY_INSTANCE_EOS_R(p_ctx->session_id, device_type);
6411 
6412  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), dataLen))
6413  {
6414  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: Cannot allocate buffer.\n");
6415  retval = NI_RETCODE_ERROR_MEM_ALOC;
6416  LRETURN;
6417  }
6418 
6419  memset(p_buffer, 0, dataLen);
6420 
6421  if (ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_buffer, dataLen, ui32LBA) < 0)
6422  {
6423  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
6425  LRETURN;
6426  }
6427 
6428  memcpy((void*)p_stream_complete, p_buffer, sizeof(ni_instance_mgr_stream_complete_t));
6429 
6430  //flip the bytes to host order
6431  p_stream_complete->is_flushed = ni_htons(p_stream_complete->is_flushed);
6432 
6433 END:
6434 
6435  ni_aligned_free(p_buffer);
6436  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
6437 
6438  return retval;
6439 }
6440 
6441 static const char* ni_get_device_type_str(int type)
6442 {
6443  if (type < NI_DEVICE_TYPE_DECODER || type > NI_DEVICE_TYPE_AI)
6444  {
6445  return "Invalid device type";
6446  }
6447  return g_device_type_str[type];
6448 }
6449 
6450 static void
6451 ni_parse_session_statistic_info(ni_session_context_t *p_ctx,
6452  ni_session_statistic_t *p_session_statistic,
6453  void *p_buffer)
6454 {
6455  memcpy((void *)p_session_statistic, p_buffer,
6456  sizeof(ni_session_statistic_t));
6457 
6458  //flip the bytes to host order
6459  p_session_statistic->ui32RdBufAvailSize =
6460  ni_htonl(p_session_statistic->ui32RdBufAvailSize);
6461  p_session_statistic->ui32WrBufAvailSize =
6462  ni_htonl(p_session_statistic->ui32WrBufAvailSize);
6463 
6464  p_session_statistic->ui32FramesInput =
6465  ni_htonl(p_session_statistic->ui32FramesInput);
6466  p_session_statistic->ui32FramesBuffered =
6467  ni_htonl(p_session_statistic->ui32FramesBuffered);
6468  p_session_statistic->ui32FramesCompleted =
6469  ni_htonl(p_session_statistic->ui32FramesCompleted);
6470  p_session_statistic->ui32FramesOutput =
6471  ni_htonl(p_session_statistic->ui32FramesOutput);
6472  p_session_statistic->ui32FramesDropped =
6473  ni_htonl(p_session_statistic->ui32FramesDropped);
6474  p_session_statistic->ui32InstErrors =
6475  ni_htonl(p_session_statistic->ui32InstErrors);
6476 
6477  p_session_statistic->ui16SessionId =
6478  ni_htons(p_session_statistic->ui16SessionId);
6479  p_session_statistic->ui16ErrorCount =
6480  ni_htons(p_session_statistic->ui16ErrorCount);
6481  p_session_statistic->ui32LastTransactionId =
6482  ni_htonl(p_session_statistic->ui32LastTransactionId);
6483  p_session_statistic->ui32LastTransactionCompletionStatus =
6484  ni_htonl(p_session_statistic->ui32LastTransactionCompletionStatus);
6485  p_session_statistic->ui32LastErrorTransactionId =
6486  ni_htonl(p_session_statistic->ui32LastErrorTransactionId);
6487  p_session_statistic->ui32LastErrorStatus =
6488  ni_htonl(p_session_statistic->ui32LastErrorStatus);
6489  p_session_statistic->ui32Session_timestamp_high =
6490  ni_htonl(p_session_statistic->ui32Session_timestamp_high);
6491  p_session_statistic->ui32Session_timestamp_low =
6492  ni_htonl(p_session_statistic->ui32Session_timestamp_low);
6493 
6494  // p_session_statistic->ui8AdditionalFramesDelay does not require endian conversion
6495 
6496  if (p_ctx->session_id != p_session_statistic->ui16SessionId)
6497  {
6498  uint64_t ct = ni_gettime_ns();
6499  uint64_t dt = ct - p_ctx->last_access_time;
6500  ni_log2(p_ctx, NI_LOG_ERROR,
6501  "%s(): %s device 0x%" PRIx64 " last command Failed due to wrong "
6502  "session ID. Expected 0x%x, got 0x%x w_r <%u %u> keep alive "
6503  "last access time %" PRIu64 ", current %" PRIu64 "\n", __func__,
6504  ni_get_device_type_str(p_ctx->device_type),
6505  (int64_t)p_ctx->device_handle, p_ctx->session_id,
6506  p_session_statistic->ui16SessionId,
6507  p_session_statistic->ui32WrBufAvailSize,
6508  p_session_statistic->ui32RdBufAvailSize, p_ctx->last_access_time,
6509  ct);
6510  if (dt > 1000000000)
6511  {
6512  ni_log2(p_ctx, NI_LOG_ERROR,
6513  "%s():long delay between last command dt = %" PRId64 " ns, "
6514  "process was possibly blocked.\n", __func__, dt);
6515  }
6516  p_session_statistic->ui32LastErrorStatus =
6518  //Mark session id to INVALID so that all commands afterward are blocked
6520  } else
6521  {
6522  //Acknowledge that total error count here
6523  }
6524 
6525  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): error count %u last rc 0x%x inst_err_no 0x%x\n",
6526  __func__, p_session_statistic->ui16ErrorCount,
6527  p_session_statistic->ui32LastTransactionCompletionStatus,
6528  p_session_statistic->ui32LastErrorStatus);
6529 }
6530 
6531 /*!*****************************************************************************
6532  * \brief Query a particular xcoder session to get session statistics
6533  *
6534  * \param ni_session_context_t p_ctx - xcoder Context
6535  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
6536  * \param ni_session_statistic_t*out - Struct preallocated from the caller
6537  * where the resulting data will be placed
6538  *
6539  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION,
6540  * NI_RETCODE_ERROR_MEM_ALOC or NI_RETCODE_ERROR_NVME_CMD_FAILED on
6541  * failure
6542  ******************************************************************************/
6545  ni_device_type_t device_type,
6546  ni_session_statistic_t *p_session_statistic)
6547 {
6548  void *p_buffer = NULL;
6550  uint32_t ui32LBA = 0;
6551  uint32_t dataLen =
6552  ((sizeof(ni_session_statistic_t) + (NI_MEM_PAGE_ALIGNMENT - 1)) /
6555 
6556  if (!p_ctx || !p_session_statistic)
6557  {
6558  ni_log2(p_ctx, NI_LOG_ERROR,
6559  "ERROR: %s() passed parameters are null!, return\n", __func__);
6560  retval = NI_RETCODE_INVALID_PARAM;
6561  LRETURN;
6562  }
6563 
6565  "65") < 0)
6566  {
6567  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() not supported on device with FW api version < 6.5\n", __func__);
6569  }
6570 
6571  if (!(NI_DEVICE_TYPE_DECODER == device_type ||
6572  NI_DEVICE_TYPE_ENCODER == device_type ||
6573  NI_DEVICE_TYPE_AI == device_type))
6574  {
6575  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown device type %d, return\n",
6576  __func__, device_type);
6577  retval = NI_RETCODE_INVALID_PARAM;
6578  LRETURN;
6579  }
6580 
6581  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
6582  {
6583  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
6584  __func__);
6586  LRETURN;
6587  }
6588 
6589  ui32LBA = QUERY_INSTANCE_CUR_STATUS_INFO_R(p_ctx->session_id, device_type);
6590 
6591  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), dataLen))
6592  {
6593  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
6594  NI_ERRNO, __func__);
6595  retval = NI_RETCODE_ERROR_MEM_ALOC;
6596  LRETURN;
6597  }
6598  memset(p_buffer, 0, dataLen);
6599 
6600  // Set session ID to be invalid. In case, the last command fails because the invalid session ID was submitted
6601  // with the command, the session id would remain invalid.
6602  // If the Last command is processed successfully in session manager, the session id would become valid.
6603  ((ni_session_statistic_t *)p_buffer)->ui16SessionId =
6604  (uint16_t)NI_INVALID_SESSION_ID;
6605 
6607  p_buffer, dataLen, ui32LBA) < 0)
6608  {
6609  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): NVME command Failed\n", __func__);
6610  p_session_statistic->ui32LastTransactionCompletionStatus =
6612  p_session_statistic->ui32LastErrorStatus =
6615  LRETURN;
6616  }
6617 
6618  ni_parse_session_statistic_info(p_ctx, p_session_statistic, p_buffer);
6619  if (p_ctx->session_id == NI_INVALID_SESSION_ID)
6620  {
6622  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, %s return.\n",
6623  __func__, ni_get_device_type_str(p_ctx->device_type));
6624  LRETURN;
6625  }
6626  p_ctx->session_statistic = *p_session_statistic;
6627 
6628 END:
6629  ni_aligned_free(p_buffer);
6630  ni_log2(p_ctx, NI_LOG_TRACE, "%s():exit\n", __func__);
6631 
6632  return retval;
6633 }
6634 /*!*****************************************************************************
6635  * \brief Query a particular xcoder instance to get buffer/data Info data
6636  *
6637  * \param ni_session_context_t p_ctx - xcoder Context
6638  * \param ni_instance_buf_info_rw_type_t rw_type
6639  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
6640  * \param ni_instance_buf_info_t *out - Struct preallocated from the caller
6641  * where the resulting data will be placed
6642  *
6643  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION,
6644  * NI_RETCODE_ERROR_MEM_ALOC or NI_RETCODE_ERROR_NVME_CMD_FAILED on
6645  * failure
6646  ******************************************************************************/
6649  ni_device_type_t device_type,
6650  ni_instance_buf_info_t *p_inst_buf_info)
6651 {
6652  void* p_buffer = NULL;
6654  uint32_t ui32LBA = 0;
6655  uint32_t dataLen =
6656  ((sizeof(ni_instance_buf_info_t) + (NI_MEM_PAGE_ALIGNMENT - 1)) /
6659 
6660  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
6661 
6662  if (!p_ctx || !p_inst_buf_info)
6663  {
6664  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
6665  __func__);
6666  retval = NI_RETCODE_INVALID_PARAM;
6667  LRETURN;
6668  }
6669 
6670  if (!(NI_DEVICE_TYPE_DECODER == device_type ||
6671  NI_DEVICE_TYPE_ENCODER == device_type ||
6672  NI_DEVICE_TYPE_SCALER == device_type ||
6673  NI_DEVICE_TYPE_AI == device_type))
6674  {
6675  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown device type %d, return\n",
6676  __func__, device_type);
6677  retval = NI_RETCODE_INVALID_PARAM;
6678  LRETURN;
6679  }
6680 
6681  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
6682  {
6683  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
6684  __func__);
6686  LRETURN;
6687  }
6688 
6689  if (INST_BUF_INFO_RW_READ == rw_type)
6690  {
6691  ui32LBA = QUERY_INSTANCE_RBUFF_SIZE_R(p_ctx->session_id, device_type);
6692  }
6693  else if (INST_BUF_INFO_RW_WRITE == rw_type)
6694  {
6695  ui32LBA = QUERY_INSTANCE_WBUFF_SIZE_R(p_ctx->session_id, device_type);
6696  }
6697  else if(INST_BUF_INFO_RW_WRITE_BY_EP == rw_type)
6698  {
6700  "65") >= 0)
6701  {
6702  ui32LBA = QUERY_INSTANCE_WBUFF_SIZE_R_BY_EP(p_ctx->session_id, device_type);
6703  }
6704  else
6705  {
6707  LRETURN;
6708  }
6709  }
6710  else if (INST_BUF_INFO_RW_UPLOAD == rw_type)
6711  {
6712  ui32LBA = QUERY_INSTANCE_UPLOAD_ID_R(p_ctx->session_id, device_type);
6713  } else if (INST_BUF_INFO_R_ACQUIRE == rw_type)
6714  {
6715  ui32LBA = QUERY_INSTANCE_ACQUIRE_BUF(p_ctx->session_id, device_type);
6716  } else if (INST_BUF_INFO_RW_READ_BUSY == rw_type)
6717  {
6718  ui32LBA =
6719  QUERY_INSTANCE_RBUFF_SIZE_BUSY_R(p_ctx->session_id, device_type);
6720  } else if (INST_BUF_INFO_RW_WRITE_BUSY == rw_type)
6721  {
6722  ui32LBA =
6723  QUERY_INSTANCE_WBUFF_SIZE_BUSY_R(p_ctx->session_id, device_type);
6724  } else if (INST_BUF_INFO_RW_READ_BY_AI == rw_type)
6725  {
6726  ui32LBA = QUERY_INSTANCE_AI_INFO_R(p_ctx->session_id, device_type);
6727  } else
6728  {
6729  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown query type %d, return\n",
6730  __func__, rw_type);
6731  retval = NI_RETCODE_INVALID_PARAM;
6732  LRETURN;
6733  }
6734 
6735  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), dataLen))
6736  {
6737  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
6738  NI_ERRNO, __func__);
6739  retval = NI_RETCODE_ERROR_MEM_ALOC;
6740  LRETURN;
6741  }
6742 
6743  memset(p_buffer, 0, dataLen);
6744 
6745  if (ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_buffer, dataLen, ui32LBA) < 0)
6746  {
6747  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
6749  LRETURN;
6750  }
6751 
6752  memcpy((void*)p_inst_buf_info, p_buffer, sizeof(ni_instance_buf_info_t));
6753 
6754  p_inst_buf_info->buf_avail_size = ni_htonl(p_inst_buf_info->buf_avail_size);
6755 
6756 END:
6757 
6758  ni_aligned_free(p_buffer);
6759  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
6760 
6761  return retval;
6762 }
6763 
6764 /*!*****************************************************************************
6765  * \brief Configure the read/write pipe for a session to control its behavior
6766  *
6767  * \param ni_session_context_t p_ctx - xcoder Context
6768  * \param ni_session_config_rw_t rw_type
6769  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
6770  * \param uint8_t enable
6771  * \param uint8_t hw_action
6772  * \param uint16_t frame_id
6773  *
6774  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION,
6775  * NI_RETCODE_ERROR_MEM_ALOC or NI_RETCODE_ERROR_NVME_CMD_FAILED on
6776  * failure
6777  ******************************************************************************/
6780  uint8_t enable, uint8_t hw_action,
6781  uint16_t frame_id)
6782 {
6784  uint32_t ui32LBA = 0;
6785  void * p_buffer = NULL;
6786  uint32_t buffer_size = 0;
6787  ni_session_config_rw_t * rw_config = NULL;
6788 
6789  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
6790 
6791  if (!p_ctx)
6792  {
6793  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
6794  __func__);
6795  retval = NI_RETCODE_INVALID_PARAM;
6796  LRETURN;
6797  }
6798 
6799  if (!((SESSION_READ_CONFIG == rw_type) || (SESSION_WRITE_CONFIG == rw_type)))
6800  {
6801  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown config type %d, return\n",
6802  __func__, rw_type);
6803  retval = NI_RETCODE_INVALID_PARAM;
6804  LRETURN;
6805  }
6806 
6808  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), buffer_size))
6809  {
6810  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
6811  NI_ERRNO, __func__);
6812  retval = NI_RETCODE_ERROR_MEM_ALOC;
6813  LRETURN;
6814  }
6815  memset(p_buffer, 0, buffer_size);
6816  rw_config = (ni_session_config_rw_t *)p_buffer;
6817  rw_config->ui8Enable = enable;
6818  rw_config->ui8HWAccess = hw_action;
6819  switch(rw_type)
6820  {
6821  case SESSION_READ_CONFIG:
6822  rw_config->uHWAccessField.ui16ReadFrameId = frame_id;
6823  break;
6824  case SESSION_WRITE_CONFIG:
6825  rw_config->uHWAccessField.ui16WriteFrameId = frame_id;
6826  break;
6827  default:
6828  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown config type %d, return\n",
6829  __func__, rw_type);
6830  retval = NI_RETCODE_INVALID_PARAM;
6831  LRETURN;
6832  }
6833 
6834  switch(rw_type)
6835  {
6836  case SESSION_READ_CONFIG:
6837  ui32LBA = CONFIG_SESSION_Read_W(p_ctx->session_id);
6838  break;
6839  case SESSION_WRITE_CONFIG:
6840  ui32LBA = CONFIG_SESSION_Write_W(p_ctx->session_id);
6841  break;
6842  default:
6843  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown config type %d, return\n",
6844  __func__, rw_type);
6845  retval = NI_RETCODE_INVALID_PARAM;
6846  LRETURN;
6847  }
6848  if (ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_buffer, buffer_size, ui32LBA) < 0)
6849  {
6850  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
6852  LRETURN;
6853  }
6854 
6855 END:
6856 
6857  ni_aligned_free(p_buffer);
6858  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
6859 
6860  return retval;
6861 }
6862 
6863 /*!******************************************************************************
6864  * \brief Send a p_config command for Start Of Stream
6865  *
6866  * \param ni_session_context_t p_ctx - xcoder Context
6867  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
6868  *
6869  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION. NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
6870  *******************************************************************************/
6872 {
6874  uint32_t ui32LBA = 0;
6875 
6876  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
6877 
6878  if (!p_ctx)
6879  {
6880  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
6881  __func__);
6882  retval = NI_RETCODE_INVALID_PARAM;
6883  LRETURN;
6884  }
6885 
6886  if (! (NI_DEVICE_TYPE_DECODER == device_type ||
6887  NI_DEVICE_TYPE_ENCODER == device_type))
6888  {
6889  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown device type %d, return\n",
6890  __func__, device_type);
6891  retval = NI_RETCODE_INVALID_PARAM;
6892  LRETURN;
6893  }
6894 
6895  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
6896  {
6897  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
6898  __func__);
6900  LRETURN;
6901  }
6902 
6903  ui32LBA = CONFIG_INSTANCE_SetSOS_W(p_ctx->session_id, device_type);
6904 
6905  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)
6906  {
6907  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
6909  }
6910 
6911 END:
6912 
6913  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
6914  return retval;
6915 }
6916 
6917 /*!******************************************************************************
6918  * \brief Send a p_config command for End Of Stream
6919  *
6920  * \param ni_session_context_t p_ctx - xcoder Context
6921  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
6922  *
6923  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION, NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
6924  *******************************************************************************/
6926 {
6928  uint32_t ui32LBA = 0;
6929 
6930  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
6931 
6932  if (!p_ctx)
6933  {
6934  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
6935  __func__);
6936  retval = NI_RETCODE_INVALID_PARAM;
6937  LRETURN;
6938  }
6939 
6940  if (! (NI_DEVICE_TYPE_DECODER == device_type ||
6941  NI_DEVICE_TYPE_ENCODER == device_type))
6942  {
6943  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown device type %d, return\n",
6944  __func__, device_type);
6945  retval = NI_RETCODE_INVALID_PARAM;
6946  LRETURN;
6947  }
6948 
6949  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
6950  {
6951  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
6952  __func__);
6954  LRETURN;
6955  }
6956 
6957  ui32LBA = CONFIG_INSTANCE_SetEOS_W(p_ctx->session_id, device_type);
6958 
6959  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)
6960  {
6961  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
6963  }
6964 
6965 END:
6966 
6967  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
6968 
6969  return retval;
6970 }
6971 
6972 /*!******************************************************************************
6973  * \brief Send a p_config command to flush the stream
6974  *
6975  * \param ni_session_context_t p_ctx - xcoder Context
6976  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
6977  *
6978  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION, NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
6979  *******************************************************************************/
6981 {
6983  uint32_t ui32LBA = 0;
6984 
6985  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
6986 
6987  if (!p_ctx)
6988  {
6989  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
6990  __func__);
6991  retval = NI_RETCODE_INVALID_PARAM;
6992  LRETURN;
6993  }
6994 
6995  if (NI_DEVICE_TYPE_DECODER != device_type)
6996  {
6997  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown device type %d, return\n",
6998  __func__, device_type);
6999  retval = NI_RETCODE_INVALID_PARAM;
7000  LRETURN;
7001  }
7002 
7003  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
7004  {
7005  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
7006  __func__);
7008  LRETURN;
7009  }
7010 
7011  ui32LBA = CONFIG_INSTANCE_Flush_W(p_ctx->session_id, device_type);
7012  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
7014  ui32LBA);
7015  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
7016  p_ctx->device_type, p_ctx->hw_id,
7017  &(p_ctx->session_id), OPT_1);
7018  CHECK_VPU_RECOVERY(retval);
7019 
7020 END:
7021 
7022  if (NI_RETCODE_SUCCESS != retval)
7023  {
7025  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed with %d\n", __func__, retval);
7026  }
7027 
7028  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
7029 
7030  return retval;
7031 }
7032 
7033 /*!******************************************************************************
7034  * \brief Send a p_config command to set the length for the incoming write packet
7035  *
7036  * \param ni_session_context_t p_ctx - xcoder Context
7037  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
7038  * \param
7039  *
7040  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION, NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
7041  *******************************************************************************/
7043 {
7045  uint32_t ui32LBA = 0;
7046  void * p_buffer = NULL;
7047  uint32_t buffer_size = 0;
7048 
7049  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
7050 
7051  if (!p_ctx)
7052  {
7053  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
7054  __func__);
7055  retval = NI_RETCODE_INVALID_PARAM;
7056  LRETURN;
7057  }
7058 
7059  if (!(NI_DEVICE_TYPE_DECODER == device_type ||
7060  NI_DEVICE_TYPE_ENCODER == device_type))
7061  {
7062  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown device type %d, return\n",
7063  __func__, device_type);
7064  retval = NI_RETCODE_INVALID_PARAM;
7065  LRETURN;
7066  }
7067 
7068  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
7069  {
7070  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
7071  __func__);
7073  LRETURN;
7074  }
7075 
7076  buffer_size = ((sizeof(len) + (NI_MEM_PAGE_ALIGNMENT - 1)) / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT;
7077  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), buffer_size))
7078  {
7079  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
7080  NI_ERRNO, __func__);
7081  retval = NI_RETCODE_ERROR_MEM_ALOC;
7082  LRETURN;
7083  }
7084  memset(p_buffer, 0, buffer_size);
7085  memcpy(p_buffer, &len, sizeof(len));
7086 
7087  ui32LBA = CONFIG_INSTANCE_SetPktSize_W(p_ctx->session_id, device_type);
7088 
7089  if (ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_buffer, buffer_size, ui32LBA) < 0)
7090  {
7091  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
7093  }
7094 
7095 END:
7096 
7097  ni_aligned_free(p_buffer);
7098  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
7099 
7100  return retval;
7101 }
7102 
7103 /*!******************************************************************************
7104  * \brief Send a p_config command to inform encoder sequence change
7105  *
7106  * \param ni_session_context_t p_ctx - xcoder Context
7107  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
7108  * \param ni_resolution_t p_resolution - sequence change resolution
7109  *
7110  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION, NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
7111  *******************************************************************************/
7113 {
7115  uint32_t ui32LBA = 0;
7116  void * p_buffer = NULL;
7117  uint32_t buffer_size = 0;
7118 
7119  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
7120 
7121  if (!p_ctx)
7122  {
7123  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
7124  __func__);
7125  retval = NI_RETCODE_INVALID_PARAM;
7126  LRETURN;
7127  }
7128 
7129  if (!(NI_DEVICE_TYPE_ENCODER == device_type))
7130  {
7131  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: Seq Change not supported for device type %d, return\n", device_type);
7132  retval = NI_RETCODE_INVALID_PARAM;
7133  LRETURN;
7134  }
7135 
7136  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
7137  {
7138  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
7139  __func__);
7141  LRETURN;
7142  }
7143 
7145  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), buffer_size))
7146  {
7147  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
7148  NI_ERRNO, __func__);
7149  retval = NI_RETCODE_ERROR_MEM_ALOC;
7150  LRETURN;
7151  }
7152  memset(p_buffer, 0, buffer_size);
7153  memcpy(p_buffer, p_resolution, sizeof(ni_resolution_t));
7154 
7155  ui32LBA = CONFIG_INSTANCE_SetSeqChange_W(p_ctx->session_id, device_type);
7156 
7157  if (ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_buffer, buffer_size, ui32LBA) < 0)
7158  {
7159  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
7161  }
7162 
7163 END:
7164 
7165  ni_aligned_free(p_buffer);
7166  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
7167 
7168  return retval;
7169 }
7170 
7171 /*!******************************************************************************
7172  * \brief Send a p_config command to configure encoding parameters.
7173  *
7174  * \param ni_session_context_t p_ctx - xcoder Context
7175  *
7176  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION, NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
7177  *******************************************************************************/
7179 {
7180  void* p_encoder_config = NULL;
7181  void* p_encoder_roi_qp_map = NULL;
7182  ni_encoder_config_t* p_cfg = NULL;
7183  uint32_t buffer_size = 0;
7185  uint32_t ui32LBA = 0;
7186  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
7187 
7188  if (!p_ctx)
7189  {
7190  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
7191  __func__);
7192  retval = NI_RETCODE_INVALID_PARAM;
7193  LRETURN;
7194  }
7195 
7196  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
7197  {
7198  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
7199  __func__);
7201  LRETURN;
7202  }
7203 
7204  if (ni_cmp_fw_api_ver((char*) &p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6rh") >= 0)
7205  {
7207  ni_xcoder_params_t *session_config = (ni_xcoder_params_t *)p_ctx->p_session_config;
7208  if (session_config->cfg_enc_params.customize_roi_qp_level > 64) {
7210  buffer_size = ((buffer_size + (NI_MEM_PAGE_ALIGNMENT - 1)) / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT;
7211  if (ni_posix_memalign(&p_encoder_roi_qp_map, sysconf(_SC_PAGESIZE), buffer_size))
7212  {
7213  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
7214  NI_ERRNO, __func__);
7215  retval = NI_RETCODE_ERROR_MEM_ALOC;
7216  LRETURN;
7217  }
7218  memset(p_encoder_roi_qp_map, 0, buffer_size);
7219  memcpy(p_encoder_roi_qp_map, session_config->customize_roi_qp_map,
7221  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
7222  p_encoder_roi_qp_map, buffer_size, ui32LBA);
7223  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
7224  p_ctx->device_type, p_ctx->hw_id,
7225  &(p_ctx->session_id), OPT_1);
7226  if (NI_RETCODE_SUCCESS != retval)
7227  {
7228  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);
7229  //Close the session since we can't configure it
7230  retval = ni_encoder_session_close(p_ctx, 0);
7231  if (NI_RETCODE_SUCCESS != retval)
7232  {
7233  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);
7234  }
7236  LRETURN;
7237  }
7238  session_config->cfg_enc_params.customize_roi_qp_level -= 64;
7239  }
7240  }
7241 
7242  buffer_size = sizeof(ni_encoder_config_t);
7243  buffer_size = ((buffer_size + (NI_MEM_PAGE_ALIGNMENT - 1)) / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT;
7244  if (ni_posix_memalign(&p_encoder_config, sysconf(_SC_PAGESIZE), buffer_size))
7245  {
7246  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
7247  NI_ERRNO, __func__);
7248  retval = NI_RETCODE_ERROR_MEM_ALOC;
7249  LRETURN;
7250  }
7251  memset(p_encoder_config, 0, buffer_size);
7252 
7253  ni_set_custom_template(p_ctx, p_encoder_config, p_ctx->p_session_config);
7254  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));
7255  if (NI_RETCODE_PARAM_WARN == retval)
7256  {
7257  ni_log2(p_ctx, NI_LOG_INFO, "WARNING: ni_validate_custom_template() . %s\n", p_ctx->param_err_msg);
7258  fflush(stdout);
7259  }
7260  else if (NI_RETCODE_SUCCESS != retval)
7261  {
7262  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: ni_validate_custom_template() failed. %s\n", p_ctx->param_err_msg);
7263  fflush(stdout);
7264  retval = NI_RETCODE_INVALID_PARAM;
7265  LRETURN;
7266  }
7267  //configure the session here
7269 
7270  //Flip the bytes!! any param 16bits in size need ni_htons, 32bit need ni_htonl
7271  p_cfg = (ni_encoder_config_t*)p_encoder_config;
7272  p_cfg->i32picWidth = ni_htonl(p_cfg->i32picWidth);
7273  p_cfg->i32picHeight = ni_htonl(p_cfg->i32picHeight);
7274  p_cfg->i32meBlkMode = ni_htonl(p_cfg->i32meBlkMode);
7275  p_cfg->i32frameRateInfo = ni_htonl(p_cfg->i32frameRateInfo);
7276  p_cfg->i32vbvBufferSize = ni_htonl(p_cfg->i32vbvBufferSize);
7277  p_cfg->i32userQpMax = ni_htonl(p_cfg->i32userQpMax);
7278  p_cfg->i32maxIntraSize = ni_htonl(p_cfg->i32maxIntraSize);
7279  p_cfg->i32userMaxDeltaQp = ni_htonl(p_cfg->i32userMaxDeltaQp);
7280  p_cfg->i32userMinDeltaQp = ni_htonl(p_cfg->i32userMinDeltaQp);
7281  p_cfg->i32userQpMin = ni_htonl(p_cfg->i32userQpMin);
7282  p_cfg->i32bitRate = ni_htonl(p_cfg->i32bitRate);
7283  p_cfg->i32bitRateBL = ni_htonl(p_cfg->i32bitRateBL);
7284  p_cfg->i32srcBitDepth = ni_htonl(p_cfg->i32srcBitDepth);
7285  p_cfg->hdrEnableVUI = ni_htonl(p_cfg->hdrEnableVUI);
7286  p_cfg->ui32VuiDataSizeBits = ni_htonl(p_cfg->ui32VuiDataSizeBits);
7287  p_cfg->ui32VuiDataSizeBytes = ni_htonl(p_cfg->ui32VuiDataSizeBytes);
7288  p_cfg->i32hwframes = ni_htonl(p_cfg->i32hwframes);
7289  p_cfg->ui16HDR10MaxLight = ni_htons(p_cfg->ui16HDR10MaxLight);
7290  p_cfg->ui16HDR10AveLight = ni_htons(p_cfg->ui16HDR10AveLight);
7291  p_cfg->ui16gdrDuration = ni_htons(p_cfg->ui16gdrDuration);
7292  p_cfg->ui32ltrRefInterval = ni_htonl(p_cfg->ui32ltrRefInterval);
7293  p_cfg->i32ltrRefQpOffset = ni_htonl(p_cfg->i32ltrRefQpOffset);
7294  p_cfg->ui32ltrFirstGap = ni_htonl(p_cfg->ui32ltrFirstGap);
7295  p_cfg->i32tolCtbRcInter = ni_htonl(p_cfg->i32tolCtbRcInter);
7296  p_cfg->i32tolCtbRcIntra = ni_htonl(p_cfg->i32tolCtbRcIntra);
7297  p_cfg->i16bitrateWindow = ni_htons(p_cfg->i16bitrateWindow);
7298  p_cfg->ui16hdr10_dx0 = ni_htons(p_cfg->ui16hdr10_dx0);
7299  p_cfg->ui16hdr10_dy0 = ni_htons(p_cfg->ui16hdr10_dy0);
7300  p_cfg->ui16hdr10_dx1 = ni_htons(p_cfg->ui16hdr10_dx1);
7301  p_cfg->ui16hdr10_dy1 = ni_htons(p_cfg->ui16hdr10_dy1);
7302  p_cfg->ui16hdr10_dx2 = ni_htons(p_cfg->ui16hdr10_dx2);
7303  p_cfg->ui16hdr10_dy2 = ni_htons(p_cfg->ui16hdr10_dy2);
7304  p_cfg->ui16hdr10_wx = ni_htons(p_cfg->ui16hdr10_wx);
7305  p_cfg->ui16hdr10_wy = ni_htons(p_cfg->ui16hdr10_wy);
7306  p_cfg->ui32hdr10_maxluma = ni_htonl(p_cfg->ui32hdr10_maxluma);
7307  p_cfg->ui32hdr10_minluma = ni_htonl(p_cfg->ui32hdr10_minluma);
7308  p_cfg->ui32lumaLinesize = ni_htons(p_cfg->ui32lumaLinesize);
7309  p_cfg->ui32chromaLinesize = ni_htons(p_cfg->ui32chromaLinesize);
7310 
7311  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
7312  p_encoder_config, buffer_size, ui32LBA);
7313  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
7314  p_ctx->device_type, p_ctx->hw_id,
7315  &(p_ctx->session_id), OPT_1);
7316  if (NI_RETCODE_SUCCESS != retval)
7317  {
7318  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);
7319  //Close the session since we can't configure it
7320  retval = ni_encoder_session_close(p_ctx, 0);
7321  if (NI_RETCODE_SUCCESS != retval)
7322  {
7323  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);
7324  }
7325 
7327  }
7328 
7329 END:
7330 
7331  ni_aligned_free(p_encoder_config);
7332  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
7333  return retval;
7334 }
7335 
7336 /*!******************************************************************************
7337  * \brief Send a p_config command to configure encoding p_frame parameters.
7338  *
7339  * \param ni_session_context_t p_ctx - xcoder Context
7340  * \param ni_encoder_frame_params_t * params - pointer to the encoder ni_encoder_frame_params_t struct
7341  *
7342  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION, NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
7343  *******************************************************************************/
7345 {
7347  uint32_t buffer_size = sizeof(ni_encoder_frame_params_t);
7349  uint32_t ui32LBA = 0;
7350 
7351  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
7352 
7353  if (!p_ctx || !p_params)
7354  {
7355  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
7356  __func__);
7357  return NI_RETCODE_INVALID_PARAM;
7358  }
7359 
7360  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
7361  {
7362  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
7363  __func__);
7365  }
7366 
7367  buffer_size = ((buffer_size + (NI_MEM_PAGE_ALIGNMENT - 1)) / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT;
7368  if (ni_posix_memalign((void **)&p_cfg, sysconf(_SC_PAGESIZE), buffer_size))
7369  {
7370  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
7371  NI_ERRNO, __func__);
7373  }
7374 
7375  //configure the session here
7377 
7378  //Flip the bytes!!
7379  p_cfg->force_picture_type = ni_htons(p_params->force_picture_type);
7380  p_cfg->data_format = ni_htons(p_params->data_format);
7381  p_cfg->picture_type = ni_htons(p_params->picture_type);
7382  p_cfg->video_width = ni_htons(p_params->video_width);
7383  p_cfg->video_height = ni_htons(p_params->video_height);
7384  p_cfg->timestamp = ni_htonl(p_params->timestamp);
7385 
7386  if (ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_cfg,
7387  buffer_size, ui32LBA) < 0)
7388  {
7389  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);
7390  //Close the session since we can't configure it
7391  retval = ni_encoder_session_close(p_ctx, 0);
7392  if (NI_RETCODE_SUCCESS != retval)
7393  {
7394  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);
7395  }
7396 
7398  }
7399 
7400  ni_aligned_free(p_cfg);
7401  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
7402 
7403  return retval;
7404 }
7405 
7406 /*!******************************************************************************
7407  * \brief Get info from received p_frame
7408  *
7409  * \param
7410  *
7411  * \return
7412  *******************************************************************************/
7413 int ni_create_frame(ni_frame_t* p_frame, uint32_t read_length, uint64_t* p_frame_offset, bool is_hw_frame)
7414 {
7415  uint32_t rx_size =
7416  read_length; //get the length since its the only thing in DW10 now
7417 
7418  if (!p_frame || !p_frame_offset)
7419  {
7420  ni_log(NI_LOG_ERROR, "ERROR: %s(): Null pointer parameters passed\n",
7421  __func__);
7422  return NI_RETCODE_INVALID_PARAM;
7423  }
7424 
7425  *p_frame_offset = 0;
7426 
7427  unsigned int metadata_size = NI_FW_META_DATA_SZ -
7429  unsigned int video_data_size = p_frame->data_len[0] + p_frame->data_len[1] +
7430  p_frame->data_len[2] + ((is_hw_frame) ? p_frame->data_len[3] : 0);
7431  ni_log(NI_LOG_DEBUG, "rx_size = %d metadataSize = %d\n", rx_size,
7432  metadata_size);
7433 
7434  p_frame->p_custom_sei_set = NULL;
7435 
7436  if (rx_size == metadata_size)
7437  {
7438  video_data_size = 0;
7439  }
7440 
7441  if (rx_size > video_data_size)
7442  {
7443  ni_metadata_dec_frame_t *p_meta =
7444  (ni_metadata_dec_frame_t *)((uint8_t *)p_frame->p_buffer +
7445  video_data_size);
7446 
7447  *p_frame_offset = p_meta->metadata_common.ui64_data.frame_offset;
7448  rx_size -= metadata_size;
7449  p_frame->crop_top = p_meta->metadata_common.crop_top;
7450  p_frame->crop_bottom = p_meta->metadata_common.crop_bottom;
7451  p_frame->crop_left = p_meta->metadata_common.crop_left;
7452  p_frame->crop_right = p_meta->metadata_common.crop_right;
7453  p_frame->ni_pict_type = p_meta->metadata_common.frame_type;
7454 
7455  p_frame->video_width = p_meta->metadata_common.frame_width;
7456  p_frame->video_height = p_meta->metadata_common.frame_height;
7457 
7458  ni_log(
7459  NI_LOG_DEBUG,
7460  "%s: [metadata] cropRight=%u, cropLeft=%u, "
7461  "cropBottom=%u, cropTop=%u, frame_offset=%" PRIu64 ", pic=%ux%u, "
7462  "pict_type=%d, crop=%ux%u, sei header: 0x%0x number %u size %u\n",
7463  __func__, p_frame->crop_right, p_frame->crop_left,
7464  p_frame->crop_bottom, p_frame->crop_top,
7466  p_meta->metadata_common.frame_width,
7467  p_meta->metadata_common.frame_height, p_frame->ni_pict_type,
7468  p_frame->crop_right - p_frame->crop_left,
7469  p_frame->crop_bottom - p_frame->crop_top, p_meta->sei_header,
7470  p_meta->sei_number, p_meta->sei_size);
7471 
7472  p_frame->sei_total_len = 0;
7473  p_frame->sei_cc_offset = 0;
7474  p_frame->sei_cc_len = 0;
7479  p_frame->sei_hdr_plus_offset = 0;
7480  p_frame->sei_hdr_plus_len = 0;
7481  p_frame->sei_user_data_unreg_offset = 0;
7482  p_frame->sei_user_data_unreg_len = 0;
7483 
7484  if (p_meta->sei_number)
7485  {
7486 #if 1 // QUADRA_SEI_FMT
7487  ni_log(NI_LOG_DEBUG, "ui32SeiHeader 0x%x ui16SeiNumber %d ui16SeiSize %d SEI 0x%02x%02x\n",
7488  p_meta->sei_header, p_meta->sei_number, p_meta->sei_size,
7489  *((uint8_t*)p_meta + metadata_size),
7490  *((uint8_t*)p_meta + metadata_size+1));
7491 
7492  { // retrieve sei from new format
7493  uint16_t ui16SeiProcessed = 0;
7494  ni_sei_header_t * pEntryHeader = &p_meta->first_sei_header;
7495  uint32_t ui32Offset = 0;
7496  uint32_t ui32Size;
7497 
7498  rx_size -= p_meta->sei_size;
7499 
7500  do
7501  {
7502  ui16SeiProcessed++;
7503 
7504  if (pEntryHeader->status)
7505  {
7506  ui32Size = pEntryHeader->size;
7507  }
7508  else
7509  {
7510  ui32Size = 0;
7511  }
7512 
7513  ni_log(NI_LOG_DEBUG, "SEI #%x st %d size %d ty %d sz/of %u/%u 0x%02x%02x\n",
7514  ui16SeiProcessed, pEntryHeader->status,
7515  pEntryHeader->size, pEntryHeader->type, ui32Size,
7516  ui32Offset,
7517  *((uint8_t *)p_meta + metadata_size + ui32Offset),
7518  *((uint8_t *)p_meta + metadata_size + ui32Offset + 1));
7519 
7520  // - if multiple entries with same SEI type/subtype, only the last entry is saved;
7521  // consider to use sei_offset[] array instead of explicit sei_*_offset
7522  // - user_data_unreg (UDU) and custom sei passthru via HW and SW respectively
7523  // thus custom SEI is not processed here as it is by SW.
7524 
7525  switch(pEntryHeader->type)
7526  {
7527  case 4: //HEVC_SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35
7528  if (ui32Size)
7529  {
7530  uint8_t *ptr = (uint8_t*)p_meta + metadata_size + ui32Offset;
7531  if(ptr[0] == NI_HDR10P_SEI_BYTE0 && ptr[1] == NI_HDR10P_SEI_BYTE1 &&
7532  ptr[2] == NI_HDR10P_SEI_BYTE2 && ptr[3] == NI_HDR10P_SEI_BYTE3 &&
7533  ptr[4] == NI_HDR10P_SEI_BYTE4 && ptr[5] == NI_HDR10P_SEI_BYTE5)
7534  {
7535  p_frame->sei_hdr_plus_len = ui32Size;
7536  p_frame->sei_hdr_plus_offset =
7537  video_data_size + metadata_size + ui32Offset;
7538 
7539  p_frame->sei_total_len += ui32Size;
7540 
7541  ni_log(NI_LOG_DEBUG, "%s: hdr10+ size=%u hdr10+ offset=%u\n",
7542  __func__, p_frame->sei_hdr_plus_len,
7543  p_frame->sei_hdr_plus_offset);
7544  }
7545  else if(ptr[0] == NI_CC_SEI_BYTE0 && ptr[1] == NI_CC_SEI_BYTE1 &&
7546  ptr[2] == NI_CC_SEI_BYTE2 && ptr[3] == NI_CC_SEI_BYTE3 &&
7547  ptr[4] == NI_CC_SEI_BYTE4 && ptr[5] == NI_CC_SEI_BYTE5 &&
7548  ptr[6] == NI_CC_SEI_BYTE6 && ptr[7] == NI_CC_SEI_BYTE7)
7549  {
7550  // Found CC data
7551  // number of 3 byte close captions is bottom 5 bits of
7552  // 9th byte of T35 payload
7553  // uint32_t ui32CCSize = (ptr[8] & 0x1F) * 3; // avoid overwriting ui32CCSize
7554 
7555  // return close caption data offset and length, and
7556  // skip past 10 header bytes to close caption data
7557  p_frame->sei_cc_len = (ptr[8] & 0x1F) * 3; // ui32CCSize;
7558  p_frame->sei_cc_offset = video_data_size + metadata_size
7559  + ui32Offset + 10;
7560 
7561  p_frame->sei_total_len += p_frame->sei_cc_len;
7562 
7564  "%s: close caption size %u ,"
7565  "offset %u = video size %u meta size %d off "
7566  " %u + 10\n",
7567  __func__, p_frame->sei_cc_len, p_frame->sei_cc_offset,
7568  video_data_size, metadata_size, ui32Offset);
7569  }
7570  else
7571  {
7573  "%s: unsupported T35; type %u size %u status %u "
7574  "offset %u\n",
7575  __func__, pEntryHeader->type, pEntryHeader->size,
7576  pEntryHeader->status, ui32Offset);
7577  }
7578  }
7579  else
7580  {
7582  "Error %s: T35 (missing payload); type %u size %u "
7583  "status %u offset %u\n",
7584  __func__, pEntryHeader->type, pEntryHeader->size,
7585  pEntryHeader->status, ui32Offset);
7586  }
7587  break;
7588 
7589  case 5: // HEVC_SEI_TYPE_USER_DATA_UNREGISTERED
7590  // set offset now so len=0 will signify an error if this SEI is dropped
7591  p_frame->sei_user_data_unreg_offset = video_data_size + metadata_size + ui32Offset;
7592  if (ui32Size)
7593  {
7594  p_frame->sei_user_data_unreg_len = ui32Size;
7595  p_frame->sei_total_len += ui32Size;
7596  ni_log(NI_LOG_DEBUG, "User Data Unreg size = %u\n", ui32Size);
7597  }
7598  else
7599  {
7600  p_frame->sei_user_data_unreg_len = 0; // in case there are multiple UDU SEI entries
7602  "Error %s: User Data Unreg dropped (missing payload); "
7603  "type %u size %u status %u offset %u\n",
7604  __func__, pEntryHeader->type, pEntryHeader->size,
7605  pEntryHeader->status, ui32Offset);
7606  }
7607  break;
7608 
7609  case 137: //HEVC_SEI_TYPE_MASTERING_DISPLAY_INFO
7610  if (ui32Size)
7611  {
7612  p_frame->sei_hdr_mastering_display_color_vol_len = ui32Size;
7614  video_data_size + metadata_size + ui32Offset;
7615 
7616  p_frame->sei_total_len += ui32Size;
7619  *)((uint8_t *)p_meta + metadata_size + ui32Offset);
7620 
7621  ni_log(NI_LOG_DEBUG, "Display Primaries x[0]=%u y[0]=%u\n",
7622  ni_ntohs(pColourVolume->display_primaries[0][0]),
7623  ni_ntohs(pColourVolume->display_primaries[0][1]));
7624  ni_log(NI_LOG_DEBUG, "Display Primaries x[1]=%u y[1]=%u\n",
7625  ni_ntohs(pColourVolume->display_primaries[1][0]),
7626  ni_ntohs(pColourVolume->display_primaries[1][1]));
7627  ni_log(NI_LOG_DEBUG, "Display Primaries x[2]=%u y[2]=%u\n",
7628  ni_ntohs(pColourVolume->display_primaries[2][0]),
7629  ni_ntohs(pColourVolume->display_primaries[2][1]));
7630 
7631  ni_log(NI_LOG_DEBUG, "White Point x=%u y=%u\n",
7632  ni_ntohs(pColourVolume->white_point_x),
7633  ni_ntohs(pColourVolume->white_point_y));
7634  ni_log(NI_LOG_DEBUG, "Display Mastering Lum, Max=%u Min=%u\n",
7635  ni_ntohl(pColourVolume->max_display_mastering_luminance),
7636  ni_ntohl(pColourVolume->min_display_mastering_luminance));
7637  }
7638  else
7639  {
7641  "Error %s: mastering display info dropped (missing "
7642  "payload); type %u size %u status %u offset %u\n",
7643  __func__, pEntryHeader->type, pEntryHeader->size,
7644  pEntryHeader->status, ui32Offset);
7645  }
7646  break;
7647 
7648  case 144: //HEVC_SEI_TYPE_CONTENT_LIGHT_LEVEL_INFO
7649  if (ui32Size)
7650  {
7651  p_frame->sei_hdr_content_light_level_info_len = ui32Size;
7653  video_data_size + metadata_size + ui32Offset;
7654 
7655  p_frame->sei_total_len += ui32Offset;
7656 
7659  (uint8_t*)p_meta + metadata_size + ui32Offset);
7660 
7661  ni_log(NI_LOG_DEBUG, "Max Content Light level=%u Max Pic Avg Light Level=%u\n",
7662  ni_ntohs(pLightLevel->max_content_light_level),
7663  ni_ntohs(pLightLevel->max_pic_average_light_level));
7664  }
7665  else
7666  {
7668  "Error %s: content light level info dropped (missing "
7669  "payload); type %u size %u status %u offset %u\n",
7670  __func__, pEntryHeader->type, pEntryHeader->size,
7671  pEntryHeader->status, ui32Offset);
7672  }
7673  break;
7674 
7675  case 200: // Custom SEI not included in HEVC_SEI_Type
7676  if (ui32Size)
7677  {
7678  p_frame->vui_len = ui32Size;
7679  p_frame->vui_offset =
7680  video_data_size + metadata_size + ui32Offset;
7681  p_frame->sei_total_len += ui32Size;
7682  }
7683  break;
7684  case 206: // Customer reset ppu resolution, need dropped
7685  ni_log(NI_LOG_DEBUG, "Custom SEI PPU_RECONFIG dropped", __func__);
7686  break;
7687  default:
7689  "Warning %s: SEI message dropped (unsupported - check "
7690  "decoder SEI bitmap settings);"
7691  " type %u size %u status %u offset %u payload bytes %u\n",
7692  __func__, pEntryHeader->type, pEntryHeader->size,
7693  pEntryHeader->status, ui32Offset, ui32Size);
7694  break;
7695  }
7696  ui32Offset += ui32Size;
7697  pEntryHeader = (ni_sei_header_t *)((uint8_t*)p_meta + metadata_size + ui32Offset);
7698  ui32Offset += sizeof(ni_sei_header_t);
7699  } while (ui32Offset <= p_meta->sei_size && ui16SeiProcessed < p_meta->sei_number);
7700 
7701  if (p_meta->sei_number != ui16SeiProcessed)
7702  {
7703  ni_log(
7704  NI_LOG_ERROR,
7705  "Error %s: number of SEI messages reported %u != processed %u\n",
7706  __func__, p_meta->sei_number, ui16SeiProcessed);
7707  }
7708  }
7709 
7710 #else // QUADRA_SEI_FMT
7711 
7712  // ni_assert(p_meta->sei_header);
7713  // else // backward compatibility
7714  {
7715  ni_sei_user_data_entry_t *pEntry;
7716  uint32_t ui32CCOffset = 0, ui32CCSize = 0;
7717 
7718  rx_size -= p_meta->sei_size;
7719 
7720  pEntry = (ni_sei_user_data_entry_t *)((uint8_t*)p_meta + metadata_size);
7721 
7722  if (find_t35_sei(p_meta->sei_header, NI_T35_SEI_HDR10_PLUS, pEntry,
7723  &ui32CCOffset, &ui32CCSize))
7724  {
7725  p_frame->sei_hdr_plus_len = ui32CCSize;
7726  p_frame->sei_hdr_plus_offset =
7727  video_data_size + metadata_size + ui32CCOffset;
7728 
7729  p_frame->sei_total_len += ui32CCSize;
7730 
7731  ni_log(NI_LOG_DEBUG, "%s: hdr10+ size=%u hdr10+ offset=%u\n", __func__,
7732  p_frame->sei_hdr_plus_len, p_frame->sei_hdr_plus_offset);
7733  }
7734  else
7735  {
7736  ni_log(NI_LOG_DEBUG, "%s: hdr+ NOT found in meta data!\n", __func__);
7737  }
7738 
7739  if (find_t35_sei(p_meta->sei_header, NI_T35_SEI_CLOSED_CAPTION, pEntry,
7740  &ui32CCOffset, &ui32CCSize))
7741  {
7742  uint8_t *ptr;
7743  // Found CC data at pEntry + ui32CCOffset
7744  ptr = (uint8_t*)pEntry + ui32CCOffset;
7745  // number of 3 byte close captions is bottom 5 bits of
7746  // 9th byte of T35 payload
7747  ui32CCSize = (ptr[8] & 0x1F) * 3;
7748 
7749  // return close caption data offset and length, and
7750  // skip past 10 header bytes to close caption data
7751  p_frame->sei_cc_len = ui32CCSize;
7752  p_frame->sei_cc_offset = video_data_size + metadata_size
7753  + ui32CCOffset + 10;
7754 
7755  p_frame->sei_total_len += p_frame->sei_cc_len;
7756 
7758  "%s: close caption size %u ,"
7759  "offset %u = video size %u meta size %u off "
7760  " %u + 10\n",
7761  __func__, p_frame->sei_cc_len, p_frame->sei_cc_offset,
7762  video_data_size, metadata_size, ui32CCOffset);
7763  }
7764  else
7765  {
7766  ni_log(NI_LOG_DEBUG, "%s: close caption NOT found in meta data!\n",
7767  __func__);
7768  }
7769 
7770  if (find_sei(p_meta->sei_header, pEntry,
7772  &ui32CCOffset, &ui32CCSize))
7773  {
7774  p_frame->sei_hdr_mastering_display_color_vol_len = ui32CCSize;
7776  video_data_size + metadata_size + ui32CCOffset;
7777 
7778  p_frame->sei_total_len += ui32CCSize;
7779 
7780  ni_dec_mastering_display_colour_volume_t* pColourVolume =
7781  (ni_dec_mastering_display_colour_volume_t*)((uint8_t*)pEntry + ui32CCOffset);
7782 
7783  ni_log(NI_LOG_DEBUG, "Display Primaries x[0]=%u y[0]=%u\n",
7784  pColourVolume->display_primaries_x[0],
7785  pColourVolume->display_primaries_y[0]);
7786  ni_log(NI_LOG_DEBUG, "Display Primaries x[1]=%u y[1]=%u\n",
7787  pColourVolume->display_primaries_x[1],
7788  pColourVolume->display_primaries_y[1]);
7789  ni_log(NI_LOG_DEBUG, "Display Primaries x[2]=%u y[2]=%u\n",
7790  pColourVolume->display_primaries_x[2],
7791  pColourVolume->display_primaries_y[2]);
7792 
7793  ni_log(NI_LOG_DEBUG, "White Point x=%u y=%u\n",
7794  pColourVolume->white_point_x,
7795  pColourVolume->white_point_y);
7796  ni_log(NI_LOG_DEBUG, "Display Mastering Lum, Max=%u Min=%u\n",
7797  pColourVolume->max_display_mastering_luminance, pColourVolume->min_display_mastering_luminance);
7798  }
7799  if (find_sei(p_meta->sei_header, pEntry,
7801  &ui32CCOffset, &ui32CCSize))
7802  {
7803  p_frame->sei_hdr_content_light_level_info_len = ui32CCSize;
7805  video_data_size + metadata_size + ui32CCOffset;
7806 
7807  p_frame->sei_total_len += ui32CCSize;
7808 
7809  ni_content_light_level_info_t* pLightLevel =
7810  (ni_content_light_level_info_t*)((uint8_t*)pEntry + ui32CCOffset);
7811  ni_log(NI_LOG_DEBUG, "Max Content Light level=%u Max Pic Avg Light Level=%u\n",
7812  pLightLevel->max_content_light_level, pLightLevel->max_pic_average_light_level);
7813  }
7814 
7815  if (find_sei(p_meta->sei_header, pEntry,
7817  &ui32CCOffset, &ui32CCSize) ||
7818  find_sei(p_meta->sei_header, pEntry,
7820  &ui32CCOffset, &ui32CCSize))
7821  {
7822  p_frame->sei_user_data_unreg_len = ui32CCSize;
7823  p_frame->sei_user_data_unreg_offset =
7824  video_data_size + metadata_size + ui32CCOffset;
7825 
7826  p_frame->sei_total_len += ui32CCSize;
7827 
7828  ni_log(NI_LOG_DEBUG, "User Data Unreg size = %u\n", ui32CCSize);
7829  }
7830  }
7831 #endif // QUADRA_SEI_FMT
7832  if (0 == p_frame->sei_total_len)
7833  {
7834  ni_log(NI_LOG_DEBUG, "Warning retrieved 0 supported SEI !\n");
7835  }
7836  }
7837  }
7838 
7839  p_frame->dts = NI_NOPTS_VALUE;
7840  p_frame->pts = NI_NOPTS_VALUE;
7841  //p_frame->end_of_stream = isEndOfStream;
7842  p_frame->start_of_stream = 0;
7843 
7844  if (rx_size == 0)
7845  {
7846  p_frame->data_len[0] = 0;
7847  p_frame->data_len[1] = 0;
7848  p_frame->data_len[2] = 0;
7849  p_frame->data_len[3] = 0;
7850  }
7851 
7852  ni_log(NI_LOG_DEBUG, "received [0x%08x] data size: %d, end of stream=%u\n",
7853  read_length, rx_size, p_frame->end_of_stream);
7854 
7855  return rx_size;
7856 }
7857 
7858 /*!******************************************************************************
7859  * \brief Get info from received xcoder capability
7860  *
7861  * \param
7862  *
7863  * \return
7864  *******************************************************************************/
7866  ni_device_handle_t device_handle, bool device_in_ctxt)
7867 {
7868  int i, total_types = 0, total_modules = 0;
7869  ni_nvme_identity_t *p_id_data = (ni_nvme_identity_t *)p_data;
7870 
7871  COMPILE_ASSERT(sizeof(p_cap->xcoder_cnt) <= sizeof(p_id_data->xcoder_cnt) &&
7873  sizeof(p_id_data->xcoder_cnt[0]) ==
7874  sizeof(p_cap->xcoder_cnt));
7875 
7876  if (!p_cap || !p_data)
7877  {
7878  ni_log(NI_LOG_ERROR, "ERROR: %s(): Null pointer parameters passed\n",
7879  __func__);
7880  LRETURN;
7881  }
7882 
7883  if ((p_id_data->ui16Vid != NETINT_PCI_VENDOR_ID) ||
7884  (p_id_data->ui16Ssvid != NETINT_PCI_VENDOR_ID))
7885  {
7886  if (device_in_ctxt)
7887  {
7889  "ERROR: Previously in context device got an invalid vendor ID 0x%X SSVID 0x%X. Netint "
7890  "ID 0x%X. Retrying\n",
7891  p_id_data->ui16Vid, p_id_data->ui16Ssvid, NETINT_PCI_VENDOR_ID);
7892  //print some other fields as a test to see if they are invalid too
7893  ni_log(NI_LOG_ERROR, "Model Number: %.*s\n",
7894  (int)sizeof(p_id_data->ai8Sn), p_id_data->ai8Sn);
7895  ni_log(NI_LOG_ERROR, "Serial Number: %.*s\n",
7896  (int)sizeof(p_id_data->ai8Mn), p_id_data->ai8Mn);
7897  ni_log(NI_LOG_ERROR, "Firmware Revision: %.*s\n",
7898  (int)sizeof(p_id_data->ai8Fr), p_id_data->ai8Fr);
7899  ni_log(NI_LOG_ERROR, "xcoder_num_elements: %d\n",
7900  p_id_data->xcoder_num_elements);
7901 
7902  ni_event_handle_t event_handle = NI_INVALID_EVENT_HANDLE;
7903  uint32_t ui32LBA = IDENTIFY_DEVICE_R;
7904  if (ni_nvme_send_read_cmd(device_handle, event_handle, p_data,
7905  NI_NVME_IDENTITY_CMD_DATA_SZ, ui32LBA) < 0)
7906  {
7907  LRETURN;
7908  }
7909  if ((p_id_data->ui16Vid != NETINT_PCI_VENDOR_ID) ||
7910  (p_id_data->ui16Ssvid != NETINT_PCI_VENDOR_ID))
7911  {
7913  "ERROR: %s(): Retry got an invalid vendor ID too 0x%X SSVID "
7914  "0x%X!\n",
7915  __func__, p_id_data->ui16Vid, p_id_data->ui16Ssvid);
7916  //print some other fields as a test to see if they are invalid too
7917  ni_log(NI_LOG_ERROR, "Model Number: %.*s\n",
7918  (int)sizeof(p_id_data->ai8Sn), p_id_data->ai8Sn);
7919  ni_log(NI_LOG_ERROR, "Serial Number: %.*s\n",
7920  (int)sizeof(p_id_data->ai8Mn), p_id_data->ai8Mn);
7921  ni_log(NI_LOG_ERROR, "Firmware Revision: %.*s\n",
7922  (int)sizeof(p_id_data->ai8Fr), p_id_data->ai8Fr);
7923  ni_log(NI_LOG_ERROR, "xcoder_num_elements: %d\n",
7924  p_id_data->xcoder_num_elements);
7925  LRETURN;
7926  }
7927  else
7928  {
7930  "Retry got valid a vendor ID 0x%X SSVID 0x%X. Netint ID 0x%X\n",
7931  p_id_data->ui16Vid, p_id_data->ui16Ssvid, NETINT_PCI_VENDOR_ID);
7932  }
7933  }
7934  else
7935  {
7936  LRETURN;
7937  }
7938  }
7939 
7940  memcpy(p_cap->serial_number, p_id_data->ai8Sn, sizeof(p_cap->serial_number));
7941  memcpy(p_cap->model_number, p_id_data->ai8Mn, sizeof(p_cap->model_number));
7942 
7943  memset(p_cap->fw_rev, 0, sizeof(p_cap->fw_rev));
7944  memcpy(p_cap->fw_rev, p_id_data->ai8Fr, sizeof(p_cap->fw_rev));
7945  ni_log(NI_LOG_DEBUG, "F/W rev: %.*s\n", (int)sizeof(p_cap->fw_rev),
7946  p_cap->fw_rev);
7947 
7948  if (p_id_data->xcoder_num_elements)
7949  {
7950  ni_log(NI_LOG_DEBUG, "xcoder_num_elements: %d xcoder_num_devices: %d\n",
7951  p_id_data->xcoder_num_elements,
7952  p_id_data->xcoder_num_devices);
7953 
7954  for (i = 0; i < NI_DEVICE_TYPE_XCODER_MAX; i++)
7955  {
7956  if (p_id_data->xcoder_cnt[i])
7957  {
7958  total_types++;
7959  total_modules += p_id_data->xcoder_cnt[i];
7960  ni_log(NI_LOG_DEBUG, "type #%d: xcoder_cnt[%d] = %d\n", total_types, i,
7961  p_id_data->xcoder_cnt[i]);
7962  }
7963  }
7964 
7965  if (p_id_data->xcoder_num_elements != total_types ||
7966  p_id_data->xcoder_num_devices != total_modules)
7967  {
7969  "Error: mismatch; xcoder_num_elements: %d (calculated: %d) "
7970  "xcoder_num_devices: %d (calculated: %d)\n",
7971  p_id_data->xcoder_num_elements, total_types,
7972  p_id_data->xcoder_num_devices, total_modules);
7973  LRETURN;
7974  }
7975 
7977  p_cap->hw_elements_cnt = p_id_data->xcoder_num_elements;
7978  p_cap->xcoder_devices_cnt = p_id_data->xcoder_num_devices;
7979  memcpy(p_cap->xcoder_cnt, p_id_data->xcoder_cnt,
7980  NI_DEVICE_TYPE_XCODER_MAX * sizeof(p_id_data->xcoder_cnt[0]));
7981 
7983  {
7984  if (!p_id_data->xcoder_cnt[i])
7985  continue;
7986 
7987  p_cap->xcoder_devices[i].hw_id = p_id_data->xcoder_devices[i].hw_id;
7991  p_cap->xcoder_devices[i].codec_format =
7992  p_id_data->xcoder_devices[i].hw_codec_format;
7993  p_cap->xcoder_devices[i].codec_type =
7994  p_id_data->xcoder_devices[i].hw_codec_type;
7997  if (i == NI_DEVICE_TYPE_ENCODER)
7998  {
8001  }
8002  else
8003  {
8006  }
8007 
8008  p_cap->xcoder_devices[i].video_profile =
8009  p_id_data->xcoder_devices[i].hw_video_profile;
8010  p_cap->xcoder_devices[i].video_level =
8011  p_id_data->xcoder_devices[i].hw_video_level;
8012  }
8013 
8014  goto CAP_POPULATED;
8015  }
8016 
8017  p_cap->device_is_xcoder = p_id_data->device_is_xcoder;
8018  ni_log(NI_LOG_DEBUG, "device_is_xcoder: value in id cmd: %u\n",
8019  p_cap->device_is_xcoder);
8020  if (0 == p_cap->device_is_xcoder)
8021  {
8022  ni_log(NI_LOG_ERROR, "Not an xcoder device !\n");
8023 
8024  if (device_in_ctxt)
8025  {
8027  "ERROR: Previously in context device is not a xcoder device "
8028  "now!\n");
8029  }
8030  LRETURN;
8031  }
8032 
8033  p_cap->hw_elements_cnt = p_id_data->xcoder_num_hw;
8034 
8035  total_modules = p_cap->xcoder_cnt[NI_DEVICE_TYPE_DECODER] =
8036  p_id_data->xcoder_num_h264_decoder_hw +
8037  p_id_data->xcoder_num_h265_decoder_hw;
8038 
8040  p_id_data->xcoder_num_h264_encoder_hw +
8041  p_id_data->xcoder_num_h265_encoder_hw;
8042 
8043  total_modules += p_cap->xcoder_cnt[NI_DEVICE_TYPE_ENCODER];
8044 
8045  p_cap->xcoder_devices_cnt = total_modules;
8046 
8047  if (total_modules >= 1)
8048  {
8049  p_cap->xcoder_devices[0].hw_id = p_id_data->hw0_id;
8053  p_cap->xcoder_devices[0].codec_format = p_id_data->hw0_codec_format;
8054  p_cap->xcoder_devices[0].codec_type = p_id_data->hw0_codec_type;
8059  p_cap->xcoder_devices[0].video_profile = p_id_data->hw0_video_profile;
8060  p_cap->xcoder_devices[0].video_level = p_id_data->hw0_video_level;
8061  }
8062  if (total_modules >= 2)
8063  {
8064  p_cap->xcoder_devices[1].hw_id = p_id_data->hw1_id;
8068  p_cap->xcoder_devices[1].codec_format = p_id_data->hw1_codec_format;
8069  p_cap->xcoder_devices[1].codec_type = p_id_data->hw1_codec_type;
8074  p_cap->xcoder_devices[1].video_profile = p_id_data->hw1_video_profile;
8075  p_cap->xcoder_devices[1].video_level = p_id_data->hw1_video_level;
8076  }
8077  if (total_modules >= 3)
8078  {
8079  p_cap->xcoder_devices[2].hw_id = p_id_data->hw2_id;
8083  p_cap->xcoder_devices[2].codec_format = p_id_data->hw2_codec_format;
8084  p_cap->xcoder_devices[2].codec_type = p_id_data->hw2_codec_type;
8089  p_cap->xcoder_devices[2].video_profile = p_id_data->hw2_video_profile;
8090  p_cap->xcoder_devices[2].video_level = p_id_data->hw2_video_level;
8091  }
8092  if (total_modules >= 4)
8093  {
8094  p_cap->xcoder_devices[3].hw_id = p_id_data->hw3_id;
8098  p_cap->xcoder_devices[3].codec_format = p_id_data->hw3_codec_format;
8099  p_cap->xcoder_devices[3].codec_type = p_id_data->hw3_codec_type;
8104  p_cap->xcoder_devices[3].video_profile = p_id_data->hw3_video_profile;
8105  p_cap->xcoder_devices[3].video_level = p_id_data->hw3_video_level;
8106  }
8107 
8108 CAP_POPULATED:
8109 
8110  for (i = 0; i < NI_MAX_DEVICES_PER_HW_INSTANCE; i++)
8111  {
8112  ni_log(NI_LOG_DEBUG, "HW%d hw_id: %d\n", i, p_cap->xcoder_devices[i].hw_id);
8113  ni_log(NI_LOG_DEBUG, "HW%d max_number_of_contexts: %d\n", i,
8115  ni_log(NI_LOG_DEBUG, "HW%d max_4k_fps: %d\n", i,
8116  p_cap->xcoder_devices[i].max_4k_fps);
8117  ni_log(NI_LOG_DEBUG, "HW%d codec_format: %d\n", i,
8118  p_cap->xcoder_devices[i].codec_format);
8119  ni_log(NI_LOG_DEBUG, "HW%d codec_type: %d\n", i,
8120  p_cap->xcoder_devices[i].codec_type);
8121  ni_log(NI_LOG_DEBUG, "HW%d max_video_width: %d\n", i,
8122  p_cap->xcoder_devices[i].max_video_width);
8123  ni_log(NI_LOG_DEBUG, "HW%d max_video_height: %d\n", i,
8124  p_cap->xcoder_devices[i].max_video_height);
8125  ni_log(NI_LOG_DEBUG, "HW%d min_video_width: %d\n", i,
8126  p_cap->xcoder_devices[i].min_video_width);
8127  ni_log(NI_LOG_DEBUG, "HW%d min_video_height: %d\n", i,
8128  p_cap->xcoder_devices[i].min_video_height);
8129  ni_log(NI_LOG_DEBUG, "HW%d video_profile: %d\n", i,
8130  p_cap->xcoder_devices[i].video_profile);
8131  ni_log(NI_LOG_DEBUG, "HW%d video_level: %d\n", i,
8132  p_cap->xcoder_devices[i].video_level);
8133  }
8134 
8135  memset(p_cap->fw_branch_name, 0, sizeof(p_cap->fw_branch_name));
8136  memcpy(p_cap->fw_branch_name, p_id_data->fw_branch_name,
8137  sizeof(p_cap->fw_branch_name) - 1);
8138  ni_log(NI_LOG_DEBUG, "F/W branch name: %s\n", p_cap->fw_branch_name);
8139  memset(p_cap->fw_commit_time, 0, sizeof(p_cap->fw_commit_time));
8140  memcpy(p_cap->fw_commit_time, p_id_data->fw_commit_time,
8141  sizeof(p_cap->fw_commit_time) - 1);
8142  ni_log(NI_LOG_DEBUG, "F/W commit time: %s\n", p_cap->fw_commit_time);
8143  memset(p_cap->fw_commit_hash, 0, sizeof(p_cap->fw_commit_hash));
8144  memcpy(p_cap->fw_commit_hash, p_id_data->fw_commit_hash,
8145  sizeof(p_cap->fw_commit_hash) - 1);
8146  ni_log(NI_LOG_DEBUG, "F/W commit hash: %s\n", p_cap->fw_commit_hash);
8147  memset(p_cap->fw_build_time, 0, sizeof(p_cap->fw_build_time));
8148  memcpy(p_cap->fw_build_time, p_id_data->fw_build_time,
8149  sizeof(p_cap->fw_build_time) - 1);
8150  ni_log(NI_LOG_DEBUG, "F/W build time: %s\n", p_cap->fw_build_time);
8151  memset(p_cap->fw_build_id, 0, sizeof(p_cap->fw_build_id));
8152  memcpy(p_cap->fw_build_id, p_id_data->fw_build_id,
8153  sizeof(p_cap->fw_build_id) - 1);
8154  ni_log(NI_LOG_DEBUG, "F/W build id: %s\n", p_cap->fw_build_id);
8155 
8156 END:
8157  return;
8158 }
8159 
8160 static uint32_t presetGopSize[] = {
8161  1,
8162  1,
8163  1,
8164  1,
8165  2,
8166  4,
8167  4,
8168  4,
8169  8 };
8170 
8171 /*!******************************************************************************
8172  * \brief insert the 32 bits of integer value at bit position pos
8173  *
8174  * \param int pos, int value
8175  *
8176  * \return void
8177  ******************************************************************************/
8178 void ni_fix_VUI(uint8_t *vui, int pos, int value)
8179 {
8180  int pos_byte = (pos/8);
8181  int pos_in_byte = pos%8;
8182  int remaining_bytes_in_current_byte = 8 - pos_in_byte;
8183 
8184  if (pos_in_byte == 0) // at beginning of the byte
8185  {
8186  vui[pos_byte] = (uint8_t)(value >> 24);
8187  vui[pos_byte+1] = (uint8_t)(value >> 16);
8188  vui[pos_byte+2] = (uint8_t)(value >> 8);
8189  vui[pos_byte+3] = (uint8_t)(value);
8190  }
8191  else
8192  {
8193  vui[pos_byte] = vui[pos_byte] + (uint8_t)(value >> (32-remaining_bytes_in_current_byte));
8194  vui[pos_byte+1] = (uint8_t)(value >> (32-remaining_bytes_in_current_byte-8));
8195  vui[pos_byte+2] = (uint8_t)(value >> (32-remaining_bytes_in_current_byte-16));
8196  vui[pos_byte+3] = (uint8_t)(value >> (32-remaining_bytes_in_current_byte-24));
8197  vui[pos_byte+4] = vui[pos_byte+4] + ((uint8_t)(value << remaining_bytes_in_current_byte));
8198  }
8199 
8200 }
8201 
8202 /*!******************************************************************************
8203 * \brief Setup all xcoder configurations with custom parameters (Rev. B)
8204 *
8205 * \param
8206 *
8207 * \return
8208 ******************************************************************************/
8210  ni_decoder_config_t *p_cfg,
8211  ni_xcoder_params_t *p_src,
8212  uint32_t max_pkt_size)
8213 {
8214  int i,j;
8215  ni_decoder_input_params_t* p_dec = NULL;
8216  if ((!p_ctx) || (!p_cfg) || (!p_src))
8217  {
8218  ni_log(NI_LOG_ERROR, "ERROR: %s() Null pointer parameters passed\n",
8219  __func__);
8220  return;
8221  }
8222  p_dec = &p_src->dec_input_params;
8223  int w = p_src->source_width;
8224  int h = p_src->source_height;
8225  bool shift_params = false;
8226 
8227  p_cfg->ui8HWFrame = p_dec->hwframes;
8228  p_cfg->ui8MCMode = p_dec->mcmode;
8231  p_cfg->ui8DisablePictureReordering = p_dec->decoder_low_delay > 0;
8232  p_ctx->force_low_delay = p_dec->force_low_delay;
8233  p_cfg->ui8EnablelowDelayCheck = p_dec->enable_low_delay_check == 1;
8234  p_cfg->ui32SourceWidth = w;
8235  p_cfg->ui32SourceHeight = h;
8236 
8237  if (max_pkt_size)
8238  {
8239  p_cfg->ui32MaxPktSize = max_pkt_size;
8240  } else {
8241  // p_cfg->ui32MaxPktSize = width x height x 3/2 x min compression ratio(QP=0);
8242  // set min compression ratio = 1/2, so MaxPktSize = w * h * 3/4
8243  p_cfg->ui32MaxPktSize = w * h * 3 / 4;
8244  }
8245  // packet buffer aligned to NI_MAX_PACKET_SZ(128k)
8246  p_cfg->ui32MaxPktSize =
8247  (((p_cfg->ui32MaxPktSize) / NI_MAX_PACKET_SZ) + 1) * NI_MAX_PACKET_SZ;
8248 
8249  p_cfg->fps_number =
8250  ((ni_xcoder_params_t *)p_ctx->p_session_config)->fps_number;
8251  p_cfg->fps_denominator =
8252  ((ni_xcoder_params_t *)p_ctx->p_session_config)->fps_denominator;
8253  ni_log2(p_ctx, NI_LOG_INFO, "%s height %d width %d fps_number %d fps_denominator %d\n",
8254  __func__, h, w, p_cfg->fps_number, p_cfg->fps_denominator);
8255 
8256  p_cfg->asOutputConfig[0].ui8Enabled = 1; // always enabled
8257  p_cfg->asOutputConfig[1].ui8Enabled = p_dec->enable_out1;
8258  p_cfg->asOutputConfig[2].ui8Enabled = p_dec->enable_out2;
8259  if (p_cfg->asOutputConfig[2].ui8Enabled && p_cfg->asOutputConfig[1].ui8Enabled == 0)
8260  {
8261  p_cfg->asOutputConfig[1].ui8Enabled = 1;
8262  p_cfg->asOutputConfig[2].ui8Enabled = 0;
8263  shift_params = true;
8264  ni_log2(p_ctx, NI_LOG_DEBUG, "Output 2 used before output 1, Shifting output2 settings to output1 and disabling output 2\n");
8265  }
8266 
8267  for (i = 0; i < NI_MAX_NUM_OF_DECODER_OUTPUTS; i++)
8268  {
8269  if (!shift_params || i == 0)
8270  {
8271  j = i;
8272  }
8273  else
8274  {
8275  j = (i == 1) ? 2 : 1; //swap settings
8276  }
8277  p_cfg->asOutputConfig[i].ui8Force8Bit = p_dec->force_8_bit[j];
8278  p_cfg->asOutputConfig[i].ui8SemiPlanarEnabled = p_dec->semi_planar[j];
8279  p_cfg->asOutputConfig[i].ui8CropMode = p_dec->crop_mode[j];
8281  (uint16_t)((p_dec->crop_whxy[j][0]) & 0xFFFE);
8283  (uint16_t)((p_dec->crop_whxy[j][1]) & 0xFFFE);
8285  (uint16_t)((p_dec->crop_whxy[j][2]) & 0xFFFE);
8287  (uint16_t)((p_dec->crop_whxy[j][3]) & 0xFFFE);
8288 
8289  //Offset resized if out of bounds
8291  {
8293  }
8295  {
8297  }
8298 
8299  if (p_dec->enable_ppu_scale_adapt)
8300  {
8302  }
8303  else if (p_dec->scale_long_short_edge[j] == 1)
8304  {
8305  p_cfg->asOutputConfig[i].ui8EnablePpuScaleAdapt = 3;
8306  }
8307  else if (p_dec->scale_long_short_edge[j] == 2)
8308  {
8309  p_cfg->asOutputConfig[i].ui8EnablePpuScaleAdapt = 4;
8310  }
8311  else
8312  {
8313  p_cfg->asOutputConfig[i].ui8EnablePpuScaleAdapt = 0;
8314  }
8316 
8317  if (p_dec->scale_round[j] == -1)
8318  {
8319  if (p_cfg->asOutputConfig[i].ui8EnablePpuScaleAdapt == 1 ||
8320  p_cfg->asOutputConfig[i].ui8EnablePpuScaleAdapt == 2)
8321  {
8322  p_cfg->asOutputConfig[i].ui8ScaleResCeil = p_dec->scale_resolution_ceil[j] - 1;
8323  }
8324  else
8325  {
8327  }
8328  }
8329  else if (p_dec->scale_round[j] == 0)
8330  {
8332  }
8333  else
8334  {
8335  p_cfg->asOutputConfig[i].ui8ScaleResCeil = p_dec->scale_resolution_ceil[j] - 1;
8336  }
8337 
8339  (uint16_t)((p_dec->scale_wh[j][0]+1) & 0xFFFE);
8341  (uint16_t)((p_dec->scale_wh[j][1]+1) & 0xFFFE);
8342 
8345  {
8346  p_cfg->asOutputConfig[i].ui8ScaleEnabled = 1;
8347  }
8348  else
8349  {
8350  p_cfg->asOutputConfig[i].ui8ScaleEnabled = 0;
8351  }
8352  }
8353 
8355  if (p_cfg->ui8MaxExtraHwFrameCnt != 255 &&
8356  ni_cmp_fw_api_ver((char *)&p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6rB") < 0)
8357  {
8358  ni_log2(p_ctx, NI_LOG_INFO, "Warning %s(): maxExtraHwFrameCnt is not support for FW < 6rB\n", __func__);
8359  }
8360  p_cfg->ui8EcPolicy = p_dec->ec_policy;
8361  p_cfg->ui8EnableAdvancedEc = p_dec->enable_advanced_ec;
8363  if (p_cfg->ui8EnableAdvancedEc == 2 &&
8364  ni_cmp_fw_api_ver((char *)&p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6rO") < 0)
8365  {
8366  ni_log2(p_ctx, NI_LOG_INFO, "Warning %s(): (enableAdvancedEc == 2) is not support for FW < 6rO\n", __func__);
8367  p_cfg->ui8EnableAdvancedEc = 1;
8368  ni_log2(p_ctx, NI_LOG_INFO, "Warning %s(): reset enableAdvancedEc to %d\n", __func__, p_cfg->ui8EnableAdvancedEc);
8369  }
8370  if (p_cfg->ui8EcPolicy == NI_EC_POLICY_LIMITED_ERROR &&
8371  ni_cmp_fw_api_ver((char *)&p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6ri") < 0)
8372  {
8373  ni_log2(p_ctx, NI_LOG_INFO, "Warning %s(): (EcPolicy == limited_error) not supported for FW < 6ri\n", __func__);
8375  ni_log2(p_ctx, NI_LOG_INFO, "Warning %s(): reset EcPolicy to %d\n", __func__, p_cfg->ui8EcPolicy);
8376  }
8378  ni_cmp_fw_api_ver((char *)&p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6ri") < 0)
8379  {
8380  ni_log2(p_ctx, NI_LOG_INFO, "Warning %s(): setting ecErrThreshold not supported for FW < 6ri\n", __func__);
8382  ni_log2(p_ctx, NI_LOG_INFO, "Warning %s(): reset ecErrThreshold to %d\n", __func__, NI_EC_ERR_THRESHOLD_DEFAULT);
8383  }
8385  ni_cmp_fw_api_ver((char *)&p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6s1") < 0)
8386  {
8387  ni_log2(p_ctx, NI_LOG_INFO, "Warning %s(): (EcPolicy == best_effort_out_dc) not supported for FW < 6s1\n", __func__);
8389  ni_log2(p_ctx, NI_LOG_INFO, "Warning %s(): reset EcPolicy to %d\n", __func__, p_cfg->ui8EcPolicy);
8390  }
8392  p_cfg->ui8SurviveStreamErr = p_dec->survive_stream_err;
8393  if (p_cfg->ui8SurviveStreamErr != 0 &&
8394  ni_cmp_fw_api_ver((char *)&p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6rl") < 0) {
8395  ni_log2(p_ctx, NI_LOG_INFO, "Warning %s(): surviveStreamErr is not supported for FW < 6rl\n", __func__);
8396  }
8397  if (p_dec->reduce_dpb_delay)
8398  {
8399  if (p_ctx->codec_format == NI_CODEC_FORMAT_H264 &&
8400  ni_cmp_fw_api_ver((char *)&p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6rs") >= 0)
8401  p_cfg->ui8ReduceDpbDelay = p_dec->reduce_dpb_delay;
8402  else
8403  {
8404  ni_log2(p_ctx, NI_LOG_INFO, "Warning %s():Not support to reduce dpb delay and reset.\n", __func__);
8405  p_dec->reduce_dpb_delay = 0;
8406  }
8407  }
8408 
8409  //print it all out
8410  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8HWFrame = %d\n", p_cfg->ui8HWFrame);
8411  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8MCMode = %d\n", p_cfg->ui8MCMode);
8412  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8UduSeiEnabled = %d\n", p_cfg->ui8UduSeiEnabled);
8413  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16MaxSeiDataSize = %d\n", p_cfg->ui16MaxSeiDataSize);
8414  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8DisablePictureReordering = %d\n", p_cfg->ui8DisablePictureReordering);
8415  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8Enabled0 = %d\n", p_cfg->asOutputConfig[0].ui8Enabled);
8416  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8Enabled1 = %d\n", p_cfg->asOutputConfig[1].ui8Enabled);
8417  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8Enabled2 = %d\n", p_cfg->asOutputConfig[2].ui8Enabled);
8418  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32MaxPktSize = %u\n", p_cfg->ui32MaxPktSize);
8419  for (i = 0; i < NI_MAX_NUM_OF_DECODER_OUTPUTS; i++)
8420  {
8421  ni_log2(p_ctx, NI_LOG_DEBUG, "[%d] ui8Force8Bit %d\n", i, p_cfg->asOutputConfig[i].ui8Force8Bit);
8422  ni_log2(p_ctx, NI_LOG_DEBUG, "[%d] ui8SemiPlanarEnabled %d\n", i, p_cfg->asOutputConfig[i].ui8SemiPlanarEnabled);
8423  ni_log2(p_ctx, NI_LOG_DEBUG, "[%d] ui8CropMode %d\n", i, p_cfg->asOutputConfig[i].ui8CropMode);
8424  ni_log2(p_ctx, NI_LOG_DEBUG, "[%d] sCroppingRectable.ui16XYWH %d,%d - %d x %d\n", i,
8429  ni_log2(p_ctx, NI_LOG_DEBUG, "[%d] sOutputPictureSize.ui16Width x height %d x %d\n", i,
8432  ni_log2(p_ctx, NI_LOG_DEBUG, "[%d] ui8ScaleEnabled %d\n", i, p_cfg->asOutputConfig[i].ui8ScaleEnabled);
8433  ni_log2(p_ctx, NI_LOG_DEBUG, "[%d] ui8EnablePpuScaleAdapt %u\n", i, p_cfg->asOutputConfig[i].ui8EnablePpuScaleAdapt);
8434  ni_log2(p_ctx, NI_LOG_DEBUG, "[%d] ui8EnablePpuScaleLimit %u\n", i, p_cfg->asOutputConfig[i].ui8EnablePpuScaleLimit);
8435  ni_log2(p_ctx, NI_LOG_DEBUG, "[%d] ui8ScaleResCeil %u\n", i, p_cfg->asOutputConfig[i].ui8ScaleResCeil);
8436  }
8437  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8MaxExtraHwFrameCnt %u\n", p_cfg->ui8MaxExtraHwFrameCnt);
8438  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8EcPolicy = %u\n", p_cfg->ui8EcPolicy);
8439  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8EnableAdvancedEc = %u\n", p_cfg->ui8EnableAdvancedEc);
8440  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8EnablelowDelayCheck = %u\n", p_cfg->ui8EnablelowDelayCheck);
8441  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8DisableAdaptiveBuffers = %u\n", p_cfg->ui8DisableAdaptiveBuffers);
8442  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32ErrRatioThreshold = %u\n", p_cfg->ui32ErrRatioThreshold);
8443  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8ReduceDpbDelay = %u\n", p_cfg->ui8ReduceDpbDelay);
8444 
8445 }
8446 
8447 
8448 /*!******************************************************************************
8449  * \brief Setup all xcoder configurations with custom parameters (Rev. B)
8450  *
8451  * \param
8452  *
8453  * \return
8454  ******************************************************************************/
8456  ni_encoder_config_t *p_cfg,
8457  ni_xcoder_params_t *p_src)
8458 {
8459  ni_t408_config_t* p_t408 = NULL;
8460  ni_encoder_cfg_params_t *p_enc = NULL;
8461  int i = 0;
8462 
8463  if ((!p_ctx) || (!p_cfg) || (!p_src))
8464  {
8465  ni_log(NI_LOG_ERROR, "ERROR: %s() Null pointer parameters passed\n",
8466  __func__);
8467  return;
8468  }
8469  p_t408 = &(p_cfg->niParamT408);
8470  p_enc = &p_src->cfg_enc_params;
8471 
8472  ni_set_default_template(p_ctx, p_cfg);
8473 
8474  p_cfg->i32picWidth = p_src->source_width;
8475  p_cfg->i32picHeight = p_src->source_height;
8476  p_t408->tier = p_enc->high_tier;
8477  p_t408->gop_preset_index = p_enc->gop_preset_index;
8479  p_t408->cu_size_mode = p_enc->cu_size_mode;
8480  p_t408->max_num_merge = p_enc->max_num_merge;
8481  p_cfg->ui8AiEnhanceMode = p_src->enable_ai_enhance;
8482  p_cfg->ui8enable2PassGopPatern = p_src->enable2PassGop;
8483  // enhance_level is in range [1,3] set level when paramters is valid, otherwise make it to 1.
8484  if(p_cfg->ui8AiEnhanceMode && p_src->ai_enhance_level > 0 && p_src->ai_enhance_level < 4){
8485  p_cfg->ui8AiEnhanceLevel = p_src->ai_enhance_level;
8486  }else{
8487  p_cfg->ui8AiEnhanceLevel = 1;
8488  }
8490  p_cfg->i8skipFrameEnable = p_enc->skip_frame_enable;
8492  p_cfg->u8skipFrameInterval = p_enc->skip_frame_interval;
8493  p_cfg->ui16iFrameSizeRatio = p_enc->iframe_size_ratio;
8494  p_cfg->ui8EnableAcqLimit = p_enc->enable_acq_limit;
8497  if (p_enc->crfFloat == (float)-1.0 && p_enc->enable_smooth_crf == 1)
8498  {
8499  //smooth crf only support when setting float crf
8500  p_enc->enable_smooth_crf = 0;
8501  }
8502  p_cfg->ui8enableSmoothCrf = p_enc->enable_smooth_crf;
8504 
8505  // enable_dynamic_8x8_merge, enable_dynamic_16x16_merge, enable_dynamic_32x32_merge,
8506  // trans_rate, enable_hvs_qp_scale:
8507  // are not present in Rev B p_config
8508 
8509  p_cfg->ui8rcEnable = p_enc->rc.enable_rate_control;
8510 
8511  if(p_ctx->last_bitrate != 0)
8512  {
8513  // Slow sequence change happened. Retain the last bitrate.
8514  ni_log2(p_ctx, NI_LOG_DEBUG, "### %s: Slow sequence happened retain last_bitrate %d. assigned bitrate %d\n",
8515  __FUNCTION__, p_ctx->last_bitrate, p_src->bitrate);
8516  p_src->bitrate = p_ctx->last_bitrate;
8517  }
8518 
8519  if (p_src->bitrate != 0)
8520  {
8521  p_cfg->i32bitRate = p_src->bitrate;
8522  }
8523 
8524  // Update the bitrate to be used after Slow sequence change
8525  p_ctx->last_bitrate = p_cfg->i32bitRate;
8526 
8527 #if 0
8528  if ((p_enc->rc.enable_rate_control == 0) &&
8529  ((p_enc->rc.enable_mb_level_rc == 1) || (p_enc->rc.enable_cu_level_rate_control == 1)))
8530  {
8532  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");
8533  }
8534 #endif
8535 
8537  p_t408->enable_hvs_qp = p_enc->rc.enable_hvs_qp;
8538  p_t408->hvs_qp_scale = p_enc->rc.hvs_qp_scale;
8539  p_t408->minQpI = p_enc->rc.min_qp;
8540  p_t408->minQpP = p_enc->rc.min_qp;
8541  p_t408->minQpB = p_enc->rc.min_qp;
8542  p_t408->maxQpI = p_enc->rc.max_qp;
8543  p_t408->maxQpP = p_enc->rc.max_qp;
8544  p_t408->maxQpB = p_enc->rc.max_qp;
8545 
8546  p_t408->max_delta_qp = p_enc->rc.max_delta_qp;
8547  if (p_enc->rc.vbv_buffer_size != -1)
8548  {
8549  p_cfg->i32vbvBufferSize = p_enc->rc.vbv_buffer_size;
8550  }
8551  else
8552  {
8553  if (p_enc->rc.enable_rate_control)
8554  p_cfg->i32vbvBufferSize = 3000; // enable CBR (default vbv buffer size 3000) even if user does not set vbvBufferSize
8555  else
8556  p_cfg->i32vbvBufferSize = 0; // if user sets CRF but not vbvBufferSize, do not eanble capped CRF
8557  }
8558  p_cfg->ui32vbvMaxRate = p_enc->rc.vbv_max_rate;
8559  p_cfg->i8intraQpDelta = p_enc->rc.intra_qp_delta;
8560  p_cfg->ui8fillerEnable = p_enc->rc.enable_filler;
8561  p_cfg->ui8picSkipEnable = p_enc->rc.enable_pic_skip;
8562  p_cfg->ui16maxFrameSize = p_enc->maxFrameSize / 2000;
8563  p_t408->intra_period = p_enc->intra_period;
8564  p_t408->roiEnable = p_enc->roi_enable;
8565  p_t408->useLongTerm = p_enc->long_term_ref_enable;
8566  if (QUADRA)
8567  {
8569  p_cfg->ui8setLongTermCount = p_enc->long_term_ref_count;
8570  }
8571  p_t408->conf_win_top = p_enc->conf_win_top;
8572  p_t408->conf_win_bottom = p_enc->conf_win_bottom;
8573  p_t408->conf_win_left = p_enc->conf_win_left;
8574  p_t408->conf_win_right = p_enc->conf_win_right;
8575  p_t408->avcIdrPeriod = p_enc->intra_period;
8576 
8577  if (QUADRA)
8578  {
8579  if(p_ctx->last_framerate.framerate_num != 0)
8580  {
8581  // Slow sequence change happened. Retain the last framerate.
8582  ni_log2(p_ctx, NI_LOG_DEBUG, "### %s: Slow sequence happened retain last_framerate num %d den %d. assigned num %d den %d\n",
8583  __FUNCTION__, p_ctx->last_framerate.framerate_num, p_ctx->last_framerate.framerate_denom,
8584  p_cfg->i32frameRateInfo, p_cfg->i32frameRateDenominator);
8585  p_src->fps_number = p_ctx->last_framerate.framerate_num;
8587 
8588  if (!p_src->enable_vfr) {
8589  p_enc->frame_rate = (int)(p_src->fps_number / p_src->fps_denominator);
8590  }
8591  }
8592 
8593  if (p_cfg->i32frameRateInfo != p_enc->frame_rate)
8594  {
8595  p_cfg->i32frameRateInfo = p_enc->frame_rate;
8596  p_cfg->i32frameRateDenominator = 1;
8597  if (p_src->fps_denominator != 0 &&
8598  (p_src->fps_number % p_src->fps_denominator) != 0)
8599  {
8600  uint32_t numUnitsInTick = 1000;
8601  p_cfg->i32frameRateDenominator = numUnitsInTick + 1;
8602  p_cfg->i32frameRateInfo += 1;
8603  p_cfg->i32frameRateInfo *= numUnitsInTick;
8604  }
8605  }
8606 
8607  // Update the framerate to be used after Slow sequence change
8610  }
8611  else
8612  {
8613  if (p_cfg->i32frameRateInfo != p_enc->frame_rate)
8614  {
8615  p_cfg->i32frameRateInfo = p_enc->frame_rate;
8616  p_t408->numUnitsInTick = 1000;
8617  if (p_src->fps_denominator != 0 &&
8618  (p_src->fps_number % p_src->fps_denominator) != 0)
8619  {
8620  p_t408->numUnitsInTick += 1;
8621  p_cfg->i32frameRateInfo += 1;
8622  }
8623  p_t408->timeScale = p_cfg->i32frameRateInfo * 1000;
8624  if (NI_CODEC_FORMAT_H264 == p_ctx->codec_format)
8625  {
8626  p_t408->timeScale *= 2;
8627  }
8628  }
8629  }
8630 
8631  p_t408->intra_qp = p_enc->rc.intra_qp;
8632 
8633  // "repeatHeaders" value 1 (all I frames) maps to forcedHeaderEnable
8634  // value 2; all other values are ignored
8635  if (p_t408->forcedHeaderEnable != p_enc->forced_header_enable &&
8637  {
8638  p_t408->forcedHeaderEnable = 2;
8639  p_cfg->ui8repeatHeaders = p_enc->forced_header_enable;
8640  }
8641 
8643 
8644  if (STD_AVC == p_cfg->ui8bitstreamFormat)
8645  {
8646  switch (p_t408->decoding_refresh_type)
8647  {
8648  case 0: // Non-IRAP I-p_frame
8649  {
8650  // intra_period set to user-configured (above), avcIdrPeriod set to 0
8651  p_t408->avcIdrPeriod = 0;
8652  break;
8653  }
8654  case 1: // CRA
8655  case 2: // IDR
8656  {
8657  // intra_period set to 0, avcIdrPeriod set to user-configured (above)
8658  p_t408->intra_period = 0;
8659  break;
8660  }
8661  default:
8662  {
8663  ni_log(
8664  NI_LOG_ERROR,
8665  "ERROR: %s() unknown value for p_t408->decoding_refresh_type: %d\n",
8666  __func__, p_t408->decoding_refresh_type);
8667  break;
8668  }
8669  }
8670  } else if (STD_HEVC == p_cfg->ui8bitstreamFormat ||
8671  STD_AV1 == p_cfg->ui8bitstreamFormat)
8672  {
8673  p_t408->avcIdrPeriod = 0;
8674  }
8675 
8676  // Rev. B: H.264 only parameters.
8677  p_t408->enable_transform_8x8 = p_enc->enable_transform_8x8;
8678  p_t408->entropy_coding_mode = p_enc->entropy_coding_mode;
8679 
8680  // Rev. B: shared between HEVC and H.264
8681  p_t408->slice_mode = p_enc->slice_mode;
8682  p_t408->slice_arg = p_enc->slice_arg;
8683  if (p_t408->intra_mb_refresh_mode != p_enc->intra_mb_refresh_mode)
8684  {
8686  if (1 != p_t408->intra_mb_refresh_mode)
8687  {
8688  p_t408->intra_mb_refresh_mode = 1;
8689  ni_log2(p_ctx, NI_LOG_DEBUG, "force intraRefreshMode to 1 because quadra only supports intra refresh by rows\n");
8690  }
8691  }
8692 
8693  if (p_t408->intra_mb_refresh_arg != p_enc->intra_mb_refresh_arg)
8694  {
8695  p_t408->intra_mb_refresh_arg = p_enc->intra_mb_refresh_arg;
8696  if (1 == p_t408->intra_mb_refresh_mode)
8697  {
8698  int mbHeight = (p_cfg->ui8bitstreamFormat == STD_AVC) ? 16 : 64;
8699  int mbRows = (p_cfg->i32picHeight + mbHeight - 1) / mbHeight;
8700  p_cfg->ui16gdrDuration = (mbRows + p_t408->intra_mb_refresh_arg - 1) / p_t408->intra_mb_refresh_arg;
8701  }
8702  }
8703 
8704  p_cfg->ui8intraResetRefresh = p_enc->intra_reset_refresh;
8705 
8706  // Rev. B: could be shared for HEVC and H.264
8707  p_t408->enable_mb_level_rc = p_enc->rc.enable_mb_level_rc;
8708 
8709  // profile setting: if user specified profile
8710  if (0 != p_enc->profile)
8711  {
8712  p_t408->profile = p_enc->profile;
8713  }
8714 
8715  p_t408->level = p_enc->level_idc;
8716 
8717  // main, extended or baseline profile of 8 bit (if input is 10 bit, Quadra auto converts to 8 bit) H.264 requires the following:
8718  // main: profile = 2 transform8x8Enable = 0
8719  // extended: profile = 3 entropyCodingMode = 0, transform8x8Enable = 0
8720  // baseline: profile = 1 entropyCodingMode = 0, transform8x8Enable = 0 and
8721  // gop with no B frames (gopPresetIdx=1, 2, 6, or 0
8722  // (custom with no B frames)
8723  if (STD_AVC == p_cfg->ui8bitstreamFormat)
8724  {
8725  if (2 == p_t408->profile)
8726  {
8727  p_t408->enable_transform_8x8 = 0;
8728  ni_log2(p_ctx, NI_LOG_DEBUG, "enable_transform_8x8 set to 0 for profile 2 (main)\n");
8729  }
8730  else if (3 == p_t408->profile || 1 == p_t408->profile)
8731  {
8732  p_t408->entropy_coding_mode = p_t408->enable_transform_8x8 = 0;
8733  ni_log2(p_ctx, NI_LOG_DEBUG, "entropy_coding_mode and enable_transform_8x8 set to 0 "
8734  "for profile 3 (extended) or 1 (baseline)\n");
8735  }
8736  }
8737 
8738  if (QUADRA)
8739  {
8740  if (0 == p_t408->entropy_coding_mode && 1 == p_enc->EnableRdoQuant)
8741  {
8742  ni_log2(p_ctx, NI_LOG_DEBUG, "RDOQ does not support entropy_coding_mode 0 (CAVLC) "
8743  "force EnableRdoQuant 0 to accommodate HW limiation\n");
8744  p_enc->EnableRdoQuant = 0;
8745  }
8746  }
8747 
8748 #ifndef QUADRA
8749  if (!QUADRA)
8750  {
8751  if (GOP_PRESET_IDX_CUSTOM == p_t408->gop_preset_index)
8752  {
8754  for (i = 0; i < p_t408->custom_gop_params.custom_gop_size; i++)
8755  {
8758  p_t408->custom_gop_params.pic_param[i].pic_qp = p_enc->custom_gop_params.pic_param[i].pic_qp + p_t408->intra_qp;
8759  p_t408->custom_gop_params.pic_param[i].num_ref_pic_L0 = p_enc->custom_gop_params.pic_param[i].num_ref_pic_L0;
8760  p_t408->custom_gop_params.pic_param[i].ref_poc_L0 = p_enc->custom_gop_params.pic_param[i].ref_poc_L0;
8761  p_t408->custom_gop_params.pic_param[i].ref_poc_L1 = p_enc->custom_gop_params.pic_param[i].ref_poc_L1;
8763  }
8764  }
8765  }
8766  else // QUADRA
8767 #endif
8768  {
8769  if (p_enc->custom_gop_params.custom_gop_size &&
8772  {
8775  for (i = 0; i < p_t408->custom_gop_params.custom_gop_size; i++)
8776  {
8785  p_t408->custom_gop_params.pic_param[i].pic_type =
8789  for (int j = 0;
8790  j < p_enc->custom_gop_params.pic_param[i].num_ref_pics; j++)
8791  {
8792  p_t408->custom_gop_params.pic_param[i].rps[j].ref_pic =
8793  p_enc->custom_gop_params.pic_param[i].rps[j].ref_pic;
8796  }
8797  }
8798  }
8799  }
8800 
8801  p_ctx->key_frame_type = p_t408->decoding_refresh_type; //Store to use when force key p_frame
8802 
8803  // forceFrameType=1 requires intraPeriod=0 and avcIdrPeriod=0 and gopPresetIdx=8
8804  if (1 == p_src->force_frame_type)
8805  {
8806  p_t408->intra_period = 0;
8807  p_t408->avcIdrPeriod = 0;
8808  p_t408->gop_preset_index = 8;
8809  p_ctx->force_frame_type = 1;
8810  }
8811 
8812  p_cfg->hdrEnableVUI = p_src->hdrEnableVUI;
8813 
8814  if (p_cfg->i32hwframes != p_src->hwframes)
8815  {
8816  if (p_src->hwframes && p_ctx->auto_dl_handle == 0)
8817  {
8818  p_cfg->i32hwframes = p_src->hwframes;
8819  }
8820  else
8821  {
8822  p_cfg->i32hwframes = 0;
8823  }
8824  }
8825  p_cfg->ui16rootBufId = p_src->rootBufId;
8826 
8827  //set VUI info deprecated
8828  //p_cfg->ui32VuiDataSizeBits = p_src->ui32VuiDataSizeBits;
8829  //p_cfg->ui32VuiDataSizeBytes = p_src->ui32VuiDataSizeBytes;
8830  //memcpy(p_cfg->ui8VuiRbsp, p_src->ui8VuiRbsp, NI_MAX_VUI_SIZE);
8831  //if ((p_src->pos_num_units_in_tick > p_src->ui32VuiDataSizeBits) || (p_src->pos_time_scale > p_src->ui32VuiDataSizeBits))
8832  //{
8833  // ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() VUI filling error\n", __func__);
8834  // return;
8835  //}
8836  //else
8837  //{
8838  // ni_fix_VUI(p_cfg->ui8VuiRbsp, p_src->pos_num_units_in_tick, p_t408->numUnitsInTick);
8839  // ni_fix_VUI(p_cfg->ui8VuiRbsp, p_src->pos_time_scale, p_t408->timeScale);
8840  //}
8841 
8842  if (p_src->enable_vfr)
8843  {
8844  p_cfg->ui8fixedframerate = 0;
8845  } else
8846  {
8847  p_cfg->ui8fixedframerate = 1;
8848  }
8849 
8850  //new QUADRA param
8851  if (p_enc->EnableAUD != 0)
8852  {
8853  p_cfg->ui8EnableAUD = p_enc->EnableAUD;
8854  }
8855  if (p_enc->lookAheadDepth != 0)
8856  {
8857  p_cfg->ui8LookAheadDepth = p_enc->lookAheadDepth;
8858  }
8859  if (p_enc->rdoLevel != 1)
8860  {
8861  p_cfg->ui8rdoLevel = p_enc->rdoLevel;
8862  }
8863  if (p_enc->crf != -1)
8864  {
8865  p_cfg->i8crf = p_enc->crf;
8866  }
8867  if (p_enc->HDR10MaxLight != 0)
8868  {
8869  p_cfg->ui16HDR10MaxLight = p_enc->HDR10MaxLight;
8870  }
8871  if (p_enc->HDR10AveLight != 0)
8872  {
8873  p_cfg->ui16HDR10AveLight = p_enc->HDR10AveLight;
8874  }
8875  if (p_enc->HDR10CLLEnable != 0)
8876  {
8877  p_cfg->ui8HDR10CLLEnable = p_enc->HDR10CLLEnable;
8878  }
8879  if (p_enc->HDR10Enable != 0)
8880  {
8881  p_cfg->ui8hdr10_enable = p_enc->HDR10Enable;
8882  p_cfg->ui16hdr10_dx0 = p_enc->HDR10dx0;
8883  p_cfg->ui16hdr10_dy0 = p_enc->HDR10dy0;
8884  p_cfg->ui16hdr10_dx1 = p_enc->HDR10dx1;
8885  p_cfg->ui16hdr10_dy1 = p_enc->HDR10dy1;
8886  p_cfg->ui16hdr10_dx2 = p_enc->HDR10dx2;
8887  p_cfg->ui16hdr10_dy2 = p_enc->HDR10dy2;
8888  p_cfg->ui16hdr10_wx = p_enc->HDR10wx;
8889  p_cfg->ui16hdr10_wy = p_enc->HDR10wy;
8890  p_cfg->ui32hdr10_maxluma = p_enc->HDR10maxluma;
8891  p_cfg->ui32hdr10_minluma = p_enc->HDR10minluma;
8892  }
8893 
8894  if (p_enc->EnableRdoQuant != 0)
8895  {
8896  p_cfg->ui8EnableRdoQuant = p_enc->EnableRdoQuant;
8897  }
8898  if (p_enc->ctbRcMode != 0)
8899  {
8900  p_cfg->ui8ctbRcMode = p_enc->ctbRcMode;
8901  }
8902  if (p_enc->gopSize != 0)
8903  {
8904  p_cfg->ui8gopSize = p_enc->gopSize;
8905  }
8906  if (p_src->use_low_delay_poc_type != 0)
8907  {
8909  }
8910  if (p_enc->gopLowdelay != 0)
8911  {
8912  p_cfg->ui8gopLowdelay = p_enc->gopLowdelay;
8913  }
8914  if (p_enc->gdrDuration != 0)
8915  {
8916  p_cfg->ui16gdrDuration = p_enc->gdrDuration;
8917  }
8918  if (p_enc->colorDescPresent)
8919  {
8920  p_cfg->ui8colorDescPresent = 1;
8921  p_cfg->ui8colorPrimaries = p_enc->colorPrimaries;
8922  p_cfg->ui8colorTrc = p_enc->colorTrc;
8923  p_cfg->ui8colorSpace = p_enc->colorSpace;
8924  p_cfg->ui8videoFullRange = p_enc->videoFullRange;
8925  }
8926  if (p_enc->videoFullRange)
8927  {
8928  p_cfg->ui8videoFullRange = p_enc->videoFullRange;
8929  }
8930  if (p_enc->hrdEnable != 0)
8931  {
8932  p_cfg->ui8hrdEnable = p_enc->hrdEnable;
8933  }
8934 
8935  p_cfg->ui8planarFormat = p_src->cfg_enc_params.planar;
8936  p_cfg->ui16aspectRatioWidth = p_enc->aspectRatioWidth;
8937  p_cfg->ui16aspectRatioHeight = p_enc->aspectRatioHeight;
8938 
8939  if (p_enc->ltrRefInterval != 0)
8940  {
8941  p_cfg->ui32ltrRefInterval = p_enc->ltrRefInterval;
8942  p_cfg->i32ltrRefQpOffset = p_enc->ltrRefQpOffset;
8943  p_cfg->ui32ltrFirstGap = p_enc->ltrFirstGap;
8944  p_cfg->ui32ltrNextInterval = p_enc->ltrNextInterval;
8945  }
8946  if (p_enc->multicoreJointMode != 0)
8947  {
8948  p_cfg->ui8multicoreJointMode = p_enc->multicoreJointMode;
8949  }
8950  p_cfg->ui32QLevel = p_enc->qlevel;
8951 
8952  if (p_enc->chromaQpOffset != 0)
8953  {
8954  p_cfg->i8chromaQpOffset = p_enc->chromaQpOffset;
8955  }
8956 
8957  if (p_enc->tolCtbRcInter != (float)0.1)
8958  {
8959  p_cfg->i32tolCtbRcInter = (int32_t)(p_enc->tolCtbRcInter * 1000);
8960  }
8961 
8962  if (p_enc->tolCtbRcIntra != (float)0.1)
8963  {
8964  p_cfg->i32tolCtbRcIntra = (int32_t)(p_enc->tolCtbRcIntra * 1000);
8965  }
8966 
8967  if (p_enc->bitrateWindow != -255)
8968  {
8969  p_cfg->i16bitrateWindow = p_enc->bitrateWindow;
8970  }
8971 
8972  if (p_enc->inLoopDSRatio != 1)
8973  {
8974  p_cfg->ui8inLoopDSRatio = p_enc->inLoopDSRatio;
8975  }
8976 
8977  if (p_enc->blockRCSize != 0)
8978  {
8979  p_cfg->ui8blockRCSize = p_enc->blockRCSize;
8980  }
8981 
8982  if (p_enc->rcQpDeltaRange != 10)
8983  {
8984  p_cfg->ui8rcQpDeltaRange = p_enc->rcQpDeltaRange;
8985  }
8986 
8987  if (p_enc->ctbRowQpStep != 0)
8988  {
8989  p_cfg->i16ctbRowQpStep = p_enc->ctbRowQpStep;
8990  }
8991 
8992  if (p_enc->newRcEnable != -1)
8993  {
8994  p_cfg->ui8NewRCEnable = p_enc->newRcEnable;
8995  }
8996 
8997  // convert enable_mb_level_rc, enable_cu_level_rate_control, and enable_hvs_qp to ctbRcMode
8998  if (QUADRA)
8999  {
9000  // ctbRcMode has priority over enable_mb_level_rc, enable_cu_level_rate_control, and enable_hvs_qp
9001  if (!p_cfg->ui8ctbRcMode)
9002  {
9003  if (p_t408->enable_mb_level_rc || p_t408->enable_cu_level_rate_control)
9004  {
9005  if (p_t408->enable_hvs_qp)
9006  {
9007  p_cfg->ui8ctbRcMode = 3;
9008  // If hvsQP is enabled, disable strongIntraSmooth to apply one filtering at a time for better VQ.
9009  p_t408->strongIntraSmoothEnable = 0;
9011  "Turning off strongIntraSmoothing because hvsQPEnable=1 "
9012  "for better subjective VQ\n");
9013  } else
9014  {
9015  p_cfg->ui8ctbRcMode = 2;
9016  }
9017  }
9018  else if (p_t408->enable_hvs_qp)
9019  {
9020  p_cfg->ui8ctbRcMode = 1;
9021  // If hvsQP is enabled, disable strongIntraSmooth to apply one filtering at a time for better VQ.
9022  p_t408->strongIntraSmoothEnable = 0;
9024  "Turning off strongIntraSmoothing because hvsQPEnable=1 for "
9025  "better subjective VQ\n");
9026  }
9027  }
9028  }
9029 
9030  if (p_src->low_delay_mode != 0)
9031  {
9032  p_cfg->ui8LowDelay = !!(p_src->low_delay_mode);
9033  }
9034 
9035  if (p_enc->enable_ssim != 0)
9036  {
9037  p_cfg->ui8enableSSIM = p_enc->enable_ssim;
9038  }
9039 
9040  if (p_enc->avcc_hvcc != 0)
9041  {
9042  p_cfg->ui8avccHvcc = p_enc->avcc_hvcc;
9043  }
9044 
9045  if (p_enc->av1_error_resilient_mode != 0)
9046  {
9048  }
9049 
9050  if (p_enc->temporal_layers_enable != 0)
9051  {
9053  }
9054 
9055  if (p_enc->spatial_layers > 1)
9056  {
9057  p_cfg->ui8spatialLayersMinusOne = p_enc->spatial_layers - 1;
9058  if (ni_cmp_fw_api_ver((char *)&p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6rw") < 0) {
9059  ni_log2(p_ctx, NI_LOG_INFO, "Warning %s(): spatialLayers is not supported for FW < 6rw\n", __func__);
9060  }
9061  }
9062 
9063  if (p_enc->spatial_layers_ref_base_layer != 0)
9064  {
9066  if (ni_cmp_fw_api_ver((char *)&p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6s0") < 0) {
9067  ni_log2(p_ctx, NI_LOG_INFO, "Warning %s(): spatialLayers is not supported for FW < 6s0\n", __func__);
9068  }
9069  }
9070 
9071  if (p_ctx->pixel_format != NI_PIX_FMT_YUV420P)
9072  {
9073  p_cfg->ui8PixelFormat = p_ctx->pixel_format;
9074  }
9075 
9076  if (p_enc->get_psnr_mode != 3)
9077  {
9078  if (p_enc->get_psnr_mode == 2 && p_ctx->codec_format == NI_CODEC_FORMAT_H265)
9079  {
9080  // h.265 psnr_y is supported by HW for 8-bit & 10-bit, no need to reject setting or disable luma RFC
9083  {
9084  p_enc->get_psnr_mode = 3;
9085  ni_log(NI_LOG_INFO, "Warning h.265 psnr_y is only supported for YUV420P, YUV420P10LE, NV12, and P010LE\n");
9086  }
9087  }
9088  else if (p_enc->get_psnr_mode == 4)
9089  {
9090  p_cfg->ui8compressor = 0;
9092  {
9093  p_enc->get_psnr_mode = 3;
9094  p_cfg->ui8compressor = 3;
9095  ni_log(NI_LOG_INFO, "Warning reconstructed frames only supported for YUV420P, NV12\n");
9096  }
9097  }
9098  else
9099  {
9100  p_cfg->ui8compressor = p_enc->get_psnr_mode;
9101  if (p_cfg->ui8PixelFormat != NI_PIX_FMT_YUV420P)
9102  {
9103  p_cfg->ui8compressor = 3;
9104  p_enc->get_psnr_mode = 3;
9105  ni_log(NI_LOG_INFO, "Warning get psnr feature only support YUV420P (except for h.265 psnr_y)\n");
9106  }
9107  if (p_enc->crop_width || p_enc->crop_height)
9108  {
9109  p_cfg->ui8compressor = 3;
9110  p_enc->get_psnr_mode = 3;
9111  ni_log(NI_LOG_INFO, "Warning get psnr feature is not supported when cropWidth x cropHeight are set\n");
9112  }
9113  }
9114  }
9115 
9116  if (p_src->zerocopy_mode == -1) // zero copy auto mode - disable zero copy for low resolution
9117  {
9118  bool is_rgba = (p_ctx->pixel_format == NI_PIX_FMT_RGBA ||
9119  p_ctx->pixel_format == NI_PIX_FMT_BGRA ||
9120  p_ctx->pixel_format == NI_PIX_FMT_ARGB ||
9121  p_ctx->pixel_format == NI_PIX_FMT_ABGR) ? true : false;
9122  if (is_rgba ||
9124  "6Q") >= 0) &&
9126  p_src->zerocopy_mode = 1;
9127  else
9128  p_src->zerocopy_mode = 0;
9129  }
9130 
9131  if (p_src->zerocopy_mode)
9132  {
9133  p_cfg->ui32lumaLinesize = p_src->luma_linesize;
9134  p_cfg->ui32chromaLinesize = p_src->chroma_linesize;
9135  }
9136  else
9137  {
9138  p_cfg->ui32lumaLinesize = p_src->luma_linesize = 0;
9139  p_cfg->ui32chromaLinesize = p_src->chroma_linesize = 0;
9140  }
9141  // for fast sequence change linesize check
9142  p_ctx->ori_luma_linesize = p_src->luma_linesize;
9143  p_ctx->ori_chroma_linesize = p_src->chroma_linesize;
9144 
9145  // calculate number for frames delay for minFramesDelay
9146  int lookAheadEnable = !!p_cfg->ui8LookAheadDepth;
9147  int gopSize = g_map_preset_to_gopsize[lookAheadEnable][p_t408->gop_preset_index + 1];
9148  int mulitcoreDelay = p_cfg->ui8multicoreJointMode ? 3 : 0;
9149 
9150  if (p_t408->gop_preset_index == 0) // Custom GOP
9151  gopSize = p_t408->custom_gop_params.custom_gop_size;
9152 
9153  if (lookAheadEnable)
9154  {
9155  int firstGopEnd = gopSize + 1 + mulitcoreDelay; // first I-frame gopSize is 1
9156  int lookaheadGopEnd = mulitcoreDelay ?
9157  p_cfg->ui8LookAheadDepth + mulitcoreDelay + (gopSize - ((p_cfg->ui8LookAheadDepth-1+mulitcoreDelay) % gopSize)) :
9158  p_cfg->ui8LookAheadDepth + (gopSize - ((p_cfg->ui8LookAheadDepth-1) % gopSize)); // lookAheadDepth-1 because lookahead queue includes first I-frame
9159  int initialDelayNum = (firstGopEnd > lookaheadGopEnd) ? firstGopEnd : lookaheadGopEnd;
9160  int maxDelayNum = p_cfg->ui8LookAheadDepth + 1 + gopSize / 2 + mulitcoreDelay;
9161  int maxLookaheadQueue = initialDelayNum + (gopSize - 1) + mulitcoreDelay; // assume worst case scenario - gop size changes from initial gop to gop size 1
9162 
9163  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
9164  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
9166  "6r2") >= 0)
9167  {
9168  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
9170  "6rX") >= 0)
9171  {
9172  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
9173  p_ctx->current_frame_delay = p_ctx->max_frame_delay;
9174  else
9175  p_ctx->current_frame_delay = p_ctx->initial_frame_delay;
9176  }
9177  else
9178  {
9179  p_ctx->current_frame_delay = p_ctx->max_frame_delay;
9180  }
9181  }
9182  else
9183  {
9184  p_ctx->last_gop_size = gopSize;
9185  p_ctx->current_frame_delay = p_ctx->max_frame_delay;
9186  }
9187  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: firstGopEnd %d lookaheadGopEnd %d initialDelayNum %d maxDelayNum %d maxLookaheadQueue %d\n",
9188  __FUNCTION__, firstGopEnd, lookaheadGopEnd, initialDelayNum, maxDelayNum, maxLookaheadQueue);
9189  }
9190  else
9191  {
9192  p_ctx->last_gop_size = gopSize;
9193  p_ctx->initial_frame_delay = p_ctx->max_frame_delay = p_ctx->current_frame_delay = gopSize + mulitcoreDelay;
9194  }
9195 
9196  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: preset %d lookAheadDepth %d gopSize %d mulitcoreDelay %d "
9197  "last_gop_size %d linitial_frame_delay %d current_frame_delay %d max_frame_delay %d\n",
9198  __FUNCTION__, p_t408->gop_preset_index, p_cfg->ui8LookAheadDepth, gopSize, mulitcoreDelay,
9199  p_ctx->last_gop_size, p_ctx->initial_frame_delay, p_ctx->current_frame_delay, p_ctx->max_frame_delay);
9200 
9201  if (p_enc->crop_width != 0 && p_enc->crop_height != 0)
9202  {
9203  p_cfg->ui32cropWidth = p_enc->crop_width;
9204  p_cfg->ui32cropHeight = p_enc->crop_height;
9205  p_cfg->ui32horOffset = p_enc->hor_offset;
9206  p_cfg->ui32verOffset = p_enc->ver_offset;
9207  }
9208 
9209  if (p_enc->crfMax != -1)
9210  {
9211  p_cfg->i8crfMax = (int8_t)(p_enc->crfMax) + 1; // for old libxcoder backward compatibility, use 1 to represent 0
9212  }
9213 
9214  if (p_enc->qcomp != (float)0.6)
9215  {
9216  p_cfg->i32qcomp = (int32_t)(p_enc->qcomp * 1000) + 1000; // for old libxcoder backward compatibility, use 1000 to represent 0
9217  }
9218 
9219  if (p_enc->noMbtree != 0)
9220  {
9221  p_cfg->ui8noMbtree = p_enc->noMbtree;
9222  }
9223 
9224  if (p_enc->noHWMultiPassSupport != 0)
9225  {
9227  }
9228 
9229  if (p_enc->cuTreeFactor != 5)
9230  {
9231  p_cfg->i8cuTreeFactor = p_enc->cuTreeFactor;
9232  }
9233 
9234  if (p_enc->ipRatio != (float)1.4)
9235  {
9236  p_cfg->i32ipRatio = (int32_t)(p_enc->ipRatio * 1000) + 1000; // for old libxcoder backward compatibility, use 1000 to represent 0
9237  }
9238 
9239  if (p_enc->pbRatio != (float)1.3)
9240  {
9241  p_cfg->i32pbRatio = (int32_t)(p_enc->pbRatio * 1000) + 1000; // for old libxcoder backward compatibility, use 1000 to represent 0
9242  }
9243 
9244  if (p_enc->cplxDecay != (float)0.5)
9245  {
9246  p_cfg->i32cplxDecay = (int32_t)(p_enc->cplxDecay * 1000) + 1000; // for old libxcoder backward compatibility, use 1000 to represent 0
9247  }
9248 
9249  if (p_enc->pps_init_qp != -1)
9250  {
9251  p_cfg->i8ppsInitQp = (int8_t)(p_enc->pps_init_qp) + 1; // for old libxcoder backward compatibility, use 1 to represent 0
9252  }
9253 
9254  if (p_enc->bitrateMode != -1)
9255  {
9256  p_cfg->ui8bitrateMode = p_enc->bitrateMode;
9257  }
9258 
9259  if (p_enc->pass1_qp != -1)
9260  {
9261  p_cfg->i8pass1Qp = (int8_t)(p_enc->pass1_qp) + 1; // for old libxcoder backward compatibility, use 1 to represent 0
9262  }
9263 
9264  if (p_enc->crfFloat != (float)-1.0)
9265  {
9266  // for old libxcoder backward compatibility
9267  p_cfg->i8crf = (int8_t)(p_enc->crfFloat);
9268  p_cfg->i8crfDecimal = (int8_t)((p_enc->crfFloat - (float)p_cfg->i8crf) * 100);
9269  }
9270 
9271  if (p_enc->hvsBaseMbComplexity != 15)
9272  {
9273  p_cfg->i8hvsBaseMbComplexity = (int8_t)p_enc->hvsBaseMbComplexity - 15; // for old libxcoder backward compatibility, use -15 to represent 0
9274  }
9275 
9276  if (p_enc->enableipRatio != 0 && p_cfg->i32vbvBufferSize != 0) //vbvBufferSize !=0 for CBR not for ABR
9277  {
9278  p_cfg->i8enableipRatio = p_enc->enableipRatio;
9279  }
9280 
9281  if (p_enc->crf_max_iframe_enable != 0)
9282  {
9284  }
9285 
9286  if (p_enc->vbv_min_rate != 0)
9287  {
9288  p_cfg->ui32vbvMinRate = p_enc->vbv_min_rate;
9289  }
9290 
9291  if (p_enc->disableBframeRdoq != 0)
9292  {
9293  p_cfg->ui8disableBframeRDOQ = p_enc->disableBframeRdoq;
9294  }
9295 
9296  if (p_enc->forceBframeQpfactor != (float)-1.0)
9297  {
9298  p_cfg->i32forceBframeQpFactor = (int32_t)(p_enc->forceBframeQpfactor * 1000) + 1000; // for old libxcoder backward compatibility, use 1000 to represent 0
9299  }
9300 
9301  if (p_enc->tune_bframe_visual != 0)
9302  {
9303  p_cfg->ui8tuneBframeVisual = p_enc->tune_bframe_visual;
9304  }
9305 
9306  if (p_enc->customize_roi_qp_level != 0) {
9308  }
9309 
9310  if (p_enc->motionConstrainedMode != 0)
9311  {
9313  }
9314 
9315  if(p_enc->encMallocStrategy != 0)
9316  {
9317  p_cfg->ui8mallocStrategy = p_enc->encMallocStrategy;
9318  }
9319 
9320  if (p_enc->enable_timecode != 0)
9321  {
9322  p_cfg->ui8enableTimecode = p_enc->enable_timecode;
9323  }
9324 
9325  if (p_enc->vbvBufferReencode != 0)
9326  {
9327  p_cfg->ui8vbvBufferReencode = p_enc->vbvBufferReencode;
9328  }
9329 
9330  ni_log2(p_ctx, NI_LOG_DEBUG, "lowDelay=%d\n", p_src->low_delay_mode);
9331  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8bitstreamFormat=%d\n", p_cfg->ui8bitstreamFormat);
9332  ni_log2(p_ctx, NI_LOG_DEBUG, "i32picWidth=%d\n", p_cfg->i32picWidth);
9333  ni_log2(p_ctx, NI_LOG_DEBUG, "i32picHeight=%d\n", p_cfg->i32picHeight);
9334  ni_log2(p_ctx, NI_LOG_DEBUG, "i32meBlkMode=%d\n", p_cfg->i32meBlkMode);
9335  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8sliceMode=%d\n", p_cfg->ui8sliceMode);
9336  ni_log2(p_ctx, NI_LOG_DEBUG, "i32frameRateInfo=%d\n", p_cfg->i32frameRateInfo);
9337  ni_log2(p_ctx, NI_LOG_DEBUG, "i32vbvBufferSize=%d\n", p_cfg->i32vbvBufferSize);
9338  ni_log2(p_ctx, NI_LOG_DEBUG, "i32userQpMax=%d\n", p_cfg->i32userQpMax);
9339  ni_log2(p_ctx, NI_LOG_DEBUG, "enableSSIM=%d\n", p_cfg->ui8enableSSIM);
9340  // AVC only
9341  ni_log2(p_ctx, NI_LOG_DEBUG, "i32maxIntraSize=%d\n", p_cfg->i32maxIntraSize);
9342  ni_log2(p_ctx, NI_LOG_DEBUG, "i32userMaxDeltaQp=%d\n", p_cfg->i32userMaxDeltaQp);
9343  ni_log2(p_ctx, NI_LOG_DEBUG, "i32userMinDeltaQp=%d\n", p_cfg->i32userMinDeltaQp);
9344  ni_log2(p_ctx, NI_LOG_DEBUG, "i32userQpMin=%d\n", p_cfg->i32userQpMin);
9345  ni_log2(p_ctx, NI_LOG_DEBUG, "i32bitRate=%d\n", p_cfg->i32bitRate);
9346  ni_log2(p_ctx, NI_LOG_DEBUG, "i32bitRateBL=%d\n", p_cfg->i32bitRateBL);
9347  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8rcEnable=%d\n", p_cfg->ui8rcEnable);
9348  ni_log2(p_ctx, NI_LOG_DEBUG, "i32srcBitDepth=%d\n", p_cfg->i32srcBitDepth);
9349  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8enablePTS=%d\n", p_cfg->ui8enablePTS);
9350  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8lowLatencyMode=%d\n", p_cfg->ui8lowLatencyMode);
9351  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32sourceEndian=%u\n", p_cfg->ui32sourceEndian);
9352  ni_log2(p_ctx, NI_LOG_DEBUG, "hdrEnableVUI=%u\n", p_cfg->hdrEnableVUI);
9353  ni_log2(p_ctx, NI_LOG_DEBUG, "i32hwframes=%i\n", p_cfg->i32hwframes);
9354 
9355  ni_log2(p_ctx, NI_LOG_DEBUG, "** ni_t408_config_t: \n");
9356  ni_log2(p_ctx, NI_LOG_DEBUG, "profile=%d\n", p_t408->profile);
9357  ni_log2(p_ctx, NI_LOG_DEBUG, "level=%d\n", p_t408->level);
9358  ni_log2(p_ctx, NI_LOG_DEBUG, "tier=%d\n", p_t408->tier);
9359 
9360  ni_log2(p_ctx, NI_LOG_DEBUG, "internalBitDepth=%d\n", p_t408->internalBitDepth);
9361  ni_log2(p_ctx, NI_LOG_DEBUG, "losslessEnable=%d\n", p_t408->losslessEnable);
9362  ni_log2(p_ctx, NI_LOG_DEBUG, "constIntraPredFlag=%d\n", p_t408->constIntraPredFlag);
9363 
9364  ni_log2(p_ctx, NI_LOG_DEBUG, "decoding_refresh_type=%d\n", p_t408->decoding_refresh_type);
9365  ni_log2(p_ctx, NI_LOG_DEBUG, "intra_qp=%d\n", p_t408->intra_qp);
9366  ni_log2(p_ctx, NI_LOG_DEBUG, "intra_period=%d\n", p_t408->intra_period);
9367  ni_log2(p_ctx, NI_LOG_DEBUG, "roi_enable=%d\n", p_t408->roiEnable);
9368 
9369  ni_log2(p_ctx, NI_LOG_DEBUG, "useLongTerm=%u\n", p_t408->useLongTerm);
9370  ni_log2(p_ctx, NI_LOG_DEBUG, "setLongTermInterval=%u\n", p_cfg->ui32setLongTermInterval);
9371  ni_log2(p_ctx, NI_LOG_DEBUG, "setLongTermCount=%u\n", p_cfg->ui8setLongTermCount);
9372 
9373  ni_log2(p_ctx, NI_LOG_DEBUG, "conf_win_top=%d\n", p_t408->conf_win_top);
9374  ni_log2(p_ctx, NI_LOG_DEBUG, "conf_win_bottom=%d\n", p_t408->conf_win_bottom);
9375  ni_log2(p_ctx, NI_LOG_DEBUG, "conf_win_left=%d\n", p_t408->conf_win_left);
9376  ni_log2(p_ctx, NI_LOG_DEBUG, "conf_win_right=%d\n", p_t408->conf_win_right);
9377 
9378  ni_log2(p_ctx, NI_LOG_DEBUG, "independSliceMode=%d\n", p_t408->independSliceMode);
9379  ni_log2(p_ctx, NI_LOG_DEBUG, "independSliceModeArg=%d\n", p_t408->independSliceModeArg);
9380 
9381  ni_log2(p_ctx, NI_LOG_DEBUG, "dependSliceMode=%d\n", p_t408->dependSliceMode);
9382  ni_log2(p_ctx, NI_LOG_DEBUG, "dependSliceModeArg=%d\n", p_t408->dependSliceModeArg);
9383 
9384  ni_log2(p_ctx, NI_LOG_DEBUG, "intraRefreshMode=%d\n", p_t408->intraRefreshMode);
9385 
9386  ni_log2(p_ctx, NI_LOG_DEBUG, "intraRefreshArg=%d\n", p_t408->intraRefreshArg);
9387 
9388  ni_log2(p_ctx, NI_LOG_DEBUG, "use_recommend_enc_params=%d\n", p_t408->use_recommend_enc_params);
9389  ni_log2(p_ctx, NI_LOG_DEBUG, "scalingListEnable=%d\n", p_t408->scalingListEnable);
9390 
9391  ni_log2(p_ctx, NI_LOG_DEBUG, "cu_size_mode=%d\n", p_t408->cu_size_mode);
9392  ni_log2(p_ctx, NI_LOG_DEBUG, "tmvpEnable=%d\n", p_t408->tmvpEnable);
9393  ni_log2(p_ctx, NI_LOG_DEBUG, "wppEnable=%d\n", p_t408->wppEnable);
9394  ni_log2(p_ctx, NI_LOG_DEBUG, "max_num_merge=%d\n", p_t408->max_num_merge);
9395  ni_log2(p_ctx, NI_LOG_DEBUG, "disableDeblk=%d\n", p_t408->disableDeblk);
9396  ni_log2(p_ctx, NI_LOG_DEBUG, "lfCrossSliceBoundaryEnable=%d\n", p_t408->lfCrossSliceBoundaryEnable);
9397  ni_log2(p_ctx, NI_LOG_DEBUG, "betaOffsetDiv2=%d\n", p_t408->betaOffsetDiv2);
9398  ni_log2(p_ctx, NI_LOG_DEBUG, "tcOffsetDiv2=%d\n", p_t408->tcOffsetDiv2);
9399  ni_log2(p_ctx, NI_LOG_DEBUG, "skipIntraTrans=%d\n", p_t408->skipIntraTrans);
9400  ni_log2(p_ctx, NI_LOG_DEBUG, "saoEnable=%d\n", p_t408->saoEnable);
9401  ni_log2(p_ctx, NI_LOG_DEBUG, "intraNxNEnable=%d\n", p_t408->intraNxNEnable);
9402  ni_log2(p_ctx, NI_LOG_DEBUG, "bitAllocMode=%d\n", p_t408->bitAllocMode);
9403 
9404  ni_log2(p_ctx, NI_LOG_DEBUG, "enable_cu_level_rate_control=%d\n", p_t408->enable_cu_level_rate_control);
9405 
9406  ni_log2(p_ctx, NI_LOG_DEBUG, "enable_hvs_qp=%d\n", p_t408->enable_hvs_qp);
9407 
9408  ni_log2(p_ctx, NI_LOG_DEBUG, "hvs_qp_scale=%d\n", p_t408->hvs_qp_scale);
9409 
9410  ni_log2(p_ctx, NI_LOG_DEBUG, "max_delta_qp=%d\n", p_t408->max_delta_qp);
9411 
9412  // CUSTOM_GOP
9413  ni_log2(p_ctx, NI_LOG_DEBUG, "gop_preset_index=%d\n", p_t408->gop_preset_index);
9414 #ifndef QUADRA
9415  if (!QUADRA)
9416  {
9417  if (p_t408->gop_preset_index == GOP_PRESET_IDX_CUSTOM)
9418  {
9419  ni_log2(p_ctx, NI_LOG_DEBUG, "custom_gop_params.custom_gop_size=%d\n", p_t408->custom_gop_params.custom_gop_size);
9420  for (i = 0; i < 8; i++)
9421  //for (i = 0; i < p_t408->custom_gop_params.custom_gop_size; i++)
9422  {
9423  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);
9424  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);
9425  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);
9426  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);
9427  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);
9428  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);
9429  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);
9430  }
9431  }
9432  }
9433  else // QUADRA
9434 #endif
9435  {
9436  if (p_t408->custom_gop_params.custom_gop_size)
9437  {
9438  int j;
9439  ni_log2(p_ctx, NI_LOG_DEBUG, "custom_gop_params.custom_gop_size=%d\n", p_t408->custom_gop_params.custom_gop_size);
9440  for (i = 0; i < NI_MAX_GOP_NUM; i++)
9441  {
9442  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);
9443  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);
9444  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);
9445  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);
9446  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);
9447  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);
9448  for (j = 0; j < NI_MAX_REF_PIC; j++)
9449  {
9450  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);
9451  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);
9452  }
9453  }
9454  }
9455  }
9456 
9457  ni_log2(p_ctx, NI_LOG_DEBUG, "roiEnable=%d\n", p_t408->roiEnable);
9458 
9459  ni_log2(p_ctx, NI_LOG_DEBUG, "numUnitsInTick=%u\n", p_t408->numUnitsInTick);
9460  ni_log2(p_ctx, NI_LOG_DEBUG, "timeScale=%u\n", p_t408->timeScale);
9461  ni_log2(p_ctx, NI_LOG_DEBUG, "numTicksPocDiffOne=%u\n", p_t408->numTicksPocDiffOne);
9462 
9463  ni_log2(p_ctx, NI_LOG_DEBUG, "chromaCbQpOffset=%d\n", p_t408->chromaCbQpOffset);
9464  ni_log2(p_ctx, NI_LOG_DEBUG, "chromaCrQpOffset=%d\n", p_t408->chromaCrQpOffset);
9465 
9466  ni_log2(p_ctx, NI_LOG_DEBUG, "initialRcQp=%d\n", p_t408->initialRcQp);
9467 
9468  ni_log2(p_ctx, NI_LOG_DEBUG, "nrYEnable=%u\n", p_t408->nrYEnable);
9469  ni_log2(p_ctx, NI_LOG_DEBUG, "nrCbEnable=%u\n", p_t408->nrCbEnable);
9470  ni_log2(p_ctx, NI_LOG_DEBUG, "nrCrEnable=%u\n", p_t408->nrCrEnable);
9471 
9472  // ENC_NR_WEIGHT
9473  ni_log2(p_ctx, NI_LOG_DEBUG, "nrIntraWeightY=%u\n", p_t408->nrIntraWeightY);
9474  ni_log2(p_ctx, NI_LOG_DEBUG, "nrIntraWeightCb=%u\n", p_t408->nrIntraWeightCb);
9475  ni_log2(p_ctx, NI_LOG_DEBUG, "nrIntraWeightCr=%u\n", p_t408->nrIntraWeightCr);
9476  ni_log2(p_ctx, NI_LOG_DEBUG, "nrInterWeightY=%u\n", p_t408->nrInterWeightY);
9477  ni_log2(p_ctx, NI_LOG_DEBUG, "nrInterWeightCb=%u\n", p_t408->nrInterWeightCb);
9478  ni_log2(p_ctx, NI_LOG_DEBUG, "nrInterWeightCr=%u\n", p_t408->nrInterWeightCr);
9479 
9480  ni_log2(p_ctx, NI_LOG_DEBUG, "nrNoiseEstEnable=%u\n", p_t408->nrNoiseEstEnable);
9481  ni_log2(p_ctx, NI_LOG_DEBUG, "nrNoiseSigmaY=%u\n", p_t408->nrNoiseSigmaY);
9482  ni_log2(p_ctx, NI_LOG_DEBUG, "nrNoiseSigmaCb=%u\n", p_t408->nrNoiseSigmaCb);
9483  ni_log2(p_ctx, NI_LOG_DEBUG, "nrNoiseSigmaCr=%u\n", p_t408->nrNoiseSigmaCr);
9484 
9485  // newly added for T408
9486  ni_log2(p_ctx, NI_LOG_DEBUG, "monochromeEnable=%u\n", p_t408->monochromeEnable);
9487  ni_log2(p_ctx, NI_LOG_DEBUG, "strongIntraSmoothEnable=%u\n",
9488  p_t408->strongIntraSmoothEnable);
9489 
9490  ni_log2(p_ctx, NI_LOG_DEBUG, "weightPredEnable=%u\n", p_t408->weightPredEnable);
9491  ni_log2(p_ctx, NI_LOG_DEBUG, "bgDetectEnable=%u\n", p_t408->bgDetectEnable);
9492  ni_log2(p_ctx, NI_LOG_DEBUG, "bgThrDiff=%u\n", p_t408->bgThrDiff);
9493  ni_log2(p_ctx, NI_LOG_DEBUG, "bgThrMeanDiff=%u\n", p_t408->bgThrMeanDiff);
9494  ni_log2(p_ctx, NI_LOG_DEBUG, "bgLambdaQp=%u\n", p_t408->bgLambdaQp);
9495  ni_log2(p_ctx, NI_LOG_DEBUG, "bgDeltaQp=%d\n", p_t408->bgDeltaQp);
9496 
9497  ni_log2(p_ctx, NI_LOG_DEBUG, "customLambdaEnable=%u\n", p_t408->customLambdaEnable);
9498  ni_log2(p_ctx, NI_LOG_DEBUG, "customMDEnable=%u\n", p_t408->customMDEnable);
9499  ni_log2(p_ctx, NI_LOG_DEBUG, "pu04DeltaRate=%d\n", p_t408->pu04DeltaRate);
9500  ni_log2(p_ctx, NI_LOG_DEBUG, "pu08DeltaRate=%d\n", p_t408->pu08DeltaRate);
9501  ni_log2(p_ctx, NI_LOG_DEBUG, "pu16DeltaRate=%d\n", p_t408->pu16DeltaRate);
9502  ni_log2(p_ctx, NI_LOG_DEBUG, "pu32DeltaRate=%d\n", p_t408->pu32DeltaRate);
9503  ni_log2(p_ctx, NI_LOG_DEBUG, "pu04IntraPlanarDeltaRate=%d\n", p_t408->pu04IntraPlanarDeltaRate);
9504  ni_log2(p_ctx, NI_LOG_DEBUG, "pu04IntraDcDeltaRate=%d\n", p_t408->pu04IntraDcDeltaRate);
9505  ni_log2(p_ctx, NI_LOG_DEBUG, "pu04IntraAngleDeltaRate=%d\n", p_t408->pu04IntraAngleDeltaRate);
9506  ni_log2(p_ctx, NI_LOG_DEBUG, "pu08IntraPlanarDeltaRate=%d\n", p_t408->pu08IntraPlanarDeltaRate);
9507  ni_log2(p_ctx, NI_LOG_DEBUG, "pu08IntraDcDeltaRate=%d\n", p_t408->pu08IntraDcDeltaRate);
9508  ni_log2(p_ctx, NI_LOG_DEBUG, "pu08IntraAngleDeltaRate=%d\n", p_t408->pu08IntraAngleDeltaRate);
9509  ni_log2(p_ctx, NI_LOG_DEBUG, "pu16IntraPlanarDeltaRate=%d\n", p_t408->pu16IntraPlanarDeltaRate);
9510  ni_log2(p_ctx, NI_LOG_DEBUG, "pu16IntraDcDeltaRate=%d\n", p_t408->pu16IntraDcDeltaRate);
9511  ni_log2(p_ctx, NI_LOG_DEBUG, "pu16IntraAngleDeltaRate=%d\n", p_t408->pu16IntraAngleDeltaRate);
9512  ni_log2(p_ctx, NI_LOG_DEBUG, "pu32IntraPlanarDeltaRate=%d\n", p_t408->pu32IntraPlanarDeltaRate);
9513  ni_log2(p_ctx, NI_LOG_DEBUG, "pu32IntraDcDeltaRate=%d\n", p_t408->pu32IntraDcDeltaRate);
9514  ni_log2(p_ctx, NI_LOG_DEBUG, "pu32IntraAngleDeltaRate=%d\n", p_t408->pu32IntraAngleDeltaRate);
9515  ni_log2(p_ctx, NI_LOG_DEBUG, "cu08IntraDeltaRate=%d\n", p_t408->cu08IntraDeltaRate);
9516  ni_log2(p_ctx, NI_LOG_DEBUG, "cu08InterDeltaRate=%d\n", p_t408->cu08InterDeltaRate);
9517  ni_log2(p_ctx, NI_LOG_DEBUG, "cu08MergeDeltaRate=%d\n", p_t408->cu08MergeDeltaRate);
9518  ni_log2(p_ctx, NI_LOG_DEBUG, "cu16IntraDeltaRate=%d\n", p_t408->cu16IntraDeltaRate);
9519  ni_log2(p_ctx, NI_LOG_DEBUG, "cu16InterDeltaRate=%d\n", p_t408->cu16InterDeltaRate);
9520  ni_log2(p_ctx, NI_LOG_DEBUG, "cu16MergeDeltaRate=%d\n", p_t408->cu16MergeDeltaRate);
9521  ni_log2(p_ctx, NI_LOG_DEBUG, "cu32IntraDeltaRate=%d\n", p_t408->cu32IntraDeltaRate);
9522  ni_log2(p_ctx, NI_LOG_DEBUG, "cu32InterDeltaRate=%d\n", p_t408->cu32InterDeltaRate);
9523  ni_log2(p_ctx, NI_LOG_DEBUG, "cu32MergeDeltaRate=%d\n", p_t408->cu32MergeDeltaRate);
9524  ni_log2(p_ctx, NI_LOG_DEBUG, "coefClearDisable=%d\n", p_t408->coefClearDisable);
9525  ni_log2(p_ctx, NI_LOG_DEBUG, "minQpI=%d\n", p_t408->minQpI);
9526  ni_log2(p_ctx, NI_LOG_DEBUG, "maxQpI=%d\n", p_t408->maxQpI);
9527  ni_log2(p_ctx, NI_LOG_DEBUG, "minQpP=%d\n", p_t408->minQpP);
9528  ni_log2(p_ctx, NI_LOG_DEBUG, "maxQpP=%d\n", p_t408->maxQpP);
9529  ni_log2(p_ctx, NI_LOG_DEBUG, "minQpB=%d\n", p_t408->minQpB);
9530  ni_log2(p_ctx, NI_LOG_DEBUG, "maxQpB=%d\n", p_t408->maxQpB);
9531 
9532  // for H.264 on T408
9533  ni_log2(p_ctx, NI_LOG_DEBUG, "avcIdrPeriod=%d\n", p_t408->avcIdrPeriod);
9534  ni_log2(p_ctx, NI_LOG_DEBUG, "rdoSkip=%d\n", p_t408->rdoSkip);
9535  ni_log2(p_ctx, NI_LOG_DEBUG, "lambdaScalingEnable=%d\n", p_t408->lambdaScalingEnable);
9536  ni_log2(p_ctx, NI_LOG_DEBUG, "enable_transform_8x8=%d\n", p_t408->enable_transform_8x8);
9537  ni_log2(p_ctx, NI_LOG_DEBUG, "slice_mode=%d\n", p_t408->slice_mode);
9538  ni_log2(p_ctx, NI_LOG_DEBUG, "slice_arg=%d\n", p_t408->slice_arg);
9539  ni_log2(p_ctx, NI_LOG_DEBUG, "intra_mb_refresh_mode=%d\n", p_t408->intra_mb_refresh_mode);
9540  ni_log2(p_ctx, NI_LOG_DEBUG, "intra_mb_refresh_arg=%d\n", p_t408->intra_mb_refresh_arg);
9541  ni_log2(p_ctx, NI_LOG_DEBUG, "enable_mb_level_rc=%d\n", p_t408->enable_mb_level_rc);
9542  ni_log2(p_ctx, NI_LOG_DEBUG, "entropy_coding_mode=%d\n", p_t408->entropy_coding_mode);
9543  ni_log2(p_ctx, NI_LOG_DEBUG, "forcedHeaderEnable=%u\n", p_t408->forcedHeaderEnable);
9544 
9545  //QUADRA
9546  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8EnableAUD=%d\n", p_cfg->ui8EnableAUD);
9547  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8LookAheadDepth=%d\n", p_cfg->ui8LookAheadDepth);
9548  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8rdoLevel=%d\n", p_cfg->ui8rdoLevel);
9549  ni_log2(p_ctx, NI_LOG_DEBUG, "i8crf=%d\n", p_cfg->i8crf);
9550  ni_log2(p_ctx, NI_LOG_DEBUG, "i8crfDecimal=%d\n", p_cfg->i8crfDecimal);
9551  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16HDR10MaxLight=%d\n", p_cfg->ui16HDR10MaxLight);
9552  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16HDR10AveLight=%d\n", p_cfg->ui16HDR10AveLight);
9553  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8HDR10CLLEnable=%d\n", p_cfg->ui8HDR10CLLEnable);
9554  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8EnableRdoQuant=%d\n", p_cfg->ui8EnableRdoQuant);
9555  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8ctbRcMode=%d\n", p_cfg->ui8ctbRcMode);
9556  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8gopSize=%d\n", p_cfg->ui8gopSize);
9557  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8useLowDelayPocType=%d\n", p_cfg->ui8useLowDelayPocType);
9558  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8gopLowdelay=%d\n", p_cfg->ui8gopLowdelay);
9559  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16gdrDuration=%d\n", p_cfg->ui16gdrDuration);
9560  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8hrdEnable=%d\n", p_cfg->ui8hrdEnable);
9561  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8colorDescPresent=%d\n", p_cfg->ui8colorDescPresent);
9562  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8colorPrimaries=%d\n", p_cfg->ui8colorPrimaries);
9563  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8colorTrc=%d\n", p_cfg->ui8colorTrc);
9564  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8colorSpace=%d\n", p_cfg->ui8colorSpace);
9565  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16aspectRatioWidth=%d\n", p_cfg->ui16aspectRatioWidth);
9566  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16aspectRatioHeight=%d\n", p_cfg->ui16aspectRatioHeight);
9567  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16rootBufId=%d\n", p_cfg->ui16rootBufId);
9568  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8planarFormat=%d\n", p_cfg->ui8planarFormat);
9569  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8PixelFormat=%d\n", p_cfg->ui8PixelFormat);
9570  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32ltrRefInterval=%u\n", p_cfg->ui32ltrRefInterval);
9571  ni_log2(p_ctx, NI_LOG_DEBUG, "i32ltrRefQpOffset=%d\n", p_cfg->i32ltrRefQpOffset);
9572  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32ltrFirstGap=%u\n", p_cfg->ui32ltrFirstGap);
9573  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32ltrNextInterval=%u\n", p_cfg->ui32ltrNextInterval);
9574  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8multicoreJointMode=%d\n", p_cfg->ui8multicoreJointMode);
9575  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8videoFullRange=%u\n", p_cfg->ui8videoFullRange);
9576  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32QLevel=%u\n", p_cfg->ui32QLevel);
9577  ni_log2(p_ctx, NI_LOG_DEBUG, "i8chromaQpOffset=%d\n", p_cfg->i8chromaQpOffset);
9578  ni_log2(p_ctx, NI_LOG_DEBUG, "i32tolCtbRcInter=0x%x\n", p_cfg->i32tolCtbRcInter);
9579  ni_log2(p_ctx, NI_LOG_DEBUG, "i32tolCtbRcIntra=0x%x\n", p_cfg->i32tolCtbRcIntra);
9580  ni_log2(p_ctx, NI_LOG_DEBUG, "i16bitrateWindow=%d\n", p_cfg->i16bitrateWindow);
9581  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8inLoopDSRatio=%u\n", p_cfg->ui8inLoopDSRatio);
9582  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8blockRCSize=%u\n", p_cfg->ui8blockRCSize);
9583  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8rcQpDeltaRange=%u\n", p_cfg->ui8rcQpDeltaRange);
9584  ni_log2(p_ctx, NI_LOG_DEBUG, "i16ctbRowQpStep=%u\n", p_cfg->i16ctbRowQpStep);
9585  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8NewRCEnable=%u\n", p_cfg->ui8NewRCEnable);
9586  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8LowDelay=%d\n", p_cfg->ui8LowDelay);
9587  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8hdr10_enable=%u\n", p_cfg->ui8hdr10_enable);
9588  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16hdr10_dx0=%u\n", p_cfg->ui16hdr10_dx0);
9589  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16hdr10_dy0=%u\n", p_cfg->ui16hdr10_dy0);
9590  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16hdr10_dx1=%u\n", p_cfg->ui16hdr10_dx1);
9591  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16hdr10_dy1=%u\n", p_cfg->ui16hdr10_dy1);
9592  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16hdr10_dx2=%u\n", p_cfg->ui16hdr10_dx2);
9593  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16hdr10_dy2=%u\n", p_cfg->ui16hdr10_dy2);
9594  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16hdr10_wx=%u\n", p_cfg->ui16hdr10_wx);
9595  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16hdr10_wy=%u\n", p_cfg->ui16hdr10_wy);
9596  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32hdr10_maxluma=%u\n", p_cfg->ui32hdr10_maxluma);
9597  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32hdr10_minluma=%u\n", p_cfg->ui32hdr10_minluma);
9598  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8avccHvcc=%u\n", p_cfg->ui8avccHvcc);
9599  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8av1ErrResilientMode=%u\n", p_cfg->ui8av1ErrResilientMode);
9600  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8intraResetRefresh=%d\n", p_cfg->ui8intraResetRefresh);
9601  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8temporalLayersEnable=%u\n", p_cfg->ui8temporalLayersEnable);
9602  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8enable2PassGopPattern=%u\n", p_cfg->ui8enable2PassGopPatern);
9603  ni_log2(p_ctx, NI_LOG_DEBUG, "zerocopy_mode=%d\n", p_src->zerocopy_mode);
9604  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32lumaLinesize=%u\n", p_cfg->ui32lumaLinesize);
9605  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32chromaLinesize=%u\n", p_cfg->ui32chromaLinesize);
9606  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32cropWidth=%u\n", p_cfg->ui32cropWidth);
9607  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32cropHeight=%u\n", p_cfg->ui32cropHeight);
9608  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32horOffset=%u\n", p_cfg->ui32horOffset);
9609  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32verOffset=%u\n", p_cfg->ui32verOffset);
9610  ni_log2(p_ctx, NI_LOG_DEBUG, "i8crfMax=%d\n", p_cfg->i8crfMax);
9611  ni_log2(p_ctx, NI_LOG_DEBUG, "i32qcomp=%d\n", p_cfg->i32qcomp);
9612  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8noMbtree=%u\n", p_cfg->ui8noMbtree);
9613  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8noHWMultiPassSupport=%u\n", p_cfg->ui8noHWMultiPassSupport);
9614  ni_log2(p_ctx, NI_LOG_DEBUG, "i8cuTreeFactor=%d\n", p_cfg->i8cuTreeFactor);
9615  ni_log2(p_ctx, NI_LOG_DEBUG, "i32ipRatio=%d\n", p_cfg->i32ipRatio);
9616  ni_log2(p_ctx, NI_LOG_DEBUG, "i32pbRatio=%d\n", p_cfg->i32pbRatio);
9617  ni_log2(p_ctx, NI_LOG_DEBUG, "i32cplxDecay=%d\n", p_cfg->i32cplxDecay);
9618  ni_log2(p_ctx, NI_LOG_DEBUG, "i8ppsInitQp=%d\n", p_cfg->i8ppsInitQp);
9619  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8bitrateMode=%u\n", p_cfg->ui8bitrateMode);
9620  ni_log2(p_ctx, NI_LOG_DEBUG, "i8pass1Qp=%d\n", p_cfg->i8pass1Qp);
9621  ni_log2(p_ctx, NI_LOG_DEBUG, "i8crfDecimal=%d\n", p_cfg->i8crfDecimal);
9622  ni_log2(p_ctx, NI_LOG_DEBUG, "i8hvsBaseMbComplexity=%u\n", p_cfg->i8hvsBaseMbComplexity);
9623  ni_log2(p_ctx, NI_LOG_DEBUG, "i8enableipRatio=%d\n",p_cfg->i8enableipRatio);
9624  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16iFrameSizeRatio=%u\n",p_cfg->ui16iFrameSizeRatio);
9625  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8crfMaxIframeEnable=%u\n", p_cfg->ui8crfMaxIframeEnable);
9626  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32vbvMinRate=%u\n", p_cfg->ui32vbvMinRate);
9627  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8disableBframeRDOQ=%u\n", p_cfg->ui8disableBframeRDOQ);
9628  ni_log2(p_ctx, NI_LOG_DEBUG, "i32forceBframeQpFactor=%d\n", p_cfg->i32forceBframeQpFactor);
9629  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8tuneBframeVisual=%u\n", p_cfg->ui8tuneBframeVisual);
9630  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8EnableAcqLimit=%u\n", p_cfg->ui8EnableAcqLimit);
9631  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8compressor=%u\n", p_cfg->ui8compressor);
9632  ni_log2(p_ctx, NI_LOG_DEBUG, "u8customizeRoiQpLevel=%u\n", p_cfg->u8customizeRoiQpLevel);
9633  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8motionConstrainedMode=%d\n", p_cfg->ui8motionConstrainedMode);
9634  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8mallocStrategy=%d\n", p_cfg->ui8mallocStrategy);
9635  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8spatialLayersMinusOne=%d\n", p_cfg->ui8spatialLayersMinusOne);
9636  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8enableTimecode=%d\n", p_cfg->ui8enableTimecode);
9637  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8spatialLayersRefBaseLayer=%d\n", p_cfg->ui8spatialLayersRefBaseLayer);
9638  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8vbvBufferReencode=%d\n", p_cfg->ui8vbvBufferReencode);
9639 }
9640 
9641 /*!******************************************************************************
9642  * \brief Setup and initialize all xcoder configuration to default (Rev. B)
9643  *
9644  * \param
9645  *
9646  * \return
9647  ******************************************************************************/
9649 {
9650  int i = 0;
9651 
9652  if( (!p_ctx) || (!p_config) )
9653  {
9654  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Null pointer parameters passed\n",
9655  __func__);
9656  return;
9657  }
9658 
9659  memset(p_config, 0, sizeof(ni_encoder_config_t));
9660 
9661  // fill in common attributes values
9662  p_config->i32picWidth = 720;
9663  p_config->i32picHeight = 480;
9664  p_config->i32meBlkMode = 0; // (AVC ONLY) 0 means use all possible block partitions
9665  p_config->ui8sliceMode = 0; // 0 means 1 slice per picture
9666  p_config->i32frameRateInfo = 30;
9667  p_config->i32frameRateDenominator = 1;
9668  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
9669  p_config->i32userQpMax = 51; // this should also be h264-only parameter
9670 
9671  // AVC only
9672  if (NI_CODEC_FORMAT_H264 == p_ctx->codec_format)
9673  {
9674  p_config->i32maxIntraSize = 8000000; // how big an intra p_frame can get?
9675  p_config->i32userMaxDeltaQp = 51;
9676  p_config->i32userMinDeltaQp = 51;
9677  p_config->i32userQpMin = 8;
9678  }
9679 
9680  p_config->i32bitRate = 0;
9681  p_config->i32bitRateBL = 0;
9682  p_config->ui8rcEnable = 0;
9683  p_config->i32srcBitDepth = p_ctx->src_bit_depth;
9684  p_config->ui8enablePTS = 0;
9685  p_config->ui8lowLatencyMode = 0;
9686 
9687  // profiles for H.264: 1 = baseline, 2 = main, 3 = extended, 4 = high
9688  // 5 = high10 (default 8 bit: 4, 10 bit: 5)
9689  // profiles for HEVC: 1 = main, 2 = main10 (default 8 bit: 1, 10 bit: 2)
9690 
9691  // bitstream type: H.264 or HEVC
9692  if (NI_CODEC_FORMAT_H264 == p_ctx->codec_format)
9693  {
9694  p_config->ui8bitstreamFormat = STD_AVC;
9695 
9696  p_config->niParamT408.profile = 4;
9697  if (10 == p_ctx->src_bit_depth)
9698  {
9699  p_config->niParamT408.profile = 5;
9700  }
9701  } else if (NI_CODEC_FORMAT_JPEG == p_ctx->codec_format)
9702  {
9703  p_config->ui8bitstreamFormat = STD_JPEG;
9704  } else if (NI_CODEC_FORMAT_AV1 == p_ctx->codec_format)
9705  {
9706  p_config->ui8bitstreamFormat = STD_AV1;
9707 
9708  p_config->niParamT408.profile = 1;
9709  } else
9710  {
9712 
9713  p_config->ui8bitstreamFormat = STD_HEVC;
9714 
9715  p_config->niParamT408.profile = 1;
9716  if (10 == p_ctx->src_bit_depth)
9717  {
9718  p_config->niParamT408.profile = 2;
9719  }
9720  }
9721 
9722  p_config->hdrEnableVUI = 0;
9723  p_config->ui32sourceEndian = p_ctx->src_endian;
9724 
9725  p_config->niParamT408.level = 0;
9726  p_config->niParamT408.tier = 0; // 0 means main tier
9727 
9728  p_config->niParamT408.internalBitDepth = p_ctx->src_bit_depth;
9729  p_config->niParamT408.losslessEnable = 0;
9730  p_config->niParamT408.constIntraPredFlag = 0;
9731 
9732  if (QUADRA)
9734  else
9736 
9737  p_config->niParamT408.decoding_refresh_type = 1;
9738  p_config->niParamT408.intra_qp = 22;
9739  // avcIdrPeriod (H.264 on T408), NOT shared with intra_period
9740  p_config->niParamT408.intra_period = 120;
9741  p_config->niParamT408.avcIdrPeriod = 120;
9742 
9743  p_config->niParamT408.conf_win_top = 0;
9744  p_config->niParamT408.conf_win_bottom = 0;
9745  p_config->niParamT408.conf_win_left = 0;
9746  p_config->niParamT408.conf_win_right = 0;
9747 
9748  p_config->niParamT408.independSliceMode = 0;
9749  p_config->niParamT408.independSliceModeArg = 0;
9750  p_config->niParamT408.dependSliceMode = 0;
9751  p_config->niParamT408.dependSliceModeArg = 0;
9752  p_config->niParamT408.intraRefreshMode = 0;
9753  p_config->niParamT408.intraRefreshArg = 0;
9754 
9755  p_config->niParamT408.use_recommend_enc_params = 0; //1;
9756  p_config->niParamT408.scalingListEnable = 0;
9757 
9758  p_config->niParamT408.cu_size_mode = 7;
9759  p_config->niParamT408.tmvpEnable = 1;
9760  p_config->niParamT408.wppEnable = 0;
9761  p_config->niParamT408.max_num_merge = 2;
9762  p_config->niParamT408.disableDeblk = 0;
9764  p_config->niParamT408.betaOffsetDiv2 = 0;
9765  p_config->niParamT408.tcOffsetDiv2 = 0;
9766  p_config->niParamT408.skipIntraTrans = 1;
9767  p_config->niParamT408.saoEnable = 1;
9768  p_config->niParamT408.intraNxNEnable = 1;
9769 
9770  p_config->niParamT408.bitAllocMode = 0;
9771 
9772  for (i = 0; i < NI_MAX_GOP_NUM; i++)
9773  {
9774  p_config->niParamT408.fixedBitRatio[i] = 1;
9775  }
9776 
9777  if (QUADRA)
9779  else
9781 
9782  p_config->niParamT408.enable_hvs_qp = 0;
9783  p_config->niParamT408.hvs_qp_scale = 2;
9784 
9785  p_config->niParamT408.max_delta_qp = 10;
9786 
9787  // CUSTOM_GOP
9789 #ifndef QUADRA
9790  if (!QUADRA)
9791  {
9792  for (i = 0; i < p_config->niParamT408.custom_gop_params.custom_gop_size; i++)
9793  {
9796  p_config->niParamT408.custom_gop_params.pic_param[i].pic_qp = 0;
9797  p_config->niParamT408.custom_gop_params.pic_param[i].num_ref_pic_L0 = 0;
9798  p_config->niParamT408.custom_gop_params.pic_param[i].ref_poc_L0 = 0;
9799  p_config->niParamT408.custom_gop_params.pic_param[i].ref_poc_L1 = 0;
9801  }
9802  }
9803  else // QUADRA
9804 #endif
9805  {
9806  int j;
9807  for (i = 0; i < NI_MAX_GOP_NUM; i++)
9808  {
9812  (float)0.3; // QP Factor range is between 0.3 and 1, higher values mean lower quality and less bits
9816  for (j = 0; j < NI_MAX_REF_PIC; j++)
9817  {
9818  p_config->niParamT408.custom_gop_params.pic_param[i].rps[j].ref_pic = 0;
9820  }
9821  }
9822  }
9823 
9824  p_config->niParamT408.roiEnable = 0;
9825 
9826  p_config->niParamT408.numUnitsInTick = 1000;
9827  p_config->niParamT408.timeScale = p_config->i32frameRateInfo * 1000;
9828  if (NI_CODEC_FORMAT_H264 == p_ctx->codec_format)
9829  {
9830  p_config->niParamT408.timeScale *= 2;
9831  }
9832 
9833  p_config->niParamT408.numTicksPocDiffOne = 0;
9834 
9835  p_config->niParamT408.chromaCbQpOffset = 0;
9836  p_config->niParamT408.chromaCrQpOffset = 0;
9837 
9838  p_config->niParamT408.initialRcQp = 63; //-1;
9839 
9840  p_config->niParamT408.nrYEnable = 0;
9841  p_config->niParamT408.nrCbEnable = 0;
9842  p_config->niParamT408.nrCrEnable = 0;
9843 
9844  // ENC_NR_WEIGHT
9845  p_config->niParamT408.nrIntraWeightY = 7;
9846  p_config->niParamT408.nrIntraWeightCb = 7;
9847  p_config->niParamT408.nrIntraWeightCr = 7;
9848  p_config->niParamT408.nrInterWeightY = 4;
9849  p_config->niParamT408.nrInterWeightCb = 4;
9850  p_config->niParamT408.nrInterWeightCr = 4;
9851 
9852  p_config->niParamT408.nrNoiseEstEnable = 0;
9853  p_config->niParamT408.nrNoiseSigmaY = 0;
9854  p_config->niParamT408.nrNoiseSigmaCb = 0;
9855  p_config->niParamT408.nrNoiseSigmaCr = 0;
9856 
9857  p_config->niParamT408.useLongTerm = 0;
9858 
9859  // newly added for T408
9860  p_config->niParamT408.monochromeEnable = 0;
9861  p_config->niParamT408.strongIntraSmoothEnable = 1;
9862 
9863  p_config->niParamT408.weightPredEnable = 0;
9864  p_config->niParamT408.bgDetectEnable = 0;
9865  p_config->niParamT408.bgThrDiff = 8; // matching the C-model
9866  p_config->niParamT408.bgThrMeanDiff = 1; // matching the C-model
9867  p_config->niParamT408.bgLambdaQp = 32; // matching the C-model
9868  p_config->niParamT408.bgDeltaQp = 3; // matching the C-model
9869 
9870  p_config->niParamT408.customLambdaEnable = 0;
9871  p_config->niParamT408.customMDEnable = 0;
9872  p_config->niParamT408.pu04DeltaRate = 0;
9873  p_config->niParamT408.pu08DeltaRate = 0;
9874  p_config->niParamT408.pu16DeltaRate = 0;
9875  p_config->niParamT408.pu32DeltaRate = 0;
9876  p_config->niParamT408.pu04IntraPlanarDeltaRate = 0;
9877  p_config->niParamT408.pu04IntraDcDeltaRate = 0;
9878  p_config->niParamT408.pu04IntraAngleDeltaRate = 0;
9879  p_config->niParamT408.pu08IntraPlanarDeltaRate = 0;
9880  p_config->niParamT408.pu08IntraDcDeltaRate = 0;
9881  p_config->niParamT408.pu08IntraAngleDeltaRate = 0;
9882  p_config->niParamT408.pu16IntraPlanarDeltaRate = 0;
9883  p_config->niParamT408.pu16IntraDcDeltaRate = 0;
9884  p_config->niParamT408.pu16IntraAngleDeltaRate = 0;
9885  p_config->niParamT408.pu32IntraPlanarDeltaRate = 0;
9886  p_config->niParamT408.pu32IntraDcDeltaRate = 0;
9887  p_config->niParamT408.pu32IntraAngleDeltaRate = 0;
9888  p_config->niParamT408.cu08IntraDeltaRate = 0;
9889  p_config->niParamT408.cu08InterDeltaRate = 0;
9890  p_config->niParamT408.cu08MergeDeltaRate = 0;
9891  p_config->niParamT408.cu16IntraDeltaRate = 0;
9892  p_config->niParamT408.cu16InterDeltaRate = 0;
9893  p_config->niParamT408.cu16MergeDeltaRate = 0;
9894  p_config->niParamT408.cu32IntraDeltaRate = 0;
9895  p_config->niParamT408.cu32InterDeltaRate = 0;
9896  p_config->niParamT408.cu32MergeDeltaRate = 0;
9897  p_config->niParamT408.coefClearDisable = 0;
9898  p_config->niParamT408.minQpI = 8;
9899  p_config->niParamT408.maxQpI = 51;
9900  p_config->niParamT408.minQpP = 8;
9901  p_config->niParamT408.maxQpP = 51;
9902  p_config->niParamT408.minQpB = 8;
9903  p_config->niParamT408.maxQpB = 51;
9904 
9905  // for H.264 on T408
9906  p_config->niParamT408.rdoSkip = 0;
9907  p_config->niParamT408.lambdaScalingEnable = 0;
9908  p_config->niParamT408.enable_transform_8x8 = 1;
9909  p_config->niParamT408.slice_mode = 0;
9910  p_config->niParamT408.slice_arg = 0;
9911  p_config->niParamT408.intra_mb_refresh_mode = 0;
9912  p_config->niParamT408.intra_mb_refresh_arg = 0;
9913  if (QUADRA)
9914  p_config->niParamT408.enable_mb_level_rc = 0;
9915  else
9916  p_config->niParamT408.enable_mb_level_rc = 1;
9917  p_config->niParamT408.entropy_coding_mode = 1; // 1 means CABAC, make sure profile is main or above, can't have CABAC in baseline
9918  p_config->niParamT408.forcedHeaderEnable = 0; // first IDR frame
9919 
9920  //QUADRA
9921  p_config->ui8EnableAUD = 0;
9922  p_config->ui8LookAheadDepth = 0;
9923  p_config->ui8rdoLevel = (NI_CODEC_FORMAT_JPEG == p_ctx->codec_format) ? 0 : 1;
9924  p_config->i8crf = -1;
9925  p_config->ui16HDR10MaxLight = 0;
9926  p_config->ui16HDR10AveLight = 0;
9927  p_config->ui8HDR10CLLEnable = 0;
9928  p_config->ui8EnableRdoQuant = 0;
9929  p_config->ui8ctbRcMode = 0;
9930  p_config->ui8gopSize = 0;
9931  p_config->ui8useLowDelayPocType = 0;
9932  p_config->ui8gopLowdelay = 0;
9933  p_config->ui16gdrDuration = 0;
9934  p_config->ui8hrdEnable = 0;
9935  p_config->ui8colorDescPresent = 0;
9937  p_config->ui8colorPrimaries = 2;
9938  //AVCOL_TRC_UNSPECIFIED
9939  p_config->ui8colorTrc = 2;
9940  //AVCOL_SPC_UNSPECIFIED
9941  p_config->ui8colorSpace = 2;
9942  p_config->ui16aspectRatioWidth = 0;
9943  p_config->ui16aspectRatioHeight = 0;
9945  p_config->ui8PixelFormat = NI_PIX_FMT_YUV420P;
9946  p_config->ui32ltrRefInterval = 0;
9947  p_config->i32ltrRefQpOffset = 0;
9948  p_config->ui32ltrFirstGap= 0;
9949  p_config->ui32ltrNextInterval = 1;
9950  p_config->ui8multicoreJointMode = 0;
9951  p_config->ui8videoFullRange = 0;
9952  p_config->ui32setLongTermInterval = 0;
9953  p_config->ui8setLongTermCount = 2;
9954  p_config->ui32QLevel = -1;
9955  p_config->i8chromaQpOffset = 0;
9956  p_config->i32tolCtbRcInter = (int32_t)(0.1 * 1000);
9957  p_config->i32tolCtbRcIntra = (int32_t)(0.1 * 1000);
9958  p_config->i16bitrateWindow = -255;
9959  p_config->ui8inLoopDSRatio = 1;
9960  p_config->ui8blockRCSize = 0;
9961  p_config->ui8rcQpDeltaRange = 10;
9962  p_config->i16ctbRowQpStep = 0;
9963  p_config->ui8NewRCEnable = 255;
9964  p_config->ui8LowDelay = 0;
9965  p_config->ui8fixedframerate = 1;
9966  p_config->ui8enableSSIM = 0;
9967  p_config->ui8hdr10_enable = 0;
9968  p_config->ui16hdr10_dx0 = 0;
9969  p_config->ui16hdr10_dy0 = 0;
9970  p_config->ui16hdr10_dx1 = 0;
9971  p_config->ui16hdr10_dy1 = 0;
9972  p_config->ui16hdr10_dx2 = 0;
9973  p_config->ui16hdr10_dy2 = 0;
9974  p_config->ui16hdr10_wx = 0;
9975  p_config->ui16hdr10_wy = 0;
9976  p_config->ui32hdr10_maxluma = 0;
9977  p_config->ui32hdr10_minluma = 0;
9978  p_config->ui8avccHvcc = 0;
9979  p_config->ui8av1ErrResilientMode = 0;
9980  p_config->ui8intraResetRefresh = 0;
9981  p_config->ui8temporalLayersEnable = 0;
9982  p_config->ui8enable2PassGopPatern = 0;
9983  p_config->i8crfMax = (int8_t)(-1) + 1; // for old libxcoder backward compatibility, use 1 to represent 0
9984  p_config->i32qcomp = (int32_t)(0.6 * 1000) + 1000; // for old libxcoder backward compatibility, use 1000 to represent 0
9985  p_config->ui8noMbtree = 0;
9986  p_config->ui8noHWMultiPassSupport = 0;
9987  p_config->i8cuTreeFactor = 5;
9988  p_config->i32ipRatio = (int32_t)(1.4 * 1000) + 1000; // for old libxcoder backward compatibility, use 1000 to represent 0
9989  p_config->i32pbRatio = (int32_t)(1.3 * 1000) + 1000; // for old libxcoder backward compatibility, use 1000 to represent 0
9990  p_config->i32cplxDecay = (int32_t)(0.5 * 1000) + 1000; // for old libxcoder backward compatibility, use 1000 to represent 0
9991  p_config->ui32vbvMaxRate = 0;
9992  p_config->i8ppsInitQp = (int8_t)(-1) + 1; // for old libxcoder backward compatibility, use 1 to represent 0
9993  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)
9994  p_config->i8pass1Qp = (int8_t)(-1) + 1; // for old libxcoder backward compatibility, use 1 to represent 0
9995  p_config->i8crfDecimal = 0;
9996  p_config->i8hvsBaseMbComplexity = (int8_t)(15)-15; // for old libxcoder backward compatibility, use -15 to represent 0
9997  p_config->i8statisticOutputLevel = 0;
9998  p_config->i8enableipRatio = 0;
9999  p_config->ui16iFrameSizeRatio = 100;
10000  p_config->ui8compressor = 3;
10001  p_config->u8skipFrameInterval = 0;
10002  p_config->ui8crfMaxIframeEnable = 0;
10003  p_config->ui32vbvMinRate = 0;
10004  p_config->ui8disableBframeRDOQ = 0;
10005  p_config->i32forceBframeQpFactor = (int32_t)(-1.0 * 1000) + 1000; // for old libxcoder backward compatibility, use 1000 to represent 0
10006  p_config->ui8tuneBframeVisual = 0;
10007  p_config->ui8EnableAcqLimit = 0;
10008  p_config->u8customizeRoiQpLevel = 0;
10009  p_config->ui32cropWidth = 0;
10010  p_config->ui32cropHeight = 0;
10011  p_config->ui32horOffset = 0;
10012  p_config->ui32verOffset = 0;
10013  p_config->ui8motionConstrainedMode = 0;
10014  p_config->ui8stillImageDetectLevel = 0;
10015  p_config->ui8sceneChangeDetectLevel = 0;
10016  p_config->ui8mallocStrategy = 0;
10017  p_config->ui8spatialLayersMinusOne = 0;
10018  p_config->ui8enableTimecode = 0;
10019  p_config->ui8spatialLayersRefBaseLayer = 0;
10020  p_config->ui8vbvBufferReencode = 0;
10021 }
10022 
10023 /*!******************************************************************************
10024 * \brief Perform validation on custom dec parameters (Rev. B)
10025 *
10026 * \param
10027 *
10028 * \return
10029 ******************************************************************************/
10031  ni_session_context_t *p_ctx,
10032  ni_decoder_config_t *p_cfg,
10033  char *p_param_err,
10034  uint32_t max_err_len)
10035 {
10036  ni_retcode_t param_ret = NI_RETCODE_SUCCESS;
10037  ni_retcode_t warning = NI_RETCODE_SUCCESS;
10038  int w = p_src->source_width;
10039  int h = p_src->source_height;
10040  char *p_param_warn = NULL;
10041  int i;
10042 
10043  if (!p_ctx || !p_cfg || !p_param_err)
10044  {
10045  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Null pointer parameters passed\n",
10046  __func__);
10047  param_ret = NI_RETCODE_INVALID_PARAM;
10048  LRETURN;
10049  }
10050 
10051  // Zero out the error buffer
10052  p_param_warn = malloc(sizeof(p_ctx->param_err_msg));
10053  if (!p_param_warn)
10054  {
10055  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() p_param_warn malloc failure\n",
10056  NI_ERRNO, __func__);
10057  param_ret = NI_RETCODE_ERROR_MEM_ALOC;
10058  LRETURN;
10059  }
10060 
10061  memset(p_param_err, 0, max_err_len);
10062  memset(p_param_warn, 0, max_err_len);
10063 
10064 
10065  //if (p_cfg->i32bitRate > NI_MAX_BITRATE)
10066  //{
10067  // strncpy(p_param_err, "Invalid i32bitRate: too big", max_err_len);
10068  // param_ret = NI_RETCODE_PARAM_ERROR_BRATE;
10069  // LRETURN;
10070  //}
10071 
10072  if (p_cfg->ui8HWFrame == 0 &&
10073  (p_cfg->asOutputConfig[1].ui8Enabled || p_cfg->asOutputConfig[2].ui8Enabled))
10074  {
10075  strncpy(p_param_err, "Incompatible output format: hw frame must be used if out1 or out2 used", max_err_len);
10076  param_ret = NI_RETCODE_INVALID_PARAM;
10077  LRETURN;
10078  }
10079 
10080  if (p_src->ddr_priority_mode >= 0)
10081  {
10083  "6e") < 0)
10084  {
10085  strncpy(p_param_err, "ddr_priority_mode not supported on device with FW api version < 6.e",
10086  max_err_len);
10088  LRETURN;
10089  }
10090  }
10091 
10092  for (i = 0; i < NI_MAX_NUM_OF_DECODER_OUTPUTS; i++)
10093  {
10094  //checking cropping param
10096  {
10099  {
10100  strncpy(p_param_err, "Invalid crop offset: extends past 48x48 minimum window", max_err_len);
10101  param_ret = NI_RETCODE_PARAM_ERROR_OOR;
10102  LRETURN;
10103  }
10106  {
10107  strncpy(p_param_err, "Invalid crop w or h: must be at least 48x48 minimum window", max_err_len);
10109  LRETURN;
10110  }
10111  if (p_cfg->asOutputConfig[i].sCroppingRectable.ui16W > w ||
10112  p_cfg->asOutputConfig[i].sCroppingRectable.ui16H > h)
10113  {
10114  strncpy(p_param_err, "Invalid crop w or h: must be smaller than input", max_err_len);
10115  param_ret = NI_RETCODE_PARAM_ERROR_OOR;
10116  LRETURN;
10117  }
10120  {
10121  strncpy(p_param_err, "Invalid crop rect: must fit in input", max_err_len);
10122  param_ret = NI_RETCODE_PARAM_ERROR_OOR;
10123  LRETURN;
10124  }
10125  if (p_cfg->asOutputConfig[i].sCroppingRectable.ui16X & 1 ||
10126  p_cfg->asOutputConfig[i].sCroppingRectable.ui16Y & 1 ||
10127  p_cfg->asOutputConfig[i].sCroppingRectable.ui16W & 1 ||
10128  p_cfg->asOutputConfig[i].sCroppingRectable.ui16H & 1)
10129  {
10130  strncpy(p_param_err, "Invalid crop value: even values only", max_err_len);
10131  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
10132  LRETURN;
10133  }
10134  if (p_cfg->asOutputConfig[i].ui8Enabled == 0)
10135  {
10136  strncpy(p_param_warn, "crop param used but output not enabled!", max_err_len);
10137  warning = NI_RETCODE_PARAM_WARN;
10138  }
10139 
10140  }
10141 
10142  //checking scaling param
10143  if (p_cfg->asOutputConfig[i].ui8ScaleEnabled)
10144  {
10145  if (p_cfg->asOutputConfig[i].sOutputPictureSize.ui16Height == 0 ||
10147  {
10148  if (p_cfg->asOutputConfig[i].ui8EnablePpuScaleAdapt)
10149  {
10150  //may need to revisit if based on cropped input or base input for w/h limit
10151  strncpy(p_param_err, "Invalid scale dimensions: zero", max_err_len);
10153  LRETURN;
10154  }
10155  }
10156  if (p_cfg->asOutputConfig[i].sOutputPictureSize.ui16Height & 1 ||
10158  {
10159  strncpy(p_param_err, "Invalid scale value: even values only", max_err_len);
10160  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
10161  LRETURN;
10162  }
10163 
10164  if (p_cfg->asOutputConfig[i].ui8EnablePpuScaleLimit == 1)
10165  {
10166  if (p_cfg->asOutputConfig[i].sOutputPictureSize.ui16Width > w ||
10168  {
10169  strncpy(p_param_err, "Invalid scale value: downscale only", max_err_len);
10170  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
10171  LRETURN;
10172  }
10173  }
10174 
10176  {
10177  //reject if scale dimensions exceed crop dimensions
10180  {
10181  strncpy(p_param_err, "Invalid scale dimensions: downscale only after cropping", max_err_len);
10182  param_ret = NI_RETCODE_PARAM_ERROR_OOR;
10183  LRETURN;
10184  }
10185  }
10186  if (p_cfg->asOutputConfig[i].ui8Enabled == 0)
10187  {
10188  strncpy(p_param_warn, "scale param used but output not enabled!", max_err_len);
10189  warning = NI_RETCODE_PARAM_WARN;
10190  }
10191  }
10192  if (p_cfg->asOutputConfig[i].ui8Enabled == 0)
10193  {
10195  {
10196  strncpy(p_param_warn, "force8bit or semiPlanar used but output not enabled!", max_err_len);
10197  warning = NI_RETCODE_PARAM_WARN;
10198  }
10199  }
10200 
10201  //check tiled format compatibility
10203  {
10204  if (p_cfg->asOutputConfig[i].ui8Enabled == 0)
10205  {
10206  continue;
10207  }
10208  if (p_cfg->ui8HWFrame == 0)
10209  {
10210  strncpy(p_param_err,
10211  "Invalid pairing: out=HW must be set with tiled format",
10212  max_err_len);
10213  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
10214  LRETURN;
10215  }
10216  if (p_ctx->codec_format == NI_CODEC_FORMAT_VP9)
10217  {
10218  strncpy(p_param_err, "Invalid pairing: VP9 not compatible with tiled format",
10219  max_err_len);
10220  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
10221  LRETURN;
10222  }
10223  if (p_cfg->asOutputConfig[i].ui8ScaleEnabled)
10224  {
10225  //only no need to check crop compat if scale is set
10226  if (p_cfg->asOutputConfig[i].sOutputPictureSize.ui16Height % 4 ||
10228  {
10229  strncpy(p_param_err,
10230  "Invalid scale height: mult of 4 only, >= 128",
10231  max_err_len);
10232  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
10233  LRETURN;
10234  }
10235  if (p_cfg->asOutputConfig[i].sOutputPictureSize.ui16Width % 4 ||
10237  {
10238  //minimum supported dec is 128 but min enc is 144 so round up
10239  strncpy(p_param_err,
10240  "Invalid scale width: mult of 128 only, >= 144",
10241  max_err_len);
10242  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
10243  LRETURN;
10244  }
10245  }
10246  else if (p_cfg->asOutputConfig[i].ui8CropMode == NI_DEC_CROP_MODE_MANUAL)
10247  {
10248  if (p_cfg->asOutputConfig[i].sCroppingRectable.ui16H % 4 ||
10249  p_cfg->asOutputConfig[i].sCroppingRectable.ui16H < 128)
10250  {
10251  strncpy(p_param_err,
10252  "Invalid crop height: mult of 4 only, >= 128",
10253  max_err_len);
10254  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
10255  LRETURN;
10256  }
10257  if (p_cfg->asOutputConfig[i].sCroppingRectable.ui16W % 4 ||
10259  {
10260  //minimum supported dec is 128 but min enc is 144 so round up
10261  strncpy(p_param_err,
10262  "Invalid crop width: mult of 128 only, >= 144",
10263  max_err_len);
10264  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
10265  LRETURN;
10266  }
10267  }
10268  if (p_cfg->asOutputConfig[i].ui8Force8Bit)
10269  {
10270  strncpy(p_param_err, "Force 8 bit: not supported with tiled format\n",
10271  max_err_len);
10272  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
10273  LRETURN;
10274  }
10275  }
10276 
10277  }//end forloop
10278  if (warning == NI_RETCODE_PARAM_WARN && param_ret == NI_RETCODE_SUCCESS)
10279  {
10280  param_ret = NI_RETCODE_PARAM_WARN;
10281  strncpy(p_param_err, p_param_warn, max_err_len);
10282  }
10283 
10284 END:
10285 
10286  free(p_param_warn);
10287  return param_ret;
10288 }
10289 
10290 // Check encoder level parameters
10291 static ni_retcode_t ni_check_level(int level, int codec_id)
10292 {
10293  const int l_levels_264[] = {10, 11, 12, 13, 20, 21, 22, 30, 31, 32,
10294  40, 41, 42, 50, 51, 52, 60, 61, 62, 0};
10295  const int l_levels_265[] = {10, 20, 21, 30, 31, 40, 41,
10296  50, 51, 52, 60, 61, 62, 0};
10297  const int l_levels_av1[] = {20, 21, 30, 31, 40, 41, 50, 51, 0};
10298  const int *l_levels = l_levels_264;
10299 
10300  if (level == 0)
10301  {
10302  return NI_RETCODE_SUCCESS;
10303  }
10304 
10305  if (codec_id == NI_CODEC_FORMAT_H265)
10306  {
10307  l_levels = l_levels_265;
10308  } else if (codec_id == NI_CODEC_FORMAT_AV1)
10309  {
10310  l_levels = l_levels_av1;
10311  }
10312 
10313  while (*l_levels != 0)
10314  {
10315  if (*l_levels == level)
10316  {
10317  return NI_RETCODE_SUCCESS;
10318  }
10319  l_levels++;
10320  }
10321 
10322  return NI_RETCODE_FAILURE;
10323 }
10324 
10325 /*!******************************************************************************
10326  * \brief Perform validation on custom parameters (Rev. B)
10327  *
10328  * \param
10329  *
10330  * \return
10331  ******************************************************************************/
10333  ni_encoder_config_t *p_cfg,
10334  ni_xcoder_params_t *p_src,
10335  char *p_param_err,
10336  uint32_t max_err_len)
10337 {
10338  ni_retcode_t param_ret = NI_RETCODE_SUCCESS;
10339  ni_retcode_t warning = NI_RETCODE_SUCCESS;
10340  char* p_param_warn = malloc(sizeof(p_ctx->param_err_msg));
10341  ni_encoder_cfg_params_t *p_enc;
10342  int i;
10343 
10344  if( (!p_ctx) || (!p_cfg) || (!p_src) || (!p_param_err) )
10345  {
10346  ni_log(NI_LOG_ERROR, "ERROR: %s() Null pointer parameters passed\n",
10347  __func__);
10348  param_ret = NI_RETCODE_INVALID_PARAM;
10349  LRETURN;
10350  }
10351 
10352  //Zero out the error buffer
10353  p_enc = &p_src->cfg_enc_params;
10354  memset(p_param_err, 0, max_err_len);
10355  memset(p_param_warn, 0, max_err_len);
10356 
10357  if (0 == p_cfg->i32frameRateInfo)
10358  {
10359  strncpy(p_param_err, "Invalid frame_rate of 0 value", max_err_len);
10360  param_ret = NI_RETCODE_PARAM_ERROR_FRATE;
10361  LRETURN;
10362  }
10363 
10364  if (((p_cfg->i32frameRateInfo + p_cfg->i32frameRateDenominator - 1) /
10366  {
10367  strncpy(p_param_err, "Invalid i32frameRateInfo: too big", max_err_len);
10368  param_ret = NI_RETCODE_PARAM_ERROR_FRATE;
10369  LRETURN;
10370  }
10371 
10372  if (p_cfg->i32bitRate <= p_cfg->i32frameRateInfo)
10373  {
10374  strncpy(p_param_err, "Invalid i32bitRate: smaller than or equal to frame rate", max_err_len);
10375  param_ret = NI_RETCODE_PARAM_ERROR_BRATE;
10376  LRETURN;
10377  }
10378 
10379  if (p_cfg->i32bitRate > NI_MAX_BITRATE)
10380  {
10381  strncpy(p_param_err, "Invalid i32bitRate: too big", max_err_len);
10382  param_ret = NI_RETCODE_PARAM_ERROR_BRATE;
10383  LRETURN;
10384  }
10385 
10386  if (p_cfg->i32bitRate < NI_MIN_BITRATE )
10387  {
10388  strncpy(p_param_err, "Invalid i32bitRate: too low", max_err_len);
10389  param_ret = NI_RETCODE_PARAM_ERROR_BRATE;
10390  LRETURN;
10391  }
10392 
10394  {
10395  strncpy(p_param_err, "Invalid Picture Width: too small", max_err_len);
10397  LRETURN;
10398  }
10399 
10401  {
10402  strncpy(p_param_err, "Invalid Picture Width: too big", max_err_len);
10404  LRETURN;
10405  }
10406 
10408  {
10409  strncpy(p_param_err, "Invalid Picture Height: too small", max_err_len);
10411  LRETURN;
10412  }
10413 
10415  {
10416  strncpy(p_param_err, "Invalid Picture Height: too big", max_err_len);
10418  LRETURN;
10419  }
10420 
10421  if (p_cfg->ui32cropWidth || p_cfg->ui32cropHeight)
10422  {
10423  // check cropping width & height are both non-zero
10424  if (!p_cfg->ui32cropWidth || !p_cfg->ui32cropHeight)
10425  {
10426  snprintf(p_param_err, max_err_len, "Invalid Crop Width x Height (%u x %u): both need to be specified",
10427  p_cfg->ui32cropWidth, p_cfg->ui32cropHeight);
10428  param_ret = NI_RETCODE_INVALID_PARAM;
10429  LRETURN;
10430  }
10431  // check cropping width & height are even
10432  if ((p_cfg->ui32cropWidth % 2) || (p_cfg->ui32cropHeight % 2))
10433  {
10434  snprintf(p_param_err, max_err_len, "Invalid Crop Width x Height (%u x %uu): must be even",
10435  p_cfg->ui32cropWidth, p_cfg->ui32cropHeight);
10436  param_ret = NI_RETCODE_INVALID_PARAM;
10437  LRETURN;
10438  }
10439  // check cropping width & height meet resoultion constraint (including AV1 max resoultion)
10440  if (p_cfg->ui32cropWidth < NI_MIN_WIDTH)
10441  {
10442  snprintf(p_param_err, max_err_len, "Invalid Crop Width: less than %d",
10443  NI_MIN_WIDTH);
10445  LRETURN;
10446  }
10447  if (p_cfg->ui32cropHeight < NI_MIN_HEIGHT)
10448  {
10449  snprintf(p_param_err, max_err_len, "Invalid Crop Height: less than %d",
10450  NI_MIN_HEIGHT);
10452  LRETURN;
10453  }
10454  if (p_cfg->ui32cropWidth > NI_PARAM_MAX_WIDTH)
10455  {
10456  snprintf(p_param_err, max_err_len, "Invalid Crop Width: exceeds %d",
10459  LRETURN;
10460  }
10461  if (p_cfg->ui32cropHeight > NI_PARAM_MAX_HEIGHT)
10462  {
10463  snprintf(p_param_err, max_err_len, "Invalid Crop Height: exceeds %d",
10466  LRETURN;
10467  }
10468  if (p_cfg->ui32cropWidth * p_cfg->ui32cropHeight > NI_MAX_RESOLUTION_AREA)
10469  {
10470  snprintf(p_param_err, max_err_len, "Invalid Crop Width x Height: exceeds %d",
10473  LRETURN;
10474  }
10475 
10476  if (NI_CODEC_FORMAT_AV1 == p_ctx->codec_format)
10477  {
10479  {
10480  snprintf(p_param_err, max_err_len, "Invalid Crop Width: exceeds %d",
10483  LRETURN;
10484  }
10486  {
10487  snprintf(p_param_err, max_err_len, "Invalid Crop Height: exceeds %d",
10490  LRETURN;
10491  }
10492  if (p_cfg->ui32cropWidth * p_cfg->ui32cropHeight > NI_PARAM_AV1_MAX_AREA)
10493  {
10494  snprintf(p_param_err, max_err_len, "Invalid Crop Width x Height: exceeds %d",
10497  LRETURN;
10498  }
10499  }
10500 
10501  if (p_src->source_width < (p_cfg->ui32horOffset + p_cfg->ui32cropWidth))
10502  {
10503  snprintf(p_param_err, max_err_len, "Invalid Crop Width: offset %u + crop width %u too big > %d",
10504  p_cfg->ui32horOffset, p_cfg->ui32cropWidth, p_src->source_width);
10506  LRETURN;
10507  }
10508  if (p_src->source_height < (p_cfg->ui32verOffset + p_cfg->ui32cropHeight))
10509  {
10510  snprintf(p_param_err, max_err_len, "Invalid Crop Height: offset %u + crop height %u too big > %d",
10511  p_cfg->ui32verOffset, p_cfg->ui32cropHeight, p_src->source_height);
10513  LRETURN;
10514  }
10515  }
10516  else // check source width / height meet AV1 max resoultion constraint if cropping not specified
10517  {
10518  if (NI_CODEC_FORMAT_AV1 == p_ctx->codec_format)
10519  {
10520  if (p_src->source_width > NI_PARAM_AV1_MAX_WIDTH)
10521  {
10522  snprintf(p_param_err, max_err_len, "Invalid Picture Width: exceeds %d",
10525  LRETURN;
10526  }
10528  {
10529  snprintf(p_param_err, max_err_len, "Invalid Picture Height: exceeds %d",
10532  LRETURN;
10533  }
10534  if (p_src->source_width * p_src->source_height > NI_PARAM_AV1_MAX_AREA)
10535  {
10536  snprintf(p_param_err, max_err_len, "Invalid Picture Width x Height: exceeds %d",
10539  LRETURN;
10540  }
10541  }
10542  }
10543 
10544  if (p_cfg->ui8planarFormat >= NI_PIXEL_PLANAR_MAX)
10545  {
10546  strncpy(p_param_err, "Invalid input planar format: out of range", max_err_len);
10547  param_ret = NI_RETCODE_PARAM_ERROR_OOR;
10548  LRETURN;
10549  }
10551  {
10553  "68") < 0)
10554  {
10555  strncpy(p_param_err, "Invalid input planar format for device with FW api version < 6.8",
10556  max_err_len);
10558  LRETURN;
10559  }
10560  if (p_ctx->auto_dl_handle)
10561  {
10562  strncpy(p_param_err, "Invalid Encoder Selected: Tiled format must be on same device",
10563  max_err_len);
10564  param_ret = NI_RETCODE_ERROR_INVALID_HANDLE;
10565  LRETURN;
10566  }
10567  if (p_src->source_height % 4 != 0)
10568  {
10569  strncpy(p_param_err,
10570  "Invalid Picture Height: tiled format only supports "
10571  "multiples of 4",
10572  max_err_len);
10574  LRETURN;
10575  }
10576  if (p_src->source_width % 4 != 0)
10577  {
10578  strncpy(p_param_err,
10579  "Invalid Picture Width: tiled format only supports "
10580  "multiples of 4",
10581  max_err_len);
10583  LRETURN;
10584  }
10585  }
10586 
10587  if (p_cfg->ui8PixelFormat == NI_PIX_FMT_RGBA ||
10588  p_cfg->ui8PixelFormat == NI_PIX_FMT_BGRA ||
10589  p_cfg->ui8PixelFormat == NI_PIX_FMT_ABGR ||
10590  p_cfg->ui8PixelFormat == NI_PIX_FMT_ARGB)
10591  {
10592  if (p_src->zerocopy_mode == 0)
10593  {
10594  strncpy(p_param_err, "zeroCopyMode must not be disabled for RGBA / BGRA / ABGR / ARGB pixel formats", max_err_len);
10595  param_ret = NI_RETCODE_INVALID_PARAM;
10596  LRETURN;
10597  }
10598  if (p_cfg->ui8PixelFormat == NI_PIX_FMT_RGBA ||
10599  p_cfg->ui8PixelFormat == NI_PIX_FMT_BGRA)
10600  {
10602  "6Y") < 0)
10603  {
10604  strncpy(p_param_err, "RGBA / BGRA pixel formats not supported on device with FW api version < 6.Y",
10605  max_err_len);
10607  LRETURN;
10608  }
10609  }
10610  }
10611 
10612  if (p_src->ddr_priority_mode >= 0)
10613  {
10615  "6e") < 0)
10616  {
10617  strncpy(p_param_err, "ddr_priority_mode not supported on device with FW api version < 6.e",
10618  max_err_len);
10620  LRETURN;
10621  }
10622  }
10623 
10624  if (NI_RETCODE_SUCCESS !=
10625  ni_check_level(p_src->cfg_enc_params.level_idc, p_ctx->codec_format))
10626  {
10627  strncpy(p_param_err, "Invalid Encoder Level: out of range", max_err_len);
10628  param_ret = NI_RETCODE_PARAM_ERROR_OOR;
10629  LRETURN;
10630  }
10631 
10632  if ((p_src->cfg_enc_params.intra_mb_refresh_mode < 0 ||
10633  p_src->cfg_enc_params.intra_mb_refresh_mode > 4) ||
10634  (NI_CODEC_FORMAT_H264 == p_ctx->codec_format &&
10636  {
10637  strncpy(p_param_err, "Invalid intra_mb_refresh_mode: out of range",
10638  max_err_len);
10639  param_ret = NI_RETCODE_PARAM_ERROR_OOR;
10640  LRETURN;
10641  }
10642 
10643  if (!QUADRA)
10644  {
10646  {
10648  {
10649  strncpy(p_param_err, "Invalid custom GOP paramaters: custom_gop_size too small", max_err_len);
10651  LRETURN;
10652  }
10655  {
10656  strncpy(p_param_err, "Invalid custom GOP paramaters: custom_gop_size too big", max_err_len);
10658  LRETURN;
10659  }
10660  }
10661  }
10662  else // QUADRA
10663  {
10665  {
10667  {
10668  strncpy(p_param_err,
10669  "Invalid custom GOP paramaters: custom gop size must > 0",
10670  max_err_len);
10672  LRETURN;
10673  }
10674  }
10676  {
10679  {
10680  strncpy(p_param_err,
10681  "Invalid custom GOP paramaters: selected gopPresetIdx is "
10682  "not compatible with custom gop",
10683  max_err_len);
10685  LRETURN;
10686  }
10688  {
10689  strncpy(p_param_err, "Invalid custom GOP paramaters: custom_gop_size too small", max_err_len);
10691  LRETURN;
10692  }
10695  {
10696  strncpy(p_param_err, "Invalid custom GOP paramaters: custom_gop_size too big", max_err_len);
10698  LRETURN;
10699  }
10700 
10701  for (i = 0; i < p_cfg->niParamT408.custom_gop_params.custom_gop_size; i++)
10702  {
10705  {
10706  strncpy(p_param_err, "Invalid custom gop parameters: poc_offset larger"
10707  " than GOP size", max_err_len);
10709  LRETURN;
10710  }
10711  }
10712  }
10713 
10714  if(p_cfg->ui8useLowDelayPocType != 0)
10715  {
10716  if (NI_CODEC_FORMAT_H264 != p_ctx->codec_format)
10717  {
10718  p_cfg->ui8useLowDelayPocType = 0;
10719  strncpy(p_param_warn, "useLowDelayPocType is only supported for H.264. Change useLowDelayPocType to 0", max_err_len);
10720  warning = NI_RETCODE_PARAM_WARN;
10721  }
10722  }
10723  if(p_cfg->niParamT408.entropy_coding_mode != 1)
10724  {
10725  if (NI_CODEC_FORMAT_H264 != p_ctx->codec_format)
10726  {
10727  strncpy(p_param_err,
10728  "entropyCodingMode is only supported for H.264.",
10729  max_err_len);
10731  LRETURN;
10732  }
10733  }
10734  if (p_cfg->ui8av1ErrResilientMode)
10735  {
10736  if (NI_CODEC_FORMAT_AV1 != p_ctx->codec_format)
10737  {
10738  strncpy(p_param_err,
10739  "av1ErrorResilientMode is only supported for AV1.",
10740  max_err_len);
10742  LRETURN;
10743  }
10744  }
10745  if (-1 != p_cfg->ui32QLevel)
10746  {
10747  if (NI_CODEC_FORMAT_JPEG != p_ctx->codec_format)
10748  {
10749  strncpy(p_param_err, "qLevel is only supported for JPEG.",
10750  max_err_len);
10752  LRETURN;
10753  }
10754  }
10755 
10756  if(p_cfg->ui8enableSSIM != 0)
10757  {
10759  "62") < 0 || NI_CODEC_FORMAT_AV1 == p_ctx->codec_format)
10760  {
10761  p_cfg->ui8enableSSIM = 0;
10762  strncpy(p_param_warn, "enableSSIM only supported on device with FW api version < 6.2 "
10763  "and the encoder is not av1_ni_quadra_enc. Reported ssim will be 0.", max_err_len);
10764  warning = NI_RETCODE_PARAM_WARN;
10765  }
10766  }
10767 
10768  if (p_cfg->niParamT408.slice_mode || p_cfg->niParamT408.slice_arg)
10769  {
10771  {
10772  strncpy(p_param_err, "sliceMode/sliceArg is only supported for H.264 or H.265.",
10773  max_err_len);
10775  LRETURN;
10776  }
10777 
10778  if (p_cfg->niParamT408.slice_mode)
10779  {
10780  int ctu_mb_size = (NI_CODEC_FORMAT_H264 == p_ctx->codec_format) ? 16 : 64;
10781  int max_num_ctu_mb_row = (p_src->source_height + ctu_mb_size - 1) / ctu_mb_size;
10782  if (p_cfg->niParamT408.slice_arg < 1 || p_cfg->niParamT408.slice_arg > max_num_ctu_mb_row ||
10784  {
10785  snprintf(p_param_err, max_err_len, "Invalid number of rows per slice: should be between 1 and %d",
10786  max_num_ctu_mb_row < NI_MAX_SLICE_SIZE ? max_num_ctu_mb_row : NI_MAX_SLICE_SIZE);
10787  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
10788  LRETURN;
10789  }
10790  }
10791  }
10792  if (p_src->use_low_delay_poc_type)
10793  {
10794  if (NI_CODEC_FORMAT_H264 != p_ctx->codec_format)
10795  {
10796  strncpy(p_param_err, "useLowDelayPocType is only supported for H.264.",
10797  max_err_len);
10799  LRETURN;
10800  }
10801  }
10802 
10803  if(p_cfg->ui8enableCompensateQp != 0)
10804  {
10805  if (ni_cmp_fw_api_ver((char*) &p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6ru") < 0 ||
10806  NI_CODEC_FORMAT_AV1 == p_ctx->codec_format || p_cfg->ui16maxFrameSize == 0)
10807  {
10808  p_cfg->ui8enableCompensateQp = 0;
10809  strncpy(p_param_warn, "ui8enableCompensateQp only supported when device with FW api version < 6rt "
10810  "and the encoder is not av1_ni_quadra_enc and ui16maxFrameSize > 0. Reported enableCompensateQp will be 0.", max_err_len);
10811  warning = NI_RETCODE_PARAM_WARN;
10812  }
10813  }
10814 
10815  if (p_cfg->ui8enableTimecode != 0)
10816  {
10817  if (NI_CODEC_FORMAT_H264 != p_ctx->codec_format)
10818  {
10819  p_cfg->ui8enableTimecode = 0;
10820  if (NI_CODEC_FORMAT_H265 == p_ctx->codec_format)
10821  {
10822  strncpy(p_param_warn, "enableTimecode does not need to be set for H.265. Use ni_enc_insert_timecode API"
10823  "from libxcoder directly to insert time code SEI", max_err_len);
10824  }
10825  else
10826  {
10827  strncpy(p_param_warn, "enableTimecode not supported for the codec used. Forcing value to 0.", max_err_len);
10828  }
10829  warning = NI_RETCODE_PARAM_WARN;
10830  }
10831  }
10832  }
10833 
10834  if (QUADRA)
10835  {
10836  if (p_cfg->ui8LookAheadDepth != 0 || p_cfg->i8crf >= 0)
10837  {
10838  if ((1 == p_cfg->niParamT408.gop_preset_index && p_cfg->ui8LookAheadDepth != 0) ||
10839  3 == p_cfg->niParamT408.gop_preset_index ||
10840  7 == p_cfg->niParamT408.gop_preset_index ||
10841  10 == p_cfg->niParamT408.gop_preset_index ||
10842  15 == p_cfg->niParamT408.gop_preset_index)
10843  {
10844  strncpy(p_param_err,
10845  "this gopPreset is not supported for lookahead and/or CRF",
10846  max_err_len);
10848  LRETURN;
10849  }
10850 
10852  {
10853  bool bIsgopLowdelay = true;
10854  // lookahead does not support gopLowDelay (all B-frames, encode in order) gop patterns
10855  for (i = 0; i < p_cfg->niParamT408.custom_gop_params.custom_gop_size;
10856  i++)
10857  {
10858  // check if all frames are B-frames
10859  if (2 != p_cfg->niParamT408.custom_gop_params.pic_param[i].pic_type)
10860  {
10861  bIsgopLowdelay = false;
10862  break;
10863  }
10864  // check if all frames are encoded in display order
10865  if (p_cfg->niParamT408.custom_gop_params.pic_param[i].poc_offset != (i+1))
10866  {
10867  bIsgopLowdelay = false;
10868  break;
10869  }
10870  }
10871  if (bIsgopLowdelay)
10872  {
10873  strncpy(p_param_err, "B-frames low delay custom gop is not supported for "
10874  "lookahead and/or CRF", max_err_len);
10875  param_ret = NI_RETCODE_INVALID_PARAM;
10876  LRETURN;
10877  }
10878  }
10879 
10880  if (p_enc->bitrateMode != -1)
10881  {
10882  strncpy(p_param_err,
10883  "bitrateMode is invalid when lookahead is enabled (or in CRF mode)",
10884  max_err_len);
10886  LRETURN;
10887  }
10888  }
10889 
10891  {
10892  // when gop_preset_index = -1 (default), gop pattern is decided by gopSize and gopLowdelay
10893  //p_cfg->ui8gopSize = 0;
10894  //p_cfg->ui8gopLowdelay = 0;
10895  }
10896  if (1 == p_cfg->niParamT408.gop_preset_index)
10897  {
10898  p_cfg->niParamT408.intra_period = 1;
10899  p_cfg->niParamT408.avcIdrPeriod = 1;
10900  p_cfg->ui8gopSize = 1;
10901  p_cfg->ui16gdrDuration = 0;
10902  }
10903  if (2 == p_cfg->niParamT408.gop_preset_index)
10904  {
10905  strncpy(p_param_err,
10906  "gopPresetIdx 2 is obsolete, suggest to use gopPresetIdx 9 "
10907  "instead",
10908  max_err_len);
10910  LRETURN;
10911  }
10912  if (3 == p_cfg->niParamT408.gop_preset_index)
10913  {
10914  p_cfg->ui8gopSize = 1;
10915  p_cfg->ui8gopLowdelay = 1;
10916  }
10917  if (4 == p_cfg->niParamT408.gop_preset_index)
10918  {
10919  p_cfg->ui8gopSize = 2;
10920  p_cfg->ui8gopLowdelay = 0;
10921  }
10922  if (5 == p_cfg->niParamT408.gop_preset_index)
10923  {
10924  p_cfg->ui8gopSize = 4;
10925  p_cfg->ui8gopLowdelay = 0;
10926  }
10927  if (6 == p_cfg->niParamT408.gop_preset_index)
10928  {
10929  strncpy(p_param_err,
10930  "gopPresetIdx 6 is obsolete, suggest to use gopPresetIdx 7 "
10931  "instead",
10932  max_err_len);
10934  LRETURN;
10935  }
10936  if (7 == p_cfg->niParamT408.gop_preset_index)
10937  {
10938  p_cfg->ui8gopSize = 4;
10939  p_cfg->ui8gopLowdelay = 1;
10940  }
10941  if (8 == p_cfg->niParamT408.gop_preset_index)
10942  {
10943  p_cfg->ui8gopSize = 8;
10944  p_cfg->ui8gopLowdelay = 0;
10945  }
10946  if (9 == p_cfg->niParamT408.gop_preset_index)
10947  {
10948  p_cfg->ui8gopSize = 1;
10949  p_cfg->ui8gopLowdelay = 0;
10950  }
10951  if (10 == p_cfg->niParamT408.gop_preset_index)
10952  {
10953  p_cfg->ui8gopSize = 4;
10954  p_cfg->ui8gopLowdelay = 0;
10955  }
10956  if (15 == p_cfg->niParamT408.gop_preset_index)
10957  {
10958  p_cfg->ui8gopSize = 16;
10959  p_cfg->ui8gopLowdelay = 0;
10960  }
10961 
10962  if (p_cfg->ui8LookAheadDepth != 0
10963  || p_cfg->ui8bitrateMode == 1
10964  || p_cfg->ui32vbvMaxRate
10965  || p_cfg->ui32vbvMinRate
10966  || p_cfg->i8enableipRatio)
10967  {
10968  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");
10969  p_cfg->ui8NewRCEnable = 0;
10970  }
10971  else if (p_cfg->ui8NewRCEnable == 255)
10972  {
10973  if ((p_cfg->ui8gopSize > 1 || p_cfg->ui8gopSize == 0) && p_cfg->ui8gopLowdelay == 0)
10974  {
10975  ni_log2(p_ctx, NI_LOG_DEBUG, "Set newRcEnable to 0 in non low delay gop preset\n");
10976  p_cfg->ui8NewRCEnable = 0;
10977  }
10978  else if (p_cfg->niParamT408.intra_period == 1 || p_cfg->niParamT408.avcIdrPeriod == 1)
10979  {
10980  ni_log2(p_ctx, NI_LOG_DEBUG, "Set newRcEnable to 0 in intra only mode\n");
10981  p_cfg->ui8NewRCEnable = 0;
10982  }
10983  }
10984 
10985  if ((p_cfg->ui8stillImageDetectLevel != 0 || p_cfg->ui8sceneChangeDetectLevel != 0) &&
10986  (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))
10987  {
10988  strncpy(p_param_warn, "StillImageDetect or SceneChangeDetect only support gopPresetIdx=9 with FW > 6rm\n", max_err_len);
10989  warning = NI_RETCODE_PARAM_WARN;
10990  p_cfg->ui8stillImageDetectLevel = 0;
10991  p_cfg->ui8sceneChangeDetectLevel = 0;
10992  }
10993 
10994  if (p_cfg->ui8spatialLayersMinusOne > 0)
10995  {
10997  {
10998  strncpy(p_param_err,
10999  "currently do not support gop preset in multi spatial layers encode",
11000  max_err_len);
11002  LRETURN;
11003  }
11004  p_cfg->ui8gopSize = p_cfg->ui8spatialLayersMinusOne + 1;
11005  p_cfg->ui8gopLowdelay = 0;
11006  }
11007 
11008  if (p_cfg->ui8spatialLayersRefBaseLayer != 0)
11009  {
11010  if (p_cfg->ui8spatialLayersMinusOne == 0)
11011  {
11012  strncpy(p_param_err,
11013  "higher spatial layers referencing base layer is only supported in multi spatial layers encode",
11014  max_err_len);
11016  LRETURN;
11017  }
11018  }
11019  }
11020 
11021  if (NI_CODEC_FORMAT_H264 == p_ctx->codec_format)
11022  {
11023  if (!QUADRA)
11024  {
11025  if (10 == p_ctx->src_bit_depth)
11026  {
11027  if (p_cfg->niParamT408.profile != 5)
11028  {
11029  strncpy(p_param_err, "Invalid profile: must be 5 (high10)",
11030  max_err_len);
11031  param_ret = NI_RETCODE_INVALID_PARAM;
11032  LRETURN;
11033  }
11034  }
11035  else
11036  {
11037  if (p_cfg->niParamT408.profile < 1 || p_cfg->niParamT408.profile > 5)
11038  {
11039  strncpy(p_param_err, "Invalid profile: must be 1 (baseline), 2 (main),"
11040  " 3 (extended), 4 (high), or 5 (high10)", max_err_len);
11041  param_ret = NI_RETCODE_INVALID_PARAM;
11042  LRETURN;
11043  }
11044  }
11045  if (1 == p_cfg->niParamT408.profile &&
11046  ! (0 == p_cfg->niParamT408.gop_preset_index ||
11047  1 == p_cfg->niParamT408.gop_preset_index ||
11048  2 == p_cfg->niParamT408.gop_preset_index ||
11049  6 == p_cfg->niParamT408.gop_preset_index))
11050  {
11051  strncpy(p_param_err, "Invalid gopPresetIdx for H.264 baseline profile:"
11052  " must be 1, 2, 6 or 0 (custom with no B frames)", max_err_len);
11053  param_ret = NI_RETCODE_INVALID_PARAM;
11054  LRETURN;
11055  }
11056  if (1 == p_cfg->niParamT408.profile &&
11058  {
11059  for (i = 0; i < p_cfg->niParamT408.custom_gop_params.custom_gop_size;
11060  i++)
11061  {
11062  if (2 == p_cfg->niParamT408.custom_gop_params.pic_param[i].pic_type)
11063  {
11064  strncpy(p_param_err, "H.264 baseline profile: custom GOP can not "
11065  "have B frames", max_err_len);
11066  param_ret = NI_RETCODE_INVALID_PARAM;
11067  LRETURN;
11068  }
11069  }
11070  }
11071  }
11072  else // QUADRA
11073  {
11074  if (p_cfg->niParamT408.profile < 1 || p_cfg->niParamT408.profile > 5 || p_cfg->niParamT408.profile == 3)
11075  {
11076  strncpy(p_param_err, "Invalid profile: must be 1 (baseline), 2 (main),"
11077  " 4 (high), or 5 (high10)", max_err_len);
11078  param_ret = NI_RETCODE_INVALID_PARAM;
11079  LRETURN;
11080  }
11081  if (10 == p_ctx->src_bit_depth && p_cfg->niParamT408.profile != 5)
11082  {
11083  strncpy(p_param_warn, "AVC Baseline/Main/High Profile do not support 10-bit, auto convert to 8-bit", max_err_len);
11084  warning = NI_RETCODE_PARAM_WARN;
11085  }
11086  if (1 == p_cfg->niParamT408.profile)
11087  {
11089  {
11090  for (i = 0; i < p_cfg->niParamT408.custom_gop_params.custom_gop_size; i++)
11091  {
11092  if (2 == p_cfg->niParamT408.custom_gop_params.pic_param[i].pic_type)
11093  {
11094  strncpy(p_param_err, "H.264 baseline profile: custom GOP can not "
11095  "have B frames", max_err_len);
11096  param_ret = NI_RETCODE_INVALID_PARAM;
11097  LRETURN;
11098  }
11099  }
11100  } else if (((p_cfg->ui8gopSize != 1 || p_cfg->ui8gopLowdelay)) &&
11101  (p_cfg->niParamT408.intra_period != 1) &&
11103  {
11105  p_cfg->ui8gopSize != 0 ||
11106  p_cfg
11107  ->ui8gopLowdelay) // if gopSize is 0 (default / adapative gop), autoset to 1
11108  {
11109  strncpy(p_param_err,
11110  "Must use gopPresetIdx 1,9,10 (no "
11111  "B frames) for profile 1",
11112  max_err_len);
11113  param_ret = NI_RETCODE_INVALID_PARAM;
11114  LRETURN;
11115  }
11116  p_cfg->ui8gopSize = 1; //autoset to 1
11117  }
11118  }
11119  if (p_cfg->niParamT408.tier)
11120  {
11121  strncpy(p_param_err, "Tier is not supported for H.264", max_err_len);
11122  param_ret = NI_RETCODE_INVALID_PARAM;
11123  LRETURN;
11124  }
11125  if (p_cfg->ui8spatialLayersMinusOne > 0)
11126  {
11127  strncpy(p_param_err,
11128  "spatialLayers is not supported for h.264 encode",
11129  max_err_len);
11131  LRETURN;
11132  }
11133  }
11134  }
11135  else if (NI_CODEC_FORMAT_H265 == p_ctx->codec_format)
11136  {
11137  if (!QUADRA)
11138  {
11139  if (10 == p_ctx->src_bit_depth)
11140  {
11141  if (p_cfg->niParamT408.profile != 2)
11142  {
11143  strncpy(p_param_err, "Invalid profile: must be 2 (main10)",
11144  max_err_len);
11145  param_ret = NI_RETCODE_INVALID_PARAM;
11146  LRETURN;
11147  }
11148  }
11149  else
11150  {
11151  if (p_cfg->niParamT408.profile < 1 || p_cfg->niParamT408.profile > 2)
11152  {
11153  strncpy(p_param_err, "Invalid profile: must be 1 (main) or 2 (main10)",
11154  max_err_len);
11155  param_ret = NI_RETCODE_INVALID_PARAM;
11156  LRETURN;
11157  }
11158  }
11159  }
11160  else // QUADRA
11161  {
11162  if (p_cfg->niParamT408.profile < 1 || p_cfg->niParamT408.profile > 2)
11163  {
11164  strncpy(p_param_err, "Invalid profile: must be 1 (main) or 2 (main10)",
11165  max_err_len);
11166  param_ret = NI_RETCODE_INVALID_PARAM;
11167  LRETURN;
11168  }
11169  if (10 == p_ctx->src_bit_depth && p_cfg->niParamT408.profile != 2)
11170  {
11171  strncpy(p_param_warn, "HEVC Main Profile does not support 10-bit, auto convert to 8-bit", max_err_len);
11172  warning = NI_RETCODE_PARAM_WARN;
11173  }
11174  if (p_cfg->ui8spatialLayersMinusOne > 0)
11175  {
11176  strncpy(p_param_err,
11177  "spatialLayers is not supported for h.265 encode",
11178  max_err_len);
11180  LRETURN;
11181  }
11182  }
11183  } else if (NI_CODEC_FORMAT_AV1 == p_ctx->codec_format)
11184  {
11185  if (p_cfg->niParamT408.profile != 1)
11186  {
11187  strncpy(p_param_err, "Invalid profile: must be 1 (main)",
11188  max_err_len);
11189  param_ret = NI_RETCODE_INVALID_PARAM;
11190  LRETURN;
11191  }
11192  if (p_cfg->niParamT408.level) // 0 means auto level
11193  {
11194  if (p_cfg->niParamT408.level < 20)
11195  {
11196  p_cfg->niParamT408.level = 20;
11197  strncpy(p_param_warn,
11198  "AV1 level < 2.0 is not supported, change to level 2.0",
11199  max_err_len);
11200  warning = NI_RETCODE_PARAM_WARN;
11201  } else if (p_cfg->niParamT408.level > 51)
11202  {
11203  p_cfg->niParamT408.level = 51;
11204  strncpy(p_param_warn,
11205  "AV1 level > 5.1 is not supported, change to level 5.1",
11206  max_err_len);
11207  warning = NI_RETCODE_PARAM_WARN;
11208  }
11209  }
11210 
11211  if (p_cfg->niParamT408.conf_win_top != 0)
11212  {
11214  0;
11215  strncpy(p_param_warn, "confWinTop is not supported in AV1",
11216  max_err_len);
11217  warning = NI_RETCODE_PARAM_WARN;
11218  }
11219  if (p_cfg->niParamT408.conf_win_bottom != 0)
11220  {
11221  p_cfg->niParamT408.conf_win_bottom =
11222  p_src->cfg_enc_params.conf_win_bottom = 0;
11223  strncpy(p_param_warn, "confWinBottom is not supported in AV1",
11224  max_err_len);
11225  warning = NI_RETCODE_PARAM_WARN;
11226  }
11227  if (p_cfg->niParamT408.conf_win_left != 0)
11228  {
11229  p_cfg->niParamT408.conf_win_left =
11230  p_src->cfg_enc_params.conf_win_left = 0;
11231  strncpy(p_param_warn, "confWinLeft is not supported in AV1",
11232  max_err_len);
11233  warning = NI_RETCODE_PARAM_WARN;
11234  }
11235  if (p_cfg->niParamT408.conf_win_right != 0)
11236  {
11237  p_cfg->niParamT408.conf_win_right =
11238  p_src->cfg_enc_params.conf_win_right = 0;
11239  strncpy(p_param_warn, "confWinRight is not supported in AV1",
11240  max_err_len);
11241  warning = NI_RETCODE_PARAM_WARN;
11242  }
11243  if (p_cfg->ui8hdr10_enable)
11244  {
11245  strncpy(p_param_err,
11246  "masterDisplay not supported for AV1",
11247  max_err_len);
11248  warning = NI_RETCODE_PARAM_WARN;
11249  }
11250  if (p_cfg->ui8hrdEnable)
11251  {
11252  strncpy(p_param_err, "hrdEnable is not supported on av1 encoder",
11253  max_err_len);
11255  LRETURN;
11256  }
11257  if (p_cfg->ui8EnableAUD)
11258  {
11259  strncpy(p_param_err, "enableAUD is not supported on av1 encoder",
11260  max_err_len);
11262  LRETURN;
11263  }
11264  if (p_cfg->ui8HDR10CLLEnable)
11265  {
11266  strncpy(p_param_err, "maxCLL is not supported on av1 encoder",
11267  max_err_len);
11269  LRETURN;
11270  }
11271  if (p_cfg->ui8repeatHeaders)
11272  {
11273  strncpy(p_param_err, "repeatHeaders is not supported on av1 encoder",
11274  max_err_len);
11275  warning = NI_RETCODE_PARAM_WARN;
11276  }
11277  if (p_cfg->ui8enableSSIM)
11278  {
11279  strncpy(p_param_err, "enableSSIM is not supported on av1 encoder",
11280  max_err_len);
11282  LRETURN;
11283  }
11284  if (p_cfg->ui8EnableRdoQuant)
11285  {
11286  strncpy(p_param_err, "EnableRdoQuant is not supported on av1 encoder",
11287  max_err_len);
11289  LRETURN;
11290  }
11291  if (p_cfg->ui8fillerEnable)
11292  {
11293  strncpy(p_param_err, "fillerEnable is not supported on av1 encoder",
11294  max_err_len);
11296  LRETURN;
11297  }
11298  if (p_cfg->i8ppsInitQp)
11299  {
11300  strncpy(p_param_err, "ppsInitQp is not supported for av1 encoder",
11301  max_err_len);
11303  LRETURN;
11304  }
11305  if (p_cfg->ui8vbvBufferReencode && p_cfg->ui8multicoreJointMode)
11306  {
11307  strncpy(p_param_err, "vbvBufferReencode is not supported for av1 multicoreJointMode",
11308  max_err_len);
11310  LRETURN;
11311  }
11312  } else if (NI_CODEC_FORMAT_JPEG == p_ctx->codec_format)
11313  {
11314  if (p_cfg->ui8hdr10_enable)
11315  {
11316  strncpy(p_param_err,
11317  "masterDisplay not supported for jpeg",
11318  max_err_len);
11319  warning = NI_RETCODE_PARAM_WARN;
11320  }
11321  if (p_cfg->niParamT408.conf_win_top != 0)
11322  {
11323  strncpy(p_param_err, "confWinTop is not supported in jpeg",
11324  max_err_len);
11326  LRETURN;
11327  }
11328  if (p_cfg->niParamT408.conf_win_bottom != 0)
11329  {
11330  strncpy(p_param_err, "confWinBottom is not supported in jpeg",
11331  max_err_len);
11333  LRETURN;
11334  }
11335  if (p_cfg->niParamT408.conf_win_left != 0)
11336  {
11337  strncpy(p_param_err, "confWinLeft is not supported in jpeg",
11338  max_err_len);
11340  LRETURN;
11341  }
11342  if (p_cfg->niParamT408.conf_win_right != 0)
11343  {
11344  strncpy(p_param_err, "confWinRight is not supported in jpeg",
11345  max_err_len);
11347  LRETURN;
11348  }
11349  if (p_cfg->ui8hrdEnable)
11350  {
11351  strncpy(p_param_err, "hrdEnable is not supported on jpeg encoder",
11352  max_err_len);
11354  LRETURN;
11355  }
11356  if (p_cfg->ui8EnableAUD)
11357  {
11358  strncpy(p_param_err, "enableAUD is not supported on jpeg encoder",
11359  max_err_len);
11361  LRETURN;
11362  }
11363  if (p_cfg->ui8repeatHeaders)
11364  {
11365  strncpy(p_param_err, "repeatHeaders is not supported on jpeg encoder",
11366  max_err_len);
11368  LRETURN;
11369  }
11371  {
11372  strncpy(p_param_err, "prefTRC is not supported on jpeg encoder",
11373  max_err_len);
11375  LRETURN;
11376  }
11377  if (p_cfg->ui8HDR10CLLEnable)
11378  {
11379  strncpy(p_param_err, "maxCLL is not supported on jpeg encoder",
11380  max_err_len);
11382  LRETURN;
11383  }
11384  if (p_cfg->ui8colorPrimaries != 2)
11385  {
11386  strncpy(p_param_err, "colorPri is not supported on jpeg encoder",
11387  max_err_len);
11389  LRETURN;
11390  }
11391  if (p_cfg->ui8colorTrc != 2)
11392  {
11393  strncpy(p_param_err, "colorTrc is not supported on jpeg encoder",
11394  max_err_len);
11396  LRETURN;
11397  }
11398  if (p_cfg->ui8colorSpace != 2)
11399  {
11400  strncpy(p_param_err, "colorSpc is not supported on jpeg encoder",
11401  max_err_len);
11403  LRETURN;
11404  }
11405  if (p_src->sar_num)
11406  {
11407  strncpy(p_param_err, "sarNum is not supported on jpeg encoder",
11408  max_err_len);
11410  LRETURN;
11411  }
11412  if (p_src->sar_denom != 1)
11413  {
11414  strncpy(p_param_err, "sarDenom is not supported on jpeg encoder",
11415  max_err_len);
11417  LRETURN;
11418  }
11419  if (p_src->video_full_range_flag != -1)
11420  {
11421  strncpy(p_param_err,
11422  "videoFullRangeFlag is not supported on jpeg encoder",
11423  max_err_len);
11425  LRETURN;
11426  }
11427  if (p_cfg->ui8temporalLayersEnable)
11428  {
11429  strncpy(p_param_err,
11430  "temporalLayersEnable is not supported on jpeg encoder",
11431  max_err_len);
11433  LRETURN;
11434  }
11435  if (p_cfg->ui8spatialLayersMinusOne > 0)
11436  {
11437  strncpy(p_param_err,
11438  "spatialLayers is not supported for jpeg encode",
11439  max_err_len);
11441  LRETURN;
11442  }
11443  if (p_cfg->ui8LowDelay)
11444  {
11445  strncpy(p_param_err, "LowDelay is not supported on jpeg encoder",
11446  max_err_len);
11448  LRETURN;
11449  }
11450  if (p_cfg->ui8rdoLevel)
11451  {
11452  strncpy(p_param_err, "rdoLevel is not supported on jpeg encoder",
11453  max_err_len);
11455  LRETURN;
11456  }
11457  if (p_cfg->ui8EnableRdoQuant)
11458  {
11459  strncpy(p_param_err, "EnableRdoQuant is not supported on jpeg encoder",
11460  max_err_len);
11462  LRETURN;
11463  }
11464  if (p_cfg->ui8enable2PassGopPatern)
11465  {
11466  strncpy(p_param_err,
11467  "enable2PassGop is not supported on jpeg encoder",
11468  max_err_len);
11470  LRETURN;
11471  }
11472  if (p_cfg->ui8LookAheadDepth)
11473  {
11474  strncpy(p_param_err,
11475  "lookAheadDepth is not supported on jpeg encoder",
11476  max_err_len);
11478  LRETURN;
11479  }
11481  {
11482  strncpy(p_param_err,
11483  "gopPresetIdx is not supported on jpeg encoder",
11484  max_err_len);
11486  LRETURN;
11487  }
11488  if (p_cfg->niParamT408.roiEnable)
11489  {
11490  strncpy(p_param_err, "roiEnable is not supported on jpeg encoder",
11491  max_err_len);
11493  LRETURN;
11494  }
11495  if(p_src->roi_demo_mode)
11496  {
11497  strncpy(p_param_err,
11498  "RoiDemoMode is not supported on jpeg encoder",
11499  max_err_len);
11501  LRETURN;
11502  }
11503  if (p_src->cacheRoi)
11504  {
11505  strncpy(p_param_err, "cacheRoi is not supported on jpeg encoder",
11506  max_err_len);
11508  LRETURN;
11509  }
11511  {
11512  strncpy(p_param_err,
11513  "ReconfDemoMode is not supported on jpeg encoder",
11514  max_err_len);
11516  LRETURN;
11517  }
11518  if (p_cfg->niParamT408.intraRefreshMode)
11519  {
11520  strncpy(p_param_err,
11521  "intraRefreshMode is not supported on jpeg encoder",
11522  max_err_len);
11524  LRETURN;
11525  }
11526  if (p_cfg->niParamT408.intraRefreshArg)
11527  {
11528  strncpy(p_param_err,
11529  "intraRefreshArg is not supported on jpeg encoder",
11530  max_err_len);
11532  LRETURN;
11533  }
11534  if (p_cfg->niParamT408.intra_period != 120)
11535  {
11536  strncpy(p_param_err,
11537  "intraPeriod is not supported on jpeg encoder",
11538  max_err_len);
11540  LRETURN;
11541  }
11542  if (p_cfg->ui8intraResetRefresh)
11543  {
11544  strncpy(
11545  p_param_err,
11546  "IntraRefreshResetOnForceIDR is not supported on jpeg encoder",
11547  max_err_len);
11549  LRETURN;
11550  }
11551  if (p_cfg->niParamT408.useLongTerm)
11552  {
11553  strncpy(p_param_err,
11554  "longTermReferenceEnable is not supported on jpeg encoder",
11555  max_err_len);
11557  LRETURN;
11558  }
11559  if (p_cfg->ui32setLongTermInterval)
11560  {
11561  strncpy(p_param_err,
11562  "longTermReferenceInterval is not supported on jpeg encoder",
11563  max_err_len);
11565  LRETURN;
11566  }
11567  if (p_cfg->ui8setLongTermCount != 2)
11568  {
11569  strncpy(p_param_err,
11570  "longTermReferenceCount is not supported on jpeg encoder",
11571  max_err_len);
11573  LRETURN;
11574  }
11575  if (p_cfg->ui8multicoreJointMode)
11576  {
11577  strncpy(p_param_err,
11578  "multicoreJointMode is not supported on jpeg encoder",
11579  max_err_len);
11581  LRETURN;
11582  }
11583  if (p_cfg->ui8enableSSIM)
11584  {
11585  strncpy(p_param_err, "enableSSIM is not supported on jpeg encoder",
11586  max_err_len);
11588  LRETURN;
11589  }
11590  if (p_enc->rc.vbv_buffer_size != -1)
11591  {
11592  strncpy(p_param_err,
11593  "vbvBufferSize is not supported on jpeg encoder",
11594  max_err_len);
11596  LRETURN;
11597  }
11598  if (p_cfg->ui8fillerEnable)
11599  {
11600  strncpy(p_param_err,
11601  "fillerEnable is not supported on jpeg encoder",
11602  max_err_len);
11604  LRETURN;
11605  }
11606  if (p_cfg->ui8picSkipEnable)
11607  {
11608  strncpy(p_param_err, "picSkip is not supported on jpeg encoder",
11609  max_err_len);
11611  LRETURN;
11612  }
11613  if (p_cfg->ui16maxFrameSize)
11614  {
11615  strncpy(p_param_err, "maxFrameSize is not supported on jpeg encoder",
11616  max_err_len);
11618  LRETURN;
11619  }
11621  {
11622  strncpy(p_param_err,
11623  "cuLevelRCEnable is not supported on jpeg encoder",
11624  max_err_len);
11626  LRETURN;
11627  }
11628  if (p_cfg->niParamT408.enable_hvs_qp)
11629  {
11630  strncpy(p_param_err, "hvsQPEnable is not supported on jpeg encoder",
11631  max_err_len);
11633  LRETURN;
11634  }
11635  if (p_cfg->niParamT408.profile)
11636  {
11637  strncpy(p_param_err, "profile is not supported on jpeg encoder",
11638  max_err_len);
11640  LRETURN;
11641  }
11643  {
11644  strncpy(p_param_err, "intraRefreshMode or intraRefreshArg is not supported on jpeg encoder",
11645  max_err_len);
11647  LRETURN;
11648  }
11649  if (p_cfg->i8crf != -1)
11650  {
11651  strncpy(p_param_err, "crf is not supported on jpeg encoder",
11652  max_err_len);
11654  LRETURN;
11655  }
11656  if (p_cfg->i32tolCtbRcInter != (int32_t)(0.1 * 1000))
11657  {
11658  strncpy(p_param_err, "tolCtbRcInter is not supported on jpeg encoder",
11659  max_err_len);
11661  LRETURN;
11662  }
11663  if (p_cfg->i32tolCtbRcIntra != (int32_t)(0.1 * 1000))
11664  {
11665  strncpy(p_param_err, "tolCtbRcIntra is not supported on jpeg encoder",
11666  max_err_len);
11668  LRETURN;
11669  }
11670  if (p_cfg->ui8rcQpDeltaRange != 10)
11671  {
11672  strncpy(p_param_err,
11673  "rcQpDeltaRange is not supported on jpeg encoder",
11674  max_err_len);
11676  LRETURN;
11677  }
11678  if (p_cfg->i16bitrateWindow != -255)
11679  {
11680  strncpy(p_param_err, "bitrateWindow is not supported on jpeg encoder",
11681  max_err_len);
11683  LRETURN;
11684  }
11685  if (p_cfg->i16ctbRowQpStep)
11686  {
11687  strncpy(p_param_err, "ctbRowQpStep is not supported on jpeg encoder",
11688  max_err_len);
11690  LRETURN;
11691  }
11692  if (p_cfg->ui8AiEnhanceMode)
11693  {
11694  strncpy(p_param_err,
11695  "enableAIEnhance is not supported on jpeg encoder",
11696  max_err_len);
11698  LRETURN;
11699  }
11700  if (p_cfg->i8ppsInitQp)
11701  {
11702  strncpy(p_param_err, "ppsInitQp is not supported for jpeg encoder",
11703  max_err_len);
11705  LRETURN;
11706  }
11707  if (p_cfg->i8pass1Qp)
11708  {
11709  strncpy(p_param_err, "pass1Qp is not supported for jpeg encoder",
11710  max_err_len);
11712  LRETURN;
11713  }
11714  if (p_enc->bitrateMode != -1)
11715  {
11716  strncpy(p_param_err, "bitrateMode is not supported for jpeg encoder",
11717  max_err_len);
11719  LRETURN;
11720  }
11721  if (p_cfg->ui8vbvBufferReencode)
11722  {
11723  strncpy(p_param_err,
11724  "vbvBufferReencode is not supported on jpeg encoder",
11725  max_err_len);
11727  LRETURN;
11728  }
11729  }
11730 
11731  if (p_src->force_frame_type != 0 && p_src->force_frame_type != 1)
11732  {
11733  strncpy(p_param_err, "Invalid forceFrameType: out of range",
11734  max_err_len);
11735  param_ret = NI_RETCODE_INVALID_PARAM;
11736  LRETURN;
11737  }
11738 
11739  if (p_cfg->niParamT408.forcedHeaderEnable > 2)
11740  {
11741  strncpy(p_param_err, "Invalid forcedHeaderEnable: out of range",
11742  max_err_len);
11743  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
11744  LRETURN;
11745  }
11746 
11747  if (p_cfg->niParamT408.decoding_refresh_type < 0 ||
11749  {
11750  strncpy(p_param_err, "Invalid decoding_refresh_type: out of range", max_err_len);
11752  LRETURN;
11753  }
11754 
11755  if (!QUADRA)
11756  {
11757  if (p_cfg->niParamT408.gop_preset_index < 0 ||
11758  p_cfg->niParamT408.gop_preset_index > 8)
11759  {
11760  strcpy(p_param_err, "Invalid gop_preset_index: out of range");
11762  LRETURN;
11763  }
11764 
11765  if (p_src->low_delay_mode && 1 != p_cfg->niParamT408.gop_preset_index &&
11766  2 != p_cfg->niParamT408.gop_preset_index &&
11767  3 != p_cfg->niParamT408.gop_preset_index &&
11768  6 != p_cfg->niParamT408.gop_preset_index &&
11769  7 != p_cfg->niParamT408.gop_preset_index &&
11770  !(0 == p_cfg->niParamT408.gop_preset_index &&
11772  {
11773  strcpy(p_param_err, "GOP size must be 1 when lowDelay is enabled");
11775  LRETURN;
11776  }
11777  }
11778  else // QUADRA
11779  {
11780  if (p_cfg->ui8gopSize > 16)
11781  {
11782  strncpy(p_param_err, "Invalid gopSize out of range", max_err_len);
11784  LRETURN;
11785  }
11786 
11787  if (p_cfg->ui8gopLowdelay &&
11788  p_cfg->ui8gopSize > 4)
11789  {
11790  strncpy(p_param_err, "GOP size must be <= 4 for low delay GOP", max_err_len);
11792  LRETURN;
11793  }
11794 
11795  if (p_cfg->ui8LookAheadDepth)
11796  {
11797  if (p_cfg->ui8LookAheadDepth < 4 || p_cfg->ui8LookAheadDepth > 40)
11798  {
11799  strncpy(p_param_err, "Invalid LookAheadDepth: out of range. <[4-40]>", max_err_len);
11801  LRETURN;
11802  }
11803  if (p_cfg->ui8gopLowdelay)
11804  {
11805  strncpy(p_param_err, "2-pass encode does not support low delay GOP", max_err_len);
11807  LRETURN;
11808  }
11810  {
11811  strncpy(p_param_err, "2-pass encode does not support tile4x4 format",
11812  max_err_len);
11814  LRETURN;
11815  }
11816  if (p_cfg->ui8spatialLayersMinusOne > 0)
11817  {
11818  strncpy(p_param_err, "currently do not support lookahead encode with multi spatial layers", max_err_len);
11820  LRETURN;
11821  }
11822  }
11823 
11824  if (p_src->low_delay_mode || p_cfg->ui8picSkipEnable)
11825  {
11827  {
11828  for (i = 0; i < p_cfg->niParamT408.custom_gop_params.custom_gop_size; i++)
11829  {
11830  if (p_cfg->niParamT408.custom_gop_params.pic_param[i].poc_offset != (i+1))
11831  {
11832  if (p_src->low_delay_mode)
11833  strncpy(p_param_err, "Custom GOP must not include backward prediction when lowDelay is enabled", max_err_len);
11834  else
11835  strncpy(p_param_err, "Custom GOP must not include backward prediction when picSkip is enabled", max_err_len);
11836  param_ret = NI_RETCODE_INVALID_PARAM;
11837  LRETURN;
11838  }
11839  }
11840  } else if (1 != p_cfg->ui8gopSize && !p_cfg->ui8gopLowdelay &&
11841  p_cfg->niParamT408.intra_period != 1 &&
11843  {
11844  if (p_src->low_delay_mode)
11845  {
11846  if (p_cfg->ui8spatialLayersMinusOne == 0)
11847  {
11848  strncpy(p_param_err, "Must use low delay GOP (gopPresetIdx 1,3,7,9,10) when lowDelay is enabled", max_err_len);
11850  LRETURN;
11851  }
11852  }
11853  else
11854  {
11855  strncpy(p_param_err, "Must use low delay GOP (gopPresetIdx 1,3,7,9,10) when picSkip is enabled", max_err_len);
11857  LRETURN;
11858  }
11859  } else if ((p_cfg->ui8LookAheadDepth != 0) && (!p_cfg->ui8useLowDelayPocType))
11860  {
11861  if (p_src->low_delay_mode)
11862  strncpy(p_param_err, "lookAheadDepth must be 0 when lowDelay is enabled", max_err_len);
11863  else
11864  strncpy(p_param_err, "lookAheadDepth must be 0 when picSkip is enabled", max_err_len);
11865  param_ret = NI_RETCODE_INVALID_PARAM;
11866  LRETURN;
11867  }
11868 
11869  if (p_cfg->ui8multicoreJointMode)
11870  {
11871  if (p_src->low_delay_mode)
11872  strncpy(p_param_err,
11873  "Cannot use multicoreJointMode when lowDelay is enabled",
11874  max_err_len);
11875  else
11876  strncpy(p_param_err,
11877  "Cannot use multicoreJointMode when picSkip is enabled",
11878  max_err_len);
11879  param_ret = NI_RETCODE_INVALID_PARAM;
11880  LRETURN;
11881  }
11882 
11883  if (p_src->minFramesDelay)
11884  {
11885  if (p_src->low_delay_mode)
11886  strncpy(p_param_err,
11887  "Cannot enable minFramesDelay when lowDelay is enabled",
11888  max_err_len);
11889  else
11890  strncpy(p_param_err,
11891  "Cannot enable minFramesDelay when picSkip is enabled",
11892  max_err_len);
11893  param_ret = NI_RETCODE_INVALID_PARAM;
11894  LRETURN;
11895  }
11896  }
11897 
11898  if (p_cfg->ui8spatialLayersMinusOne > 0)
11899  {
11900  if ((p_cfg->niParamT408.intra_period % (p_cfg->ui8spatialLayersMinusOne+1)) > 0)
11901  {
11902  strncpy(p_param_err, "intra period must be divisible by the number of spatial layers", max_err_len);
11904  LRETURN;
11905  }
11906  }
11907 
11908  if (p_cfg->ui8useLowDelayPocType)
11909  {
11911  {
11912  strncpy(p_param_err, "Custom GOP size must be 1 when useLowDelayPocType is enabled", max_err_len);
11913  param_ret = NI_RETCODE_INVALID_PARAM;
11914  LRETURN;
11915  } else if (1 != p_cfg->ui8gopSize && !p_cfg->ui8gopLowdelay &&
11916  p_cfg->niParamT408.intra_period != 1)
11917  {
11918  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);
11920  LRETURN;
11921  }
11922  }
11923 
11924  if (p_src->low_delay_mode)
11925  {
11926  // minimum acceptable value of maxFrameSize is bitrate / framerate in bytes
11927  uint32_t min_maxFrameSize = p_cfg->i32bitRate / p_cfg->i32frameRateInfo * p_cfg->i32frameRateDenominator / 8;
11928 
11929  if (p_cfg->ui16maxFrameSize == 0)
11930  {
11931  if (p_enc->maxFrameSizeRatio > 0)
11932  {
11933  if (min_maxFrameSize * p_enc->maxFrameSizeRatio > NI_MAX_FRAME_SIZE)
11934  {
11935  p_cfg->ui16maxFrameSize = NI_MAX_FRAME_SIZE / 2000;
11936  } else
11937  {
11938  p_cfg->ui16maxFrameSize = min_maxFrameSize * p_enc->maxFrameSizeRatio / 2000;
11939  }
11940  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: Set maxFrameSize to %d times the "
11941  "minimum frame size %d bytes in low delay mode\n", __func__,
11942  p_enc->maxFrameSizeRatio, p_cfg->ui16maxFrameSize * 2000);
11943  } else
11944  {
11945  p_cfg->ui16maxFrameSize = ((p_src->source_width * p_src->source_height * 3 / 4) * p_ctx->bit_depth_factor) / 2000;
11946  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: maxFrameSize is not set in low delay "
11947  "mode. Set it to half of the maximum frame size %d bytes\n",
11948  __func__, p_cfg->ui16maxFrameSize*2000);
11949  }
11950  }
11951 
11952  if (p_cfg->ui16maxFrameSize < min_maxFrameSize / 2000)
11953  {
11954  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: maxFrameSize %u is too small. Changed to minimum value (bitrate/framerate in byte): %u\n",
11955  __func__, p_cfg->ui16maxFrameSize*2000, min_maxFrameSize);
11956  p_cfg->ui16maxFrameSize = min_maxFrameSize / 2000;
11957  }
11958  }
11959  else
11960  {
11961  if (p_cfg->ui16maxFrameSize != 0 || p_enc->maxFrameSizeRatio > 0)
11962  {
11963  strncpy(p_param_err, "maxFrameSize can only be used when lowDelay is enabled", max_err_len);
11964  param_ret = NI_RETCODE_INVALID_PARAM;
11965  LRETURN;
11966  }
11967  }
11968  }
11969 
11970  if (QUADRA)
11971  {
11972  if (p_cfg->ui16gdrDuration)
11973  {
11975  {
11976  for (i = 0; i < p_cfg->niParamT408.custom_gop_params.custom_gop_size; i++)
11977  {
11978  if (2 == p_cfg->niParamT408.custom_gop_params.pic_param[i].pic_type)
11979  {
11980  strncpy(p_param_err, "Custom GOP can not have B frames for intra refresh", max_err_len);
11981  param_ret = NI_RETCODE_INVALID_PARAM;
11982  LRETURN;
11983  }
11984  }
11985  }
11986  else if (p_cfg->ui8gopSize != 1 || p_cfg->ui8gopLowdelay)
11987  {
11989  {
11990  strncpy(p_param_err,
11991  "Must use gopPresetIdx 9 (consecutive P frame) for intra refresh",
11992  max_err_len);
11993  param_ret = NI_RETCODE_INVALID_PARAM;
11994  LRETURN;
11995  }
11996  strncpy(p_param_warn, "GOP size forced to 1 and low delay GOP force disabled (no B frames) for intra refresh", max_err_len);
11997  warning = NI_RETCODE_PARAM_WARN;
11998  p_cfg->ui8gopSize = 1;
11999  p_cfg->ui8gopLowdelay = 0;
12000  }
12001  if (p_cfg->ui16gdrDuration == 1)
12002  {
12003  strncpy(p_param_err,
12004  "intra refresh cycle (height / intraRefreshArg MB or CTU) must > 1",
12005  max_err_len);
12006  param_ret = NI_RETCODE_INVALID_PARAM;
12007  LRETURN;
12008  }
12009  if (p_cfg->ui8LookAheadDepth != 0)
12010  {
12011  strncpy(p_param_err, "lookaheadDepth must be 0 for intra refresh", max_err_len);
12012  param_ret = NI_RETCODE_INVALID_PARAM;
12013  LRETURN;
12014  }
12015  if (STD_HEVC == p_cfg->ui8bitstreamFormat ||
12016  STD_AV1 == p_cfg->ui8bitstreamFormat)
12017  {
12018  if (p_cfg->niParamT408.intra_period < p_cfg->ui16gdrDuration)
12019  {
12020  strncpy(p_param_warn, "intraPeriod forced to match intra refersh cycle (intraPeriod must >= intra refersh cycle)", max_err_len);
12021  warning = NI_RETCODE_PARAM_WARN;
12022  p_cfg->niParamT408.intra_period = p_cfg->ui16gdrDuration;
12023  }
12024  }
12025  else if (STD_AVC == p_cfg->ui8bitstreamFormat)
12026  {
12027  if (p_cfg->niParamT408.avcIdrPeriod < p_cfg->ui16gdrDuration)
12028  {
12029  strncpy(p_param_warn, "intraPeriod forced to match intra refersh cycle (intraPeriod must >= intra refersh cycle)", max_err_len);
12030  warning = NI_RETCODE_PARAM_WARN;
12031  p_cfg->niParamT408.avcIdrPeriod = p_cfg->ui16gdrDuration;
12032  }
12033  }
12034  }
12035 
12036  if (p_cfg->ui8multicoreJointMode)
12037  {
12038  if (p_cfg->ui8hrdEnable)
12039  {
12040  strncpy(p_param_warn, "HRD conformance is not guaranteed in multicoreJointMode", max_err_len);
12041  warning = NI_RETCODE_PARAM_WARN;
12042  }
12043  }
12044 
12045  if ((p_cfg->ui8hrdEnable) || (p_cfg->ui8fillerEnable))
12046  {
12047  // enable rate control
12048  if (p_cfg->ui8rcEnable == 0)
12049  {
12050  p_cfg->ui8rcEnable = p_src->cfg_enc_params.rc.enable_rate_control = 1;
12051  }
12052 
12053  // enable hrd if it is off
12054  if (p_cfg->i32vbvBufferSize == 0)
12055  {
12056  p_cfg->i32vbvBufferSize = 3000;
12057  }
12058  }
12059 
12060  // maxrate must >= bitrate
12061  if (p_cfg->ui32vbvMaxRate != 0)
12062  {
12063  if (p_cfg->ui32vbvMaxRate < p_cfg->i32bitRate)
12064  {
12065  snprintf(p_param_err, max_err_len, "vbvMaxRate %u cannot be smaller than bitrate %d",
12066  p_cfg->ui32vbvMaxRate, p_cfg->i32bitRate);
12067  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
12068  LRETURN;
12069  }
12070  }
12071 
12072  // minrate must <= bitrate
12073  if (p_cfg->ui32vbvMinRate != 0)
12074  {
12075  if (p_cfg->ui32vbvMinRate > p_cfg->i32bitRate)
12076  {
12077  snprintf(p_param_err, max_err_len, "vbvMinRate %u cannot be larger than bitrate %d",
12078  p_cfg->ui32vbvMinRate, p_cfg->i32bitRate);
12079  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
12080  LRETURN;
12081  }
12082  }
12083 
12084  // The supported range of vbvBufferSize are 0, or 1/framerate to 3000. If the vbvBufferSize is not 0.
12085  // The minimum vbvBufferSize in msec is 1/framerate. The actual minimum in bits is bitrate/framerate.
12086  if (p_cfg->i32vbvBufferSize != 0)
12087  {
12088  // check buffer size compatible to bitrate
12089  uint32_t minVbvBufferSize = p_cfg->i32frameRateDenominator * 1000 / p_cfg->i32frameRateInfo;
12090  if (p_cfg->i32vbvBufferSize < minVbvBufferSize)
12091  {
12092  snprintf(p_param_err, max_err_len, "vbvBufferSize must be greater than the average frame size. Minimum is %u msec for framerate %d fps",
12093  minVbvBufferSize, (p_cfg->i32frameRateInfo / p_cfg->i32frameRateDenominator));
12094  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
12095  LRETURN;
12096  }
12097 
12098  // check buffer size compatible to maxrate
12099  if (p_cfg->ui32vbvMaxRate != 0)
12100  {
12101  uint32_t maxRateMinVbvBufferSize = ((int64_t)p_cfg->i32frameRateDenominator * 1000 / p_cfg->i32frameRateInfo) * p_cfg->ui32vbvMaxRate / p_cfg->i32bitRate;
12102  if (p_cfg->i32vbvBufferSize < maxRateMinVbvBufferSize)
12103  {
12104  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",
12105  maxRateMinVbvBufferSize, p_cfg->i32bitRate, p_cfg->ui32vbvMaxRate, (p_cfg->i32frameRateInfo / p_cfg->i32frameRateDenominator));
12106  warning = NI_RETCODE_PARAM_WARN;
12107  p_cfg->i32vbvBufferSize = maxRateMinVbvBufferSize;
12108  }
12109  }
12110  // check buffer size compatible to minrate (capped CRF may set minrate without maxrate)
12111  else if (p_cfg->ui32vbvMinRate != 0)
12112  {
12113  uint32_t minRateMinVbvBufferSize = ((int64_t)p_cfg->i32frameRateDenominator * 1000 / p_cfg->i32frameRateInfo) * p_cfg->ui32vbvMinRate / p_cfg->i32bitRate;
12114  if (p_cfg->i32vbvBufferSize < minRateMinVbvBufferSize)
12115  {
12116  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",
12117  minRateMinVbvBufferSize, p_cfg->i32bitRate, p_cfg->ui32vbvMinRate, (p_cfg->i32frameRateInfo / p_cfg->i32frameRateDenominator));
12118  warning = NI_RETCODE_PARAM_WARN;
12119  p_cfg->i32vbvBufferSize = minRateMinVbvBufferSize;
12120  }
12121  }
12122  }
12123  else
12124  {
12125  // check buffer size compatible to maxrate and/or minrate
12126  if ( p_cfg->ui32vbvMaxRate != 0 || p_cfg->ui32vbvMinRate != 0 )
12127  {
12128  snprintf(p_param_warn, max_err_len, "vbvMaxRate %u vbvMinRate %u does not take effect when vbvBufferSize is 0, force vbvMaxRate vbvMinRate to 0",
12129  p_cfg->ui32vbvMaxRate, p_cfg->ui32vbvMinRate);
12130  warning = NI_RETCODE_PARAM_WARN;
12131  p_cfg->ui32vbvMaxRate = 0;
12132  p_cfg->ui32vbvMinRate = 0;
12133  }
12134  }
12135 
12136  if (p_cfg->ui32ltrRefInterval || p_cfg->niParamT408.useLongTerm)
12137  {
12138  if (p_cfg->ui32ltrRefInterval && p_cfg->niParamT408.useLongTerm)
12139  {
12140  strncpy(p_param_err,
12141  "Can't enable ltrRefInterval and longTermReferenceEnable "
12142  "at same time",
12143  max_err_len);
12144  param_ret = NI_RETCODE_INVALID_PARAM;
12145  LRETURN;
12146  }
12147 
12149  {
12151  {
12152  strncpy(p_param_err, "Custom GOP size can not be > 1 for long term reference", max_err_len);
12153  param_ret = NI_RETCODE_INVALID_PARAM;
12154  LRETURN;
12155  }
12156  } else if ((p_cfg->ui8gopSize != 1) && (p_cfg->ui8gopLowdelay == 0) &&
12157  (p_cfg->niParamT408.intra_period != 1))
12158  {
12160  {
12161  strncpy(p_param_err,
12162  "Must use low delay GOP (gopPresetIdx 1,3,7,9) for long term reference",
12163  max_err_len);
12164  param_ret = NI_RETCODE_INVALID_PARAM;
12165  LRETURN;
12166  }
12167  strncpy(p_param_warn, "GOP size forced to 1 for long term reference", max_err_len);
12168  warning = NI_RETCODE_PARAM_WARN;
12169  p_cfg->ui8gopSize = 1;
12170  }
12171 
12172  if (p_cfg->ui8LookAheadDepth != 0)
12173  {
12174  strncpy(p_param_err, "lookaheadDepth must be 0 for long term reference", max_err_len);
12175  param_ret = NI_RETCODE_INVALID_PARAM;
12176  LRETURN;
12177  }
12178  }
12179 
12180  if (p_cfg->ui32setLongTermInterval && (p_cfg->niParamT408.useLongTerm == 0))
12181  {
12182  strncpy(
12183  p_param_err,
12184  "Must set longTermReferenceEnable for longTermReferenceInterval",
12185  max_err_len);
12186  param_ret = NI_RETCODE_INVALID_PARAM;
12187  LRETURN;
12188  }
12189 
12190  if (p_cfg->ui8av1ErrResilientMode)
12191  {
12192  if (STD_AV1 != p_cfg->ui8bitstreamFormat)
12193  {
12194  strncpy(p_param_warn, "AV1 err resilient mode forced to 0 when using other codecs", max_err_len);
12195  warning = NI_RETCODE_PARAM_WARN;
12196  p_cfg->ui8av1ErrResilientMode = 0;
12197  }
12198  }
12199 
12200  if (p_cfg->ui8motionConstrainedMode)
12201  {
12202  if (STD_HEVC != p_cfg->ui8bitstreamFormat)
12203  {
12204  strncpy(p_param_warn, "Motion Constrained mode force disabled for codecs other than HEVC", max_err_len);
12205  warning = NI_RETCODE_PARAM_WARN;
12206  p_cfg->ui8motionConstrainedMode = 0;
12207  }
12208  else
12209  {
12210  if (p_cfg->ui8rdoLevel != 1)
12211  {
12212  strncpy(
12213  p_param_err,
12214  "rdoLevel must be 1 for Motion Constrained mode 1 or 2",
12215  max_err_len);
12216  param_ret = NI_RETCODE_INVALID_PARAM;
12217  LRETURN;
12218  }
12219 
12221  {
12222  if (p_cfg->ui8multicoreJointMode)
12223  {
12224  strncpy(
12225  p_param_err,
12226  "multicoreJointMode must be 0 for Motion Constrained mode 2",
12227  max_err_len);
12228  param_ret = NI_RETCODE_INVALID_PARAM;
12229  LRETURN;
12230  }
12231  if (p_src->source_width % 64 || p_src->source_height % 64)
12232  {
12233  if (!p_cfg->ui32cropWidth || !p_cfg->ui32cropHeight ||
12234  p_cfg->ui32cropWidth % 64 || p_cfg->ui32cropHeight % 64)
12235  {
12236  strncpy(
12237  p_param_err,
12238  "input resolution (or cropping window) must be 64x64 aligned for Motion Constrained mode 2",
12239  max_err_len);
12240  param_ret = NI_RETCODE_INVALID_PARAM;
12241  LRETURN;
12242  }
12243  }
12244  }
12245  }
12246  }
12247 
12248  if (p_cfg->ui8avccHvcc)
12249  {
12250  if ((STD_AVC != p_cfg->ui8bitstreamFormat) && (STD_HEVC != p_cfg->ui8bitstreamFormat))
12251  {
12252  strncpy(p_param_warn, "AVCC HVCC forced to 0 for codecs other than AVC HEVC", max_err_len);
12253  warning = NI_RETCODE_PARAM_WARN;
12254  p_cfg->ui8avccHvcc = 0;
12255  }
12256  }
12257  }
12258 
12259  if (p_cfg->niParamT408.cu_size_mode < 0 ||
12260  p_cfg->niParamT408.cu_size_mode > 7)
12261  {
12262  strncpy(p_param_err, "Invalid cu_size_mode: out of range", max_err_len);
12264  LRETURN;
12265  }
12266 
12267 
12268 
12269  if (p_cfg->niParamT408.use_recommend_enc_params < 0 ||
12271  {
12272  strncpy(p_param_err, "Invalid use_recommend_enc_params: out of range", max_err_len);
12274  LRETURN;
12275  }
12276 
12277  switch (p_cfg->niParamT408.use_recommend_enc_params)
12278  {
12279  case 0:
12280  case 2:
12281  case 3:
12282  {
12283  if (p_cfg->niParamT408.use_recommend_enc_params != 3)
12284  {
12285  // in FAST mode (recommendEncParam==3), max_num_merge value will be
12286  // decided in FW
12287  if (p_cfg->niParamT408.max_num_merge < 0 ||
12288  p_cfg->niParamT408.max_num_merge > 3)
12289  {
12290  strncpy(p_param_err, "Invalid max_num_merge: out of range", max_err_len);
12292  LRETURN;
12293  }
12294  }
12295  break;
12296  }
12297 
12298  default: break;
12299  }
12300 
12301  if ( p_cfg->niParamT408.intra_qp < -1 ||
12302  p_cfg->niParamT408.intra_qp > 51 )
12303  {
12304  strncpy(p_param_err, "Invalid intra_qp: out of range", max_err_len);
12305  param_ret = NI_RETCODE_PARAM_ERROR_INTRA_QP;
12306  LRETURN;
12307  }
12308 
12309  if (QUADRA)
12310  {
12311  if (p_cfg->i8crf >= 0 && p_cfg->i8crf <= 51)
12312  {
12313  if (p_cfg->ui8LookAheadDepth < 4 || p_cfg->ui8LookAheadDepth > 40)
12314  {
12316  "6X") < 0)
12317  {
12318  strncpy(p_param_err, "CRF requres LookAheadDepth <[4-40]>", max_err_len);
12320  LRETURN;
12321  }
12322  else
12323  {
12324  p_cfg->ui8LookAheadDepth = 1;
12325  p_cfg->ui8noMbtree = 1;
12326  strncpy(p_param_warn, "enable lookahead of current frame", max_err_len);
12327  warning = NI_RETCODE_PARAM_WARN;
12328  }
12329  }
12330 
12331  if (p_cfg->ui8rcEnable == 1)
12332  {
12333  strncpy(p_param_err, "CRF requires RcEnable 0", max_err_len);
12334  param_ret = NI_RETCODE_PARAM_ERROR_RCENABLE;
12335  LRETURN;
12336  }
12337  #if 0
12338  if (p_cfg->ui8ctbRcMode > 0)
12339  {
12340  strncpy(p_param_warn, "Lookahead with cuLevelRCEnable or hvsQPEnable may degrade quality", max_err_len);
12341  warning = NI_RETCODE_PARAM_WARN;
12342  //LRETURN;
12343  }
12344  #endif
12345  }
12346 
12348  {
12349  if (p_cfg->ui8LookAheadDepth == 0)
12350  {
12351  strncpy(p_param_err, "tuneBframeVisual level 1 (medium) requires lookahead or crf encode", max_err_len);
12352  param_ret = NI_RETCODE_PARAM_ERROR_RCENABLE;
12353  LRETURN;
12354  }
12355  }
12356  }
12357 
12358  if ( p_cfg->niParamT408.enable_mb_level_rc != 1 &&
12359  p_cfg->niParamT408.enable_mb_level_rc != 0 )
12360  {
12361  strncpy(p_param_err, "Invalid enable_mb_level_rc: out of range", max_err_len);
12362  param_ret = NI_RETCODE_PARAM_ERROR_RCENABLE;
12363  LRETURN;
12364  }
12365 
12366  {
12367  if ( p_cfg->niParamT408.minQpI < 0 ||
12368  p_cfg->niParamT408.minQpI > 51 )
12369  {
12370  strncpy(p_param_err, "Invalid min_qp: out of range", max_err_len);
12371  param_ret = NI_RETCODE_PARAM_ERROR_MN_QP;
12372  LRETURN;
12373  }
12374 
12375  if ( p_cfg->niParamT408.maxQpI < 0 ||
12376  p_cfg->niParamT408.maxQpI > 51 )
12377  {
12378  strncpy(p_param_err, "Invalid max_qp: out of range", max_err_len);
12379  param_ret = NI_RETCODE_PARAM_ERROR_MX_QP;
12380  LRETURN;
12381  }
12382 
12383  if ( p_cfg->niParamT408.enable_cu_level_rate_control != 1 &&
12385  {
12386  strncpy(p_param_err, "Invalid enable_cu_level_rate_control: out of range", max_err_len);
12388  LRETURN;
12389  }
12390 
12391  //if (p_cfg->niParamT408.enable_cu_level_rate_control == 1)
12392  {
12393  if ( p_cfg->niParamT408.enable_hvs_qp != 1 &&
12394  p_cfg->niParamT408.enable_hvs_qp != 0 )
12395  {
12396  strncpy(p_param_err, "Invalid enable_hvs_qp: out of range", max_err_len);
12398  LRETURN;
12399  }
12400 
12401  if (p_cfg->niParamT408.enable_hvs_qp)
12402  {
12403  if ( p_cfg->niParamT408.max_delta_qp < 0 ||
12404  p_cfg->niParamT408.max_delta_qp > 51 )
12405  {
12406  strncpy(p_param_err, "Invalid max_delta_qp: out of range", max_err_len);
12408  LRETURN;
12409  }
12410  }
12411  }
12412  // hrd is off when i32vbvBufferSize is 0
12413  if ((p_cfg->i32vbvBufferSize < 10 && p_cfg->i32vbvBufferSize != 0) || p_cfg->i32vbvBufferSize > 3000)
12414  {
12415  strncpy(p_param_err, "Invalid i32vbvBufferSize: out of range", max_err_len);
12417  LRETURN;
12418  }
12419  }
12420 
12421  // check valid for common param
12422  param_ret = ni_check_common_params(&p_cfg->niParamT408, p_src, p_param_err, max_err_len);
12423  if (param_ret != NI_RETCODE_SUCCESS)
12424  {
12425  LRETURN;
12426  }
12427 
12428  // check valid for RC param
12429  param_ret = ni_check_ratecontrol_params(p_cfg, p_param_err, max_err_len);
12430  if (param_ret != NI_RETCODE_SUCCESS)
12431  {
12432  LRETURN;
12433  }
12434 
12435  if (warning == NI_RETCODE_PARAM_WARN && param_ret == NI_RETCODE_SUCCESS)
12436  {
12437  param_ret = NI_RETCODE_PARAM_WARN;
12438  strncpy(p_param_err, p_param_warn, max_err_len);
12439  }
12440 
12441 END:
12442  free(p_param_warn);
12443  return param_ret;
12444 }
12445 
12447  ni_xcoder_params_t *p_src,
12448  char *p_param_err, uint32_t max_err_len)
12449 {
12451  int32_t low_delay = 0;
12452  int32_t intra_period_gop_step_size;
12453  int32_t i, j;
12454 
12455  if (!p_param || !p_src || !p_param_err)
12456  {
12457  ni_log(NI_LOG_ERROR, "ERROR: %s() Null pointer parameters passed\n",
12458  __func__);
12460  LRETURN;
12461  }
12462 
12463  //Zero out the error buffer
12464  memset(p_param_err, 0, max_err_len);
12465 
12466  // check low-delay gop structure
12467  if (!QUADRA)
12468  {
12469  if (0 == p_param->gop_preset_index) // common gop
12470  {
12471  if (p_param->custom_gop_params.custom_gop_size > 1)
12472  {
12473  int minVal = p_param->custom_gop_params.pic_param[0].poc_offset;
12474  low_delay = 1;
12475  for (i = 1; i < p_param->custom_gop_params.custom_gop_size; i++)
12476  {
12477  if (minVal > p_param->custom_gop_params.pic_param[i].poc_offset)
12478  {
12479  low_delay = 0;
12480  break;
12481  } else
12482  {
12483  minVal = p_param->custom_gop_params.pic_param[i].poc_offset;
12484  }
12485  }
12486  }
12487  }
12488  else if (p_param->gop_preset_index == 2 ||
12489  p_param->gop_preset_index == 3 ||
12490  p_param->gop_preset_index == 6 ||
12491  p_param->gop_preset_index == 7) // low-delay case (IPPP, IBBB)
12492  {
12493  low_delay = 1;
12494  }
12495 
12496  if (low_delay)
12497  {
12498  intra_period_gop_step_size = 1;
12499  }
12500  else
12501  {
12502  if (p_param->gop_preset_index == GOP_PRESET_IDX_CUSTOM)
12503  {
12504  intra_period_gop_step_size = p_param->custom_gop_params.custom_gop_size;
12505  }
12506  else
12507  {
12508  intra_period_gop_step_size = presetGopSize[p_param->gop_preset_index];
12509  }
12510  }
12511 
12512  if (((p_param->intra_period != 0) && ((p_param->intra_period < intra_period_gop_step_size+1) == 1)) ||
12513  ((p_param->avcIdrPeriod != 0) && ((p_param->avcIdrPeriod < intra_period_gop_step_size+1) == 1)))
12514  {
12515  strncpy(p_param_err, "Invalid intra_period and gop_preset_index: gop structure is larger than intra period", max_err_len);
12517  LRETURN;
12518  }
12519 
12520  if (((!low_delay) && (p_param->intra_period != 0) && ((p_param->intra_period % intra_period_gop_step_size) != 0)) ||
12521  ((!low_delay) && (p_param->avcIdrPeriod != 0) && ((p_param->avcIdrPeriod % intra_period_gop_step_size) != 0)))
12522  {
12523  strncpy(p_param_err, "Invalid intra_period and gop_preset_index: intra period is not a multiple of gop structure size", max_err_len);
12525  LRETURN;
12526  }
12527 
12528  if (p_param->gop_preset_index == GOP_PRESET_IDX_CUSTOM)
12529  {
12530  int temp_poc[NI_MAX_GOP_NUM];
12531  int min_poc = p_param->custom_gop_params.pic_param[0].poc_offset;
12532  for (i = 0; i < p_param->custom_gop_params.custom_gop_size; i++)
12533  {
12535  {
12536  strncpy(p_param_err, "Invalid custom gop parameters: temporal_id larger than 7", max_err_len);
12538  LRETURN;
12539  }
12540 
12541  if (p_param->custom_gop_params.pic_param[i].temporal_id < 0)
12542  {
12543  strncpy(p_param_err, "Invalid custom gop parameters: temporal_id is zero or negative", max_err_len);
12545  LRETURN;
12546  }
12547  temp_poc[i] = p_param->custom_gop_params.pic_param[i].poc_offset;
12548  if (min_poc > temp_poc[i])
12549  {
12550  min_poc = temp_poc[i];
12551  }
12552  }
12553  int count_pos = 0;
12554  for (i = 0; i < p_param->custom_gop_params.custom_gop_size; i++)
12555  {
12556  for (j = 0; j < p_param->custom_gop_params.custom_gop_size; j++)
12557  {
12558  if (temp_poc[j] == min_poc)
12559  {
12560  count_pos++;
12561  min_poc++;
12562  }
12563  }
12564  }
12565  if (count_pos != p_param->custom_gop_params.custom_gop_size)
12566  {
12567  strncpy(p_param_err, "Invalid custom gop parameters: poc_offset is invalid", max_err_len);
12569  LRETURN;
12570  }
12571  }
12572  }
12573  else // QUADRA
12574  {
12575  if (p_param->custom_gop_params.custom_gop_size)
12576  {
12577  int temp_poc[NI_MAX_GOP_NUM];
12578  int min_poc = p_param->custom_gop_params.pic_param[0].poc_offset;
12579  for (i = 0; i < p_param->custom_gop_params.custom_gop_size; i++)
12580  {
12582  {
12583  strncpy(p_param_err, "Invalid custom gop parameters: temporal_id larger than 7", max_err_len);
12585  LRETURN;
12586  }
12587 
12588  if (p_param->custom_gop_params.pic_param[i].temporal_id < 0)
12589  {
12590  strncpy(p_param_err, "Invalid custom gop parameters: temporal_id is negative", max_err_len);
12592  LRETURN;
12593  }
12594 
12595  for (j = 0; j < p_param->custom_gop_params.pic_param[i].num_ref_pics; j++)
12596  {
12597  if (p_param->custom_gop_params.pic_param[i].rps[j].ref_pic == 0)
12598  {
12599  strncpy(p_param_err, "Invalid custom gop parameters: ref pic delta cannot be 0", max_err_len);
12601  LRETURN;
12602  }
12603  }
12604 
12605  for (j = 0; j < NI_MAX_REF_PIC; j++)
12606  {
12607  if (p_param->custom_gop_params.pic_param[i].rps[j].ref_pic != 0 &&
12608  p_param->custom_gop_params.pic_param[i].rps[j].ref_pic_used == -1)
12609  {
12610  ni_log(NI_LOG_ERROR,"g%drefPic%d specified without g%drefPic%dUsed specified!\n", i, j, i, j);
12612  LRETURN;
12613  }
12614  }
12615 
12616  temp_poc[i] = p_param->custom_gop_params.pic_param[i].poc_offset;
12617  if (min_poc > temp_poc[i])
12618  {
12619  min_poc = temp_poc[i];
12620  }
12621  }
12622  int count_pos = 0;
12623  for (i = 0; i < p_param->custom_gop_params.custom_gop_size; i++)
12624  {
12625  for (j = 0; j < p_param->custom_gop_params.custom_gop_size; j++)
12626  {
12627  if (temp_poc[j] == min_poc)
12628  {
12629  count_pos++;
12630  min_poc++;
12631  }
12632  }
12633  }
12634  if (count_pos != p_param->custom_gop_params.custom_gop_size)
12635  {
12636  strncpy(p_param_err, "Invalid custom gop parameters: poc_offset is invalid", max_err_len);
12638  LRETURN;
12639  }
12640  }
12641  }
12642 
12643  if (0 == p_param->use_recommend_enc_params)
12644  {
12645  // RDO
12646  {
12647  int align_32_width_flag = p_src->source_width % 32;
12648  int align_16_width_flag = p_src->source_width % 16;
12649  int align_8_width_flag = p_src->source_width % 8;
12650  int align_32_height_flag = p_src->source_height % 32;
12651  int align_16_height_flag = p_src->source_height % 16;
12652  int align_8_height_flag = p_src->source_height % 8;
12653 
12654  if (((p_param->cu_size_mode & 0x1) == 0) && ((align_8_width_flag != 0) || (align_8_height_flag != 0)))
12655  {
12656  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);
12658  LRETURN;
12659  }
12660  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)))
12661  {
12662  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);
12664  LRETURN;
12665  }
12666  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)))
12667  {
12668  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);
12670  LRETURN;
12671  }
12672  }
12673  }
12674 
12675  if ((p_param->conf_win_top < 0) || (p_param->conf_win_top > 8192))
12676  {
12677  strncpy(p_param_err, "Invalid conf_win_top: out of range", max_err_len);
12679  LRETURN;
12680  }
12681  if (p_param->conf_win_top % 2)
12682  {
12683  strncpy(p_param_err, "Invalid conf_win_top: not multiple of 2", max_err_len);
12685  LRETURN;
12686  }
12687 
12688  if ((p_param->conf_win_bottom < 0) || (p_param->conf_win_bottom > 8192))
12689  {
12690  strncpy(p_param_err, "Invalid conf_win_bottom: out of range", max_err_len);
12692  LRETURN;
12693  }
12694  if (p_param->conf_win_bottom % 2)
12695  {
12696  strncpy(p_param_err, "Invalid conf_win_bottom: not multiple of 2", max_err_len);
12698  LRETURN;
12699  }
12700 
12701  if ((p_param->conf_win_left < 0) || (p_param->conf_win_left > 8192))
12702  {
12703  strncpy(p_param_err, "Invalid conf_win_left: out of range", max_err_len);
12705  LRETURN;
12706  }
12707  if (p_param->conf_win_left % 2)
12708  {
12709  strncpy(p_param_err, "Invalid conf_win_left: not multiple of 2", max_err_len);
12711  LRETURN;
12712  }
12713 
12714  if (p_param->conf_win_right < 0 || p_param->conf_win_right > 8192)
12715  {
12716  strncpy(p_param_err, "Invalid conf_win_right: out of range", max_err_len);
12718  LRETURN;
12719  }
12720  if (p_param->conf_win_right % 2)
12721  {
12722  strncpy(p_param_err, "Invalid conf_win_right: not multiple of 2", max_err_len);
12724  }
12725 
12726 END:
12727 
12728  return ret;
12729 }
12730 
12731 ni_retcode_t ni_check_ratecontrol_params(ni_encoder_config_t* p_cfg, char* p_param_err, uint32_t max_err_len)
12732 {
12734  ni_t408_config_t* p_param = NULL;
12735 
12736  if( (!p_cfg) || (!p_param_err) )
12737  {
12738  ni_log(NI_LOG_ERROR, "ERROR: %s() Null pointer parameters passed\n",
12739  __func__);
12741  LRETURN;
12742  }
12743  p_param = &p_cfg->niParamT408;
12744 
12745  //Zero out the error buffer
12746  memset(p_param_err, 0, max_err_len);
12747 
12748  if (p_param->roiEnable != 0 && p_param->roiEnable != 1)
12749  {
12750  strncpy(p_param_err, "Invalid roiEnable: out of range", max_err_len);
12752  LRETURN;
12753  }
12754 
12755  if (p_param->roiEnable && p_param->enable_hvs_qp)
12756  {
12757  strncpy(p_param_err, "hvsQPEnable and roiEnable: not mutually exclusive", max_err_len);
12759  LRETURN;
12760  }
12761 
12762  if (p_cfg->ui8rcEnable == 1)
12763  {
12764  if (p_param->minQpP > p_param->maxQpP || p_param->minQpB > p_param->maxQpB)
12765  {
12766  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);
12768  LRETURN;
12769  }
12770  }
12771 
12772 END:
12773 
12774  return ret;
12775 }
12776 
12777 
12778 /*!******************************************************************************
12779  * \brief Print xcoder user configurations
12780  *
12781  * \param
12782  *
12783  * \return
12784  *******************************************************************************/
12785 void ni_params_print(ni_xcoder_params_t *const p_encoder_params)
12786 {
12787  if (!p_encoder_params)
12788  {
12789  return;
12790  }
12791 
12792  ni_encoder_cfg_params_t *p_enc = &p_encoder_params->cfg_enc_params;
12793 
12794  ni_log(NI_LOG_DEBUG, "XCoder Params:\n");
12795 
12796  ni_log(NI_LOG_DEBUG, "preset=%d\n", p_encoder_params->preset);
12797  ni_log(NI_LOG_DEBUG, "fps_number / fps_denominator=%u / %u\n",
12798  p_encoder_params->fps_number,
12799  p_encoder_params->fps_denominator);
12800 
12801  ni_log(NI_LOG_DEBUG, "source_width x source_height=%dx%d\n", p_encoder_params->source_width, p_encoder_params->source_height);
12802  ni_log(NI_LOG_DEBUG, "bitrate=%d\n", p_encoder_params->bitrate);
12803 
12804  ni_log(NI_LOG_DEBUG, "profile=%d\n", p_enc->profile);
12805  ni_log(NI_LOG_DEBUG, "level_idc=%d\n", p_enc->level_idc);
12806  ni_log(NI_LOG_DEBUG, "high_tier=%d\n", p_enc->high_tier);
12807 
12808  ni_log(NI_LOG_DEBUG, "frame_rate=%d\n", p_enc->frame_rate);
12809 
12810  ni_log(NI_LOG_DEBUG, "use_recommend_enc_params=%d\n", p_enc->use_recommend_enc_params);
12811  ni_log(NI_LOG_DEBUG, "cu_size_mode=%d\n", p_enc->cu_size_mode);
12812  ni_log(NI_LOG_DEBUG, "max_num_merge=%d\n", p_enc->max_num_merge);
12813  ni_log(NI_LOG_DEBUG, "enable_dynamic_8x8_merge=%d\n", p_enc->enable_dynamic_8x8_merge);
12814  ni_log(NI_LOG_DEBUG, "enable_dynamic_16x16_merge=%d\n", p_enc->enable_dynamic_16x16_merge);
12815  ni_log(NI_LOG_DEBUG, "enable_dynamic_32x32_merge=%d\n", p_enc->enable_dynamic_32x32_merge);
12816  // trans_rate not available in Rev B
12817  ni_log(NI_LOG_DEBUG, "enable_rate_control=%d\n", p_enc->rc.enable_rate_control);
12818  ni_log(NI_LOG_DEBUG, "enable_cu_level_rate_control=%d\n", p_enc->rc.enable_cu_level_rate_control);
12819  ni_log(NI_LOG_DEBUG, "enable_hvs_qp=%d\n", p_enc->rc.enable_hvs_qp);
12820  ni_log(NI_LOG_DEBUG, "enable_hvs_qp_scale=%d\n", p_enc->rc.enable_hvs_qp_scale);
12821  ni_log(NI_LOG_DEBUG, "hvs_qp_scale=%d\n", p_enc->rc.hvs_qp_scale);
12822  ni_log(NI_LOG_DEBUG, "min_qp=%d\n", p_enc->rc.min_qp);
12823  ni_log(NI_LOG_DEBUG, "max_qp=%d\n", p_enc->rc.max_qp);
12824  ni_log(NI_LOG_DEBUG, "max_delta_qp=%d\n", p_enc->rc.max_delta_qp);
12825  ni_log(NI_LOG_DEBUG, "vbv_buffer_size=%d\n", p_enc->rc.vbv_buffer_size);
12826  ni_log(NI_LOG_DEBUG, "enable_filler=%d\n", p_enc->rc.enable_filler);
12827  ni_log(NI_LOG_DEBUG, "enable_pic_skip=%d\n", p_enc->rc.enable_pic_skip);
12828 
12829  ni_log(NI_LOG_DEBUG, "forcedHeaderEnable=%d\n", p_enc->forced_header_enable);
12830  ni_log(NI_LOG_DEBUG, "roi_enable=%d\n", p_enc->roi_enable);
12831  ni_log(NI_LOG_DEBUG, "long_term_ref_enable=%d\n", p_enc->long_term_ref_enable);
12832  ni_log(NI_LOG_DEBUG, "long_term_ref_interval=%d\n", p_enc->long_term_ref_interval);
12833  ni_log(NI_LOG_DEBUG, "long_term_ref_count=%d\n", p_enc->long_term_ref_count);
12834  ni_log(NI_LOG_DEBUG, "conf_win_top=%d\n", p_enc->conf_win_top);
12835  ni_log(NI_LOG_DEBUG, "conf_win_bottom=%d\n", p_enc->conf_win_bottom);
12836  ni_log(NI_LOG_DEBUG, "conf_win_left=%d\n", p_enc->conf_win_left);
12837  ni_log(NI_LOG_DEBUG, "conf_win_right=%d\n", p_enc->conf_win_right);
12838 
12839  ni_log(NI_LOG_DEBUG, "intra_qp=%d\n", p_enc->rc.intra_qp);
12840  ni_log(NI_LOG_DEBUG, "enable_mb_level_rc=%d\n", p_enc->rc.enable_mb_level_rc);
12841 
12842  ni_log(NI_LOG_DEBUG, "intra_period=%d\n", p_enc->intra_period);
12843  ni_log(NI_LOG_DEBUG, "decoding_refresh_type=%d\n", p_enc->decoding_refresh_type);
12844 
12845  // Rev. B: H.264 only or HEVC-shared parameters, in ni_t408_config_t
12846  ni_log(NI_LOG_DEBUG, "enable_transform_8x8=%d\n", p_enc->enable_transform_8x8);
12847  ni_log(NI_LOG_DEBUG, "slice_mode=%d\n", p_enc->slice_mode);
12848  ni_log(NI_LOG_DEBUG, "slice_arg=%d\n", p_enc->slice_arg);
12849  ni_log(NI_LOG_DEBUG, "entropy_coding_mode=%d\n", p_enc->entropy_coding_mode);
12850  ni_log(NI_LOG_DEBUG, "intra_mb_refresh_mode=%d\n", p_enc->intra_mb_refresh_mode);
12851  ni_log(NI_LOG_DEBUG, "intra_mb_refresh_arg=%d\n", p_enc->intra_mb_refresh_arg);
12852  ni_log(NI_LOG_DEBUG, "intra_reset_refresh=%d\n", p_enc->intra_reset_refresh);
12853 
12854  ni_log(NI_LOG_DEBUG, "gop_preset_index=%d\n", p_enc->gop_preset_index);
12855 #ifndef QUADRA
12856  if (!QUADRA)
12857  {
12859  {
12860  int i;
12861  ni_log(NI_LOG_DEBUG, "custom_gop_params.custom_gop_size=%d\n", p_enc->custom_gop_params.custom_gop_size);
12862  for (i = 0; i < p_enc->custom_gop_params.custom_gop_size; i++)
12863  {
12864  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);
12865  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);
12866  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);
12867  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);
12868  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);
12869  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);
12870  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);
12871  }
12872  }
12873  }
12874  else // QUADRA
12875 #endif
12876  {
12878  {
12879  int i, j;
12880  ni_log(NI_LOG_DEBUG, "custom_gop_params.custom_gop_size=%d\n", p_enc->custom_gop_params.custom_gop_size);
12881  for (i = 0; i < NI_MAX_GOP_NUM; i++)
12882  {
12883  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);
12884  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);
12885  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);
12886  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);
12887  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);
12888  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);
12889  for (j = 0; j < NI_MAX_REF_PIC; j++)
12890  {
12891  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);
12892  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);
12893  }
12894  }
12895  }
12896  }
12897 
12898  return;
12899 }
12900 
12901 /*!******************************************************************************
12902  * \brief decoder keep alive thread function triggers every 1 second
12903  *
12904  * \param void thread args
12905  *
12906  * \return void
12907  *******************************************************************************/
12908 void *ni_session_keep_alive_thread(void *arguments)
12909 {
12911  ni_thread_arg_struct_t *args = (ni_thread_arg_struct_t *)arguments;
12912  ni_session_stats_t inst_info = {0};
12913  ni_session_context_t ctx = {0};
12914  uint64_t endtime = ni_gettime_ns();
12915  uint64_t current_time;
12916  ni_pthread_mutex_t *p_mutex;
12917  //interval(nanoseconds) is equals to ctx.keep_alive_timeout/3(330,000,000ns approximately equal to 1/3 second).
12918  uint64_t interval = args->keep_alive_timeout * 330000000LL;
12919 #ifndef _ANDROID
12920 #ifdef __linux__
12921  struct sched_param sched_param;
12922 
12923  // Linux has a wide variety of signals, Windows has a few.
12924  // A large number of signals will interrupt the thread, which will cause heartbeat command interval more than 1 second.
12925  // So just mask the unuseful signals in Linux
12926  sigset_t signal;
12927  sigfillset(&signal);
12928  ni_pthread_sigmask(SIG_BLOCK, &signal, NULL);
12929 
12930  /* set up schedule priority
12931  * first try to run with RR mode.
12932  * if fails, try to set nice value.
12933  * if fails either, ignore it and run with default priority.
12934  * Note: Scheduling requires root permission. App is probably exectued
12935  * without root so the priority for this thread might just end up
12936  * being default.
12937  */
12938  if (((sched_param.sched_priority = sched_get_priority_max(SCHED_RR)) ==
12939  -1) ||
12940  sched_setscheduler(syscall(SYS_gettid), SCHED_RR, &sched_param) < 0)
12941  {
12942  ni_log(NI_LOG_DEBUG, "%s cannot set scheduler: %s\n", __func__,
12943  strerror(NI_ERRNO));
12944  if (setpriority(PRIO_PROCESS, 0, -20) != 0)
12945  {
12946  ni_log(NI_LOG_DEBUG, "%s cannot set nice value: %s\n", __func__,
12947  strerror(NI_ERRNO));
12948  }
12949  }
12950 
12951 #elif defined(_WIN32)
12952  /* set up schedule priority.
12953  * try to set the current thread to time critical level which is the highest prioriy
12954  * level.
12955  */
12956  if (SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL) ==
12957  0)
12958  {
12959  ni_log(NI_LOG_DEBUG, "%s cannot set priority: %d.\n", __func__,
12960  GetLastError());
12961  }
12962 #endif
12963 #endif
12964 #ifndef _WIN32
12965  // Set thread name, name is KAT + hw_id + session_id
12966  // hw_id need at most 2 bytes, session_id at most 4 bytes.
12967  char name[16] = {0};
12968  snprintf(name, sizeof(name), "%s%.2x%.4x", "KAT", args->hw_id, args->session_id);
12969 #if __linux__
12970  prctl(PR_SET_NAME, name);
12971 #elif __APPLE__
12972  pthread_setname_np(name);
12973 #endif
12974 #endif
12975  // Initializes the session context variables that keep alive command and query status command need.
12977  ctx.last_access_time = endtime;
12978  ctx.hw_id = args->hw_id;
12979  ctx.session_id = args->session_id;
12981  ctx.device_type = args->device_type;
12982  ctx.blk_io_handle = args->device_handle;
12983  ctx.event_handle = args->thread_event_handle;
12984  ctx.p_all_zero_buf = args->p_buffer;
12986  volatile uint64_t * plast_access_time = args->plast_access_time;
12987  if((ctx.last_access_time - *plast_access_time) >=
12988  ctx.keep_alive_timeout * 1000000000LL)
12989  {
12991  "%s creation timeout. session_id=0x%X requested timeout: %" PRIu64
12992  "ns, ping time delta: %" PRIu64 "ns\n ",
12993  __func__, ctx.session_id,
12994  (uint64_t)ctx.keep_alive_timeout * 1000000000LL,
12995  ctx.last_access_time - *plast_access_time);
12996  }
12997  ni_log(NI_LOG_DEBUG, "%s ctx.keep_alive_timeout: %us.\n", __func__,
12998  ctx.keep_alive_timeout);
12999  p_mutex = args->p_mutex;
13000 
13001  for (;;)
13002  {
13003  ni_pthread_mutex_lock(p_mutex);
13004 
13005  retval =
13007  ctx.event_handle, ctx.p_all_zero_buf);
13008 
13009  retval = ni_query_session_stats(&ctx,
13010  ctx.device_type,
13011  &inst_info,
13012  retval,
13014 
13015  if (NI_RETCODE_SUCCESS == retval)
13016  {
13019  ctx.device_type,
13020  ctx.hw_id,
13021  &(ctx.session_id));
13022  }
13023 
13024  ni_pthread_mutex_unlock(p_mutex);
13025 
13026  if(retval)
13027  {
13028  uint32_t error_status = inst_info.ui32LastTransactionCompletionStatus;
13029  if(error_status == NI_RETCODE_SUCCESS)
13030  {
13031  /* QDFWSH-971: Error is sometimes captured by keep_alive_thread
13032  but LastTransactionCompletionStatus may be overwrited and cause
13033  incorrect log. In this case, check LastErrorStatus.*/
13034  ni_log(NI_LOG_ERROR, "session_no 0x%x inst_err_no may be overwrited!\n",
13035  ctx.session_id);
13038  ctx.device_type,
13039  ctx.hw_id,
13040  &(ctx.session_id));
13041  error_status = inst_info.ui32LastErrorStatus;
13042  }
13044  "Persistent failures detected, %s() line-%d: session_no 0x%x sess_err_no %u "
13045  "inst_err_no %u\n",
13046  __func__, __LINE__, ctx.session_id, inst_info.ui16ErrorCount, error_status);
13047  LRETURN;
13048  }
13050  /*If the interval between two heartbeats is greater then expected(interval) or
13051  acceptable(timeout) then the thread might have been blocked.*/
13052  if ((current_time - ctx.last_access_time) >= (2 * interval) || //*2 is for safety
13053  (current_time - ctx.last_access_time) >=
13054  args->keep_alive_timeout * 1000000000LL)
13055  {
13056  ni_log(
13057  NI_LOG_INFO,
13058  "%s was possibly blocked. session_id=0x%X requested timeout: %" PRIu64
13059  "ns, ping time delta: %" PRIu64 "ns\n ",
13060  __func__, ctx.session_id,
13061  (uint64_t)ctx.keep_alive_timeout * 1000000000LL,
13063  }
13064  *plast_access_time = ctx.last_access_time = current_time;
13065  if (ctx.session_id == NI_INVALID_SESSION_ID)
13066  {
13068  }
13069 
13070  // 1. If received failure, set the close_thread flag to TRUE, and exit,
13071  // then main thread will check this flag and return failure directly;
13072  // 2. skip checking VPU recovery.
13073  // If keep_alive thread detect the VPU RECOVERY before main thread,
13074  // the close_thread flag may damage the vpu recovery handling process.
13075  if ((NI_RETCODE_SUCCESS != retval) &&
13076  (NI_RETCODE_NVME_SC_VPU_RECOVERY != retval))
13077  {
13078  LRETURN;
13079  }
13080  endtime += interval;
13081  while (ni_gettime_ns() < endtime)
13082  {
13083  if (args->close_thread)
13084  {
13085  LRETURN;
13086  }
13087  ni_usleep(10000); // 10ms per loop
13088  }
13089  }
13090 
13091 END:
13092 
13093  if (NI_RETCODE_SUCCESS != retval)
13094  {
13095  ni_log(NI_LOG_ERROR, "%s abnormal closed:%d\n", __func__, retval);
13096  // changing the value to be True here means the thread has been closed.
13097  args->close_thread = true;
13098  }
13099 
13101 
13102  ni_log(NI_LOG_DEBUG, "%s(): exit\n", __func__);
13103 
13104  return NULL;
13105 }
13106 
13107 /*!******************************************************************************
13108 * \brief Open a xcoder upload instance
13109 *
13110 * \param p_ctx - pointer to caller allocated uploader session context
13111 *
13112 * \return
13113 * On success
13114 * NI_RETCODE_SUCCESS
13115 *
13116 * On failure
13117 * NI_RETCODE_INVALID_PARAM
13118 * NI_RETCODE_ERROR_MEM_ALOC
13119 * NI_RETCODE_ERROR_INVALID_SESSION
13120 * NI_RETCODE_FAILURE
13121 *******************************************************************************/
13123 {
13125  void * p_buffer = NULL;
13126  uint32_t ui32LBA = 0;
13127  uint32_t modelled_load;
13128 
13129  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
13130 
13131  if (!p_ctx)
13132  {
13133  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): passed parameters are null!, return\n",
13134  __func__);
13135  retval = NI_RETCODE_INVALID_PARAM;
13136  LRETURN;
13137  }
13138 
13139  //Create the session if the create session flag is set
13140  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
13141  {
13143  p_ctx->pts_table = NULL;
13144  p_ctx->dts_queue = NULL;
13145  p_ctx->p_leftover = NULL;
13146  p_ctx->buffer_pool = NULL;
13147  p_ctx->prev_size = 0;
13148  p_ctx->sent_size = 0;
13149  p_ctx->status = 0;
13150  p_ctx->key_frame_type = 0;
13151  p_ctx->ready_to_close = 0;
13152  p_ctx->rc_error_count = 0;
13153  p_ctx->frame_num = 0;
13154  p_ctx->pkt_num = 0;
13155  p_ctx->pkt_index = 0;
13156 
13157  //malloc zero data buffer
13158  if (ni_posix_memalign(&p_ctx->p_all_zero_buf, sysconf(_SC_PAGESIZE),
13160  {
13161  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() alloc all zero buffer failed\n",
13162  NI_ERRNO, __func__);
13163  retval = NI_RETCODE_ERROR_MEM_ALOC;
13164  LRETURN;
13165  }
13166  memset(p_ctx->p_all_zero_buf, 0, NI_DATA_BUFFER_LEN);
13167 
13168  //malloc data buffer
13169  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), NI_DATA_BUFFER_LEN))
13170  {
13171  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() alloc data buffer failed\n",
13172  NI_ERRNO, __func__);
13173  retval = NI_RETCODE_ERROR_MEM_ALOC;
13174  LRETURN;
13175  }
13176  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
13177 
13178  //Set session ID to be invalid. In case we cannot open session, the session id wold remain invalid.
13179  //In case we can open sesison, the session id would become valid.
13180  ((ni_session_stats_t *)p_buffer)->ui16SessionId =
13181  (uint16_t)NI_INVALID_SESSION_ID;
13182 
13183  // First uint32_t is either an invaild session ID or a valid session ID, depending on if session could be opened
13184  ui32LBA = OPEN_SESSION_CODEC(NI_DEVICE_TYPE_ENCODER, ni_htonl(p_ctx->codec_format), 1/*1 for uploadMode*/);
13185  retval = ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
13186  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
13187  if (retval != NI_RETCODE_SUCCESS)
13188  {
13189  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR ni_nvme_send_read_cmd\n");
13190  LRETURN;
13191  }
13192  //Open will return a session status structure with a valid session id if it worked.
13193  //Otherwise the invalid session id set before the open command will stay
13194  p_ctx->session_id = ni_ntohs(((ni_session_stats_t *)p_buffer)->ui16SessionId);
13195  p_ctx->session_timestamp = ni_htonl(((ni_session_stats_t *)p_buffer)->ui32Session_timestamp_high);
13196  p_ctx->session_timestamp = (p_ctx->session_timestamp << 32) |
13197  ni_htonl(((ni_session_stats_t *)p_buffer)->ui32Session_timestamp_low);
13198  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
13199  {
13200  ni_log2(p_ctx, NI_LOG_ERROR,
13201  "ERROR %s(): p_ctx->device_handle=%" PRIx64
13202  ", p_ctx->hw_id=%d, p_ctx->session_id=%d\n",
13203  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
13204  p_ctx->session_id);
13205  ni_encoder_session_close(p_ctx, 0);
13207  LRETURN;
13208  }
13209  ni_log2(p_ctx, NI_LOG_DEBUG,
13210  "Uploader open session ID:0x%x,timestamp:%" PRIu64 "\n",
13211  p_ctx->session_id, p_ctx->session_timestamp);
13212 
13213  //Send keep alive timeout Info
13214  uint64_t keep_alive_timeout =
13215  p_ctx->keep_alive_timeout * 1000000; //send us to FW
13216  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
13217  memcpy(p_buffer, &keep_alive_timeout, sizeof(keep_alive_timeout));
13218  ni_log2(p_ctx, NI_LOG_DEBUG, "%s keep_alive_timeout %" PRIx64 "\n", __func__,
13219  keep_alive_timeout);
13221  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
13222  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
13223  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config, p_ctx->device_type,
13224  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
13225  CHECK_VPU_RECOVERY(retval);
13226 
13227  if (NI_RETCODE_SUCCESS != retval)
13228  {
13229  ni_log2(p_ctx, NI_LOG_ERROR,
13230  "ERROR %s(): nvme write keep_alive_timeout command "
13231  "failed, blk_io_handle: %" PRIx64 ", hw_id, %d\n",
13232  __func__, (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id);
13234  LRETURN;
13235  }
13236 
13237  // only update firmware with pixel rate if firmware >= 6rf
13238  if (ni_cmp_fw_api_ver(
13239  (char *) &p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6rf") >= 0)
13240  {
13241  /* Add the modelled load of the hwuploader */
13242  if (p_ctx->framerate.framerate_denom == 0)
13243  {
13244  // unknown or variable frame rate. Assume 25 fps
13245  modelled_load = (p_ctx->active_video_width * p_ctx->active_video_height) * 25;
13246  }
13247  else
13248  {
13249  modelled_load = (p_ctx->active_video_width * p_ctx->active_video_height *
13251  }
13252 
13253  // modelled_load can overflow a 4-byte value so we will downscale it to
13254  // kilopixels per sec by shifting the modelled load down by 10 bits.
13255  modelled_load >>= 10;
13256 
13258 
13259  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
13260  memcpy(p_buffer, &modelled_load, 4);
13261 
13262  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
13263  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
13264 
13265  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config, p_ctx->device_type,
13266  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
13267  CHECK_VPU_RECOVERY(retval);
13268 
13269  if (NI_RETCODE_SUCCESS != retval)
13270  {
13272  LRETURN;
13273  }
13274  }
13275 
13276  ni_log2(p_ctx, NI_LOG_DEBUG, "Open session completed\n");
13277  }
13278 
13279  // init for frame pts calculation
13280  p_ctx->is_first_frame = 1;
13281  p_ctx->last_pts = 0;
13282  p_ctx->last_dts = 0;
13283 
13284  ni_timestamp_init(p_ctx, &p_ctx->pts_table, "dec_pts");
13285  ni_timestamp_init(p_ctx, &p_ctx->dts_queue, "dec_dts");
13286 
13287  //p_ctx->active_video_width = 0;
13288  //p_ctx->active_video_height = 0;
13289 
13290  ni_log2(p_ctx, NI_LOG_DEBUG,
13291  "%s(): p_ctx->device_handle=%" PRIx64 ", p_ctx->hw_id=%d, "
13292  "p_ctx->session_id=%d\n",
13293  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
13294  p_ctx->session_id);
13295 
13296  p_ctx->hw_action = NI_CODEC_HW_NONE;
13297 #ifndef _WIN32
13298  // If this is a P2P upload session, open the Netint kernel driver
13299  if (p_ctx->isP2P)
13300  {
13301  retval = p2p_fill_pcie_address(p_ctx);
13302  if(retval != NI_RETCODE_SUCCESS)
13303  {
13304  LRETURN;
13305  }
13306  }
13307 #endif
13308 
13309 END:
13310 
13311  ni_aligned_free(p_buffer);
13312  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
13313  return retval;
13314 }
13315 
13316 /*!******************************************************************************
13317 * \brief Copy a xcoder decoder worker thread info
13318 *
13319 * \param
13320 *
13321 * \return
13322 *******************************************************************************/
13324 {
13325  if (!src_p_ctx || !dst_p_ctx)
13326  {
13327  ni_log(NI_LOG_ERROR, "ERROR %s(): passed parameters are null!, return\n", __func__);
13328  return NI_RETCODE_INVALID_PARAM;
13329  }
13330 
13331  dst_p_ctx->pext_mutex = src_p_ctx->pext_mutex; //for hwdl
13332  dst_p_ctx->max_nvme_io_size = src_p_ctx->max_nvme_io_size;
13333  dst_p_ctx->device_handle = src_p_ctx->device_handle;
13334  dst_p_ctx->blk_io_handle = src_p_ctx->blk_io_handle;
13335  dst_p_ctx->hw_id = src_p_ctx->hw_id;
13336  dst_p_ctx->session_timestamp = src_p_ctx->session_timestamp;
13337  memcpy(dst_p_ctx->fw_rev, src_p_ctx->fw_rev, sizeof(src_p_ctx->fw_rev));
13338  if (src_p_ctx->isP2P)
13339  {
13340  dst_p_ctx->isP2P = src_p_ctx->isP2P;
13341  dst_p_ctx->ddr_config = src_p_ctx->ddr_config;
13342  dst_p_ctx->domain = src_p_ctx->domain;
13343  dst_p_ctx->bus = src_p_ctx->bus;
13344  dst_p_ctx->dev = src_p_ctx->dev;
13345  dst_p_ctx->fn = src_p_ctx->fn;
13346  dst_p_ctx->netint_fd = src_p_ctx->netint_fd;
13347  }
13348 
13349  return NI_RETCODE_SUCCESS;
13350 }
13351 
13352 
13353 /*!******************************************************************************
13354  * \brief Query and acquire buffer from xcoder scaler instance
13355  *
13356  * \param[in] p_ctx pointer to scaler session context
13357  *
13358  * \return NI_RETCODE_INVALID_PARAM
13359  * NI_RETCODE_ERROR_INVALID_SESSION
13360  * NI_RETCODE_ERROR_MEM_ALOC
13361  * NI_RETCODE_ERROR_NVME_CMD_FAILED
13362  * NI_RETCODE_FAILURE
13363  *******************************************************************************/
13365 {
13367  ni_instance_buf_info_t sInstanceBuf = {0};
13368  int query_retry = 0;
13369 
13370  if (p_ctx->session_id == NI_INVALID_SESSION_ID)
13371  {
13372  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
13373  __func__);
13375  }
13376 
13377  if (p_ctx->buffered_frame_index > 0)
13378  {
13379  ni_log2(p_ctx, NI_LOG_ERROR,
13380  "Session=0x%x: %s buffered_frame_index=%d return %d\n",
13381  p_ctx->session_id,
13382  __func__,
13383  p_ctx->buffered_frame_index,
13384  retval);
13385  return retval;
13386  }
13387 
13388  for (;;)
13389  {
13390  query_sleep(p_ctx);
13391  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: query by ni_query_instance_buf_info INST_BUF_INFO_RW_UPLOAD\n", __func__);
13393  NI_DEVICE_TYPE_SCALER, &sInstanceBuf);
13394 
13395  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
13396  p_ctx->device_type, p_ctx->hw_id,
13397  &(p_ctx->session_id), OPT_3);
13398 
13399  /* A frame index of zero is invalid, the memory acquisition failed */
13400  if (NI_RETCODE_SUCCESS != retval || (sInstanceBuf.hw_inst_ind.frame_index == 0 && query_retry >=500))
13401  {
13402  if (query_retry >=500)
13403  {
13404  ni_log2(p_ctx, NI_LOG_DEBUG,
13405  "scaler write exceeded max query retries. rc=%d try=%d\n",
13406  retval, query_retry);
13407  }
13408  retval = NI_RETCODE_ERROR_MEM_ALOC;
13409  LRETURN;
13410  }
13411  if (sInstanceBuf.hw_inst_ind.frame_index == 0 && query_retry < 500)
13412  {
13413  query_retry++;
13414  ni_pthread_mutex_unlock(&p_ctx->mutex);
13415  ni_usleep(100);
13416  ni_pthread_mutex_lock(&p_ctx->mutex);
13417  }
13418  else
13419  {
13420  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: FID %d\n", __func__,
13421  sInstanceBuf.hw_inst_ind.frame_index);
13422  p_ctx->buffered_frame_index = sInstanceBuf.hw_inst_ind.frame_index;
13423  break;
13424  }
13425  }
13426 
13427  ni_log2(p_ctx, NI_LOG_DEBUG,
13428  "Session=0x%x: %s got FrameIndex=%u buffered_frame_index=%d\n",
13429  p_ctx->session_id,
13430  __func__,
13431  sInstanceBuf.hw_inst_ind.frame_index,p_ctx->buffered_frame_index);
13432 
13433 END:
13434 
13435  if (NI_RETCODE_SUCCESS != retval)
13436  {
13437  ni_log2(p_ctx, NI_LOG_DEBUG,
13438  "%s Warning scalar read hwdesc fail rc %d or ind !\n",
13439  __func__, retval);
13440  }
13441  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
13442 
13443  return retval;
13444 }
13445 
13446 
13447 /*!******************************************************************************
13448 * \brief Query and acquire buffer from xcoder upload instance
13449 *
13450 * \param p_ctx pointer to uploader session context
13451 *
13452 * \return NI_RETCODE_SUCCESS
13453 * NI_RETCODE_ERROR_INVALID_SESSION
13454 * NI_RETCODE_ERROR_MEM_ALOC
13455 *******************************************************************************/
13457 {
13459  //uint32_t metadata_size = NI_APP_ENC_FRAME_META_DATA_SIZE;
13460  uint32_t frame_size_bytes = 0;
13461  uint32_t retry_count = 0;
13462  ni_instance_buf_info_t buf_info = { 0 };
13463 
13464  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
13465 
13466  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
13467  {
13468  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
13469  __func__);
13471  LRETURN;
13472  }
13473 
13474 //#ifdef MEASURE_LATENCY
13475 #if 0
13476  if ((p_frame->pts != NI_NOPTS_VALUE) && (p_ctx->frame_time_q != NULL))
13477  {
13478  uint64_t abs_time_ns = ni_gettime_ns();
13480  abs_time_ns, p_frame->pts);
13481  }
13482 #endif
13483 
13484  //p_ctx->status = 0;
13485 
13486  if (p_ctx->buffered_frame_index > 0)
13487  return retval;
13488 
13489  for (;;)
13490  {
13491  query_sleep(p_ctx);
13492 
13494  NI_DEVICE_TYPE_ENCODER, &buf_info);
13495  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
13496  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
13497  if (NI_RETCODE_SUCCESS != retval ||
13498  (buf_info.hw_inst_ind.buffer_avail == 0 && retry_count >= 500))
13499  {
13500  if (retry_count >= 500)
13501  {
13502  ni_log2(p_ctx, NI_LOG_DEBUG,
13503  "hwupload write exceeded max query retries. rc=%d try=%d"
13504  "\n",
13505  retval, retry_count);
13506  }
13507  retval = NI_RETCODE_ERROR_MEM_ALOC;
13508  //p_ctx->status = NI_RETCODE_NVME_SC_WRITE_BUFFER_FULL;
13509  LRETURN;
13510  }
13511  if (buf_info.hw_inst_ind.buffer_avail == 0 && retry_count < 500)
13512  {
13513  retry_count++;
13514  ni_pthread_mutex_unlock(&p_ctx->mutex);
13515  ni_usleep(100);
13516  ni_pthread_mutex_lock(&p_ctx->mutex);
13517  }
13518  else
13519  {
13520  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: avail %d, FID %d\n", __func__,
13521  buf_info.hw_inst_ind.buffer_avail,
13522  buf_info.hw_inst_ind.frame_index);
13523 
13524  p_ctx->buffered_frame_index = buf_info.hw_inst_ind.frame_index;
13525  break;
13526  }
13527  }
13528  ni_log2(p_ctx, NI_LOG_DEBUG, "Info hwupload write query success, available buf "
13529  "size %u >= frame size %u , retry %u\n",
13530  buf_info.buf_avail_size, frame_size_bytes, retry_count);
13531 
13532 //#ifdef MEASURE_LATENCY
13533 #if 0
13534  if ((p_frame->pts != NI_NOPTS_VALUE) && (p_ctx->frame_time_q != NULL))
13535  {
13536  uint64_t abs_time_ns = ni_gettime_ns();
13538  ni_log(NI_LOG_INFO, "PTS:%" PRId64 ",DELTA:%" PRId64 ",uLAT:%" PRIu64 ";\n",
13539  p_frame->pts, abs_time_ns - q->last_benchmark_time,
13540  ni_lat_meas_q_check_latency(q, abs_time_ns, p_frame->pts));
13541  q->last_benchmark_time = abs_time_ns;
13542  }
13543 #endif
13544 
13545  //retval = size;
13546 
13547 END:
13548 
13549  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
13550  return retval;
13551 }
13552 
13553 
13554 /*!******************************************************************************
13555 * \brief Send a YUV p_frame to upload session
13556 *
13557 * \param
13558 *
13559 * \return
13560 *******************************************************************************/
13562  niFrameSurface1_t *hwdesc)
13563 {
13564  int retval = 0;
13565  uint32_t size = 0;
13566  //uint32_t metadata_size = NI_APP_ENC_FRAME_META_DATA_SIZE;
13567  uint32_t i = 0;
13568  uint32_t sent_size = 0;
13569  uint32_t frame_size_bytes = 0;
13570  uint32_t retry_count = 0;
13571  ni_instance_buf_info_t buf_info = { 0 };
13572 
13573  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
13574  //niFrameSurface1_t* p_data3 = (niFrameSurface1_t*)((uint8_t*)p_frame->p_data[3]);
13576  //ni_log2(p_ctx, NI_LOG_DEBUG, "%s:mar16 HW=%d ui16FrameIdx=%d i8InstID=%d device_handle=%d\n",
13577  // ishwframe, p_data3->ui16FrameIdx, p_data3->i8InstID, p_data3->device_handle);
13578 
13579  if (!p_ctx || !p_frame)
13580  {
13581  ni_log(NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
13582  __func__);
13583  retval = NI_RETCODE_INVALID_PARAM;
13584  LRETURN;
13585  }
13586 
13587  uint8_t separate_metadata = p_frame->separate_metadata;
13588  uint8_t separate_start = (p_frame->separate_start && p_frame->total_start_len) ? 1 : 0;
13589 
13590  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
13591  {
13592  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
13593  __func__);
13595  LRETURN;
13596  }
13597 
13598 #ifdef MEASURE_LATENCY
13599  if ((p_frame->pts != NI_NOPTS_VALUE) && (p_ctx->frame_time_q != NULL))
13600  {
13601  uint64_t abs_time_ns = ni_gettime_ns();
13603  abs_time_ns, p_frame->pts);
13604  }
13605 #endif
13606 
13607  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;
13608  ni_log2(p_ctx, NI_LOG_DEBUG, "frame size bytes =%u %d is metadata!\n", frame_size_bytes,
13609  0);
13610  p_ctx->status = 0;
13611 
13612  if (p_frame->end_of_stream)
13613  {
13614  retval = NI_RETCODE_SUCCESS;
13615  LRETURN;
13616  }
13617 
13618  if (p_ctx->buffered_frame_index > 0)
13619  {
13620  buf_info.hw_inst_ind.frame_index = p_ctx->buffered_frame_index;
13621  p_ctx->buffered_frame_index = 0;
13622  }
13623  else
13624  {
13625  for (;;)
13626  {
13627  query_sleep(p_ctx);
13628 
13630  NI_DEVICE_TYPE_ENCODER, &buf_info);
13631  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
13632  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
13633  if (NI_RETCODE_SUCCESS != retval ||
13634  (buf_info.hw_inst_ind.buffer_avail == 0 && retry_count >= 500))
13635  {
13636  if (retry_count >= 500)
13637  {
13638  ni_log2(p_ctx, NI_LOG_DEBUG,
13639  "hwupload write exceeded max query retries. rc=%d try=%d"
13640  "\n",
13641  retval, retry_count);
13642  }
13643  retval = NI_RETCODE_ERROR_MEM_ALOC;
13645  LRETURN;
13646  }
13647  if (buf_info.hw_inst_ind.buffer_avail == 0 && retry_count < 500)
13648  {
13649  retry_count++;
13650  ni_pthread_mutex_unlock(&p_ctx->mutex);
13651  ni_usleep(100);
13652  ni_pthread_mutex_lock(&p_ctx->mutex);
13653  } else //available
13654  {
13655  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: avail %d, FID %d\n", __func__,
13656  buf_info.hw_inst_ind.buffer_avail,
13657  buf_info.hw_inst_ind.frame_index);
13658  break;
13659  }
13660  }
13661  ni_log2(p_ctx, NI_LOG_DEBUG, "Info hwupload write query success, available buf "
13662  "size %u >= frame size %u , retry %u\n",
13663  buf_info.buf_avail_size, frame_size_bytes, retry_count);
13664  }
13665 
13666  {
13667 #ifdef XCODER_TIMESTAMP_DTS_ENABLED
13668  retval = ni_timestamp_register(p_ctx->buffer_pool, p_ctx->dts_queue,
13669  p_frame->dts, 0);
13670  if (NI_RETCODE_SUCCESS != retval)
13671  {
13672  ni_log2(p_ctx, NI_LOG_ERROR,
13673  "ERROR %s(): ni_timestamp_register() for dts "
13674  "returned: %d\n",
13675  __func__, retval);
13676  }
13677 #endif
13678 
13679  //Apply write configuration here
13680  retval = ni_config_session_rw(p_ctx, SESSION_WRITE_CONFIG, 1,
13681  NI_CODEC_HW_UPLOAD, 0);
13682  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
13683  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
13684  CHECK_VPU_RECOVERY(retval);
13685 
13686  if (separate_metadata)
13687  {
13689  "6S") < 0)
13690  {
13691  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): uploader separated metadata not supported on device with FW api version < 6.S\n",
13692  __func__);
13694  LRETURN;
13695  }
13696 
13697  if (!p_frame->p_metadata_buffer)
13698  {
13699  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): p_metadata_buffer is NULL, allocation failed?\n",
13700  __func__);
13701  retval = NI_RETCODE_ERROR_MEM_ALOC;
13702  LRETURN;
13703  }
13704 
13705  // fill in metadata
13706  ni_metadata_enc_frame_t *p_meta;
13707  p_meta = (ni_metadata_enc_frame_t *)p_frame->p_metadata_buffer;
13708 
13709  if (separate_start)
13710  {
13711  for (i = 0; i < NI_MAX_NUM_SW_FRAME_DATA_POINTERS; i++)
13712  p_meta->start_len[i] = p_frame->start_len[i];
13713  }
13714  else
13715  {
13716  memset(p_meta->start_len, 0, sizeof(p_meta->start_len));
13717  }
13719 
13720  ni_log(
13721  NI_LOG_DEBUG,
13722  "%s(): %d.%u p_ctx->frame_num=%" PRIu64 ", "
13723  "p_frame->video_width=%u, p_frame->video_height=%u, "
13724  "start_len [%u,%u,%u] inconsecutive_transfer %u\n",
13725  __func__, p_ctx->hw_id, p_ctx->session_id, p_ctx->frame_num,
13726  p_frame->video_width, p_frame->video_height,
13727  p_meta->start_len[0], p_meta->start_len[1], p_meta->start_len[2],
13728  p_meta->inconsecutive_transfer);
13729 
13730  uint32_t ui32LBA_metadata =
13732  ni_log2(p_ctx, NI_LOG_DEBUG,
13733  "%s: p_metadata_buffer = %p, metadata_buffer_size "
13734  "= %u, p_ctx->frame_num = %" PRIu64 ", LBA = 0x%x\n",
13735  __func__, p_frame->p_metadata_buffer,
13736  p_frame->metadata_buffer_size, p_ctx->frame_num,
13737  ui32LBA_metadata);
13738 
13739  sent_size =
13741 
13742  retval = ni_nvme_send_write_cmd(
13743  p_ctx->blk_io_handle, p_ctx->event_handle,
13744  p_frame->p_metadata_buffer, sent_size,
13745  ui32LBA_metadata);
13746  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_write, p_ctx->device_type,
13747  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
13748  CHECK_VPU_RECOVERY(retval);
13749  if (retval < 0)
13750  {
13751  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n",
13752  __func__);
13754  LRETURN;
13755  }
13756  }
13757 
13758  if (separate_start)
13759  {
13760  if (!p_frame->p_start_buffer)
13761  {
13762  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): p_start_buffer is NULL, allocation failed?\n",
13763  __func__);
13764  retval = NI_RETCODE_ERROR_MEM_ALOC;
13765  LRETURN;
13766  }
13767 
13768  uint32_t ui32LBA =
13770  ni_log2(p_ctx, NI_LOG_DEBUG,
13771  "%s: p_start_buffer = %p, p_frame->start_buffer_size "
13772  "= %u, p_ctx->frame_num = %" PRIu64 ", LBA = 0x%x\n",
13773  __func__, p_frame->p_start_buffer, p_frame->start_buffer_size, p_ctx->frame_num,
13774  ui32LBA);
13775 
13776  sent_size =
13778 
13779  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
13780  p_frame->p_start_buffer, sent_size, ui32LBA);
13781  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_write, p_ctx->device_type,
13782  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
13783  CHECK_VPU_RECOVERY(retval);
13784  if (retval < 0)
13785  {
13786  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
13788  LRETURN;
13789  }
13790  }
13791 
13792  if (p_frame->inconsecutive_transfer)
13793  {
13794  uint32_t ui32LBA =
13796 
13797  for (i = 0; i < NI_MAX_NUM_SW_FRAME_DATA_POINTERS; i++)
13798  {
13799  ni_log2(p_ctx, NI_LOG_DEBUG,
13800  "%s: p_data = %p, p_frame->buffer_size "
13801  "= %u, p_ctx->frame_num = %" PRIu64 ", LBA = 0x%x\n",
13802  __func__, p_frame->p_data, p_frame->buffer_size, p_ctx->frame_num,
13803  ui32LBA);
13804 
13805  if (p_frame->data_len[i])
13806  {
13807  sent_size = p_frame->data_len[i];
13808  if (separate_start)
13809  sent_size -= p_frame->start_len[i];
13810 
13811  sent_size =
13813 
13814  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
13815  p_frame->p_data[i]+p_frame->start_len[i], sent_size, ui32LBA);
13816  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_write, p_ctx->device_type,
13817  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
13818  CHECK_VPU_RECOVERY(retval);
13819  if (retval < 0)
13820  {
13821  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
13823  LRETURN;
13824  }
13825  }
13826  }
13827  }
13828  else
13829  {
13830  uint32_t ui32LBA =
13832  ni_log2(p_ctx, NI_LOG_DEBUG,
13833  "%s: p_data = %p, p_frame->buffer_size = %u, "
13834  "p_ctx->frame_num = %" PRIu64 ", LBA = 0x%x\n",
13835  __func__, p_frame->p_data, p_frame->buffer_size, p_ctx->frame_num,
13836  ui32LBA);
13837 
13838  sent_size = frame_size_bytes;
13839  if (separate_start)
13840  sent_size -= p_frame->total_start_len;
13841 
13842  sent_size =
13844 
13845  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
13846  p_frame->p_buffer+p_frame->total_start_len, sent_size, ui32LBA);
13847  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_write, p_ctx->device_type,
13848  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
13849  CHECK_VPU_RECOVERY(retval);
13850  if (retval < 0)
13851  {
13852  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
13854  LRETURN;
13855  }
13856  }
13857 
13858  hwdesc->ui16FrameIdx = buf_info.hw_inst_ind.frame_index;
13859  hwdesc->ui16session_ID = p_ctx->session_id;
13860  hwdesc->device_handle =
13861  (int32_t)((int64_t)p_ctx->blk_io_handle & 0xFFFFFFFF);
13862  hwdesc->bit_depth = p_ctx->bit_depth_factor;
13863  hwdesc->src_cpu = (uint8_t)NI_DEVICE_TYPE_ENCODER;
13864  hwdesc->output_idx = hwdesc->ui32nodeAddress = 0;
13865 
13866  p_ctx->frame_num++;
13867  size = frame_size_bytes;
13868 
13869 #ifdef XCODER_DUMP_DATA
13870  char dump_file[256];
13871  snprintf(dump_file, sizeof(dump_file), "%ld-%u-hwup-fme/fme-%04ld.yuv",
13872  (long)getpid(), p_ctx->session_id, (long)p_ctx->frame_num);
13873 
13874  FILE *f = fopen(dump_file, "wb");
13875  fwrite(p_frame->p_buffer,
13876  p_frame->data_len[0] + p_frame->data_len[1] + p_frame->data_len[2],
13877  1, f);
13878  fflush(f);
13879  fclose(f);
13880 #endif
13881  }
13882 
13883 #ifdef MEASURE_LATENCY
13884  if ((p_frame->pts != NI_NOPTS_VALUE) && (p_ctx->frame_time_q != NULL))
13885  {
13886  uint64_t abs_time_ns = ni_gettime_ns();
13888  ni_log(NI_LOG_INFO, "PTS:%" PRId64 ",DELTA:%" PRId64 ",uLAT:%" PRIu64 ";\n",
13889  p_frame->pts, abs_time_ns - q->last_benchmark_time,
13890  ni_lat_meas_q_check_latency(q, abs_time_ns, p_frame->pts));
13891  q->last_benchmark_time = abs_time_ns;
13892  }
13893 #endif
13894 
13895  retval = size;
13896 
13897 END:
13898 
13899  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
13900  return retval;
13901 }
13902 
13903 /*!******************************************************************************
13904 * \brief Retrieve a HW descriptor of uploaded frame
13905 *
13906 * \param p_ctx pointer to uploader session context
13907 * hwdesc pointer to hw descriptor
13908 *
13909 * \return
13910 * On success
13911 * NI_RETCODE_SUCCESS
13912 * On failure
13913 * NI_RETCODE_INVALID_PARAM
13914 * NI_RETCODE_ERROR_INVALID_SESSION
13915 * NI_RETCODE_FAILURE
13916 *******************************************************************************/
13918  niFrameSurface1_t *hwdesc)
13919 {
13920  int retval = 0;
13921  ni_instance_buf_info_t hwdesc_info = { 0 };
13922  int query_retry = 0;
13923 
13924  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
13925 
13926  if (!p_ctx || !hwdesc)
13927  {
13928  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
13929  __func__);
13930  retval = NI_RETCODE_INVALID_PARAM;
13931  LRETURN;
13932  }
13933 
13934  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
13935  {
13936  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
13937  __func__);
13939  LRETURN;
13940  }
13941 
13942  for (;;)
13943  {
13944  query_sleep(p_ctx);
13945 
13946  query_retry++;
13947 #ifndef _WIN32
13949  NI_DEVICE_TYPE_ENCODER, &hwdesc_info);
13950 #else
13952  NI_DEVICE_TYPE_ENCODER, &hwdesc_info);
13953 #endif
13954  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
13955  p_ctx->device_type, p_ctx->hw_id,
13956  &(p_ctx->session_id), OPT_1);
13957 
13958  if (NI_RETCODE_SUCCESS != retval)
13959  {
13960  ni_log2(p_ctx, NI_LOG_DEBUG, "Warning upload read hwdesc fail rc %d or ind "
13961  "!\n", retval);
13962 
13963  if (query_retry >= 1000)
13964  {
13965  retval = NI_RETCODE_FAILURE;
13966  LRETURN;
13967  }
13968  ni_usleep(100);
13969  }
13970  else
13971  {
13972  ni_log2(p_ctx, NI_LOG_DEBUG, "Info hwupload read hwdesc success, "
13973  "frame_ind=%d !\n", hwdesc_info.hw_inst_ind.frame_index);
13974 
13975  hwdesc->ui16FrameIdx = hwdesc_info.hw_inst_ind.frame_index;
13976  hwdesc->ui16session_ID = p_ctx->session_id;
13977  hwdesc->device_handle =
13978  (int32_t)((int64_t)p_ctx->blk_io_handle & 0xFFFFFFFF);
13979  hwdesc->bit_depth = p_ctx->bit_depth_factor;
13980  hwdesc->src_cpu = (uint8_t)NI_DEVICE_TYPE_ENCODER;
13981  hwdesc->output_idx = 0;
13982  LRETURN;
13983  }
13984  }
13985 
13986 END:
13987  return retval;
13988 }
13989 
13990 /*!*****************************************************************************
13991 * \brief clear a particular xcoder instance buffer/data
13992 *
13993 * \param ni_session_context_t p_ctx - xcoder Context
13994 * \param ni_instance_buf_info_rw_type_t rw_type
13995 * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
13996 * \param ni_instance_buf_info_t *out - Struct preallocated from the caller
13997 * where the resulting data will be placed
13998 *
13999 * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION,
14000 * NI_RETCODE_ERROR_MEM_ALOC or NI_RETCODE_ERROR_NVME_CMD_FAILED on
14001 * failure
14002 ******************************************************************************/
14004 {
14005  void* p_buffer = NULL;
14007  uint32_t ui32LBA = 0;
14008 
14009  ni_log(NI_LOG_TRACE, "%s(): enter - device_handle %d\n", __func__,
14010  surface->device_handle);
14011 
14012  if ((uint16_t)NI_INVALID_SESSION_ID == surface->ui16session_ID)
14013  {
14014  ni_log(NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
14015  __func__);
14017  LRETURN;
14018  }
14019 
14020  //malloc data buffer
14021  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), NI_DATA_BUFFER_LEN))
14022  {
14023  ni_log(NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
14024  NI_ERRNO, __func__);
14025  retval = NI_RETCODE_ERROR_MEM_ALOC;
14026  LRETURN;
14027  }
14028  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
14029  ni_log(NI_LOG_DEBUG, "%s(): FID = %d\n", __func__, surface->ui16FrameIdx);
14030  ui32LBA = CLEAR_INSTANCE_BUF_W(((uint16_t)surface->ui16FrameIdx));
14031  retval = ni_nvme_send_write_cmd((ni_device_handle_t)(int64_t)surface->device_handle,
14032  NI_INVALID_DEVICE_HANDLE, p_buffer,
14033  NI_DATA_BUFFER_LEN, ui32LBA);
14034  //Cannot check sessio stats here since this isn't a session command.
14035  if (retval < 0)
14036  {
14037  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__);
14039  LRETURN;
14040  }
14041 
14042 END:
14043 
14044  ni_aligned_free(p_buffer);
14045  ni_log(NI_LOG_TRACE, "%s(): exit\n", __func__);
14046  return retval;
14047 }
14048 
14049 /*!******************************************************************************
14050 * \brief Retrieve a hw desc p_frame from decoder
14051 * \param
14052 *
14053 * \return
14054 *******************************************************************************/
14056 {
14057  //Needs serious editing to support hwdesc read again, this is currently vanilla read
14058  //queue_info decoder_read_workerqueue;
14059  ni_instance_mgr_stream_info_t data = {0};
14060  int rx_size = 0;
14061  uint64_t frame_offset = 0;
14062  uint8_t *p_data_buffer = NULL;
14063  int i = 0;
14064  int retval = NI_RETCODE_SUCCESS;
14065  int metadata_hdr_size = NI_FW_META_DATA_SZ -
14067  int sei_size = 0;
14068  uint32_t total_bytes_to_read = 0;
14069  uint32_t total_yuv_met_size = 0;
14070  uint32_t read_size_bytes = 0;
14071  ni_instance_buf_info_t buf_info = {0};
14072  int query_retry = 0;
14073  uint32_t ui32LBA = 0;
14074  unsigned int bytes_read_so_far = 0;
14075  int query_type = INST_BUF_INFO_RW_READ;
14076  int low_delay_notify = 0;
14077  ni_session_statistic_t sessionStatistic = {0};
14078  uint32_t frames_dropped = 0;
14079  ni_xcoder_params_t *p_param;
14080  uint8_t get_first_metadata = 0;
14081  uint8_t sequence_change = 0;
14082 
14083  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
14084 
14085  if (!p_ctx || !p_frame)
14086  {
14087  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
14088  __func__);
14089  return NI_RETCODE_INVALID_PARAM;
14090  }
14091 
14092  ni_pthread_mutex_lock(&p_ctx->mutex);
14093 
14094 start:
14095  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
14096  {
14097  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
14098  __func__);
14100  LRETURN;
14101  }
14102 
14103  p_param = (ni_xcoder_params_t *)p_ctx->p_session_config;
14104  p_data_buffer = (uint8_t *)p_frame->p_buffer;
14105 
14106  // p_frame->p_data[] can be NULL before actual resolution is returned by
14107  // decoder and buffer pool is allocated, so no checking here.
14108 
14109  total_bytes_to_read = p_frame->data_len[3] + metadata_hdr_size;
14110  total_yuv_met_size = p_frame->data_len[0] + p_frame->data_len[1] + p_frame->data_len[2] + metadata_hdr_size;
14111  ni_log2(p_ctx, NI_LOG_DEBUG,
14112  "Total bytes to read %u total_yuv_met_size %u, low_delay %u\n",
14113  total_bytes_to_read, total_yuv_met_size, p_ctx->decoder_low_delay);
14114  if (p_ctx->decoder_low_delay > 0 && !p_ctx->ready_to_close)
14115  {
14116  ni_log2(p_ctx, NI_LOG_DEBUG, "frame_num = %" PRIu64 ", pkt_num = %" PRIu64 "\n",
14117  p_ctx->frame_num, p_ctx->pkt_num);
14118  frames_dropped = p_ctx->session_statistic.ui32FramesDropped;
14119  if (p_ctx->force_low_delay && (p_ctx->force_low_delay_cnt < frames_dropped)) {
14120  p_ctx->force_low_delay_cnt = frames_dropped;
14121  }
14122  if (p_ctx->frame_num + p_ctx->force_low_delay_cnt
14123  >= p_ctx->pkt_num)
14124  {
14125  //nothing to query, leave
14126  retval = NI_RETCODE_SUCCESS;
14127  LRETURN;
14128  }
14129  query_type = INST_BUF_INFO_RW_READ_BUSY;
14130  }
14131  for (;;)
14132  {
14133  query_sleep(p_ctx);
14134 
14135  query_retry++;
14136 
14137  if (ni_cmp_fw_api_ver(
14139  "6r3") >= 0)
14140  {
14142  &sessionStatistic);
14143  CHECK_ERR_RC(p_ctx, retval, &sessionStatistic,
14145  p_ctx->hw_id, &(p_ctx->session_id), OPT_2);
14146  CHECK_VPU_RECOVERY(retval);
14147 
14148  buf_info.buf_avail_size = sessionStatistic.ui32RdBufAvailSize;
14149  } else
14150  {
14151  retval = ni_query_instance_buf_info(p_ctx, query_type,
14152  NI_DEVICE_TYPE_DECODER, &buf_info);
14153  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
14154  p_ctx->device_type, p_ctx->hw_id,
14155  &(p_ctx->session_id), OPT_1);
14156  CHECK_VPU_RECOVERY(retval);
14157  }
14158 
14159  ni_log2(p_ctx, NI_LOG_TRACE, "Dec read desc query buf_info.size = %u\n",
14160  buf_info.buf_avail_size);
14161 
14162  if (NI_RETCODE_SUCCESS != retval)
14163  {
14164  ni_log2(p_ctx, NI_LOG_TRACE, "Dec read desc failed. Retry %d\n", query_retry);
14165 
14166  if (query_retry >= 1000)
14167  {
14168  ni_log2(p_ctx, NI_LOG_DEBUG, "Warning: dec read desc failed %d retries. rc=%d"
14169  "\n", query_retry, retval);
14170  p_ctx->max_retry_fail_count[1]++;
14171  low_delay_notify = 1;
14173  LRETURN;
14174  }
14175  ni_pthread_mutex_unlock(&p_ctx->mutex);
14177  ni_pthread_mutex_lock(&p_ctx->mutex);
14178  } else if (buf_info.buf_avail_size == DP_IPC_PASSTHRU)
14179  {
14180  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): Bad available buffer size %u\n", __FUNCTION__, buf_info.buf_avail_size);
14181  retval = NI_RETCODE_FAILURE;
14182  LRETURN;
14183  } else if (buf_info.buf_avail_size == metadata_hdr_size)
14184  {
14185  ni_log2(p_ctx, NI_LOG_DEBUG,
14186  "Dec read desc only hdr metadata is available. Seq change may "
14187  "have occured.\n");
14188  total_bytes_to_read = metadata_hdr_size;
14189  sequence_change = 1;
14190  break;
14191  } else if (buf_info.buf_avail_size < total_yuv_met_size)
14192  {
14193  ni_log2(p_ctx, NI_LOG_TRACE, "Dec read desc buf_size < frame_size. Retry %d\n", query_retry);
14194 
14195  // query to see if it is eos now, if we have sent it
14196  if (p_ctx->ready_to_close)
14197  {
14198  ni_log2(p_ctx, NI_LOG_TRACE,
14199  "Dec read desc query, ready_to_close %u, query eos\n",
14200  p_ctx->ready_to_close);
14201  retval = ni_query_stream_info(p_ctx, NI_DEVICE_TYPE_DECODER, &data);
14202  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
14203  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
14204  CHECK_VPU_RECOVERY(retval);
14205 
14206  if (data.is_flushed ||
14207  query_retry >=
14209  {
14210  if (query_retry >=
14212  {
14213  ni_log2(p_ctx, NI_LOG_ERROR,
14214  "WARNING: Dec read desc query eos reached but exceeded max "
14215  "retries. is_flushed=%u try=%d.\n",
14216  data.is_flushed, query_retry);
14217  } else
14218  {
14219  ni_log2(p_ctx, NI_LOG_DEBUG,
14220  "Dec read desc query eos reached. is_flushed=%u try=%d"
14221  "\n",
14222  data.is_flushed, query_retry);
14223  }
14224  p_frame->end_of_stream = 1;
14225  low_delay_notify = 1;
14226  retval = NI_RETCODE_SUCCESS;
14227  LRETURN;
14228  }
14229  else
14230  {
14231  ni_log2(p_ctx, NI_LOG_TRACE,
14232  "Dec read desc available buf size == %d, query try %d, "
14233  "retrying...\n",
14234  buf_info.buf_avail_size, query_retry);
14235  ni_pthread_mutex_unlock(&p_ctx->mutex);
14237  ni_pthread_mutex_lock(&p_ctx->mutex);
14238  continue;
14239  }
14240  }
14241 
14243  (p_ctx->decoder_low_delay > 0 &&
14244  ((p_ctx->frame_num + p_ctx->force_low_delay_cnt)
14245  < p_ctx->pkt_num))) &&
14246  query_retry < 1000 / 2)
14247  {
14248  if (p_ctx->decoder_low_delay && p_ctx->force_low_delay) {
14249  if (p_ctx->session_statistic.ui32FramesDropped > frames_dropped) {
14250  // last pkt sent to decoder marked as dropped, no output,
14251  // so just stop query and return
14252  p_ctx->force_low_delay_cnt++;
14253  low_delay_signal(p_ctx);
14254  retval = NI_RETCODE_SUCCESS;
14255  LRETURN;
14256  }
14257  } else if (NI_RETCODE_NVME_SC_WRITE_BUFFER_FULL == p_ctx->status &&
14258  sessionStatistic.ui32WrBufAvailSize > p_ctx->required_buf_size)
14259  {
14260  ni_log2(p_ctx, NI_LOG_TRACE, "Info dec write buffer is enough, available buf "
14261  "size %u >= required size %u !\n",
14262  sessionStatistic.ui32WrBufAvailSize, p_ctx->required_buf_size);
14263  p_ctx->status = 0;
14264  p_ctx->required_buf_size = 0;
14265  retval = NI_RETCODE_SUCCESS;
14266  LRETURN;
14267  }
14268  ni_pthread_mutex_unlock(&p_ctx->mutex);
14270  ni_pthread_mutex_lock(&p_ctx->mutex);
14271 
14272  continue;
14273  } else
14274  {
14275  if (p_ctx->decoder_low_delay > 0)
14276  {
14277  if (p_ctx->force_low_delay) {
14278  p_ctx->force_low_delay_cnt++;
14279  low_delay_signal(p_ctx);
14280  } else {
14281  ni_log2(p_ctx, NI_LOG_ERROR,"Warning: low delay mode with non sequential "
14282  "input (B frames)? Just cancel the low delay mode then\n");
14283  // Here it should be the last signal to release the send thread
14284  // holding the low delay mutex.
14285  low_delay_signal(p_ctx);
14286  p_ctx->decoder_low_delay = 0;
14287  }
14288  }
14289 
14290  if ((p_param->dec_input_params.min_packets_delay && p_ctx->pkt_delay_cnt))
14291  {
14292  if(p_ctx->pkt_num >= (p_ctx->frame_num + p_ctx->pkt_delay_cnt +
14294  {
14295  if(query_retry <= 2000)
14296  {
14297  ni_pthread_mutex_unlock(&p_ctx->mutex);
14298  ni_usleep(25);
14299  ni_pthread_mutex_lock(&p_ctx->mutex);
14300  continue;
14301  } else {
14302  p_ctx->pkt_delay_cnt++;
14303  ni_log2(p_ctx, NI_LOG_ERROR,
14304  "Warning: decoder pkt_num %u frame_num %u "
14305  "timeout, increasing pkt_delay_cnt to %u\n",
14306  p_ctx->pkt_num, p_ctx->frame_num,
14307  p_ctx->pkt_delay_cnt);
14308  }
14309  }
14310  }
14311  ni_log2(p_ctx, NI_LOG_DEBUG, "Warning: dec read desc failed %d retries. rc=%d"
14312  "\n", query_retry, retval);
14313  }
14314  retval = NI_RETCODE_SUCCESS;
14315  LRETURN;
14316  }
14317  else
14318  {
14319  // We have to ensure there are adequate number of DTS for picture
14320  // reorder delay otherwise wait for more packets to be sent to decoder.
14321  ni_timestamp_table_t *p_dts_queue = p_ctx->dts_queue;
14322  if ((int)p_dts_queue->list.count < p_ctx->pic_reorder_delay + 1 &&
14323  !p_ctx->ready_to_close &&
14325  {
14326  retval = NI_RETCODE_SUCCESS;
14327  ni_log2(p_ctx, NI_LOG_DEBUG,
14328  "At least %d packets should be sent before reading the "
14329  "first frame!\n",
14330  p_ctx->pic_reorder_delay + 1);
14331  LRETURN;
14332  }
14333  p_ctx->max_retry_fail_count[1] = 0;
14334 
14335  // get actual YUV transfer size if this is the stream's very first read
14336  if (0 == p_ctx->active_video_width || 0 == p_ctx->active_video_height)
14337  {
14338  retval = ni_query_stream_info(p_ctx, NI_DEVICE_TYPE_DECODER, &data);
14339  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
14340  p_ctx->device_type, p_ctx->hw_id,
14341  &(p_ctx->session_id), OPT_1);
14342  CHECK_VPU_RECOVERY(retval);
14343 
14344  ni_log2(p_ctx, NI_LOG_DEBUG, "Info dec YUV query, pic size %ux%u xfer frame size "
14345  "%ux%u frame-rate %u is_flushed %u\n",
14346  data.picture_width, data.picture_height,
14348  data.frame_rate, data.is_flushed);
14351  p_ctx->actual_video_width = data.picture_width;
14352  p_ctx->pixel_format = data.pix_format;
14354  //p_ctx->bit_depth_factor = data.transfer_frame_stride / data.picture_width;
14355  p_ctx->is_first_frame = 1;
14356  p_ctx->pixel_format_changed = 0;
14357 
14358  ni_log2(p_ctx, NI_LOG_DEBUG, "Info dec YUV, adjust frame size from %ux%u to "
14359  "%ux%u\n", p_frame->video_width, p_frame->video_height,
14360  p_ctx->active_video_width, p_ctx->active_video_height);
14361 
14362  retval = ni_frame_buffer_alloc(
14363  p_frame, p_ctx->actual_video_width, p_ctx->active_video_height,
14364  p_ctx->codec_format == NI_CODEC_FORMAT_H264, 1,
14365  p_ctx->bit_depth_factor,
14366  3, // Alloc space for write to data[3] and metadata
14367  1);
14368 
14369  if (NI_RETCODE_SUCCESS != retval)
14370  {
14371  LRETURN;
14372  }
14373  total_bytes_to_read = p_frame->data_len[3] + metadata_hdr_size;
14374  p_data_buffer = (uint8_t*)p_frame->p_buffer;
14375  // make sure we don't read more than available
14376  ni_log2(p_ctx, NI_LOG_DEBUG, "Info dec buf size: %u YUV frame + meta-hdr size: %u "
14377  "available: %u\n", p_frame->buffer_size,
14378  total_bytes_to_read, buf_info.buf_avail_size);
14379  }
14380  break;
14381  }
14382  }// end while1 query retry
14383 
14384  ni_log2(p_ctx, NI_LOG_DEBUG, "total_bytes_to_read %u max_nvme_io_size %u ylen %u cr len "
14385  "%u cb len %u hdr %d\n",
14386  total_bytes_to_read, p_ctx->max_nvme_io_size,
14387  p_frame->data_len[0], p_frame->data_len[1],
14388  p_frame->data_len[2], metadata_hdr_size);
14389 
14390  ni_log2(p_ctx, NI_LOG_DEBUG, "p_frame->data_len[3] = %u\n", p_frame->data_len[3]);
14391  if (buf_info.buf_avail_size < total_bytes_to_read)
14392  {
14393  ni_pthread_mutex_unlock(&p_ctx->mutex);
14394 
14395  ni_log2(p_ctx, NI_LOG_ERROR,
14396  "ERROR %s() avaliable size(%u) less than "
14397  "needed (%u)\n",
14398  __func__, buf_info.buf_avail_size, total_bytes_to_read);
14399  abort();
14400  } else if (total_bytes_to_read == metadata_hdr_size && !p_ctx->frame_num)
14401  {
14402  if (ni_cmp_fw_api_ver(
14404  "6rE") >= 0)
14405  {
14406  // allocate p_data_buffer to read the first metadata
14407  void *p_metadata_buffer = NULL;
14408  int buffer_size = ((metadata_hdr_size + (NI_MEM_PAGE_ALIGNMENT - 1)) /
14410  if (ni_posix_memalign(&p_metadata_buffer, sysconf(_SC_PAGESIZE), buffer_size))
14411  {
14412  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate metadata buffer.\n",
14413  NI_ERRNO, __func__);
14414  retval = NI_RETCODE_ERROR_MEM_ALOC;
14415  LRETURN;
14416  }
14417  p_data_buffer = (uint8_t *)p_metadata_buffer;
14418  get_first_metadata = 1;
14419  sequence_change = 0;
14420  }
14421  }
14422 
14423  //Apply read configuration here
14424  retval = ni_config_session_rw(p_ctx, SESSION_READ_CONFIG, 1,
14425  NI_CODEC_HW_ENABLE, 0);
14426  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config, p_ctx->device_type,
14427  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
14428  CHECK_VPU_RECOVERY(retval);
14429 
14430  read_size_bytes = total_bytes_to_read;
14432  if (read_size_bytes % NI_MEM_PAGE_ALIGNMENT)
14433  {
14434  read_size_bytes = ( (read_size_bytes / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT) + NI_MEM_PAGE_ALIGNMENT;
14435  }
14436 
14437  retval = ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
14438  p_data_buffer, read_size_bytes, ui32LBA);
14439  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_read, p_ctx->device_type,
14440  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
14441  CHECK_VPU_RECOVERY(retval);
14442  if (retval < 0)
14443  {
14444  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
14446  LRETURN;
14447  } else if (get_first_metadata) {
14448  // got first metadata alone
14449  ni_metadata_dec_frame_t *p_meta =
14450  (ni_metadata_dec_frame_t *)((uint8_t *)p_data_buffer);
14451  ni_log2(p_ctx, NI_LOG_DEBUG, "Got first pkt_delay_cnt %u\n",
14452  p_meta->metadata_common.pkt_delay_cnt);
14453  if (p_ctx->pkt_delay_cnt < p_meta->metadata_common.pkt_delay_cnt)
14454  p_ctx->pkt_delay_cnt = p_meta->metadata_common.pkt_delay_cnt;
14455  get_first_metadata = 0;
14456  ni_aligned_free(p_data_buffer);
14457  goto start;
14458  } else
14459  {
14460  // command issued successfully, now exit
14461  ni_metadata_dec_frame_t *p_meta;
14462  p_meta =
14463  (ni_metadata_dec_frame_t *)((uint8_t *)p_frame->p_buffer +
14464  p_frame->data_len[0] +
14465  p_frame->data_len[1] +
14466  p_frame->data_len[2] +
14467  p_frame->data_len[3]);
14468 
14469  if (buf_info.buf_avail_size != metadata_hdr_size)
14470  {
14471  low_delay_notify = 1;
14472  // shift metadata to end of triple output
14473 #ifdef _WIN32
14474  p_data_buffer = (uint8_t *)p_frame->p_buffer +
14476  memcpy(p_meta, p_data_buffer, metadata_hdr_size);
14477 #else
14478  memcpy(p_meta,
14479  p_frame->p_buffer +
14481  metadata_hdr_size);
14482 #endif
14483  sei_size = p_meta->sei_size;
14484  niFrameSurface1_t *p_data3 =
14485  (niFrameSurface1_t *)((uint8_t *)p_frame->p_buffer +
14486  p_frame->data_len[0] +
14487  p_frame->data_len[1] +
14488  p_frame->data_len[2]);
14489 
14490  niFrameSurface1_t *p_data3_1 =
14491  (niFrameSurface1_t *)((uint8_t *)p_frame->p_buffer +
14492  sizeof(niFrameSurface1_t));
14493  niFrameSurface1_t *p_data3_2 =
14494  (niFrameSurface1_t *)((uint8_t *)p_frame->p_buffer +
14495  2 * sizeof(niFrameSurface1_t));
14496  // Libxcoder knows the handle so overwrite here
14497  p_data3->device_handle =
14498  (int32_t)((int64_t)p_ctx->blk_io_handle & 0xFFFFFFFF);
14499  p_data3_1->device_handle =
14500  (int32_t)((int64_t)p_ctx->blk_io_handle & 0xFFFFFFFF);
14501  p_data3_2->device_handle =
14502  (int32_t)((int64_t)p_ctx->blk_io_handle & 0xFFFFFFFF);
14503  p_data3->ui16session_ID = p_data3_1->ui16session_ID =
14504  p_data3_2->ui16session_ID = (uint16_t)p_ctx->session_id;
14505  p_data3->src_cpu = p_data3_1->src_cpu = p_data3_2->src_cpu =
14506  (uint8_t)NI_DEVICE_TYPE_DECODER;
14507 
14508  p_data3->output_idx = 0;
14509  p_data3_1->output_idx = 1;
14510  p_data3_2->output_idx = 2;
14511 
14512  ni_log2(p_ctx, NI_LOG_DEBUG,
14513  "p_data3_1:sei_size=%d device_handle=%d == hw_id=%d ses_id=%d\n",
14514  sei_size, p_data3_1->device_handle, p_ctx->hw_id,
14515  p_data3_1->ui16session_ID);
14516  ni_log2(p_ctx, NI_LOG_DEBUG,
14517  "p_data3_1: ui16FrameIdx=%d NodeAddre=0x%x planar=%d bd=%d\n",
14518  p_data3_1->ui16FrameIdx, p_data3_1->ui32nodeAddress,
14519  p_data3_1->encoding_type, p_data3_1->bit_depth);
14520  ni_log2(p_ctx,
14521  NI_LOG_DEBUG,
14522  "p_data3_2:sei_size=%d device_handle=%d == hw_id=%d ses_id=%d\n",
14523  sei_size, p_data3_2->device_handle, p_ctx->hw_id,
14524  p_data3_2->ui16session_ID);
14525  ni_log2(p_ctx, NI_LOG_DEBUG,
14526  "p_data3_2: ui16FrameIdx=%d NodeAddre=0x%x planar=%d bd=%d\n",
14527  p_data3_2->ui16FrameIdx, p_data3_2->ui32nodeAddress,
14528  p_data3_2->encoding_type, p_data3_2->bit_depth);
14529 
14530  ni_log2(p_ctx, NI_LOG_DEBUG,
14531  "%s:sei_size=%d device_handle=%d == hw_id=%d "
14532  "ses_id=%d\n",
14533  __func__, sei_size, p_data3->device_handle, p_ctx->hw_id,
14534  p_data3->ui16session_ID);
14535  ni_log2(p_ctx, NI_LOG_DEBUG,
14536  "%s: session=0x%x ui16FrameIdx=%u NodeAddress=0x%x, "
14537  "planar=%d bd=%d\n",
14538  __func__, p_ctx->session_id, p_data3->ui16FrameIdx, p_data3->ui32nodeAddress,
14539  p_data3->encoding_type, p_data3->bit_depth);
14540  } else if (ni_cmp_fw_api_ver(
14542  "6rE") >= 0)
14543  {
14544  p_meta =
14545  (ni_metadata_dec_frame_t *)((uint8_t *)p_frame->p_buffer);
14546  ni_log2(p_ctx, NI_LOG_DEBUG, "Got pkt_delay_cnt %u\n",
14547  p_meta->metadata_common.pkt_delay_cnt);
14548  if (p_ctx->pkt_delay_cnt < p_meta->metadata_common.pkt_delay_cnt)
14549  p_ctx->pkt_delay_cnt = p_meta->metadata_common.pkt_delay_cnt;
14550  }
14551 
14552  total_bytes_to_read = total_bytes_to_read + sei_size;
14553  ni_log2(p_ctx, NI_LOG_DEBUG,
14554  "%s decoder read desc success, retval %d "
14555  "total_bytes_to_read include sei %u sei_size %d\n",
14556  __func__, retval, total_bytes_to_read, sei_size);
14557 
14558  if (total_bytes_to_read > NI_MEM_PAGE_ALIGNMENT)
14559  {
14560  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Oversized metadata!\n", __func__);
14561  retval = NI_RETCODE_ERROR_MEM_ALOC;
14562  LRETURN;
14563  }
14564  }
14565 
14566  //bytes_read_so_far = total_bytes_to_read;
14567  // Note: session status is NOT reset but tracked between send
14568  // and recv to catch and recover from a loop condition
14569  //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
14570  bytes_read_so_far = total_bytes_to_read;
14571  //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
14572  rx_size = ni_create_frame(p_frame, bytes_read_so_far, &frame_offset, true);
14573  p_ctx->frame_pkt_offset = frame_offset;
14574  if (p_ctx->decoder_low_delay > 0 && buf_info.buf_avail_size == metadata_hdr_size &&
14575  p_ctx->enable_low_delay_check)
14576  {
14577  ni_log2(p_ctx, NI_LOG_TRACE, "Low delay mode amd check header if has b frame\n");
14578 
14579  ni_metadata_dec_frame_t *p_meta =
14580  (ni_metadata_dec_frame_t *)((uint8_t *)p_frame->p_buffer +
14581  p_frame->data_len[0] +
14582  p_frame->data_len[1] +
14583  p_frame->data_len[2]);
14584  if (p_meta->metadata_common.has_b_frame == 1)
14585  {
14586  ni_log2(p_ctx, NI_LOG_ERROR,"Warning: session 0x%x decoder lowDelay mode "
14587  "is cancelled due to has_b_frames, frame_num %u\n",
14588  p_ctx->session_id, p_ctx->frame_num);
14589  p_ctx->decoder_low_delay = 0;
14590  }
14591  }
14592 
14593  if (rx_size > 0)
14594  {
14595  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): s-state %d first_frame %d\n", __func__,
14596  p_ctx->session_run_state, p_ctx->is_first_frame);
14597 
14598  int64_t tmp_dts, prev_dts = INT64_MIN, ts_diff = 0;
14599  int nb_diff = 0;
14600  if (p_ctx->is_first_frame)
14601  {
14602  ni_log2(p_ctx, NI_LOG_DEBUG,
14603  "%s(): First frame : session_id 0x%x, pic_reorder_delay: %d "
14604  "total frames input:%u buffered: %u completed: %u output: %u "
14605  "dropped: %u error: %u\n",
14606  __func__, p_ctx->session_id, p_ctx->pic_reorder_delay,
14613 
14614  if (p_ctx->session_statistic.ui32FramesDropped > 0)
14615  {
14616  for (i = 0; i < p_ctx->session_statistic.ui32FramesDropped; i++)
14617  {
14619  p_ctx->dts_queue, 0, &tmp_dts,
14621  p_ctx->buffer_pool) != NI_RETCODE_SUCCESS)
14622  {
14623  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): FramesDropped pop "
14624  "decoder dts queue %d %ld failed !\n",
14625  __func__, i, tmp_dts);
14626  break;
14627  } else
14628  {
14629  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): FramesDropped pop "
14630  "decoder dts queue %d %ld success !\n",
14631  __func__, i, tmp_dts);
14632  if (prev_dts != INT64_MIN) {
14633  ts_diff += labs(tmp_dts - prev_dts);
14634  nb_diff++;
14635  }
14636  prev_dts = tmp_dts;
14637  }
14638  }
14639  if (nb_diff > 1)
14640  {
14641  ts_diff = ts_diff / nb_diff;
14642  }
14644  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): FramesDropped pop dts "
14645  "average diff: %ld\n", __func__, ts_diff);
14646  }
14647  }
14648  else
14649  {
14651  {
14652  for(i = p_ctx->decoder_last_drop_frame_num; i < p_ctx->session_statistic.ui32FramesDropped; i++)
14653  {
14655  p_ctx->dts_queue, 0, &tmp_dts,
14657  p_ctx->buffer_pool) != NI_RETCODE_SUCCESS)
14658  {
14659  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): FramesDropped pop "
14660  "decoder dts queue %d %ld failed !\n",
14661  __func__, i, tmp_dts);
14662  break;
14663  }
14664  }
14666  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): FramesDropped pop dts %d\n", __func__, p_ctx->decoder_last_drop_frame_num);
14667  }
14668  }
14669 
14671  p_ctx->dts_queue, 0, (int64_t *)&p_frame->dts,
14673  p_ctx->buffer_pool) != NI_RETCODE_SUCCESS)
14674  {
14675  if (p_ctx->last_dts != NI_NOPTS_VALUE && !p_ctx->ready_to_close)
14676  {
14677  p_ctx->pic_reorder_delay++;
14678  p_frame->dts = p_ctx->last_dts + p_ctx->last_dts_interval;
14679  ni_log2(p_ctx, NI_LOG_DEBUG, "Padding DTS: %" PRId64 "\n", p_frame->dts);
14680  } else
14681  {
14682  p_frame->dts = NI_NOPTS_VALUE;
14683  }
14684  }
14685 
14686  if (p_ctx->is_first_frame)
14687  {
14688  for (i = 0; i < p_ctx->pic_reorder_delay; i++)
14689  {
14690  if (p_ctx->last_pts == NI_NOPTS_VALUE &&
14691  p_ctx->last_dts == NI_NOPTS_VALUE)
14692  {
14693  // If the p_frame->pts is unknown in the very beginning we assume
14694  // p_frame->pts == 0 as well as DTS less than PTS by 1000 * 1/timebase
14695  if (p_frame->pts >= p_frame->dts &&
14696  p_frame->pts - p_frame->dts < 1000)
14697  {
14698  break;
14699  }
14700  }
14701 
14703  p_ctx->dts_queue, 0, (int64_t *)&p_frame->dts,
14705  p_ctx->frame_num % 500 == 0,
14706  p_ctx->buffer_pool) != NI_RETCODE_SUCCESS)
14707  {
14708  p_frame->dts = NI_NOPTS_VALUE;
14709  } else
14710  {
14711  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): pic_reorder_delay pop "
14712  "decoder dts queue %d %ld success !\n",
14713  __func__, i, p_frame->dts);
14714  }
14715  }
14716  // Reset for DTS padding counting
14717  p_ctx->pic_reorder_delay = 0;
14718  }
14719  if (p_ctx->codec_format == NI_CODEC_FORMAT_JPEG)//fw won't save frameoffset when decoding jpeg.
14720  {
14721  if (p_ctx->is_first_frame)
14722  {
14723  p_ctx->is_first_frame = 0;
14724  }
14725  p_frame->pts = p_ctx->pts_offsets[p_ctx->frame_num % NI_FIFO_SZ];
14726  p_frame->flags = p_ctx->flags_array[p_ctx->frame_num % NI_FIFO_SZ];
14727  p_frame->pkt_pos = p_ctx->pkt_pos[p_ctx->frame_num % NI_FIFO_SZ];
14728  ni_log2(p_ctx, NI_LOG_DEBUG, "p_frame->pts = %u, frame_num = %d, p_frame->dts = %u\n",
14729  p_frame->pts, p_ctx->frame_num, p_frame->dts);
14730  }
14731  else
14732  {
14733  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: frame_offset %" PRIu64 "\n", __func__,
14734  frame_offset);
14735 
14736  // search for the pkt_offsets of received frame according to frame_offset.
14737  // 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)
14738  // i = -1 if not found
14739  i = rotated_array_binary_search(p_ctx->pkt_offsets_index_min,
14740  p_ctx->pkt_offsets_index, NI_FIFO_SZ,
14741  frame_offset);
14742  if (i >= 0)
14743  {
14744  p_frame->pts = p_ctx->pts_offsets[i];
14745  p_frame->flags = p_ctx->flags_array[i];
14746  p_frame->pkt_pos = p_ctx->pkt_pos[i];
14747  ni_log2(p_ctx, NI_LOG_DEBUG,
14748  "%s: (found pts) dts %" PRId64 " pts "
14749  "%" PRId64 " frame_offset %" PRIu64 " i %d "
14750  "pkt_offsets_index_min %" PRIu64 " "
14751  "pkt_offsets_index %" PRIu64 " pkt_pos %" PRIu64 "\n",
14752  __func__, p_frame->dts, p_frame->pts, frame_offset, i,
14753  p_ctx->pkt_offsets_index_min[i],
14754  p_ctx->pkt_offsets_index[i],
14755  p_ctx->pkt_pos[i]);
14756 
14757  if (p_ctx->is_first_frame)
14758  {
14759  // if the first frame is I frame and there are dropped frames,
14760  // find the dts closest to its pts using the average of dts diff
14761  if (p_frame->dts != NI_NOPTS_VALUE &&
14762  p_frame->pts != NI_NOPTS_VALUE &&
14763  PIC_TYPE_I == p_frame->ni_pict_type &&
14764  p_ctx->session_statistic.ui32FramesDropped > 0 &&
14765  ts_diff > 0)
14766  {
14767  while (p_frame->dts < p_frame->pts &&
14768  labs(p_frame->pts - p_frame->dts) > ts_diff)
14769  {
14770  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: First I frame pts %ld "
14771  "dts %ld diff. %ld > ts_diff %ld\n",
14772  __func__, p_frame->pts, p_frame->dts,
14773  labs(p_frame->pts - p_frame->dts), ts_diff);
14774 
14776  p_ctx->dts_queue, 0, (int64_t *)&p_frame->dts,
14778  {
14779  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): First I frame "
14780  "pop decoder dts queue error.\n", __func__);
14781  break;
14782  }
14783  }
14784  }
14785  }
14786 
14787  p_frame->p_custom_sei_set = p_ctx->pkt_custom_sei_set[i];
14788  p_ctx->pkt_custom_sei_set[i] = NULL;
14789  } else
14790  {
14791  //backup solution pts
14792  if (p_param->dec_input_params.skip_pts_guess && p_ctx->last_pts != NI_NOPTS_VALUE)
14793  {
14794  // if skip guess_correct_pts, use pts interval to get the correct pts
14795  p_frame->pts = p_ctx->last_pts + (p_ctx->last_pts_interval > 0 ? p_ctx->last_pts_interval : 1);
14796  }
14797  else
14798  {
14799  p_frame->pts = p_ctx->last_pts + (p_frame->dts - p_ctx->last_dts);
14800  }
14801  p_frame->pkt_pos = p_ctx->last_pkt_pos + (frame_offset - p_ctx->last_frame_offset);
14802  ni_log2(p_ctx, NI_LOG_ERROR,
14803  "ERROR: Frame pts %" PRId64 " not found for offset "
14804  "%" PRIu64 "\n", p_frame->pts, frame_offset);
14805  ni_log2(p_ctx, NI_LOG_DEBUG,
14806  "%s: (not found use default) dts %" PRId64 " pts %" PRId64
14807  "\n",
14808  __func__, p_frame->dts, p_frame->pts);
14809  }
14810 
14811  if (p_ctx->is_first_frame)
14812  {
14813  p_ctx->is_first_frame = 0;
14814  }
14815  }
14816 
14817  p_frame->orignal_pts = p_frame->pts;
14818  p_ctx->last_pkt_pos = p_frame->pkt_pos;
14819  p_ctx->last_frame_offset = frame_offset;
14820  if (!p_param->dec_input_params.skip_pts_guess)
14821  p_frame->pts = guess_correct_pts(p_ctx, p_frame->pts, p_frame->dts);
14822  if (p_frame->pts != NI_NOPTS_VALUE && p_ctx->last_pts != NI_NOPTS_VALUE)
14823  p_ctx->last_pts_interval = p_frame->pts - p_ctx->last_pts;
14824  p_ctx->last_pts = p_frame->pts;
14825  if (p_frame->dts != NI_NOPTS_VALUE && p_ctx->last_dts != NI_NOPTS_VALUE)
14826  p_ctx->last_dts_interval = p_frame->dts - p_ctx->last_dts;
14827  p_ctx->last_dts = p_frame->dts;
14828  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: (best_effort_timestamp) pts %" PRId64 "\n",
14829  __func__, p_frame->pts);
14830  p_ctx->frame_num++;
14831 
14833  if (p_frame->error_ratio > 0)
14834  {
14835  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: frame number = %d, error_ratio = %u\n",
14836  __func__, p_ctx->frame_num, p_frame->error_ratio);
14837  }
14838 
14839 #ifdef MEASURE_LATENCY
14840 #ifndef XCODER_311
14841  ni_log2(p_ctx, NI_LOG_INFO, "DEC pkt_num %d, fme_num %d, latecy is %d\n",
14842  p_ctx->pkt_num, p_ctx->frame_num, p_ctx->pkt_num - p_ctx->frame_num);
14843 #endif
14844 #endif
14845 
14846  }
14847 
14848  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): received data: [0x%08x]\n", __func__, rx_size);
14849  ni_log2(p_ctx, NI_LOG_DEBUG,
14850  "%s(): p_frame->start_of_stream=%u, "
14851  "p_frame->end_of_stream=%u, p_frame->video_width=%u, "
14852  "p_frame->video_height=%u\n",
14853  __func__, p_frame->start_of_stream, p_frame->end_of_stream,
14854  p_frame->video_width, p_frame->video_height);
14855  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): p_frame->data_len[0/1/2]=%u/%u/%u\n", __func__,
14856  p_frame->data_len[0], p_frame->data_len[1], p_frame->data_len[2]);
14857 
14858  if (p_ctx->frame_num % 500 == 0)
14859  {
14860  ni_log2(p_ctx, NI_LOG_DEBUG,
14861  "Decoder pts queue size = %d dts queue size = %d\n\n",
14862  p_ctx->pts_table->list.count, p_ctx->dts_queue->list.count);
14863  // scan and clean up
14865  p_ctx->buffer_pool);
14866  }
14867 
14868 #ifdef MEASURE_LATENCY
14869  if ((p_frame->dts != NI_NOPTS_VALUE) && (p_ctx->frame_time_q != NULL))
14870  {
14871  uint64_t abs_time_ns = ni_gettime_ns();
14873  ni_log2(p_ctx, NI_LOG_INFO, "DTS:%" PRId64 ",DELTA:%" PRId64 ",dLAT:%" PRIu64 ";\n",
14874  p_frame->dts, abs_time_ns - q->last_benchmark_time,
14875  ni_lat_meas_q_check_latency(q, abs_time_ns, p_frame->dts));
14876  q->last_benchmark_time = abs_time_ns;
14877  }
14878 #endif
14879 
14880 END:
14881 
14882  ni_pthread_mutex_unlock(&p_ctx->mutex);
14883 
14884  if (get_first_metadata && p_data_buffer)
14885  ni_aligned_free(p_data_buffer);
14886  if (sequence_change && p_ctx->frame_num)
14887  {
14888  if (p_ctx->actual_video_width == p_frame->video_width &&
14889  p_ctx->active_video_height == p_frame->video_height)
14890  {
14891  p_ctx->pixel_format_changed = 1;
14892  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): format changed\n", __func__);
14893  }
14894  }
14895  if (NI_RETCODE_SUCCESS != retval)
14896  {
14897  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): bad exit, retval = %d\n", __func__, retval);
14898  if (retval == NI_RETCODE_ERROR_VPU_RECOVERY)
14899  {
14900  low_delay_signal(p_ctx);
14901  }
14902 
14903  return retval;
14904  } else
14905  {
14906  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit, rx_size = %d\n", __func__, rx_size);
14907  if (low_delay_notify)
14908  {
14909  low_delay_signal(p_ctx);
14910  }
14911 
14912  return rx_size;
14913  }
14914 }
14915 
14916 /*!******************************************************************************
14917 * \brief Retrieve a YUV p_frame from decoder
14918 *
14919 * \param
14920 *
14921 * \return
14922 *******************************************************************************/
14924 {
14925  int retval = NI_RETCODE_SUCCESS;
14926  int rx_size = 0;
14927  uint64_t frame_offset = 0;
14928  uint8_t *p_data_buffer;
14929  int metadata_hdr_size = NI_FW_META_DATA_SZ -
14931  uint32_t total_bytes_to_read = 0;
14932  uint32_t read_size_bytes = 0;
14933  uint32_t ui32LBA = 0;
14934 
14935  //ni_log2(p_ctx, NI_LOG_DEBUG, "hwcontext.c:ni_hwdl_frame() hwdesc %d %d %d\n",
14936  // hwdesc->ui16FrameIdx,
14937  // hwdesc->i8InstID,
14938  // hwdesc->ui16session_ID);
14939 
14940  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
14941 
14942  if ((!p_ctx) || (!p_frame))
14943  {
14944  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
14945  __func__);
14946  return NI_RETCODE_INVALID_PARAM;
14947  }
14948 
14949  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
14950  {
14951  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
14952  __func__);
14954  LRETURN;
14955  }
14956 
14957  p_data_buffer = (uint8_t *)p_frame->p_buffer;
14958 
14959  if (!p_frame->p_data[0] || !p_data_buffer)
14960  {
14961  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): No receive buffer allocated.\n",
14962  __func__);
14963  retval = NI_RETCODE_INVALID_PARAM;
14964  LRETURN;
14965  }
14966 
14967  if (0 == p_frame->data_len[0])
14968  {
14969  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): p_frame->data_len[0] = 0!.\n",
14970  __func__);
14971  retval = NI_RETCODE_INVALID_PARAM;
14972  LRETURN;
14973  }
14974 
14976  {
14977  p_frame->data_len[2] = 0;
14978  } else if (hwdesc->encoding_type == NI_PIXEL_PLANAR_FORMAT_TILED4X4)
14979  {
14980  ni_log2(p_ctx, NI_LOG_ERROR,
14981  "ERROR %s(): NI_PIXEL_PLANAR_FORMAT_TILED4X4 not supported in download.\n",
14982  __func__);
14983  retval = NI_RETCODE_INVALID_PARAM;
14984  LRETURN;
14985  }
14986  total_bytes_to_read = p_frame->data_len[0] + p_frame->data_len[1] +
14987  p_frame->data_len[2];// +metadata_hdr_size;
14988  unsigned int bytes_read_so_far = 0;
14989  uint32_t output_chunk_offset = hwdesc->ui32nodeAddress / FRAME_CHUNK_INDEX_SIZE; //for reading output1 or output2
14990  uint32_t output_minor_offset = hwdesc->ui32nodeAddress - output_chunk_offset * FRAME_CHUNK_INDEX_SIZE;
14991  ni_log2(p_ctx, NI_LOG_DEBUG, "Total bytes to download %u, start offset = %u, chunkOffset "
14992  "%u, minorOffset %u\n",
14993  total_bytes_to_read, hwdesc->ui32nodeAddress,
14994  output_chunk_offset, output_minor_offset);
14995 
14996  ni_log2(p_ctx, NI_LOG_DEBUG, "total_bytes_to_read %u max_nvme_io_size %u ylen %u cr len "
14997  "%u cb len %u hdr %d\n",
14998  total_bytes_to_read, p_ctx->max_nvme_io_size,
14999  p_frame->data_len[0], p_frame->data_len[1],
15000  p_frame->data_len[2], metadata_hdr_size);
15001 
15002  //Apply read configuration here
15003  retval =
15005  (output_minor_offset << NI_CODEC_HW_PAYLOAD_OFFSET) |
15007  hwdesc->ui16FrameIdx);
15008  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config, hwdesc->src_cpu,
15009  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
15010  CHECK_VPU_RECOVERY(retval);
15011  if (retval < 0)
15012  {
15013  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
15015  LRETURN;
15016  } else
15017  {
15018  ni_log2(p_ctx, NI_LOG_DEBUG,
15019  "Config HW download read desc success, retval %d total_bytes_to_read %u\n",
15020  retval, total_bytes_to_read);
15021  }
15022 
15023  read_size_bytes = total_bytes_to_read;
15024  ui32LBA = READ_INSTANCE_R(p_ctx->session_id, hwdesc->src_cpu);
15025  ui32LBA += output_chunk_offset;
15026  if (read_size_bytes % NI_MEM_PAGE_ALIGNMENT)
15027  {
15028  read_size_bytes = ( (read_size_bytes / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT) + NI_MEM_PAGE_ALIGNMENT;
15029  }
15030 
15031  retval = ni_nvme_send_read_cmd(
15032  (ni_device_handle_t)(int64_t)hwdesc->device_handle,
15033  NI_INVALID_DEVICE_HANDLE, p_data_buffer, read_size_bytes, ui32LBA);
15034  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_read, hwdesc->src_cpu,
15035  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
15036  CHECK_VPU_RECOVERY(retval);
15037  if (retval < 0)
15038  {
15039  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
15041  LRETURN;
15042  } else
15043  {
15044  ni_log2(p_ctx, NI_LOG_DEBUG,
15045  "HW download read desc success, retval %d total_bytes_to_read %u\n",
15046  retval, total_bytes_to_read);
15047  }
15048 
15049  //Unset applied read configuration here
15050  retval = ni_config_session_rw(p_ctx, SESSION_READ_CONFIG, 0, 0, 0);
15051  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config, hwdesc->src_cpu,
15052  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
15053  CHECK_VPU_RECOVERY(retval);
15054 
15055  if (retval < 0)
15056  {
15057  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
15059  LRETURN;
15060  } else
15061  {
15062  ni_log2(p_ctx, NI_LOG_DEBUG,
15063  "Unconfig HW download read desc success, retval %d total_bytes_to_read %u\n",
15064  retval, total_bytes_to_read);
15065  }
15066 
15067  bytes_read_so_far = total_bytes_to_read;
15068  // Note: session status is NOT reset but tracked between send
15069  // and recv to catch and recover from a loop condition
15070 
15071  if (p_ctx->is_auto_dl)
15072  rx_size = (int)bytes_read_so_far;
15073  else
15074  {
15075  rx_size =
15076  ni_create_frame(p_frame, bytes_read_so_far, &frame_offset, false);
15077  p_ctx->frame_pkt_offset = frame_offset;
15078  }
15079 
15080  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): received data: [0x%08x]\n",
15081  __func__, rx_size);
15082  ni_log2(p_ctx, NI_LOG_DEBUG,
15083  "%s(): p_frame->start_of_stream=%u, "
15084  "p_frame->end_of_stream=%u, p_frame->video_width=%u, "
15085  "p_frame->video_height=%u\n",
15086  __func__, p_frame->start_of_stream, p_frame->end_of_stream,
15087  p_frame->video_width, p_frame->video_height);
15088  ni_log2(p_ctx, NI_LOG_DEBUG,
15089  "%s(): p_ctx->frame_num %" PRIu64 ", "
15090  "p_frame->data_len[0/1/2]=%u/%u/%u\n",
15091  __func__, p_ctx->frame_num, p_frame->data_len[0], p_frame->data_len[1],
15092  p_frame->data_len[2]);
15093 
15094  //if (decq_count % 500 == 0)
15095  //{
15096  // ni_log2(p_ctx, NI_LOG_DEBUG, "Decoder pts queue size = %d dts queue size = %d\n\n",
15097  // p_ctx->pts_table->list.count, p_ctx->dts_queue)->list.count);
15098  // // scan and clean up
15099  // ni_timestamp_scan_cleanup(p_ctx->pts_table, p_ctx->dts_queue, p_ctx->buffer_pool);
15100  //}
15101 
15102 END:
15103 
15104  if (NI_RETCODE_SUCCESS != retval)
15105  {
15106  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): bad exit, retval = %d\n", __func__, retval);
15107  return retval;
15108  } else
15109  {
15110  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit, rx_size = %d\n", __func__, rx_size);
15111  return rx_size;
15112  }
15113 }
15114 
15115 int ni_hwdownload_by_frame_idx(niFrameSurface1_t* hwdesc, ni_frame_t* p_frame, int is_auto_dl)
15116 {
15117  int retval = NI_RETCODE_SUCCESS;
15118  int rx_size = 0;
15119  uint64_t frame_offset = 0;
15120  uint8_t *p_data_buffer;
15121  int metadata_hdr_size = NI_FW_META_DATA_SZ -
15123  uint32_t total_bytes_to_read = 0;
15124  uint32_t read_size_bytes = 0;
15125  uint32_t ui32LBA = 0;
15126  ni_global_session_stats_t *global_state;
15127  const char *error_flag = "NetintQuadraErr";
15128 
15129  ni_log(NI_LOG_TRACE, "%s(): enter\n", __func__);
15130 
15131  p_data_buffer = (uint8_t *)p_frame->p_buffer;
15132 
15133  if (!p_frame->p_data[0] || !p_data_buffer || 0 == p_frame->data_len[0])
15134  {
15135  ni_log(NI_LOG_ERROR, "ERROR %s(): passed parameters are null or p_frame->data_len is 0.\n",
15136  __func__);
15137  retval = NI_RETCODE_INVALID_PARAM;
15138  LRETURN;
15139  }
15140 
15142  {
15143  p_frame->data_len[2] = 0;
15144  } else if (hwdesc->encoding_type >= NI_PIXEL_PLANAR_FORMAT_TILED4X4)
15145  {
15147  "ERROR %s(): Unsupported format %d for hw download.\n",
15148  __func__, hwdesc->encoding_type);
15149  retval = NI_RETCODE_INVALID_PARAM;
15150  LRETURN;
15151  }
15152  total_bytes_to_read = p_frame->data_len[0] + p_frame->data_len[1] +
15153  p_frame->data_len[2];// +metadata_hdr_size;
15154  unsigned int bytes_read_so_far = 0;
15155  uint32_t output_chunk_offset = hwdesc->ui32nodeAddress / FRAME_CHUNK_INDEX_SIZE; //for reading output1 or output2
15156  uint32_t output_minor_offset = hwdesc->ui32nodeAddress - output_chunk_offset * FRAME_CHUNK_INDEX_SIZE;
15157 
15158  ni_log(NI_LOG_DEBUG, "total_bytes_to_read %u ylen %u cr len "
15159  "%u cb len %u hdr %d\n",
15160  total_bytes_to_read,
15161  p_frame->data_len[0], p_frame->data_len[1],
15162  p_frame->data_len[2], metadata_hdr_size);
15163 
15164  // preset content of buffer, if not refresh means download fail
15165  memset(p_data_buffer, 0, NI_DATA_BUFFER_LEN);
15166  global_state = (ni_global_session_stats_t *)p_data_buffer;
15167  memcpy(global_state->error_flag, error_flag, strlen(error_flag)+1);
15168  global_state->check_flag[0] = global_state->check_flag[2] = 0x0;
15169  global_state->check_flag[1] = global_state->check_flag[3] = 0xFF;
15170 
15171  read_size_bytes = total_bytes_to_read;
15172  ui32LBA = DOWNLOAD_FRAMEIDX_R(hwdesc->ui16FrameIdx);
15173  ui32LBA += output_chunk_offset;
15174  if (read_size_bytes % NI_MEM_PAGE_ALIGNMENT)
15175  {
15176  read_size_bytes = ( (read_size_bytes / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT) + NI_MEM_PAGE_ALIGNMENT;
15177  }
15178  ni_log(NI_LOG_DEBUG, "Total bytes to download %u, start offset = %u, chunkOffset "
15179  "%u, minorOffset %u hwdesc->ui16FrameIdx %d ui32LBA 0x%x\n",
15180  total_bytes_to_read, hwdesc->ui32nodeAddress,
15181  output_chunk_offset, output_minor_offset, hwdesc->ui16FrameIdx, ui32LBA);
15182 
15183  retval = ni_nvme_send_read_cmd(
15184  (ni_device_handle_t)(int64_t)hwdesc->device_handle,
15185  NI_INVALID_DEVICE_HANDLE, p_data_buffer, read_size_bytes, ui32LBA);
15186 
15187  if (retval < 0)
15188  {
15189  ni_log(NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
15191  LRETURN;
15192  }
15193  global_state = (ni_global_session_stats_t *)p_data_buffer;
15194  if (!strcmp(global_state->error_flag, error_flag) &&
15195  global_state->check_flag[0] == 0x0 && global_state->check_flag[1] == 0xFF &&
15196  global_state->check_flag[2] == 0x0 && global_state->check_flag[3] == 0xFF) {
15197  ni_log(NI_LOG_ERROR, "ERROR %s(): nvme download failed, invalid frameidx %u or "
15198  "size %u + offset %u out of range\n",
15199  __func__, hwdesc->ui16FrameIdx, read_size_bytes, output_chunk_offset);
15200  retval = NI_RETCODE_INVALID_PARAM;
15201  LRETURN;
15202  }
15203 
15204  bytes_read_so_far = total_bytes_to_read;
15205  // Note: session status is NOT reset but tracked between send
15206  // and recv to catch and recover from a loop condition
15207 
15208  if (is_auto_dl)
15209  rx_size = (int)bytes_read_so_far;
15210  else
15211  {
15212  rx_size =
15213  ni_create_frame(p_frame, bytes_read_so_far, &frame_offset, false);
15214  }
15215 
15217  "HW download read desc success, retval %d total_bytes_to_read %u\n",
15218  retval, total_bytes_to_read);
15219 
15220 END:
15221 
15222  if (NI_RETCODE_SUCCESS != retval)
15223  {
15224  ni_log(NI_LOG_ERROR, "%s(): bad exit, retval = %d\n", __func__, retval);
15225  return retval;
15226  } else
15227  {
15228  ni_log(NI_LOG_TRACE, "%s(): exit, rx_size = %d\n", __func__, rx_size);
15229  return rx_size;
15230  }
15231 }
15232 
15234  ni_frameclone_desc_t *p_frameclone_desc)
15235 {
15236  int retval = NI_RETCODE_SUCCESS;
15237  uint8_t *p_data = NULL;
15238  uint32_t dataLen;
15239  uint32_t ui32LBA = 0;
15240 
15241  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
15242 
15243  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
15244  {
15245  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
15246  __func__);
15248  LRETURN;
15249  }
15250 
15251  dataLen = (sizeof(ni_frameclone_desc_t) + NI_MEM_PAGE_ALIGNMENT - 1) &
15252  ~(NI_MEM_PAGE_ALIGNMENT - 1);
15253  if (ni_posix_memalign((void **)&p_data, sysconf(_SC_PAGESIZE), dataLen))
15254  {
15255  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
15256  NI_ERRNO, __func__);
15257  retval = NI_RETCODE_ERROR_MEM_ALOC;
15258  LRETURN;
15259  }
15260  memset(p_data, 0, dataLen);
15261  memcpy(p_data, p_frameclone_desc, sizeof(ni_frameclone_desc_t));
15262  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): ui16DstIdx %u, ui16SrcIdx %u, size %u, offset %u\n", __func__,
15263  p_frameclone_desc->ui16DstIdx, p_frameclone_desc->ui16SrcIdx,
15264  p_frameclone_desc->ui32Size, p_frameclone_desc->ui32Offset);
15265 
15266  ui32LBA = CONFIG_SESSION_FRAME_COPY_W(p_ctx->session_id);
15267  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, NI_INVALID_EVENT_HANDLE,
15268  p_data, dataLen, ui32LBA);
15269  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
15270  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id),
15271  OPT_1);
15272  CHECK_VPU_RECOVERY(retval);
15273  if (NI_RETCODE_SUCCESS != retval)
15274  {
15275  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed!\n",
15276  __func__);
15278  LRETURN;
15279  }
15280 
15281 END:
15282  ni_aligned_free(p_data);
15283  if (NI_RETCODE_SUCCESS != retval)
15284  {
15285  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): bad exit, retval = %d\n", __func__, retval);
15286  }
15287  return retval;
15288 }
15289 
15290 /*!******************************************************************************
15291 * \brief Close an xcoder upload instance
15292 *
15293 * \param p_ctx pointer to uploader session context
15294 *
15295 * \return NI_RETCODE_SUCCESS
15296 *******************************************************************************/
15298 {
15299 #ifndef _WIN32
15300  if (p_ctx->isP2P)
15301  {
15302  if (p_ctx->netint_fd)
15303  {
15304  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: close driver fd %d\n", __func__,
15305  p_ctx->netint_fd);
15306  close(p_ctx->netint_fd);
15307  }
15308  }
15309 #endif
15310  return NI_RETCODE_SUCCESS;
15311 }
15312 
15313 /*!******************************************************************************
15314 * \brief Send a p_config command to configure uploading parameters.
15315 *
15316 * \param ni_session_context_t p_ctx - xcoder Context
15317 * \param[in] pool_size pool size to create
15318 * \param[in] pool 0 = normal pool, 1 = P2P pool
15319 *
15320 * \return - NI_RETCODE_SUCCESS on success,
15321 * NI_RETCODE_ERROR_INVALID_SESSION
15322 * NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
15323 *******************************************************************************/
15325  uint32_t pool_size,
15326  uint32_t pool)
15327 {
15328  void* p_uploader_config = NULL;
15329  ni_uploader_config_t* p_cfg = NULL;
15330  uint32_t buffer_size = sizeof(ni_encoder_config_t);
15332  uint32_t ui32LBA = 0;
15333  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
15334 
15335  if (!p_ctx)
15336  {
15337  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
15338  __func__);
15339  retval = NI_RETCODE_INVALID_PARAM;
15340  LRETURN;
15341  }
15342 
15343  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
15344  {
15345  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
15346  __func__);
15348  LRETURN;
15349  }
15350 
15351  buffer_size = ((buffer_size + (NI_MEM_PAGE_ALIGNMENT - 1)) / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT;
15352  if (ni_posix_memalign(&p_uploader_config, sysconf(_SC_PAGESIZE),
15353  buffer_size))
15354  {
15355  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
15356  NI_ERRNO, __func__);
15357  retval = NI_RETCODE_ERROR_MEM_ALOC;
15358  LRETURN;
15359  }
15360  memset(p_uploader_config, 0, buffer_size);//
15361 
15362  p_cfg = (ni_uploader_config_t*)p_uploader_config;
15363  p_cfg->ui16picWidth = p_ctx->active_video_width;
15364  p_cfg->ui16picHeight = p_ctx->active_video_height;
15365  p_cfg->ui8poolSize = pool_size;
15366  p_cfg->ui8PixelFormat = p_ctx->pixel_format;
15367  p_cfg->ui8Pool = pool;
15368  p_cfg->ui32lumaLinesize = 0; // cannot support config linesize yet because ni_frames_init is called when filter graph is created (no frame linesize info)
15369  p_cfg->ui32chromaLinesize = 0;
15370  bool isrgb = ((NI_PIX_FMT_RGBA == p_cfg->ui8PixelFormat) || (NI_PIX_FMT_BGRA == p_cfg->ui8PixelFormat) ||
15371  (NI_PIX_FMT_ARGB == p_cfg->ui8PixelFormat) || (NI_PIX_FMT_ABGR == p_cfg->ui8PixelFormat) ||
15372  (NI_PIX_FMT_BGR0 == p_cfg->ui8PixelFormat));
15376  {
15377  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);
15378  retval = NI_RETCODE_INVALID_PARAM;
15379  LRETURN;
15380  }
15381 
15382  ni_log2(p_ctx, NI_LOG_DEBUG, "ni_config_instance_set_uploader_params():%d x %d x Format %d with %d framepool\n",
15383  p_cfg->ui16picWidth, p_cfg->ui16picHeight, p_cfg->ui8PixelFormat, p_cfg->ui8poolSize);
15384 
15385  //configure the session here
15386  ui32LBA =
15388 
15389  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
15390  p_uploader_config, buffer_size, ui32LBA);
15391  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
15392  p_ctx->device_type, p_ctx->hw_id,
15393  &(p_ctx->session_id), OPT_1);
15394  if (NI_RETCODE_SUCCESS != retval)
15395  {
15396  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);
15397  //Close the session since we can't configure it
15398  retval = ni_encoder_session_close(p_ctx, 0);
15399  if (NI_RETCODE_SUCCESS != retval)
15400  {
15401  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);
15402  }
15403 
15405  }
15406  else
15407  {
15409  }
15410 
15411 END:
15412 
15413  ni_aligned_free(p_uploader_config);
15414  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
15415 
15416  return retval;
15417 }
15418 
15419 /*!******************************************************************************
15420 * \brief Send a p_config command to configure decoding parameters.
15421 *
15422 * \param ni_session_context_t p_ctx - xcoder Context
15423 * \param uint32_t max_pkt_size - overwrite maximum packet size if nonzero
15424 *
15425 * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION, NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
15426 *******************************************************************************/
15428 {
15429  void* p_decoder_config = NULL;
15430  uint32_t buffer_size = sizeof(ni_decoder_config_t);
15432  uint32_t ui32LBA = 0;
15433  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
15434 
15435  if (!p_ctx)
15436  {
15437  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
15438  __func__);
15439  retval = NI_RETCODE_INVALID_PARAM;
15440  LRETURN;
15441  }
15442 
15443  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
15444  {
15445  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
15446  __func__);
15448  LRETURN;
15449  }
15450 
15451  if (ni_cmp_fw_api_ver((char*)&p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6rT") < 0)
15452  {
15453  ni_log2(p_ctx, NI_LOG_INFO, "%s() FW rev %s < 6rT-- load balancing might be affected\n", __func__,
15455  }
15456 
15457  buffer_size = ((buffer_size + (NI_MEM_PAGE_ALIGNMENT - 1)) / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT;
15458  if (ni_posix_memalign(&p_decoder_config, sysconf(_SC_PAGESIZE), buffer_size))
15459  {
15460  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate p_decoder_config buffer\n",
15461  NI_ERRNO, __func__);
15462  retval = NI_RETCODE_ERROR_MEM_ALOC;
15463  LRETURN;
15464  }
15465  memset(p_decoder_config, 0, buffer_size);
15466 
15467  ni_set_custom_dec_template(p_ctx, p_decoder_config, p_ctx->p_session_config, max_pkt_size);
15468  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));
15469  if (NI_RETCODE_PARAM_WARN == retval)
15470  {
15471  ni_log2(p_ctx, NI_LOG_INFO, "WARNING: %s . %s\n", __func__, p_ctx->param_err_msg);
15472  fflush(stdout);
15473  }
15474  else if (NI_RETCODE_SUCCESS != retval)
15475  {
15476  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s failed. %s\n", __func__,
15477  p_ctx->param_err_msg);
15478  fflush(stdout);
15479  retval = NI_RETCODE_INVALID_PARAM;
15480  LRETURN;
15481  }
15482 
15483  // configure the session here
15485 
15486  //Flip the bytes!!
15487  //p_cfg = (ni_decoder_config_t*)p_decoder_config;
15488  //p_cfg->i32picWidth = ni_htonl(p_cfg->i32picWidth);
15489  //p_cfg->i32picHeight = ni_htonl(p_cfg->i32picHeight);
15490  //p_cfg->i32meBlkMode = ni_htonl(p_cfg->i32meBlkMode);
15491  //p_cfg->i32frameRateInfo = ni_htonl(p_cfg->i32frameRateInfo);
15492  //p_cfg->i32vbvBufferSize = ni_htonl(p_cfg->i32vbvBufferSize);
15493  //p_cfg->i32userQpMax = ni_htonl(p_cfg->i32userQpMax);
15494  //p_cfg->i32maxIntraSize = ni_htonl(p_cfg->i32maxIntraSize);
15495  //p_cfg->i32userMaxDeltaQp = ni_htonl(p_cfg->i32userMaxDeltaQp);
15496  //p_cfg->i32userMinDeltaQp = ni_htonl(p_cfg->i32userMinDeltaQp);
15497  //p_cfg->i32userQpMin = ni_htonl(p_cfg->i32userQpMin);
15498  //p_cfg->i32bitRate = ni_htonl(p_cfg->i32bitRate);
15499  //p_cfg->i32bitRateBL = ni_htonl(p_cfg->i32bitRateBL);
15500  //p_cfg->i32srcBitDepth = ni_htonl(p_cfg->i32srcBitDepth);
15501  //p_cfg->hdrEnableVUI = ni_htonl(p_cfg->hdrEnableVUI);
15502  //p_cfg->ui32VuiDataSizeBits = ni_htonl(p_cfg->ui32VuiDataSizeBits);
15503  //p_cfg->ui32VuiDataSizeBytes = ni_htonl(p_cfg->ui32VuiDataSizeBytes);
15504  //p_cfg->i32hwframes = ni_htonl(p_cfg->i32hwframes);
15505  // flip the 16 bytes of the reserved field using 32 bits pointers
15506  //for (i = 0; i < (16 >> 2); i++)
15507  //{
15508  // ((uint32_t*)p_cfg->ui8Reserved)[i] = ni_htonl(((uint32_t*)p_cfg->ui8Reserved)[i]);
15509  //}
15511  //for (i = 0; i < (NI_MAX_VUI_SIZE >> 2); i++) // apply on 32 bits
15512  //{
15513  // ((uint32_t*)p_cfg->ui8VuiRbsp)[i] = ni_htonl(((uint32_t*)p_cfg->ui8VuiRbsp)[i]);
15514  //}
15515 
15516  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: ui32LBA = 0x%x\n", __func__, ui32LBA);
15517  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
15518  p_decoder_config, buffer_size, ui32LBA);
15519  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
15520  p_ctx->device_type, p_ctx->hw_id,
15521  &(p_ctx->session_id), OPT_1);
15522  if (NI_RETCODE_SUCCESS != retval)
15523  {
15524  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);
15525  //Close the session since we can't configure it
15526  retval = ni_decoder_session_close(p_ctx, 0);
15527  if (NI_RETCODE_SUCCESS != retval)
15528  {
15529  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);
15530  }
15531 
15533  }
15534 
15535 END:
15536 
15537  ni_aligned_free(p_decoder_config);
15538  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
15539  return retval;
15540 }
15541 
15542 /*!******************************************************************************
15543  * \brief read a hardware descriptor from a scaler session
15544  *
15545  * \param[in] p_ctx pointer to session context
15546  * \param[out] p_frame pointer to frame to write hw descriptor
15547  *
15548  * \return NI_RETCODE_INVALID_PARAM
15549  * NI_RETCODE_ERROR_INVALID_SESSION
15550  * NI_RETCODE_ERROR_MEM_ALOC
15551  * NI_RETCODE_ERROR_NVME_CMD_FAILED
15552  * NI_RETCODE_FAILURE
15553  *******************************************************************************/
15555  ni_session_context_t *p_ctx,
15556  ni_frame_t *p_frame)
15557 {
15558  ni_retcode_t retval;
15559  ni_instance_buf_info_t sInstanceBuf = {0};
15560  niFrameSurface1_t *pFrameSurface;
15561  int query_retry = 0;
15562 
15563  if (!p_ctx || !p_frame || !p_frame->p_data[3])
15564  {
15565  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
15566  __func__);
15567  return NI_RETCODE_INVALID_PARAM;
15568  }
15569 
15570  if (p_ctx->session_id == NI_INVALID_SESSION_ID)
15571  {
15572  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
15573  __func__);
15575  }
15576 
15577  if (p_ctx->buffered_frame_index)
15578  {
15579  pFrameSurface = (niFrameSurface1_t *) p_frame->p_data[3];
15580  pFrameSurface->ui16FrameIdx = p_ctx->buffered_frame_index;
15581  pFrameSurface->ui16session_ID = p_ctx->session_id;
15582  pFrameSurface->device_handle =
15583  (int32_t)((int64_t)p_ctx->blk_io_handle & 0xFFFFFFFF);
15584  pFrameSurface->src_cpu = (uint8_t) NI_DEVICE_TYPE_SCALER;
15585  pFrameSurface->output_idx = 0;
15586 
15587  p_ctx->buffered_frame_index = 0;
15588  return NI_RETCODE_SUCCESS;
15589  }
15590 
15591  ni_pthread_mutex_lock(&p_ctx->mutex);
15592 
15593  for (;;)
15594  {
15595  query_retry++;
15596 
15597  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: query by ni_query_instance_buf_info INST_BUF_INFO_RW_UPLOAD\n",
15598  __func__);
15600  NI_DEVICE_TYPE_SCALER, &sInstanceBuf);
15601 
15602  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
15603  p_ctx->device_type, p_ctx->hw_id,
15604  &(p_ctx->session_id), OPT_3);
15605 
15607  {
15608  if (query_retry >= 1000)
15609  {
15610  ni_log2(p_ctx, NI_LOG_DEBUG, "Warning hwdesc read fail rc %d\n", retval);
15611  LRETURN;
15612  }
15613  }
15614  else if (retval != NI_RETCODE_SUCCESS)
15615  {
15616  LRETURN;
15617  }
15618  else
15619  {
15620  pFrameSurface = (niFrameSurface1_t *) p_frame->p_data[3];
15621  pFrameSurface->ui16FrameIdx = sInstanceBuf.hw_inst_ind.frame_index;
15622  pFrameSurface->ui16session_ID = p_ctx->session_id;
15623  pFrameSurface->device_handle =
15624  (int32_t)((int64_t)p_ctx->blk_io_handle & 0xFFFFFFFF);
15625  pFrameSurface->src_cpu = (uint8_t) NI_DEVICE_TYPE_SCALER;
15626  pFrameSurface->output_idx = 0;
15627 
15628  /* A frame index of zero is invalid, the memory acquisition failed */
15629  if (pFrameSurface->ui16FrameIdx == 0)
15630  {
15631  if (query_retry >= 1000)
15632  {
15633  ni_log2(p_ctx, NI_LOG_ERROR, "Error: 2D could not acquire frame\n");
15634  retval = NI_RETCODE_FAILURE;
15635  LRETURN;
15636  }
15637  ni_usleep(100);
15638  continue;
15639  }
15640 
15641  ni_log2(p_ctx, NI_LOG_DEBUG,
15642  "Session=0x%x: %s got FrameIndex=%u\n",
15643  p_ctx->session_id,
15644  __func__,
15645  pFrameSurface->ui16FrameIdx);
15646 
15647  LRETURN;
15648  }
15649  }
15650 
15651 END:
15652 
15653  ni_pthread_mutex_unlock(&p_ctx->mutex);
15654 
15655  if (NI_RETCODE_SUCCESS != retval)
15656  {
15657  ni_log2(p_ctx, NI_LOG_DEBUG,
15658  "%s Warning scalar read hwdesc fail rc %d or ind !\n",
15659  __func__, retval);
15660  retval = NI_RETCODE_FAILURE;
15661  }
15662 
15663  return retval;
15664 }
15665 
15666 /*!******************************************************************************
15667 * \brief Grab bitdepth factor from NI_PIX_FMT
15668 *
15669 * \param[in] pix_fmt ni_pix_fmt_t
15670 *
15671 * \return 1 or 2 for success, -1 for error
15672 *******************************************************************************/
15674 {
15675  switch (pix_fmt)
15676  {
15677  case NI_PIX_FMT_YUV420P:
15678  case NI_PIX_FMT_NV12:
15679  case NI_PIX_FMT_8_TILED4X4:
15680  return 1;
15682  case NI_PIX_FMT_P010LE:
15684  return 2;
15685  default:
15686  {
15687  ni_log(NI_LOG_ERROR, "ERROR: %s() non applicable format %d\n", __func__,
15688  pix_fmt);
15689  break;
15690  }
15691  }
15692  return -1;
15693 }
15694 
15695 /*!******************************************************************************
15696 * \brief Grab planar info from NI_PIX_FMT
15697 *
15698 * \param[in] pix_fmt ni_pix_fmt_t
15699 *
15700 * \return 0 or 1 for success, -1 for error
15701 *******************************************************************************/
15703 {
15704  switch (pix_fmt)
15705  {
15706  case NI_PIX_FMT_YUV420P:
15708  case NI_PIX_FMT_ABGR:
15709  case NI_PIX_FMT_ARGB:
15710  case NI_PIX_FMT_RGBA:
15711  case NI_PIX_FMT_BGRA:
15712  return 1;
15713  break;
15714  case NI_PIX_FMT_NV12:
15715  case NI_PIX_FMT_P010LE:
15716  return 0;
15717  break;
15718  default:
15719  {
15720  ni_log(NI_LOG_ERROR, "ERROR: %s() non applicable format %d\n", __func__,
15721  pix_fmt);
15722  break;
15723  }
15724  }
15725  return -1;
15726 }
15727 
15728 #ifndef _WIN32
15729 /*!*****************************************************************************
15730  * \brief Get an address offset from a hw descriptor
15731  *
15732  * \param[in] p_ctx ni_session_context_t to be referenced
15733  * \param[in] hwdesc Pointer to caller allocated niFrameSurface1_t
15734  * \param[out] p_offset Value of offset
15735  *
15736  * \return On success NI_RETCODE_SUCCESS
15737  * On failure NI_RETCODE_INVALID_PARAM
15738  ******************************************************************************/
15740  uint32_t *p_offset)
15741 {
15742  if (!hwdesc)
15743  {
15744  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed null parameter\n", __func__);
15745  return NI_RETCODE_INVALID_PARAM;
15746  }
15747 
15748  if (hwdesc->ui16FrameIdx <= NI_GET_MIN_HWDESC_P2P_BUF_ID(p_ctx->ddr_config) ||
15750  {
15751  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() pass invalid data. "
15752  "FrameIdx %d OOR (%d,%d]. DDR config %d \n", __func__, hwdesc->ui16FrameIdx,
15754  p_ctx->ddr_config);
15755  return NI_RETCODE_INVALID_PARAM;
15756  }
15757 
15758  *p_offset = (hwdesc->ui16FrameIdx - NI_GET_MIN_HWDESC_P2P_BUF_ID(p_ctx->ddr_config)) *
15760 
15761  return NI_RETCODE_SUCCESS;
15762 }
15763 #endif
15764 
15765 /* AI functions */
15767  void *nb_data, uint32_t nb_size)
15768 {
15769  void *p_ai_config = NULL;
15770  void *p_nb_data = NULL;
15771  uint32_t buffer_size;
15772  // uint8_t *p_data;
15773  // uint32_t transferred, this_size;
15775  uint32_t ui32LBA = 0;
15776  uint32_t config_size;
15777  void *p_buffer = NULL;
15778  uint32_t dataLen;
15779  ni_instance_buf_info_t buf_info = {0};
15780  int retry_count = 0;
15781 
15782  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
15783 
15784  if (!p_ctx)
15785  {
15786  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
15787  __func__);
15788  retval = NI_RETCODE_INVALID_PARAM;
15789  LRETURN;
15790  }
15791 
15792  ni_pthread_mutex_lock(&p_ctx->mutex);
15793 
15794  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
15795  {
15796  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
15797  __func__);
15799  LRETURN;
15800  }
15801 
15802  config_size = (sizeof(ni_ai_config_t) + (NI_MEM_PAGE_ALIGNMENT - 1)) &
15803  ~(NI_MEM_PAGE_ALIGNMENT - 1);
15804  if (ni_posix_memalign(&p_ai_config, sysconf(_SC_PAGESIZE), config_size))
15805  {
15806  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: Cannot allocate ai config buffer.\n");
15807  retval = NI_RETCODE_ERROR_MEM_ALOC;
15808  LRETURN;
15809  }
15810 
15811  ((ni_ai_config_t *)p_ai_config)->ui32NetworkBinarySize = nb_size;
15812  ni_calculate_sha256(nb_data, nb_size,
15813  ((ni_ai_config_t *)p_ai_config)->ui8Sha256);
15814 
15815  buffer_size =
15816  (nb_size + (NI_MEM_PAGE_ALIGNMENT - 1)) & ~(NI_MEM_PAGE_ALIGNMENT - 1);
15817  if (ni_posix_memalign(&p_nb_data, sysconf(_SC_PAGESIZE), buffer_size))
15818  {
15819  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: Cannot allocate encConf buffer.\n");
15820  retval = NI_RETCODE_ERROR_MEM_ALOC;
15821  LRETURN;
15822  }
15823 
15824  memcpy(p_nb_data, nb_data, nb_size);
15825 
15826  /* configure network binary size to be written */
15828  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): LBA 0x%x, nb_size %u\n", __func__, ui32LBA,
15829  nb_size);
15830 
15831  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
15832  p_ai_config, config_size, ui32LBA);
15833  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
15834  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
15835  if (NI_RETCODE_SUCCESS != retval)
15836  {
15837  ni_log2(p_ctx, NI_LOG_ERROR,
15838  "ERROR: ni_nvme_send_admin_cmd failed: blk_io_handle: %" PRIx64
15839  ", hw_id, %u, xcoder_inst_id: %d\n",
15840  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
15841  //Close the session since we can't configure it
15842  retval = ni_ai_session_close(p_ctx, 0);
15843  if (NI_RETCODE_SUCCESS != retval)
15844  {
15845  ni_log2(p_ctx, NI_LOG_ERROR,
15846  "ERROR: ni_ai_session_close failed: blk_io_handle: %" PRIx64
15847  ", hw_id, %u, xcoder_inst_id: %d\n",
15848  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id,
15849  p_ctx->session_id);
15850  }
15851 
15853  LRETURN;
15854  }
15855 
15856  dataLen = (sizeof(ni_network_layer_info_t) + (NI_MEM_PAGE_ALIGNMENT - 1)) &
15857  ~(NI_MEM_PAGE_ALIGNMENT - 1);
15858  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), dataLen))
15859  {
15860  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: Cannot allocate buffer.\n");
15861  retval = NI_RETCODE_ERROR_MEM_ALOC;
15862  LRETURN;
15863  }
15864 
15865  for (;;)
15866  {
15867  /* test if the model is already exist. if not, then continue to write binary data */
15868  memset(p_buffer, 0, dataLen);
15870  "6J") >= 0)
15871  {
15872  ui32LBA = QUERY_INSTANCE_NL_SIZE_V2_R(p_ctx->session_id,
15874  } else
15875  {
15876  ui32LBA =
15878  }
15879  retval =
15881  p_buffer, dataLen, ui32LBA);
15882  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
15883  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
15884  if (retval != NI_RETCODE_SUCCESS)
15885  {
15886  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
15888  LRETURN;
15889  }
15890  if (((ni_instance_buf_info_t *)p_buffer)->buf_avail_size > 0)
15891  {
15892  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): network binary registered\n", __func__);
15893  LRETURN;
15894  }
15895 
15896  /* if this model is not ready, try to require nbuffer to write */
15898  NI_DEVICE_TYPE_AI, &buf_info);
15899  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
15900  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
15901  if (retval != NI_RETCODE_SUCCESS)
15902  {
15903  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): Query Write buffer Failed\n", __func__);
15904  LRETURN;
15905  }
15906  if (buf_info.buf_avail_size < nb_size)
15907  {
15908  /*
15909  * Cannot aquire the write access to nbuffer because someone else on the go,
15910  * Retry to see if model is ready.
15911  */
15912  ni_log2(p_ctx, NI_LOG_TRACE,
15913  "AI write query failed or buf_size %u < "
15914  "frame_size %u.\n", buf_info.buf_avail_size, nb_size);
15915  } else
15916  {
15917  /* Grant write access to nbuffer */
15918  ni_log2(p_ctx, NI_LOG_DEBUG,
15919  "Info ai write query success, available buf "
15920  "size %u >= frame size %u !\n",
15921  buf_info.buf_avail_size, nb_size);
15922  break;
15923  }
15924  retry_count++;
15925  /* Wait for at least 30s */
15927  {
15928  ni_pthread_mutex_unlock(&p_ctx->mutex);
15929  ni_usleep(100);
15930  ni_pthread_mutex_lock(&p_ctx->mutex);
15931  } else
15932  {
15933  ni_log2(p_ctx, NI_LOG_DEBUG, "AI network binary configuration polling timeout\n");
15935  LRETURN;
15936  }
15937  }
15938 
15939 #if 1
15940  /* write network binary data */
15941  ui32LBA = WRITE_INSTANCE_W(p_ctx->session_id, NI_DEVICE_TYPE_AI);
15942  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): write nb LBA 0x%x\n", __func__, ui32LBA);
15943  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
15944  p_nb_data, buffer_size, ui32LBA);
15945  ni_log2(p_ctx, NI_LOG_DEBUG, "write complete retval %d\n", retval);
15946  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_write, p_ctx->device_type,
15947  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
15948  if (NI_RETCODE_SUCCESS != retval)
15949  {
15950  ni_log2(p_ctx, NI_LOG_ERROR,
15951  "ERROR: ni_nvme_send_admin_cmd failed: blk_io_handle: %" PRIx64
15952  ", hw_id, %u, xcoder_inst_id: %d\n",
15953  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
15954  //Close the session since we can't configure it
15955  retval = ni_ai_session_close(p_ctx, 0);
15956  if (NI_RETCODE_SUCCESS != retval)
15957  {
15958  ni_log2(p_ctx, NI_LOG_ERROR,
15959  "ERROR: ni_ai_session_close failed: blk_io_handle: %" PRIx64
15960  ", hw_id, %u, xcoder_inst_id: %d\n",
15961  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id,
15962  p_ctx->session_id);
15963  }
15964 
15966  LRETURN;
15967  }
15968 
15969 #else
15970  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): write nb buffer_size %u\n", __func__,
15971  buffer_size);
15972  for (transferred = 0; transferred < buffer_size; transferred += this_size)
15973  {
15974  this_size = p_ctx->max_nvme_io_size < (buffer_size - transferred) ?
15975  p_ctx->max_nvme_io_size :
15976  (buffer_size - transferred);
15977 
15978  if (this_size & (4096 - 1))
15979  {
15980  this_size = (this_size + (4096 - 1)) & ~(4096 - 1);
15981  }
15982  /* write network binary data */
15983  ui32LBA = WRITE_INSTANCE_W(p_ctx->session_id, NI_DEVICE_TYPE_AI) +
15984  (transferred >> 12);
15985  ni_log2(p_ctx, NI_LOG_DEBUG,
15986  "%s(): write nb LBA 0x%x, this_size %u, page_offset %u\n",
15987  __func__, ui32LBA, this_size, (transferred >> 12));
15988  p_data = (uint8_t *)p_nb_data + transferred;
15989  retval = ni_nvme_send_write_cmd(
15990  p_ctx->blk_io_handle, p_ctx->event_handle,
15991  (uint8_t *)p_nb_data + transferred, this_size, ui32LBA);
15992  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): write retval %d\n", __func__, retval);
15993  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_write, p_ctx->device_type,
15994  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
15995  if (NI_RETCODE_SUCCESS != retval)
15996  {
15997  ni_log(
15998  NI_LOG_ERROR,
15999  "ERROR: ni_nvme_send_admin_cmd failed: blk_io_handle: %" PRIx64
16000  ", hw_id, %u, xcoder_inst_id: %d\n",
16001  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
16002  //Close the session since we can't configure it
16003  retval = ni_ai_session_close(p_ctx, 0);
16004  if (NI_RETCODE_SUCCESS != retval)
16005  {
16006  ni_log(
16007  NI_LOG_ERROR,
16008  "ERROR: ni_ai_session_close failed: blk_io_handle: %" PRIx64
16009  ", hw_id, %u, xcoder_inst_id: %d\n",
16010  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id,
16011  p_ctx->session_id);
16012  }
16013 
16015  LRETURN;
16016  }
16017  }
16018 
16019 #endif
16020 
16021 END:
16022  ni_pthread_mutex_unlock(&p_ctx->mutex);
16023 
16024  ni_aligned_free(p_ai_config);
16025  ni_aligned_free(p_nb_data);
16026  ni_aligned_free(p_buffer);
16027 
16028  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
16029 
16030  return retval;
16031 }
16032 
16034 {
16035  void *p_stream_info = NULL;
16036  void *p_ai_config = NULL;
16037  void *p_nb_data = NULL;
16039  uint32_t ui32LBA = 0;
16040  uint32_t config_size;
16041 
16042  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
16043 
16044  if (!p_ctx)
16045  {
16046  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
16047  __func__);
16048  retval = NI_RETCODE_INVALID_PARAM;
16049  LRETURN;
16050  }
16051 
16052  ni_pthread_mutex_lock(&p_ctx->mutex);
16053 
16054  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
16055  {
16056  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
16057  __func__);
16059  LRETURN;
16060  }
16061 
16062  config_size = (sizeof(ni_instance_mgr_stream_info_t) + (NI_MEM_PAGE_ALIGNMENT - 1)) &
16063  ~(NI_MEM_PAGE_ALIGNMENT - 1);
16064 
16065  if (ni_posix_memalign(&p_stream_info, sysconf(_SC_PAGESIZE), config_size))
16066  {
16067  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: Cannot allocate stream info buffer.\n");
16068  retval = NI_RETCODE_ERROR_MEM_ALOC;
16069  LRETURN;
16070  }
16071 
16072  ((ni_instance_mgr_stream_info_t *)p_stream_info)->picture_width = p_ctx->active_video_width;
16073  ((ni_instance_mgr_stream_info_t *)p_stream_info)->picture_height = p_ctx->active_video_height;
16074  ((ni_instance_mgr_stream_info_t *)p_stream_info)->ai_model_type = AI_MODEL_TYPE_HVSPLUS_FILTER;
16075  ((ni_instance_mgr_stream_info_t *)p_stream_info)->ai_enhance_level = p_ctx->hvsplus_level;
16076  ((ni_instance_mgr_stream_info_t *)p_stream_info)->pix_format = p_ctx->pixel_format;
16077 
16078  ni_log2(p_ctx, NI_LOG_DEBUG,
16079  "### %s: width %d %d height %d model type %d level %d pix_format %d\n", __func__,
16080  p_ctx->active_video_width, p_ctx->ori_width, p_ctx->active_video_height,
16081  ((ni_instance_mgr_stream_info_t *)p_stream_info)->ai_model_type,
16082  ((ni_instance_mgr_stream_info_t *)p_stream_info)->ai_enhance_level,
16083  ((ni_instance_mgr_stream_info_t *)p_stream_info)->pix_format);
16084 
16085  /* configure AI manager to load the suitable network binary */
16087  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));
16088 
16089  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
16090  p_stream_info, config_size, ui32LBA);
16091  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
16092  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
16093  if (NI_RETCODE_SUCCESS != retval)
16094  {
16095  ni_log2(p_ctx, NI_LOG_ERROR,
16096  "ERROR: ni_nvme_send_admin_cmd failed: blk_io_handle: %" PRIx64
16097  ", hw_id, %u, xcoder_inst_id: %d\n",
16098  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
16099  //Close the session since we can't configure it
16100  retval = ni_ai_session_close(p_ctx, 0);
16101  if (NI_RETCODE_SUCCESS != retval)
16102  {
16103  ni_log2(p_ctx, NI_LOG_ERROR,
16104  "ERROR: ni_ai_session_close failed: blk_io_handle: %" PRIx64
16105  ", hw_id, %u, xcoder_inst_id: %d\n",
16106  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id,
16107  p_ctx->session_id);
16108  }
16109 
16111  LRETURN;
16112  }
16113 
16114 END:
16115  ni_pthread_mutex_unlock(&p_ctx->mutex);
16116 
16117  ni_aligned_free(p_ai_config);
16118  ni_aligned_free(p_nb_data);
16119  ni_aligned_free(p_stream_info);
16120 
16121  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
16122 
16123  return retval;
16124 }
16125 
16127 {
16129  void *p_buffer = NULL;
16130  uint32_t ui32LBA = 0;
16131  int retry_count = 0;
16132  uint32_t dataLen = (sizeof(ni_network_layer_info_t) + (NI_MEM_PAGE_ALIGNMENT - 1)) &
16133  ~(NI_MEM_PAGE_ALIGNMENT - 1);
16134  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), dataLen))
16135  {
16136  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: Cannot allocate buffer.\n");
16137  retval = NI_RETCODE_ERROR_MEM_ALOC;
16138  LRETURN;
16139  }
16140  for(;;)
16141  {
16142  memset(p_buffer, 0, dataLen);
16143  ui32LBA =
16145  retval =
16147  p_buffer, dataLen, ui32LBA);
16148  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
16149  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
16150  if (retval != NI_RETCODE_SUCCESS)
16151  {
16152  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
16153  }
16154  if (((ni_instance_buf_info_t *)p_buffer)->buf_avail_size > 0)
16155  {
16156  ni_log2(p_ctx, NI_LOG_INFO, "%s(): network binary registered\n", __func__);
16157  break;
16158  }
16159 
16160  retry_count++;
16161  /* Wait for at least 30s */
16163  {
16164  ni_usleep(100);
16165  } else
16166  {
16167  ni_log2(p_ctx, NI_LOG_DEBUG, "AI network binary configuration polling timeout\n");
16169  LRETURN;
16170  }
16171  }
16172 END:
16173 
16174  ni_aligned_free(p_buffer);
16175  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
16176 
16177  return retval;
16178 
16179 }
16180 
16182  ni_frame_t *p_frame)
16183 {
16185  uint32_t ui32LBA = 0;
16186  ni_instance_buf_info_t buf_info = {0};
16187  uint32_t frame_size_bytes;
16188  uint32_t sent_size = 0;
16189  int32_t query_retry = 0;
16190 
16191  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
16192 
16193  if (!p_ctx || !p_frame)
16194  {
16195  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s(): passed parameters is null\n",
16196  __func__);
16197  retval = NI_RETCODE_INVALID_PARAM;
16198  return retval;
16199  }
16200 
16201  if (p_frame->data_len[0] == 0)
16202  {
16203  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() invalid data length\n",
16204  __func__);
16205  retval = NI_RETCODE_INVALID_PARAM;
16206  return retval;
16207  }
16208 
16209  ni_pthread_mutex_lock(&p_ctx->mutex);
16210 
16211 
16212  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
16213  {
16214  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
16215  __func__);
16217  LRETURN;
16218  }
16219 
16220  frame_size_bytes = p_frame->data_len[0];
16221  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): frame_size_bytes %u\n", __func__,
16222  frame_size_bytes);
16223 
16224  for (;;)
16225  {
16226  if (p_ctx->session_statistic.ui32WrBufAvailSize >= frame_size_bytes)
16227  {
16228  buf_info.buf_avail_size =
16230  ni_log2(p_ctx, NI_LOG_DEBUG,
16231  "Info ai write query success, available buf "
16232  "size %u >= frame size %u !\n",
16233  buf_info.buf_avail_size, frame_size_bytes);
16234  break;
16235  }
16236 
16238  "6K") >= 0)
16239  {
16241  &p_ctx->session_statistic);
16242  CHECK_ERR_RC(p_ctx, retval, &p_ctx->session_statistic,
16244  p_ctx->hw_id, &(p_ctx->session_id), OPT_2);
16245  buf_info.buf_avail_size =
16247  } else
16248  {
16250  NI_DEVICE_TYPE_AI, &buf_info);
16251  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
16252  p_ctx->device_type, p_ctx->hw_id,
16253  &(p_ctx->session_id), OPT_1);
16254  }
16255  if (NI_RETCODE_SUCCESS != retval ||
16256  buf_info.buf_avail_size < frame_size_bytes)
16257  {
16258  ni_log2(p_ctx, NI_LOG_TRACE,
16259  "AI write query failed or buf_size < frame_size. Retry %d\n",
16260  query_retry);
16261  // extend to 5000 retries for 8K encode on FPGA
16262  if (query_retry >= NI_MAX_ENCODER_QUERY_RETRIES)
16263  {
16264  ni_log2(p_ctx, NI_LOG_DEBUG,
16265  "AI write query exceeded max retries: %d\n",
16268  retval = NI_RETCODE_SUCCESS;
16269  LRETURN;
16270  }
16271  ni_pthread_mutex_unlock(&p_ctx->mutex);
16273  ni_pthread_mutex_lock(&p_ctx->mutex);
16274  query_retry++;
16275  }
16276  }
16277 
16278  ui32LBA = WRITE_INSTANCE_W(p_ctx->session_id, NI_DEVICE_TYPE_AI);
16279  ni_log2(p_ctx, NI_LOG_DEBUG, "Ai session write: p_data = %p, p_frame->buffer_size = %u, "
16280  "p_ctx->frame_num = %" PRIu64 ", LBA = 0x%x\n",
16281  p_frame->p_data, p_frame->buffer_size, p_ctx->frame_num,
16282  ui32LBA);
16283 
16284  sent_size = frame_size_bytes;
16285  if (sent_size & (NI_MEM_PAGE_ALIGNMENT - 1))
16286  {
16287  sent_size = (sent_size + NI_MEM_PAGE_ALIGNMENT - 1) &
16288  ~(NI_MEM_PAGE_ALIGNMENT - 1);
16289  }
16290 
16291  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
16292  p_frame->p_buffer, sent_size, ui32LBA);
16294  "6K") >= 0)
16295  {
16296  if (retval != NI_RETCODE_SUCCESS)
16297  {
16298  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
16300  LRETURN;
16301  }
16303  &p_ctx->session_statistic);
16304  CHECK_ERR_RC(p_ctx, retval, &p_ctx->session_statistic,
16306  p_ctx->hw_id, &(p_ctx->session_id), OPT_2);
16307  } else
16308  {
16309  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_write, p_ctx->device_type,
16310  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
16311  if (retval < 0)
16312  {
16313  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
16315  LRETURN;
16316  }
16317  }
16318 
16319  p_ctx->frame_num++;
16320 
16321  retval = frame_size_bytes;
16322 
16323 END:
16324 
16325  ni_pthread_mutex_unlock(&p_ctx->mutex);
16326 
16327  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
16328 
16329  return retval;
16330 }
16331 
16333  ni_packet_t *p_packet)
16334 {
16335  uint32_t actual_read_size = 0;
16336  int retval = NI_RETCODE_SUCCESS;
16337  uint32_t ui32LBA = 0;
16338  ni_instance_buf_info_t buf_info = {0};
16339 
16340  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
16341 
16342  if (!p_ctx || !p_packet || !p_packet->p_data)
16343  {
16344  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
16345  __func__);
16346  retval = NI_RETCODE_INVALID_PARAM;
16347  return retval;
16348  }
16349 
16350  ni_pthread_mutex_lock(&p_ctx->mutex);
16351 
16352  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
16353  {
16354  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
16355  __func__);
16357  LRETURN;
16358  }
16359 
16360  for (;;)
16361  {
16362  if (p_ctx->session_statistic.ui32RdBufAvailSize >= p_packet->data_len)
16363  {
16364  buf_info.buf_avail_size =
16366  ni_log2(p_ctx, NI_LOG_DEBUG,
16367  "Info ai read query success, available buf "
16368  "size %u >= frame size %u !\n",
16369  buf_info.buf_avail_size, p_packet->data_len);
16370  break;
16371  }
16373  "6K") >= 0)
16374  {
16376  &p_ctx->session_statistic);
16377  CHECK_ERR_RC(p_ctx, retval, &p_ctx->session_statistic,
16379  p_ctx->hw_id, &(p_ctx->session_id), OPT_2);
16380  buf_info.buf_avail_size =
16382  } else
16383  {
16385  NI_DEVICE_TYPE_AI, &buf_info);
16386  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
16387  p_ctx->device_type, p_ctx->hw_id,
16388  &(p_ctx->session_id), OPT_1);
16389  }
16390  ni_log2(p_ctx, NI_LOG_TRACE,
16391  "Info ai read query rc %d, available buf size %u, "
16392  "frame_num=%" PRIu64 ", pkt_num=%" PRIu64 "\n",
16393  retval, buf_info.buf_avail_size, p_ctx->frame_num,
16394  p_ctx->pkt_num);
16395 
16396  if (NI_RETCODE_SUCCESS != retval)
16397  {
16398  ni_log2(p_ctx, NI_LOG_DEBUG, "Buffer info query failed in ai read!!!!\n");
16399  LRETURN;
16400  } else if (0 == buf_info.buf_avail_size)
16401  {
16402  ni_log2(p_ctx, NI_LOG_DEBUG, "Info ai read available buf size %u, eos %u !\n",
16403  buf_info.buf_avail_size, p_packet->end_of_stream);
16404  retval = NI_RETCODE_SUCCESS;
16405  LRETURN;
16406  }
16407  }
16408  ni_log2(p_ctx, NI_LOG_DEBUG, "Ai read buf_avail_size %u\n", buf_info.buf_avail_size);
16409 
16410  assert(buf_info.buf_avail_size >= p_packet->data_len);
16411 
16412  ui32LBA = READ_INSTANCE_R(p_ctx->session_id, NI_DEVICE_TYPE_AI);
16413  actual_read_size = p_packet->data_len;
16414  if (actual_read_size & (NI_MEM_PAGE_ALIGNMENT - 1))
16415  {
16416  actual_read_size = (actual_read_size + (NI_MEM_PAGE_ALIGNMENT - 1)) &
16417  ~(NI_MEM_PAGE_ALIGNMENT - 1);
16418  }
16419 
16420  retval = ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
16421  p_packet->p_data, actual_read_size, ui32LBA);
16423  "6K") >= 0)
16424  {
16425  if (retval != NI_RETCODE_SUCCESS)
16426  {
16427  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
16429  LRETURN;
16430  }
16432  &p_ctx->session_statistic);
16433  CHECK_ERR_RC(p_ctx, retval, &p_ctx->session_statistic,
16435  p_ctx->hw_id, &(p_ctx->session_id), OPT_2);
16436  } else
16437  {
16438  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_read, p_ctx->device_type,
16439  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
16440  if (retval < 0)
16441  {
16442  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
16444  LRETURN;
16445  }
16446  }
16447 
16448  retval = p_packet->data_len;
16449 
16450 END:
16451 
16452  ni_pthread_mutex_unlock(&p_ctx->mutex);
16453 
16454  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
16455 
16456  return retval;
16457 }
16458 
16459 static void ni_unreference_network_data(ni_network_data_t *network_data)
16460 {
16461  if (network_data)
16462  {
16463  ni_memfree(network_data->inset);
16464  ni_memfree(network_data->linfo.in_param);
16465  }
16466 }
16467 
16469  ni_network_data_t *p_network)
16470 {
16471  void *p_buffer = NULL;
16472  void *p_info = NULL;
16474  uint32_t ui32LBA = 0;
16475  uint32_t dataLen;
16476  int32_t query_retry = 0;
16477  int l;
16478  ni_network_layer_params_t *layer_param;
16479  uint32_t buffer_size;
16480  uint32_t this_size;
16481  ni_network_data_t *network_data = NULL;
16482  uint32_t total_io_num;
16483 
16484  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
16485 
16486  if (!p_ctx || !p_network)
16487  {
16488  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
16489  __func__);
16490  retval = NI_RETCODE_INVALID_PARAM;
16491  LRETURN;
16492  }
16493 
16494  network_data = p_ctx->network_data;
16495  if (network_data->linfo.in_param != NULL)
16496  {
16497  ni_log2(p_ctx, NI_LOG_ERROR,
16498  "ERROR: %s(): network parameters data is already initialized\n",
16499  __func__);
16500  retval = NI_RETCODE_SUCCESS;
16501  LRETURN;
16502  }
16503 
16504  ni_pthread_mutex_lock(&p_ctx->mutex);
16505 
16506  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
16507  {
16508  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
16509  __func__);
16511  LRETURN;
16512  }
16513 
16514  /* query available size can be read. */
16515  dataLen = (sizeof(ni_instance_buf_info_t) + (NI_MEM_PAGE_ALIGNMENT - 1)) &
16516  ~(NI_MEM_PAGE_ALIGNMENT - 1);
16517  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), dataLen))
16518  {
16519  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
16520  NI_ERRNO, __func__);
16521  retval = NI_RETCODE_ERROR_MEM_ALOC;
16522  LRETURN;
16523  }
16524  memset(p_buffer, 0, dataLen);
16525 
16526  for (;;)
16527  {
16529  "6J") >= 0)
16530  {
16531  ui32LBA = QUERY_INSTANCE_NL_SIZE_V2_R(p_ctx->session_id,
16533  } else
16534  {
16535  ui32LBA =
16537  }
16538  retval =
16540  p_buffer, dataLen, ui32LBA);
16541  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_read, p_ctx->device_type,
16542  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
16543  if (retval != NI_RETCODE_SUCCESS)
16544  {
16545  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
16547  LRETURN;
16548  }
16549 
16550  if (((ni_instance_buf_info_t *)p_buffer)->buf_avail_size > 0)
16551  {
16552  break;
16553  }
16554 
16555  query_retry++;
16556  if (query_retry > 50000)
16557  {
16558  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): exceeded max query retries: %d\n",
16559  __func__, query_retry - 1);
16561  LRETURN;
16562  }
16563  ni_pthread_mutex_unlock(&p_ctx->mutex);
16564  ni_usleep(200);
16565  ni_pthread_mutex_lock(&p_ctx->mutex);
16566  continue;
16567  }
16568 
16569  if (((ni_instance_buf_info_t *)p_buffer)->buf_avail_size == 0)
16570  {
16572  LRETURN;
16573  }
16574 
16576  "6J") >= 0)
16577  {
16578  network_data->input_num =
16579  ((ni_instance_buf_info_t *)p_buffer)->buf_avail_size >> 16;
16580  network_data->output_num =
16581  ((ni_instance_buf_info_t *)p_buffer)->buf_avail_size & 0xFFFF;
16582  } else
16583  {
16584  network_data->input_num = 4;
16585  network_data->output_num = 4;
16586  }
16587 
16588  total_io_num = network_data->input_num + network_data->output_num;
16589 
16590  network_data->linfo.in_param = (ni_network_layer_params_t *)calloc(
16591  total_io_num, sizeof(ni_network_layer_params_t));
16592  if (!network_data->linfo.in_param)
16593  {
16594  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): Unable to allocate network layer params\n",
16595  __func__);
16596  retval = NI_RETCODE_ERROR_MEM_ALOC;
16597  LRETURN;
16598  }
16599  network_data->linfo.out_param =
16600  network_data->linfo.in_param + network_data->input_num;
16601 
16602  network_data->inset = (ni_network_layer_offset_t *)calloc(
16603  total_io_num, sizeof(ni_network_layer_offset_t));
16604  if (!network_data->inset)
16605  {
16606  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): Unable to allocate network offset\n",
16607  __func__);
16608  retval = NI_RETCODE_ERROR_MEM_ALOC;
16609  LRETURN;
16610  }
16611  network_data->outset = network_data->inset + network_data->input_num;
16612 
16614  "6J") >= 0)
16615  {
16616  /* query the real network layer data */
16617  this_size = sizeof(ni_network_layer_params_t) * total_io_num;
16618  dataLen = (this_size + (NI_MEM_PAGE_ALIGNMENT - 1)) &
16619  ~(NI_MEM_PAGE_ALIGNMENT - 1);
16620  if (ni_posix_memalign(&p_info, sysconf(_SC_PAGESIZE), dataLen))
16621  {
16622  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate info buffer\n",
16623  NI_ERRNO, __func__);
16624  retval = NI_RETCODE_ERROR_MEM_ALOC;
16625  LRETURN;
16626  }
16627  memset(p_info, 0, dataLen);
16628 
16630  retval = ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
16631  p_info, dataLen, ui32LBA);
16632  if ((int32_t)retval < 0)
16633  {
16634  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
16636  LRETURN;
16637  }
16638  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_read, p_ctx->device_type,
16639  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
16640  memcpy(network_data->linfo.in_param, p_info, this_size);
16641  } else
16642  {
16643  /* query the real network layer data */
16644  this_size = sizeof(ni_network_layer_params_t) * total_io_num;
16645  dataLen = (this_size + (NI_MEM_PAGE_ALIGNMENT - 1)) &
16646  ~(NI_MEM_PAGE_ALIGNMENT - 1);
16647  if (ni_posix_memalign(&p_info, sysconf(_SC_PAGESIZE), dataLen))
16648  {
16649  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate info buffer\n",
16650  NI_ERRNO, __func__);
16651  retval = NI_RETCODE_ERROR_MEM_ALOC;
16652  LRETURN;
16653  }
16654  memset(p_info, 0, dataLen);
16655 
16657  retval = ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
16658  p_info, dataLen, ui32LBA);
16659  if ((int32_t)retval < 0)
16660  {
16661  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
16663  LRETURN;
16664  }
16665  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_read, p_ctx->device_type,
16666  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
16667  memcpy(network_data->linfo.in_param, p_info, this_size);
16668 
16669  for (l = 0, network_data->input_num = 0; l < 4; l++)
16670  {
16671  layer_param = &network_data->linfo.in_param[l];
16672  if (layer_param->num_of_dims == 0)
16673  {
16674  break;
16675  }
16676  network_data->input_num++;
16677  }
16678 
16679  for (l = 0, network_data->output_num = 0; l < 4; l++)
16680  {
16681  layer_param = &network_data->linfo.out_param[l];
16682  if (layer_param->num_of_dims == 0)
16683  {
16684  break;
16685  }
16686  network_data->output_num++;
16687  }
16688  }
16689 
16690  for (l = 0, buffer_size = 0; l < network_data->input_num; l++)
16691  {
16692  layer_param = &network_data->linfo.in_param[l];
16693  this_size = ni_ai_network_layer_size(layer_param);
16694  this_size =
16695  (this_size + NI_AI_HW_ALIGN_SIZE - 1) & ~(NI_AI_HW_ALIGN_SIZE - 1);
16696  network_data->inset[l].offset = buffer_size;
16697  buffer_size += this_size;
16698 
16699  ni_log2(p_ctx,
16700  NI_LOG_DEBUG,
16701  "%s(): network input layer %d: dims %u, %u/%u/%u/%u, f %d, q %d\n",
16702  __func__, l, layer_param->num_of_dims, layer_param->sizes[0],
16703  layer_param->sizes[1], layer_param->sizes[2], layer_param->sizes[3],
16704  layer_param->data_format, layer_param->quant_format);
16705  }
16706 
16707  for (l = 0, buffer_size = 0; l < network_data->output_num; l++)
16708  {
16709  layer_param = &network_data->linfo.out_param[l];
16710  this_size = ni_ai_network_layer_size(layer_param);
16711  this_size =
16712  (this_size + NI_AI_HW_ALIGN_SIZE - 1) & ~(NI_AI_HW_ALIGN_SIZE - 1);
16713  network_data->outset[l].offset = buffer_size;
16714  buffer_size += this_size;
16715 
16716  ni_log2(p_ctx,
16717  NI_LOG_DEBUG,
16718  "%s(): network output layer %d: dims %u, %u/%u/%u/%u, f %d, q %d\n",
16719  __func__, l, layer_param->num_of_dims, layer_param->sizes[0],
16720  layer_param->sizes[1], layer_param->sizes[2], layer_param->sizes[3],
16721  layer_param->data_format, layer_param->quant_format);
16722  }
16723 
16724  memcpy(p_network, network_data, sizeof(ni_network_data_t));
16725 
16726  retval = NI_RETCODE_SUCCESS;
16727 
16728 END:
16729  ni_pthread_mutex_unlock(&p_ctx->mutex);
16730 
16731  if (retval != NI_RETCODE_SUCCESS)
16732  {
16733  ni_unreference_network_data(network_data);
16734  }
16735  ni_aligned_free(p_buffer);
16736  ni_aligned_free(p_info);
16737  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
16738  return retval;
16739 }
16740 
16742 {
16744  void *p_buffer = NULL;
16745  uint32_t ui32LBA = 0;
16746 
16747  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
16748 
16749  if (!p_ctx)
16750  {
16751  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
16752  __func__);
16753  retval = NI_RETCODE_INVALID_PARAM;
16754  LRETURN;
16755  }
16756 
16757  //Check if there is an instance or we need a new one
16758  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
16759  {
16760  p_ctx->device_type = NI_DEVICE_TYPE_AI;
16761  p_ctx->pts_table = NULL;
16762  p_ctx->dts_queue = NULL;
16763  p_ctx->buffer_pool = NULL;
16764  p_ctx->status = 0;
16765  p_ctx->key_frame_type = 0;
16766  p_ctx->keyframe_factor = 1;
16767  p_ctx->frame_num = 0;
16768  p_ctx->pkt_num = 0;
16769  p_ctx->rc_error_count = 0;
16770  p_ctx->force_frame_type = 0;
16771  p_ctx->ready_to_close = 0;
16772  //Sequence change tracking reated stuff
16773  p_ctx->active_video_width = 0;
16774  p_ctx->active_video_height = 0;
16775  p_ctx->p_all_zero_buf = NULL;
16776  p_ctx->actual_video_width = 0;
16778  memset(&(p_ctx->param_err_msg[0]), 0, sizeof(p_ctx->param_err_msg));
16779 
16780  //malloc zero data buffer
16781  if (ni_posix_memalign(&p_ctx->p_all_zero_buf, sysconf(_SC_PAGESIZE),
16783  {
16784  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() alloc all zero buffer failed\n",
16785  NI_ERRNO, __func__);
16786  retval = NI_RETCODE_ERROR_MEM_ALOC;
16787  LRETURN;
16788  }
16789  memset(p_ctx->p_all_zero_buf, 0, NI_DATA_BUFFER_LEN);
16790 
16791  //malloc data buffer
16792  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE),
16794  {
16795  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() alloc data buffer failed\n",
16796  NI_ERRNO, __func__);
16797  retval = NI_RETCODE_ERROR_MEM_ALOC;
16798  LRETURN;
16799  }
16800  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
16801 
16802  //Set session ID to be invalid. In case we cannot open session, the session id wold remain invalid.
16803  //In case we can open sesison, the session id would become valid.
16804  ((ni_session_stats_t *)p_buffer)->ui16SessionId =
16805  (uint16_t)NI_INVALID_SESSION_ID;
16806 
16807  // First uint32_t is either an invaild session ID or a valid session ID, depending on if session could be opened
16808  ui32LBA = OPEN_SESSION_CODEC(NI_DEVICE_TYPE_AI, 0, p_ctx->hw_action);
16809  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): LBA 0x%x, hw_action %d\n", __func__,
16810  ui32LBA, p_ctx->hw_action);
16811  retval =
16813  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
16814  if (retval != NI_RETCODE_SUCCESS)
16815  {
16816  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR ni_nvme_send_read_cmd\n");
16817  LRETURN;
16818  }
16819  //Open will return a session status structure with a valid session id if it worked.
16820  //Otherwise the invalid session id set before the open command will stay
16821  if ((uint16_t)NI_INVALID_SESSION_ID ==
16822  ni_ntohs(((ni_session_stats_t *)p_buffer)->ui16SessionId))
16823  {
16824  ni_log2(p_ctx, NI_LOG_ERROR,
16825  "ERROR %s(): p_ctx->device_handle=%" PRIx64
16826  ", p_ctx->hw_id=%d, p_ctx->session_id=%d\n",
16827  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
16828  p_ctx->session_id);
16829  ni_ai_session_close(p_ctx, 0);
16831  LRETURN;
16832  }
16833  p_ctx->session_id =
16834  ni_ntohs(((ni_session_stats_t *)p_buffer)->ui16SessionId);
16835  p_ctx->session_timestamp = ni_htonl(((ni_session_stats_t *)p_buffer)->ui32Session_timestamp_high);
16836  p_ctx->session_timestamp = (p_ctx->session_timestamp << 32) |
16837  ni_htonl(((ni_session_stats_t *)p_buffer)->ui32Session_timestamp_low);
16838  ni_log2(p_ctx, NI_LOG_DEBUG, "Ai open session ID:0x%x,timestamp:%" PRIu64 "\n",
16839  p_ctx->session_id, p_ctx->session_timestamp);
16840 
16841  ni_log2(p_ctx, NI_LOG_DEBUG, "Open session completed\n");
16842  ni_log2(p_ctx, NI_LOG_DEBUG,
16843  "%s(): p_ctx->device_handle=%" PRIx64
16844  ", p_ctx->hw_id=%d, p_ctx->session_id=%d\n",
16845  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
16846  p_ctx->session_id);
16847 
16848  //Send keep alive timeout Info
16849  uint64_t keep_alive_timeout =
16850  p_ctx->keep_alive_timeout * 1000000; //send us to FW
16851  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
16852  memcpy(p_buffer, &keep_alive_timeout, sizeof(keep_alive_timeout));
16853  ni_log2(p_ctx, NI_LOG_DEBUG, "%s keep_alive_timeout %" PRIx64 "\n", __func__,
16854  keep_alive_timeout);
16856  retval =
16858  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
16859  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
16860  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
16861  CHECK_VPU_RECOVERY(retval);
16862 
16863  if (NI_RETCODE_SUCCESS != retval)
16864  {
16865  ni_log2(p_ctx, NI_LOG_ERROR,
16866  "ERROR %s(): nvme write keep_alive_timeout command "
16867  "failed, blk_io_handle: %" PRIx64 ", hw_id, %d\n",
16868  __func__, (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id);
16870  LRETURN;
16871  }
16872 
16873  p_ctx->network_data =
16874  (ni_network_data_t *)calloc(1, sizeof(ni_network_data_t));
16875  if (!p_ctx->network_data)
16876  {
16877  ni_log2(p_ctx, NI_LOG_ERROR,
16878  "ERROR %s(): Unable to allocate network_data memory\n");
16879  retval = NI_RETCODE_ERROR_MEM_ALOC;
16880  LRETURN;
16881  }
16882  }
16883 
16884  // init for frame pts calculation
16885  p_ctx->is_first_frame = 1;
16886  p_ctx->last_pts = 0;
16887  p_ctx->last_dts = 0;
16888  p_ctx->active_video_width = 0;
16889  p_ctx->active_video_height = 0;
16890  p_ctx->actual_video_width = 0;
16891 
16892 END:
16893 
16894  ni_aligned_free(p_buffer);
16895  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
16896  return retval;
16897 }
16898 
16900 {
16902  void *p_buffer = NULL;
16903  uint32_t ui32LBA = 0;
16904 
16905  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
16906 
16907  if (!p_ctx)
16908  {
16909  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
16910  __func__);
16911  return NI_RETCODE_INVALID_PARAM;
16912  }
16913 
16914  ni_pthread_mutex_lock(&p_ctx->mutex);
16915 
16916  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
16917  {
16918  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): Invalid session ID, return.\n", __func__);
16919  retval = NI_RETCODE_SUCCESS;
16920  LRETURN;
16921  }
16922 
16923  //malloc data buffer
16924  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), NI_DATA_BUFFER_LEN))
16925  {
16926  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() alloc data buffer failed\n",
16927  NI_ERRNO, __func__);
16928  retval = NI_RETCODE_ERROR_MEM_ALOC;
16929  LRETURN;
16930  }
16931  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
16932 
16933  ui32LBA = CLOSE_SESSION_R(p_ctx->session_id, NI_DEVICE_TYPE_AI);
16934 
16935  int retry = 0;
16936  while (retry < NI_SESSION_CLOSE_RETRY_MAX)
16937  {
16938  ni_log2(p_ctx, NI_LOG_DEBUG,
16939  "%s(): p_ctx->blk_io_handle=%" PRIx64 ", p_ctx->hw_id=%d, "
16940  "p_ctx->session_id=%d, close_mode=1\n",
16941  __func__, (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id,
16942  p_ctx->session_id);
16943 
16945  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA) < 0)
16946  {
16947  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): command failed\n", __func__);
16950  break;
16951  } else
16952  {
16953  //Close should always succeed
16954  retval = NI_RETCODE_SUCCESS;
16956  break;
16957  }
16958  /*
16959  else if(((ni_session_closed_status_t *)p_buffer)->session_closed)
16960  {
16961  retval = NI_RETCODE_SUCCESS;
16962  p_ctx->session_id = NI_INVALID_SESSION_ID;
16963  break;
16964  }
16965  else
16966  {
16967  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): wait for close\n", __func__);
16968  ni_usleep(NI_SESSION_CLOSE_RETRY_INTERVAL_US);
16969  retval = NI_RETCODE_ERROR_NVME_CMD_FAILED;
16970  }
16971  */
16972  retry++;
16973  }
16974 
16975 END:
16976 
16977  ni_unreference_network_data(p_ctx->network_data);
16978  ni_memfree(p_ctx->network_data);
16979 
16980  ni_aligned_free(p_buffer);
16982 
16983  //Sequence change related stuff cleanup here
16984  p_ctx->active_video_width = 0;
16985  p_ctx->active_video_height = 0;
16986  p_ctx->actual_video_width = 0;
16987 
16988  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): CTX[Card:%" PRIx64 " / HW:%d / INST:%d]\n",
16989  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
16990  p_ctx->session_id);
16991  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
16992 
16993  ni_pthread_mutex_unlock(&p_ctx->mutex);
16994 
16995  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
16996 
16997  return retval;
16998 }
16999 
17001  ni_frame_config_t p_cfg_in[],
17002  int numInCfgs,
17003  ni_frame_config_t *p_cfg_out)
17004 {
17006  ni_network_buffer_t *p_data = NULL;
17007  void *p_read_data = NULL;
17008  uint32_t dataLen;
17009  uint32_t ui32LBA = 0;
17010 
17011  if (!p_ctx)
17012  {
17013  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
17014  __func__);
17015  return NI_RETCODE_INVALID_PARAM;
17016  }
17017 
17018  if (p_ctx->session_id == NI_INVALID_SESSION_ID)
17019  {
17020  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
17021  __func__);
17023  }
17024  ni_pthread_mutex_lock(&p_ctx->mutex);
17025  ni_instance_buf_info_t buf_info = {0};
17026  int32_t query_retry = 0;
17027  for (;;)
17028  {
17029  if (p_ctx->session_statistic.ui32WrBufAvailSize > 0)
17030  {
17031  buf_info.buf_avail_size =
17033  ni_log2(p_ctx, NI_LOG_DEBUG,
17034  "Info ai write query success, available buf "
17035  "size %u !\n",
17036  buf_info.buf_avail_size);
17037  break;
17038  }
17039 
17041  &p_ctx->session_statistic);
17042  CHECK_ERR_RC(p_ctx, retval, &p_ctx->session_statistic,
17044  p_ctx->hw_id, &(p_ctx->session_id), OPT_2);
17045  buf_info.buf_avail_size =
17047  if (NI_RETCODE_SUCCESS != retval ||
17048  buf_info.buf_avail_size == 0)
17049  {
17050  ni_log2(p_ctx, NI_LOG_TRACE,
17051  "AI write query failed or buf_size < frame_size. Retry %d\n",
17052  query_retry);
17053  // extend to 5000 retries for 8K encode on FPGA
17054  if (query_retry >= NI_MAX_ENCODER_QUERY_RETRIES)
17055  {
17056  ni_log2(p_ctx, NI_LOG_TRACE,
17057  "AI write query exceeded max retries: %d\n",
17060  retval = NI_RETCODE_SUCCESS;
17061  LRETURN;
17062  }
17063  ni_pthread_mutex_unlock(&p_ctx->mutex);
17065  ni_pthread_mutex_lock(&p_ctx->mutex);
17066  query_retry++;
17067  }
17068  }
17069 
17070  dataLen = (sizeof(ni_network_buffer_t) + NI_MEM_PAGE_ALIGNMENT - 1) &
17071  ~(NI_MEM_PAGE_ALIGNMENT - 1);
17072 
17073  if (ni_posix_memalign((void **)&p_data, sysconf(_SC_PAGESIZE), dataLen))
17074  {
17075  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
17076  NI_ERRNO, __func__);
17078  }
17079 
17080  memset(p_data, 0x00, dataLen);
17081  p_data->ui16Width = p_cfg_in[0].picture_width;
17082  p_data->ui16Height = p_cfg_in[0].picture_height;
17083  p_data->ui16Option = p_cfg_in[0].options;
17084  p_data->ui8PoolSize = p_cfg_in[0].rgba_color;
17085  p_data->ui8MultiIn = 1;
17086  for(int i = 0; i < numInCfgs; i++){
17087  p_data->ui16FrameIdx[i] = p_cfg_in[i].frame_index;
17088  ni_log2(p_ctx, NI_LOG_DEBUG, "Dev alloc frame[%d]: frame_index %u, hw=%d\n",
17089  i,p_data->ui16FrameIdx[i], p_data->ui8MultiIn);
17090  }
17091 
17092 
17093  ui32LBA =
17095 
17096  retval =
17098  p_data, dataLen, ui32LBA);
17099  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
17100  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id),
17101  OPT_1);
17102  if (NI_RETCODE_SUCCESS != retval)
17103  {
17104  ni_log2(p_ctx,
17105  NI_LOG_ERROR,
17106  "ERROR: ni_nvme_send_admin_cmd failed: blk_io_handle: %" PRIx64
17107  ", hw_id, %u, xcoder_inst_id: %d\n",
17108  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
17109  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed!\n",
17110  __func__);
17112  LRETURN;
17113  }
17114 
17115  END:
17116  ni_pthread_mutex_unlock(&p_ctx->mutex);
17117  ni_aligned_free(p_data);
17118  if (p_read_data != NULL)
17119  {
17120  ni_aligned_free(p_read_data);
17121  }
17122  return retval;
17123 }
17124 
17126  niFrameSurface1_t *p_out_surface)
17127 {
17129  ni_network_buffer_t *p_data = NULL;
17130  void *p_read_data = NULL;
17131  uint32_t dataLen;
17132  uint32_t ui32LBA = 0;
17133 
17134  if (!p_ctx)
17135  {
17136  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
17137  __func__);
17138  return NI_RETCODE_INVALID_PARAM;
17139  }
17140 
17141  if (p_ctx->session_id == NI_INVALID_SESSION_ID)
17142  {
17143  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
17144  __func__);
17146  }
17147 
17148  if (ni_cmp_fw_api_ver(
17150  "6rL") < 0)
17151  {
17152  ni_log2(p_ctx, NI_LOG_ERROR,
17153  "Error: %s function not supported on device with FW API version < 6rL\n",
17154  __func__);
17156  }
17157 
17158  ni_pthread_mutex_lock(&p_ctx->mutex);
17159 
17160  int query_retry = 0;
17161  dataLen =
17162  (sizeof(ni_instance_buf_info_t) + (NI_MEM_PAGE_ALIGNMENT - 1)) &
17163  ~(NI_MEM_PAGE_ALIGNMENT - 1);
17164  if (ni_posix_memalign(&p_read_data, sysconf(_SC_PAGESIZE), dataLen))
17165  {
17166  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
17167  NI_ERRNO, __func__);
17168  retval = NI_RETCODE_ERROR_MEM_ALOC;
17169  LRETURN;
17170  }
17171  memset(p_read_data, 0, dataLen);
17172 
17173  for (;;)
17174  {
17175  ui32LBA = QUERY_INSTANCE_HW_OUT_SIZE_R(p_ctx->session_id,
17177  retval =
17179  p_read_data, dataLen, ui32LBA);
17180  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_read,
17181  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id),
17182  OPT_1);
17183  if (retval != NI_RETCODE_SUCCESS)
17184  {
17185  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
17187  LRETURN;
17188  }
17189 
17190  if (((ni_instance_buf_info_t *)p_read_data)->hw_inst_ind.buffer_avail > 0)
17191  {
17192  p_out_surface->ui16FrameIdx = ((ni_instance_buf_info_t *)p_read_data)->hw_inst_ind.frame_index;
17193  p_out_surface->ui16session_ID = p_ctx->session_id;
17194  p_out_surface->device_handle =
17195  (int32_t)((int64_t)p_ctx->blk_io_handle & 0xFFFFFFFF);
17196  p_out_surface->bit_depth = p_ctx->bit_depth_factor;
17197  p_out_surface->src_cpu = (uint8_t)NI_DEVICE_TYPE_AI;
17198  p_out_surface->output_idx = 0;
17199  break;
17200  }
17201 
17202  query_retry++;
17203  if (query_retry > 2000)
17204  {
17205  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): exceeded max query retries: %d\n",
17206  __func__, query_retry - 1);
17207  retval = NI_RETCODE_EAGAIN;
17208  LRETURN;
17209  }
17210  ni_pthread_mutex_unlock(&p_ctx->mutex);
17211  ni_usleep(200);
17212  ni_pthread_mutex_lock(&p_ctx->mutex);
17213  continue;
17214  }
17215 
17216  if (((ni_instance_buf_info_t *)p_read_data)->hw_inst_ind.buffer_avail == 0)
17217  {
17219  LRETURN;
17220  }
17221 END:
17222  ni_pthread_mutex_unlock(&p_ctx->mutex);
17223  ni_aligned_free(p_data);
17224  if (p_read_data != NULL)
17225  {
17226  ni_aligned_free(p_read_data);
17227  }
17228  return retval;
17229 }
17230 
17232  int height, int options, int pool_size,
17233  int frame_index)
17234 {
17236  ni_network_buffer_t *p_data = NULL;
17237  void *p_read_data = NULL;
17238  uint32_t dataLen;
17239  uint32_t ui32LBA = 0;
17240 
17241  if (!p_ctx)
17242  {
17243  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
17244  __func__);
17245  return NI_RETCODE_INVALID_PARAM;
17246  }
17247 
17248  if (p_ctx->session_id == NI_INVALID_SESSION_ID)
17249  {
17250  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
17251  __func__);
17253  }
17254 
17255  ni_pthread_mutex_lock(&p_ctx->mutex);
17256 
17257  if (options == NI_AI_FLAG_IO)
17258  {
17259  int query_retry = 0;
17260  dataLen =
17261  (sizeof(ni_instance_buf_info_t) + (NI_MEM_PAGE_ALIGNMENT - 1)) &
17262  ~(NI_MEM_PAGE_ALIGNMENT - 1);
17263  if (ni_posix_memalign(&p_read_data, sysconf(_SC_PAGESIZE), dataLen))
17264  {
17265  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
17266  NI_ERRNO, __func__);
17267  retval = NI_RETCODE_ERROR_MEM_ALOC;
17268  LRETURN;
17269  }
17270  memset(p_read_data, 0, dataLen);
17271 
17272  for (;;)
17273  {
17274  ui32LBA = QUERY_INSTANCE_HW_OUT_SIZE_R(p_ctx->session_id,
17276  retval =
17278  p_read_data, dataLen, ui32LBA);
17279  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_read,
17280  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id),
17281  OPT_1);
17282  if (retval != NI_RETCODE_SUCCESS)
17283  {
17284  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
17286  LRETURN;
17287  }
17288 
17289  if (((ni_instance_buf_info_t *)p_read_data)->buf_avail_size > 0)
17290  {
17291  break;
17292  }
17293 
17294  query_retry++;
17295  if (query_retry > 2000)
17296  {
17297  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): exceeded max query retries: %d\n",
17298  __func__, query_retry - 1);
17299  retval = NI_RETCODE_EAGAIN;
17300  LRETURN;
17301  }
17302  ni_pthread_mutex_unlock(&p_ctx->mutex);
17303  ni_usleep(200);
17304  ni_pthread_mutex_lock(&p_ctx->mutex);
17305  continue;
17306  }
17307 
17308  if (((ni_instance_buf_info_t *)p_read_data)->buf_avail_size == 0)
17309  {
17311  LRETURN;
17312  }
17313  } else
17314  {
17315  ni_instance_buf_info_t buf_info = {0};
17316  int32_t query_retry = 0;
17317  for (;;)
17318  {
17319  if (p_ctx->session_statistic.ui32WrBufAvailSize > 0)
17320  {
17321  buf_info.buf_avail_size =
17323  ni_log2(p_ctx, NI_LOG_DEBUG,
17324  "Info ai write query success, available buf "
17325  "size %u !\n",
17326  buf_info.buf_avail_size);
17327  break;
17328  }
17329 
17331  &p_ctx->session_statistic);
17332  CHECK_ERR_RC(p_ctx, retval, &p_ctx->session_statistic,
17334  p_ctx->hw_id, &(p_ctx->session_id), OPT_2);
17335  buf_info.buf_avail_size =
17337  if (NI_RETCODE_SUCCESS != retval ||
17338  buf_info.buf_avail_size == 0)
17339  {
17340  ni_log2(p_ctx, NI_LOG_TRACE,
17341  "AI write query failed or buf_size < frame_size. Retry %d\n",
17342  query_retry);
17343  // extend to 5000 retries for 8K encode on FPGA
17344  if (query_retry >= NI_MAX_ENCODER_QUERY_RETRIES)
17345  {
17346  ni_log2(p_ctx, NI_LOG_TRACE,
17347  "AI write query exceeded max retries: %d\n",
17350  retval = NI_RETCODE_SUCCESS;
17351  LRETURN;
17352  }
17353  ni_pthread_mutex_unlock(&p_ctx->mutex);
17355  ni_pthread_mutex_lock(&p_ctx->mutex);
17356  query_retry++;
17357  }
17358  }
17359 
17360  dataLen = (sizeof(ni_network_buffer_t) + NI_MEM_PAGE_ALIGNMENT - 1) &
17361  ~(NI_MEM_PAGE_ALIGNMENT - 1);
17362 
17363  if (ni_posix_memalign((void **)&p_data, sysconf(_SC_PAGESIZE), dataLen))
17364  {
17365  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
17366  NI_ERRNO, __func__);
17368  }
17369 
17370  memset(p_data, 0x00, dataLen);
17371  p_data->ui16FrameIdx[0] = frame_index;
17372  p_data->ui16Width = width;
17373  p_data->ui16Height = height;
17374  p_data->ui16Option = options;
17375  p_data->ui8PoolSize = pool_size;
17376  p_data->ui8MultiIn = 0;
17377  ni_log2(p_ctx, NI_LOG_DEBUG, "Dev alloc frame: frame_index %u\n",
17378  p_data->ui16FrameIdx[0]);
17379 
17380  ui32LBA =
17382 
17383  retval =
17385  p_data, dataLen, ui32LBA);
17386  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
17387  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id),
17388  OPT_1);
17389  if (NI_RETCODE_SUCCESS != retval)
17390  {
17391  ni_log2(p_ctx,
17392  NI_LOG_ERROR,
17393  "ERROR: ni_nvme_send_admin_cmd failed: blk_io_handle: %" PRIx64
17394  ", hw_id, %u, xcoder_inst_id: %d\n",
17395  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
17396  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed!\n",
17397  __func__);
17399  LRETURN;
17400  }
17401  }
17402 
17403 END:
17404  ni_pthread_mutex_unlock(&p_ctx->mutex);
17405  ni_aligned_free(p_data);
17406  if (p_read_data != NULL)
17407  {
17408  ni_aligned_free(p_read_data);
17409  }
17410  return retval;
17411 }
17412 
17413 /*!******************************************************************************
17414  * \brief read a hardware descriptor from a scaler session
17415  *
17416  * \param[in] p_ctx pointer to session context
17417  * \param[out] p_frame pointer to frame to write hw descriptor
17418  *
17419  * \return NI_RETCODE_INVALID_PARAM
17420  * NI_RETCODE_ERROR_INVALID_SESSION
17421  * NI_RETCODE_ERROR_MEM_ALOC
17422  * NI_RETCODE_ERROR_NVME_CMD_FAILED
17423  * NI_RETCODE_FAILURE
17424  *******************************************************************************/
17426  ni_frame_t *p_frame)
17427 {
17428  int retval = NI_RETCODE_SUCCESS;
17429  int retry_count = 0;
17430 
17431  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
17432 
17433  ni_pthread_mutex_lock(&p_ctx->mutex);
17434 
17435  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
17436  {
17437  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
17438  __func__);
17440  LRETURN;
17441  }
17442 
17443  for (;;)
17444  {
17445  retry_count++;
17446  ni_instance_buf_info_t sInstanceBuf = {0};
17447  niFrameSurface1_t *pFrameSurface;
17449  NI_DEVICE_TYPE_AI, &sInstanceBuf);
17450  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
17451  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id),
17452  OPT_3);
17453 
17455  {
17456  if (retry_count >= 500)
17457  {
17458  ni_log2(p_ctx, NI_LOG_DEBUG, "Warning hwdesc read fail rc %d\n",
17459  retval);
17460  LRETURN;
17461  }
17462  ni_pthread_mutex_unlock(&p_ctx->mutex);
17463  ni_usleep(100);
17464  ni_pthread_mutex_lock(&p_ctx->mutex);
17465  }
17466  else if (retval != NI_RETCODE_SUCCESS)
17467  {
17468  LRETURN;
17469  } else
17470  {
17471  pFrameSurface = (niFrameSurface1_t *)p_frame->p_data[3];
17472  pFrameSurface->ui16FrameIdx = sInstanceBuf.hw_inst_ind.frame_index;
17473  pFrameSurface->ui16session_ID = p_ctx->session_id;
17474  pFrameSurface->device_handle =
17475  (int32_t)((int64_t)p_ctx->blk_io_handle & 0xFFFFFFFF);
17476  pFrameSurface->src_cpu = (uint8_t)NI_DEVICE_TYPE_AI;
17477  pFrameSurface->output_idx = 0;
17478 
17479  /* A frame index of zero is invalid, the memory acquisition failed */
17480  if (pFrameSurface->ui16FrameIdx == 0)
17481  {
17482  if (retry_count >= 500)
17483  {
17484  ni_log2(p_ctx, NI_LOG_DEBUG, "Warning hwdesc read fail rc %d\n",
17485  retval);
17486  retval = NI_RETCODE_EAGAIN;
17487  LRETURN;
17488  }
17489  ni_pthread_mutex_unlock(&p_ctx->mutex);
17490  ni_usleep(100);
17491  ni_pthread_mutex_lock(&p_ctx->mutex);
17492  continue;
17493  }
17494  LRETURN;
17495  }
17496  }
17497 
17498 END:
17499 
17500  ni_pthread_mutex_unlock(&p_ctx->mutex);
17501 
17502  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
17503 
17504  return retval;
17505 }
17506 
17507 /*!*****************************************************************************
17508  * \brief Get DDR configuration of Quadra device
17509  *
17510  * \param[in/out] p_ctx pointer to a session context with valid file handle
17511  *
17512  * \return On success NI_RETCODE_SUCCESS
17513  * On failure NI_RETCODE_INVALID_PARAM
17514  * NI_RETCODE_ERROR_MEM_ALOC
17515  * NI_RETCODE_ERROR_NVME_CMD_FAILED
17516  ******************************************************************************/
17518 {
17519  void *p_buffer = NULL;
17520  ni_nvme_identity_t *p_id_data;
17522  ni_event_handle_t event_handle = NI_INVALID_EVENT_HANDLE;
17523  uint32_t ui32LBA = IDENTIFY_DEVICE_R;
17524  ni_device_handle_t device_handle = p_ctx->blk_io_handle;
17525 
17526  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
17527 
17528  if (NI_INVALID_DEVICE_HANDLE == device_handle)
17529  {
17530  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s(): invalid passed parameters\n",
17531  __func__);
17532  retval = NI_RETCODE_INVALID_PARAM;
17533  LRETURN;
17534  }
17535 
17536  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE),
17538  {
17539  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer.\n",
17540  NI_ERRNO, __func__);
17541  retval = NI_RETCODE_ERROR_MEM_ALOC;
17542  LRETURN;
17543  }
17544 
17545  memset(p_buffer, 0, NI_NVME_IDENTITY_CMD_DATA_SZ);
17546 
17547  if (ni_nvme_send_read_cmd(device_handle, event_handle, p_buffer,
17548  NI_NVME_IDENTITY_CMD_DATA_SZ, ui32LBA) < 0)
17549  {
17551  LRETURN;
17552  }
17553 
17554  p_id_data = (ni_nvme_identity_t *) p_buffer;
17555 
17556  if (p_id_data->memory_cfg == NI_QUADRA_MEMORY_CONFIG_SR_4G)
17557  {
17558  p_ctx->ddr_config = 6;
17559  }
17560  else
17561  {
17562  if (ni_cmp_fw_api_ver(
17564  "6rJ") >= 0)
17565  {
17566  p_ctx->ddr_config = (p_id_data->memory_cfg == NI_QUADRA_MEMORY_CONFIG_SR)
17567  ? 3 : ((p_id_data->memory_cfg == NI_QUADRA_MEMORY_CONFIG_DR)? 4 : 5);
17568  }
17569  else if (ni_cmp_fw_api_ver(
17571  "6rD") >= 0)
17572  {
17573  p_ctx->ddr_config = (p_id_data->memory_cfg == NI_QUADRA_MEMORY_CONFIG_SR)
17574  ? 3 : 4;
17575  } else
17576  {
17577  p_ctx->ddr_config = (p_id_data->memory_cfg == NI_QUADRA_MEMORY_CONFIG_SR)
17578  ? 1 : 2;
17579  }
17580  }
17581 
17582  ni_log2(p_ctx, NI_LOG_DEBUG, "Memory configuration %d\n",p_ctx->ddr_config);
17583 END:
17584 
17585  ni_aligned_free(p_buffer);
17586  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): retval: %d\n", __func__, retval);
17587 
17588  return retval;
17589 }
17590 
17591 /*!*****************************************************************************
17592  * \brief Set DDR configuration of Quadra device
17593  *
17594  * \param[in] p_ctx pointer to a session context with valid file handle
17595  * \param[in] ddr_priority_mode ddr priority mode
17596  *
17597  * \return On success NI_RETCODE_SUCCESS
17598  * On failure NI_RETCODE_INVALID_PARAM
17599  * NI_RETCODE_ERROR_MEM_ALOC
17600  * NI_RETCODE_ERROR_NVME_CMD_FAILED
17601  ******************************************************************************/
17603  uint8_t ddr_priority_mode)
17604 {
17605  void *p_buffer = NULL;
17607  ni_ddr_priority_config_t *p_cfg = NULL;
17608  uint32_t ui32LBA = CONFIG_SESSION_DDR_PRIORITY_W(p_ctx->session_id);
17609  ni_device_handle_t device_handle = p_ctx->blk_io_handle;
17610 
17611  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
17612 
17613  ni_log2(p_ctx, NI_LOG_INFO, "set ddr priority %d\n",
17614  ddr_priority_mode);
17615 
17616  if (NI_INVALID_DEVICE_HANDLE == device_handle)
17617  {
17618  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s(): invalid passed parameters\n",
17619  __func__);
17620  retval = NI_RETCODE_INVALID_PARAM;
17621  LRETURN;
17622  }
17623 
17624  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE),
17626  {
17627  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer.\n",
17628  NI_ERRNO, __func__);
17629  retval = NI_RETCODE_ERROR_MEM_ALOC;
17630  LRETURN;
17631  }
17632 
17633  memset(p_buffer, 0, NI_NVME_IDENTITY_CMD_DATA_SZ);
17634  p_cfg = (ni_ddr_priority_config_t *)p_buffer;
17635  p_cfg->ddr_mode = ddr_priority_mode;
17636  if (ni_nvme_send_write_cmd(p_ctx->blk_io_handle, NI_INVALID_EVENT_HANDLE,
17637  p_buffer,NI_DATA_BUFFER_LEN, ui32LBA) < 0)
17638  {
17639  ni_log2(p_ctx, NI_LOG_ERROR, "DDR priority setting failed with mode %d\n",
17640  ddr_priority_mode);
17642  LRETURN;
17643  }
17644 END:
17645  ni_aligned_free(p_buffer);
17646  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): retval: %d\n", __func__, retval);
17647 
17648  return retval;
17649 }
17650 
17651 /*!*****************************************************************************
17652  * \brief Allocate memory for the metadata header and auxillary data for
17653  * encoder input data.
17654  *
17655  * \param[in] p_frame Pointer to a caller allocated ni_frame_t struct
17656  *
17657  * \param[in] extra_len Length header and auxillary data
17658  *
17659  * \return On success
17660  * NI_RETCODE_SUCCESS
17661  * On failure
17662  * NI_RETCODE_INVALID_PARAM
17663  * NI_RETCODE_ERROR_MEM_ALOC
17664  *****************************************************************************/
17666  int extra_len)
17667 {
17668  void *metadata_buffer = NULL;
17669  int retval = NI_RETCODE_SUCCESS;
17670 
17671  if ((!p_frame) || (extra_len <= 0))
17672  {
17674  "ERROR: %s passed parameters are null or not supported, "
17675  "p_frame %p, extra_len %d",
17676  __func__, p_frame, extra_len);
17677  return NI_RETCODE_INVALID_PARAM;
17678  }
17679 
17680  int buffer_size = extra_len;
17681  if (buffer_size % NI_MEM_PAGE_ALIGNMENT)
17682  {
17683  buffer_size = ((buffer_size / NI_MEM_PAGE_ALIGNMENT) + 1) *
17685  }
17686 
17687  // Check if previous metadata buffer needs to be freed
17688  if ((p_frame->metadata_buffer_size != buffer_size) &&
17689  (p_frame->metadata_buffer_size > 0))
17690  {
17692  "%s: free current p_frame metadata buffer, "
17693  "p_frame->buffer_size=%u\n",
17694  __func__, p_frame->metadata_buffer_size);
17695  p_frame->metadata_buffer_size = 0;
17697  }
17698 
17699  // Check if new metadata buffer needs to be allocated
17700  if (p_frame->metadata_buffer_size != buffer_size)
17701  {
17702  if (ni_posix_memalign(&metadata_buffer, sysconf(_SC_PAGESIZE),
17703  buffer_size))
17704  {
17706  "ERROR %d: %s() Cannot allocate metadata buffer.\n",
17707  NI_ERRNO, __func__);
17708  retval = NI_RETCODE_ERROR_MEM_ALOC;
17709  LRETURN;
17710  }
17711 
17712  // init once after allocation
17713  memset(metadata_buffer, 0, buffer_size);
17714  p_frame->metadata_buffer_size = buffer_size;
17715  p_frame->p_metadata_buffer = metadata_buffer;
17716 
17717  ni_log(NI_LOG_DEBUG, "%s: allocated new metadata buffer\n", __func__);
17718  } else
17719  {
17720  ni_log(NI_LOG_DEBUG, "%s: reuse metadata buffer\n", __func__);
17721  }
17722 
17724  "%s: success: p_frame->p_metadata_buffer %p "
17725  "p_frame->metadata_buffer_size=%u\n",
17726  __func__, p_frame->p_metadata_buffer, p_frame->metadata_buffer_size);
17727 
17728 END:
17729 
17730  if (NI_RETCODE_SUCCESS != retval)
17731  {
17732  ni_aligned_free(metadata_buffer);
17733  }
17734 
17735  return retval;
17736 }
17737 
17738 /*!*****************************************************************************
17739  * \brief Allocate memory for the non-4k-aligned part at the start of YUV data for
17740  * encoder input data.
17741  *
17742  * \param[in] p_frame Pointer to a caller allocated ni_frame_t struct
17743  *
17744  * \param[in] start_len Length of non-4k-aligned part at the start of YUV data
17745  *
17746  * \return On success
17747  * NI_RETCODE_SUCCESS
17748  * On failure
17749  * NI_RETCODE_INVALID_PARAM
17750  * NI_RETCODE_ERROR_MEM_ALOC
17751  *****************************************************************************/
17753 {
17754  void *start_buffer = NULL;
17755  int retval = NI_RETCODE_SUCCESS;
17756 
17757  if (!p_frame)
17758  {
17760  "ERROR: %s passed parameters are null or not supported, "
17761  "p_frame %p\n",
17762  __func__, p_frame);
17763  return NI_RETCODE_INVALID_PARAM;
17764  }
17765 
17766  // Check if new start buffer needs to be allocated
17767  if (!p_frame->start_buffer_size)
17768  {
17769  if (ni_posix_memalign(&start_buffer, sysconf(_SC_PAGESIZE),
17771  {
17773  "ERROR %d: %s() Cannot allocate start buffer.\n",
17774  NI_ERRNO, __func__);
17775  retval = NI_RETCODE_ERROR_MEM_ALOC;
17776  LRETURN;
17777  }
17778 
17779  // init once after allocation
17782  p_frame->p_start_buffer = start_buffer;
17783 
17784  ni_log(NI_LOG_DEBUG, "%s: allocated new start buffer\n", __func__);
17785  } else
17786  {
17787  ni_log(NI_LOG_DEBUG, "%s: reuse start buffer\n", __func__);
17788  }
17789 
17791  "%s: success: p_frame->p_start_buffer %p "
17792  "p_frame->start_buffer_size=%u\n",
17793  __func__, p_frame->p_start_buffer, p_frame->start_buffer_size);
17794 
17795 END:
17796 
17797  if (NI_RETCODE_SUCCESS != retval)
17798  {
17799  ni_aligned_free(start_buffer);
17800  }
17801 
17802  return retval;
17803 }
17804 
17806  ni_network_perf_metrics_t *p_metrics)
17807 {
17809  void *p_buffer = NULL;
17810  uint32_t dataLen;
17811  uint32_t ui32LBA = 0;
17812 
17813  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
17814  if (!p_ctx || !p_metrics)
17815  {
17816  ni_log2(p_ctx, NI_LOG_ERROR,
17817  "ERROR: %s() passed parameters are null!, return\n", __func__);
17818  retval = NI_RETCODE_INVALID_PARAM;
17819  LRETURN;
17820  }
17821 
17823  "6N") >= 0)
17824  {
17825  dataLen =
17827  ~(NI_MEM_PAGE_ALIGNMENT - 1);
17828  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), dataLen))
17829  {
17830  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
17831  NI_ERRNO, __func__);
17832  retval = NI_RETCODE_ERROR_MEM_ALOC;
17833  LRETURN;
17834  }
17835  memset(p_buffer, 0, sizeof(ni_network_perf_metrics_t));
17836 
17837  ui32LBA =
17839  retval =
17841  p_buffer, dataLen, ui32LBA);
17842  if ((int32_t)retval != NI_RETCODE_SUCCESS)
17843  {
17844  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
17846  LRETURN;
17847  }
17848  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_read, p_ctx->device_type,
17849  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
17850 
17851  *p_metrics = *((ni_network_perf_metrics_t *)p_buffer);
17852  } else
17853  {
17854  p_metrics->total_cycles = (uint32_t)(-1);
17855  p_metrics->total_idle_cycles = (uint32_t)(-1);
17856  }
17857 END:
17858 
17859  ni_aligned_free(p_buffer);
17860  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
17861 
17862  return retval;
17863 }
17864 
17865 /*!*****************************************************************************
17866  * \brief Send namespace num / Opmode and SRIOv index/value to the device with
17867  * specified logic block address.
17868  *
17869  * \param[in] device_handle Device handle obtained by calling ni_device_open
17870  * \param[in] Key Represents either namespace num or opmode
17871  * \param[in] Value Represents either SRIOv index or opmode value
17872  *
17873  * \return On success
17874  * NI_RETCODE_SUCCESS
17875  * On failure
17876  * NI_RETCODE_ERROR_MEM_ALOC
17877  * NI_RETCODE_ERROR_NVME_CMD_FAILED
17878  ******************************************************************************/
17879 ni_retcode_t ni_device_config_ns_qos(ni_device_handle_t device_handle,
17880  uint32_t key,
17881  uint32_t value)
17882 {
17883  char buf[NI_DATA_BUFFER_LEN] = {'\0'};
17884  uint32_t *u32_buf = (uint32_t *)buf;
17885  u32_buf[0] = key;
17886  u32_buf[1] = value;
17887  // event handle could be ignored
17888  return ni_nvme_send_write_cmd(device_handle, NI_INVALID_EVENT_HANDLE,
17889  (void *)buf, NI_DATA_BUFFER_LEN,
17891 }
17892 
17894 {
17895  char *CoreName;
17896  switch (eCoreType)
17897  {
17898  case ALL_CORE:
17899  CoreName = (char *)"all";
17900  break;
17901  case NVME_CORE:
17902  CoreName = (char *)"np";
17903  break;
17904  case EP_CORE:
17905  CoreName = (char *)"ep";
17906  break;
17907  case DP_CORE:
17908  CoreName = (char *)"dp";
17909  break;
17910  case TP_CORE:
17911  CoreName = (char *)"tp";
17912  break;
17913  case FP_CORE:
17914  CoreName = (char *)"fp";
17915  break;
17916  default:
17917  CoreName = (char *)"Not Found";
17918  break;
17919  }
17920  return CoreName;
17921 }
17922 
17923 uint32_t ni_get_log_lba(ni_core_type_t eCoreType)
17924 {
17925  uint32_t lba;
17926  switch (eCoreType)
17927  {
17928  case NVME_CORE:
17929  lba = NVME_LOG_OFFSET_IN_4K;
17930  break;
17931  case EP_CORE:
17932  lba = EP_LOG_OFFSET_IN_4K;
17933  break;
17934  case DP_CORE:
17935  lba = DP_LOG_OFFSET_IN_4K;
17936  break;
17937  case TP_CORE:
17938  lba = TP_LOG_OFFSET_IN_4K;
17939  break;
17940  case FP_CORE:
17941  lba = FP_LOG_OFFSET_IN_4K;
17942  break;
17943  default:
17944  ni_log(NI_LOG_ERROR, "%s:() Invalid core ID:%u\n", __func__, eCoreType);
17945  lba = 0;
17946  break;
17947  }
17948 
17949  return lba;
17950 }
17951 
17952 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)
17953 {
17954  int32_t rc;
17955  uint32_t lba = 0;
17956  uint32_t data_len = CPU_LOG_BUFFER_SIZE;
17957  char *core_name = NULL;
17958  FILE *p_file = NULL;
17960 
17961  if (!p_ctx || !p_data)
17962  {
17963  ni_log2(p_ctx, NI_LOG_ERROR, "%s:():%d: ERROR invalid pointer p_ctx %p p_data %p\n",
17964  __func__, __LINE__, p_ctx, p_data);
17965  return NI_RETCODE_INVALID_PARAM;
17966  }
17967 
17968  memset(p_data, 0, CPU_LOG_BUFFER_SIZE);
17969  *(uint8_t *)p_data = 0x55;
17970 
17971  lba = ni_get_log_lba((ni_core_type_t)core_id);
17972  if (lba == 0)
17973  {
17974  ni_log2(p_ctx, NI_LOG_ERROR, "%s:():%d: ERROR core_id %u\n",
17975  __func__, __LINE__, core_id);
17976  return NI_RETCODE_INVALID_PARAM;
17977  }
17978 
17979  core_name = ni_get_core_name((ni_core_type_t)core_id);
17980 
17981  rc = ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_data, data_len, lba);
17982 
17983  if (rc != NI_RETCODE_SUCCESS)
17984  {
17985  ni_log2(p_ctx, NI_LOG_ERROR, "%s:():%d: ERROR %d: nvme read %s core failed\n",
17986  __func__, __LINE__, rc, core_name);
17987  }
17988  else if (gen_log_file)
17989  {
17990  //generate log file e.g. raw_dp_slot_0_0000.bin
17991  char filename[32] = "raw_";
17992  strcat(filename, core_name);
17993  strcat(filename, "_slot_");
17994 #ifdef __linux__
17995  bool pcie_id_name = false;
17996  char devFilePath[1024] = {0};
17997  char devFDPath[1024] = {0};
17998  char pcie[64] = {0};
17999  char domain[5] = {0}, slot[3] = {0}, dev[3] = {0}, func[2] = {0};
18000  //p_ctx->blk_dev_name might be empty so look up the file name
18001  snprintf(devFDPath, sizeof(devFDPath), "/proc/self/fd/%d", p_ctx->blk_io_handle);
18002  ssize_t len = readlink(devFDPath, devFilePath, sizeof(devFilePath)-1);
18003  if (len != -1) {
18004  devFilePath[len] = '\0';
18005  }
18006  if (strstr(devFilePath, "/dev/nvme") != NULL)
18007  {
18008  get_dev_pcie_addr(devFilePath, pcie, domain, slot, dev, func);
18009  if (strlen(pcie) > 0 && strlen(slot) > 0 && strlen(domain) > 0)
18010  {
18011  strcat(filename, slot);
18012  strcat(filename, "_");
18013  strcat(filename, domain);
18014  pcie_id_name = true;
18015  }
18016  }
18017  if (!pcie_id_name)
18018  {
18019  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",
18020  __func__, __LINE__, p_ctx->blk_io_handle);
18021  char num[4] = {0};
18022  snprintf(num, 4, "%d", p_ctx->hw_id);
18023  strcat(filename, num);
18024  strcat(filename, "_0000");
18025  }
18026  strcat(filename, ".bin");
18027  ni_log2(p_ctx, NI_LOG_INFO, "For dev %d %s core %s creating file %s\n",
18028  p_ctx->blk_io_handle, devFilePath, core_name, filename);
18029 #else
18030  char num[4] = {0};
18031  snprintf(num, 4, "%02x", p_ctx->hw_id);
18032  strcat(filename, num);
18033  strcat(filename, "_0000");
18034  strcat(filename, ".bin");
18035  ni_log2(p_ctx, NI_LOG_INFO, "For dev %d core %s creating file %s\n",
18036  p_ctx->blk_io_handle, core_name, filename);
18037 #endif
18038  p_file = fopen(filename, "wb");
18039  if (p_file)
18040  {
18041  /* Write out the stream header */
18042  if (fwrite((uint8_t *)p_data ,
18043  data_len, 1, p_file) != 1)
18044  {
18045  ni_log2(p_ctx, NI_LOG_ERROR, "%s:():%d: Error: writing data %u bytes error!\n",
18046  __func__, __LINE__, data_len);
18047  ni_log2(p_ctx, NI_LOG_ERROR, "Error: ferror rc = %d\n", ferror(p_file));
18048  retval = NI_RETCODE_FAILURE;
18049  }
18050  if (fflush(p_file))
18051  {
18052  ni_log2(p_ctx, NI_LOG_ERROR, "Error: writing data frame flush failed! errno %d\n",
18053  ferror(p_file));
18054  retval = NI_RETCODE_FAILURE;
18055  }
18056  fclose(p_file);
18057  }
18058  else
18059  {
18060  ni_log2(p_ctx, NI_LOG_ERROR, "%s:():%d: Error: cannot open %s\n",
18061  __func__, __LINE__, filename);
18062  retval = NI_RETCODE_FAILURE;
18063  }
18064  }
18065 
18066  return retval;
18067 }
18068 
18069 ni_retcode_t ni_dump_log_all_cores(ni_session_context_t *p_ctx, void* p_data, bool gen_log_file)
18070 {
18071  int i = 1;
18072 
18073  if (!p_ctx || !p_data)
18074  {
18075  ni_log2(p_ctx, NI_LOG_ERROR, "%s:():%d: ERROR invalid pointer p_ctx %p p_data %p\n",
18076  __func__, __LINE__, p_ctx, p_data);
18077  return NI_RETCODE_INVALID_PARAM;
18078  }
18079 
18080  for (i = NVME_CORE; i< NUM_OF_CORES; i++)
18081  {
18082  ni_dump_log_single_core(p_ctx, p_data, i, gen_log_file);
18083  p_data = (void*)((uint8_t *)p_data + CPU_LOG_BUFFER_SIZE);
18084  }
18085 
18086  return NI_RETCODE_SUCCESS;
18087 }
18088 
18090  niFrameSurface1_t *source,
18091  uint64_t ui64DestAddr,
18092  uint32_t ui32FrameSize)
18093 {
18094  void *p_buffer = NULL;
18095  ni_retcode_t retval;
18096  uint32_t ui32LBA;
18097  uint8_t *p_data;
18098  uint16_t ui16Direction = NI_P2P_SEND;
18099 
18100  /* allocate memory aligned buffer */
18101  retval = ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), NI_DATA_BUFFER_LEN);
18102  if (retval != 0)
18103  {
18104  retval = NI_RETCODE_ERROR_MEM_ALOC;
18105  LRETURN;
18106  }
18107 
18108  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
18109 
18110  p_data = (uint8_t *) p_buffer;
18111 
18112  /* Add payload */
18113  memcpy(p_data, &ui64DestAddr, sizeof(uint64_t));
18114  memcpy(p_data + 8, &ui32FrameSize, sizeof(uint32_t));
18115  memcpy(p_data + 12, &source->ui16FrameIdx, sizeof(uint16_t));
18116  memcpy(p_data + 14, MAGIC_P2P_VALUE, 4);
18117  memcpy(p_data + 18, &ui16Direction, sizeof(uint16_t));
18118 
18120 
18121  retval = ni_nvme_send_write_cmd((ni_device_handle_t)(int64_t)source->device_handle,
18122  NI_INVALID_DEVICE_HANDLE, p_buffer,
18123  NI_DATA_BUFFER_LEN, ui32LBA);
18124 
18125  if (retval < 0)
18126  {
18127  ni_log2(p_ctx, NI_LOG_ERROR, "%s: NVME command failed %d\n", __func__, retval);
18128  ni_aligned_free(p_buffer);
18130  LRETURN;
18131  }
18132 
18133 END:
18134  if (p_buffer != NULL)
18135  ni_aligned_free(p_buffer);
18136  return retval;
18137 }
18138 
18140  ni_session_context_t *p_ctx,
18141  const ni_p2p_sgl_t *dmaAddrs,
18142  ni_frame_t *pDstFrame)
18143 {
18144  void *p_buffer = NULL;
18145  ni_retcode_t retval;
18146  uint32_t ui32LBA = 0;
18147  niFrameSurface1_t *pSurface;
18148  uint8_t *p_data;
18149  uint16_t ui16Direction = NI_P2P_RECV;
18150  uint32_t ui32Dummy = 0;
18151  int i;
18152 
18153  if (dmaAddrs->ui32NumEntries > NI_MAX_P2P_SGL_ENTRIES)
18154  {
18155  ni_log2(p_ctx, NI_LOG_ERROR, "Too many SGL entries\n");
18156  retval = NI_RETCODE_INVALID_PARAM;
18157  LRETURN;
18158  }
18159 
18160  /* allocate memory aligned buffer */
18161  retval = ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), NI_DATA_BUFFER_LEN);
18162  if (retval != 0)
18163  {
18164  retval = NI_RETCODE_ERROR_MEM_ALOC;
18165  LRETURN;
18166  }
18167 
18168  pSurface = (niFrameSurface1_t *) pDstFrame->p_data[3];
18169 
18170  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
18171 
18172  p_data = (uint8_t *) p_buffer;
18173 
18174  /* Add payload */
18175  memcpy(p_data, &dmaAddrs->ui64DMAAddr[0], 8); // dummy data, not used for p2p read
18176  memcpy(p_data + 8, &ui32Dummy, 4); // dummy data, not used for p2p read
18177  memcpy(p_data + 12, &pSurface->ui16FrameIdx, 2); // dummy data, not used for p2p read
18178  memcpy(p_data + 14, MAGIC_P2P_VALUE, 4);
18179  memcpy(p_data + 18, &ui16Direction, 2);
18180  memcpy(p_data + 20, &dmaAddrs->ui32NumEntries, 4);
18181 
18182  for (i = 0; i < dmaAddrs->ui32NumEntries; i++)
18183  {
18184  memcpy(&p_data[24] + (i*8), &dmaAddrs->ui64DMAAddr[i], 8);
18185  }
18186 
18187  for (i = 0; i < dmaAddrs->ui32NumEntries; i++)
18188  {
18189  memcpy(&p_data[24] + (dmaAddrs->ui32NumEntries * 8) + (i * 4), &dmaAddrs->ui32DMALen[i], 4);
18190  }
18191 
18193 
18194  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
18195  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
18196 
18197  if (retval < 0)
18198  {
18199  ni_log2(p_ctx, NI_LOG_ERROR, "%s: NVME command failed\n", __func__);
18200  ni_aligned_free(p_buffer);
18202  LRETURN;
18203  }
18204 
18205 END:
18206  if (p_buffer != NULL)
18207  ni_aligned_free(p_buffer);
18208  return retval;
18209 }
18210 
18211 int lower_pixel_rate(const ni_load_query_t *pQuery, uint32_t ui32CurrentLowest)
18212 {
18213  return (pQuery->total_pixel_load < ui32CurrentLowest) ? 1 : 0;
18214 }
_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:707
_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:2731
_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:467
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:2384
_ni_encoder_cfg_params::frame_rate
int frame_rate
Definition: ni_device_api.h:2239
ni_decoder_output_picture_size::ui16Width
uint16_t ui16Width
Definition: ni_device_api_priv.h:701
_ni_frame_config::picture_width
uint16_t picture_width
Definition: ni_device_api.h:2810
_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:515
_ni_xcoder_params::fps_denominator
uint32_t fps_denominator
Definition: ni_device_api.h:2719
_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:706
_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:18089
_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:2784
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:267
CLEAR_INSTANCE_BUF_W
#define CLEAR_INSTANCE_BUF_W(frame_id)
Definition: ni_nvme.h:878
_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:2857
_ni_load_query::fw_p2p_mem_usage
uint32_t fw_p2p_mem_usage
Definition: ni_device_api.h:1211
_ni_sei_header::size
uint16_t size
Definition: ni_device_api.h:345
CONFIG_SESSION_KeepAliveTimeout_W
#define CONFIG_SESSION_KeepAliveTimeout_W(sid)
Definition: ni_nvme.h:882
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:6980
NI_CODEC_FORMAT_JPEG
@ NI_CODEC_FORMAT_JPEG
Definition: ni_device_api.h:914
_ni_encoder_cfg_params::ver_offset
int ver_offset
Definition: ni_device_api.h:2398
ni_decoder_output_picture_size::ui16Height
uint16_t ui16Height
Definition: ni_device_api_priv.h:700
NI_QUADRA_MEMORY_CONFIG_DR
#define NI_QUADRA_MEMORY_CONFIG_DR
Definition: ni_device_api_priv.h:801
_ni_dec_mastering_display_colour_volume_bytes::display_primaries
uint16_t display_primaries[3][2]
Definition: ni_device_api.h:1063
_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:807
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:17517
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:4032
_ni_device_capability::fw_commit_time
uint8_t fw_commit_time[26]
Definition: ni_device_api.h:1166
_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:384
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:2502
_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:347
_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:2707
_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:2853
_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:399
_ni_p2p_sgl_t
Definition: ni_defs.h:403
_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:2667
_ni_network_data
Definition: ni_device_api.h:1363
NI_CC_SEI_BYTE0
#define NI_CC_SEI_BYTE0
Definition: ni_device_api.h:443
_ni_session_context::decoder_low_delay
int decoder_low_delay
Definition: ni_device_api.h:1594
_ni_decoder_input_params_t::error_ratio_threshold
int error_ratio_threshold
Definition: ni_device_api.h:2538
_ni_session_context::p_all_zero_buf
void * p_all_zero_buf
Definition: ni_device_api.h:1571
NI_HDR10P_SEI_BYTE1
#define NI_HDR10P_SEI_BYTE1
Definition: ni_device_api.h:453
_ni_session_context::required_buf_size
uint32_t required_buf_size
Definition: ni_device_api.h:1720
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:7178
_ni_session_context::auto_dl_handle
ni_device_handle_t auto_dl_handle
Definition: ni_device_api.h:1469
_ni_thread_arg_struct_t::p_mutex
ni_pthread_mutex_t * p_mutex
Definition: ni_device_api.h:1267
_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:2419
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:731
NI_POOL_TYPE_P2P
@ NI_POOL_TYPE_P2P
Definition: ni_device_api.h:493
NI_H265_USERDATA_FLAG_MASTERING_COLOR_VOL
@ NI_H265_USERDATA_FLAG_MASTERING_COLOR_VOL
Definition: ni_device_api.h:369
NI_FW_ENC_BITSTREAM_META_DATA_SIZE
#define NI_FW_ENC_BITSTREAM_META_DATA_SIZE
Definition: ni_device_api_priv.h:816
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:1140
_ni_encoder_cfg_params::enable_dynamic_32x32_merge
int enable_dynamic_32x32_merge
Definition: ni_device_api.h:2352
_ni_encoder_cfg_params::hor_offset
int hor_offset
Definition: ni_device_api.h:2397
_ni_encoder_cfg_params::inLoopDSRatio
int inLoopDSRatio
Definition: ni_device_api.h:2335
_ni_session_context::max_retry_fail_count
int max_retry_fail_count[2]
Definition: ni_device_api.h:1701
_ni_metadata_enc_bstream_rev61::recycle_index
uint32_t recycle_index
Definition: ni_device_api_priv.h:270
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:6778
_ni_content_light_level_info_bytes
payload format of HDR SEI content light level info
Definition: ni_device_api.h:1074
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:14055
_ni_encoder_config_t::ui8tuneBframeVisual
uint8_t ui8tuneBframeVisual
Definition: ni_device_api_priv.h:641
_ni_scaler_config
Definition: ni_device_api_priv.h:761
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:3561
QUERY_INSTANCE_UPLOAD_ID_R
#define QUERY_INSTANCE_UPLOAD_ID_R(sid, instance)
Definition: ni_nvme.h:740
_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:237
_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:2650
_ni_frame::sei_hdr_content_light_level_info_offset
unsigned int sei_hdr_content_light_level_info_offset
Definition: ni_device_api.h:2637
QUERY_SESSION_STATS_R
#define QUERY_SESSION_STATS_R(sid, instance)
Definition: ni_nvme.h:708
GC620_ABGR8888
#define GC620_ABGR8888
Definition: ni_device_api.h:202
_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:721
_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:2525
_ni_encoder_cfg_params::pbRatio
float pbRatio
Definition: ni_device_api.h:2405
_ni_session_statistic_t::ui16ErrorCount
uint16_t ui16ErrorCount
Definition: ni_defs.h:387
_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:5715
_ni_decoder_input_params_t::enable_out1
int enable_out1
Definition: ni_device_api.h:2500
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:6871
_ni_network_buffer::ui16Option
uint16_t ui16Option
Definition: ni_device_api_priv.h:755
_ni_network_layer_info::in_param
ni_network_layer_params_t * in_param
Definition: ni_device_api.h:1354
_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:2828
_ni_instance_mgr_stream_complete
Definition: ni_device_api_priv.h:176
_ni_encoder_cfg_params::vbvBufferReencode
int vbvBufferReencode
Definition: ni_device_api.h:2442
_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:728
_ni_encoder_cfg_params::enable_filler
int enable_filler
Definition: ni_device_api.h:2376
NI_RETCODE_PARAM_ERROR_RCENABLE
@ NI_RETCODE_PARAM_ERROR_RCENABLE
Definition: ni_defs.h:476
_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:1418
_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:1699
QUERY_INSTANCE_HW_OUT_SIZE_R
#define QUERY_INSTANCE_HW_OUT_SIZE_R(sid, instance)
Definition: ni_nvme.h:748
NI_CC_SEI_BYTE7
#define NI_CC_SEI_BYTE7
Definition: ni_device_api.h:450
_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:16033
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:17125
_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:446
NI_RETCODE_NVME_SC_RESOURCE_UNAVAILABLE
@ NI_RETCODE_NVME_SC_RESOURCE_UNAVAILABLE
Definition: ni_defs.h:536
_ni_encoder_cfg_params::gopLowdelay
int gopLowdelay
Definition: ni_device_api.h:2324
CONFIG_SESSION_DDR_PRIORITY_W
#define CONFIG_SESSION_DDR_PRIORITY_W(sid)
Definition: ni_nvme.h:899
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:702
ni_decode_cropping_rectangle::ui16W
uint16_t ui16W
Definition: ni_device_api_priv.h:695
_ni_frame_config::rectangle_x
int16_t rectangle_x
Definition: ni_device_api.h:2816
_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:1956
_ni_load_query::current_load
uint32_t current_load
Definition: ni_device_api.h:1198
_ni_packet::ssim_u
double ssim_u
Definition: ni_device_api.h:2860
_ni_session_context::dev_xcoder_name
char dev_xcoder_name[MAX_CHAR_IN_DEVICE_NAME]
Definition: ni_device_api.h:1488
_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:2836
_ni_load_query::total_contexts
uint32_t total_contexts
Definition: ni_device_api.h:1201
_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:1413
NI_CC_SEI_BYTE2
#define NI_CC_SEI_BYTE2
Definition: ni_device_api.h:445
_ni_encoder_cfg_params::statistic_output_level
int statistic_output_level
Definition: ni_device_api.h:2416
_ni_dec_mastering_display_colour_volume_bytes::white_point_y
uint16_t white_point_y
Definition: ni_device_api.h:1065
_ni_session_statistic_t
Definition: ni_defs.h:374
_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:17879
_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:1420
_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:1061
_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:2523
_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:2696
_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:2528
_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:2856
_ni_encoder_cfg_params::temporal_layers_enable
int temporal_layers_enable
Definition: ni_device_api.h:2392
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:14003
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:1622
_ni_packet::flags
int flags
Definition: ni_device_api.h:2851
NI_DEVICE_TYPE_DECODER
@ NI_DEVICE_TYPE_DECODER
Definition: ni_defs.h:346
_niFrameSurface1::ui16height
uint16_t ui16height
Definition: ni_device_api.h:2798
NI_GET_MAX_HWDESC_P2P_BUF_ID
#define NI_GET_MAX_HWDESC_P2P_BUF_ID(x)
Definition: ni_defs.h:271
_ni_session_context::enc_pts_w_idx
int64_t enc_pts_w_idx
Definition: ni_device_api.h:1441
_ni_session_context::key_frame_type
int key_frame_type
Definition: ni_device_api.h:1525
_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:681
_ni_session_context::input_frame_fifo
ni_input_frame input_frame_fifo[120]
encoder:calculate PSNR start
Definition: ni_device_api.h:1709
_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:1693
_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:1480
_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:15554
_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:2723
_ni_packet::start_of_stream
uint32_t start_of_stream
Definition: ni_device_api.h:2832
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:2655
NI_MAX_PACKET_SZ
#define NI_MAX_PACKET_SZ
Definition: ni_defs.h:227
_ni_t408_config_t
Definition: ni_device_api_priv.h:368
_ni_input_frame::usable
int8_t usable
Definition: ni_device_api.h:1405
_ni_decoder_input_params_t::enable_ppu_scale_limit
int enable_ppu_scale_limit
Definition: ni_device_api.h:2526
_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:582
_ni_encoder_cfg_params::ltrRefInterval
int ltrRefInterval
Definition: ni_device_api.h:2285
_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:11509
_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:1159
_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:15766
_ni_device_capability::fw_branch_name
uint8_t fw_branch_name[256]
Definition: ni_device_api.h:1165
NI_HDR10P_SEI_HDR_H264_LEN
#define NI_HDR10P_SEI_HDR_H264_LEN
Definition: ni_device_api.h:463
_ni_session_context::netint_fd
int netint_fd
Definition: ni_device_api.h:1618
_ni_hw_capability::hw_id
uint8_t hw_id
Definition: ni_device_api.h:1136
_ni_frame_config::frame_index
uint16_t frame_index
Definition: ni_device_api.h:2819
_ni_session_context::keyframe_factor
int keyframe_factor
Definition: ni_device_api.h:1530
_ni_scaler_config::scaler_param_c
uint16_t scaler_param_c
Definition: ni_device_api_priv.h:766
_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:939
NI_PIX_FMT_YUV420P
@ NI_PIX_FMT_YUV420P
Definition: ni_device_api.h:262
_ni_uploader_config_t
Definition: ni_device_api_priv.h:657
_ni_encoder_config_t::ui16aspectRatioWidth
uint16_t ui16aspectRatioWidth
Definition: ni_device_api_priv.h:558
_ni_resolution
Definition: ni_device_api_priv.h:670
_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:341
_niFrameSurface1::ui32nodeAddress
uint32_t ui32nodeAddress
Definition: ni_device_api.h:2799
_ni_session_context::isP2P
int32_t isP2P
Definition: ni_device_api.h:1617
_ni_packet::end_of_stream
uint32_t end_of_stream
Definition: ni_device_api.h:2831
_ni_encoder_cfg_params::enable_hvs_qp_scale
int enable_hvs_qp_scale
Definition: ni_device_api.h:2381
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:1262
_ni_session_context::p_leftover
uint8_t * p_leftover
Definition: ni_device_api.h:1506
_ni_network_buffer::ui16Height
uint16_t ui16Height
Definition: ni_device_api_priv.h:754
_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:718
_ni_xcoder_params::enable2PassGop
int enable2PassGop
Definition: ni_device_api.h:2782
_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:1162
_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:2827
_ni_encoder_cfg_params::crf_max_iframe_enable
int crf_max_iframe_enable
Definition: ni_device_api.h:2423
WRITE_INSTANCE_W
#define WRITE_INSTANCE_W(sid, instance)
Definition: ni_nvme.h:675
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:8455
NI_RETCODE_SUCCESS
@ NI_RETCODE_SUCCESS
Definition: ni_defs.h:427
_ni_session_context::active_video_width
uint32_t active_video_width
Definition: ni_device_api.h:1549
_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:1328
_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:1331
_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:663
_ni_gop_params::poc_offset
int poc_offset
Definition: ni_device_api.h:1961
_ni_gop_params::qp_offset
int qp_offset
Definition: ni_device_api.h:1962
_ni_framerate::framerate_denom
int32_t framerate_denom
Definition: ni_device_api.h:701
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:480
NI_DEVICE_TYPE_UPLOAD
@ NI_DEVICE_TYPE_UPLOAD
Definition: ni_defs.h:353
_ni_scaler_config::numInputs
uint8_t numInputs
Definition: ni_device_api_priv.h:764
QUERY_INSTANCE_AI_INFO_R
#define QUERY_INSTANCE_AI_INFO_R(sid, instance)
Definition: ni_nvme.h:715
NI_CC_SEI_BYTE1
#define NI_CC_SEI_BYTE1
Definition: ni_device_api.h:444
_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:1443
ni_ai_session_open
ni_retcode_t ni_ai_session_open(ni_session_context_t *p_ctx)
Definition: ni_device_api_priv.c:16741
_ni_session_context::status
int status
Definition: ni_device_api.h:1524
_ni_decoder_config_t::ui8EcPolicy
uint8_t ui8EcPolicy
Definition: ni_device_api_priv.h:734
_ni_session_context::domain
unsigned short domain
Definition: ni_device_api.h:1619
_ni_session_context::is_dec_pkt_512_aligned
int is_dec_pkt_512_aligned
Definition: ni_device_api.h:1459
_ni_session_context::pool_type
ni_frame_pool_type_t pool_type
Definition: ni_device_api.h:1697
_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:2387
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:1528
_ni_device_capability::xcoder_cnt
uint8_t xcoder_cnt[NI_DEVICE_TYPE_XCODER_MAX]
Definition: ni_device_api.h:1158
_ni_session_context::ori_chroma_linesize
int ori_chroma_linesize
Definition: ni_device_api.h:1681
_ni_encoder_cfg_params::enable_compensate_qp
int enable_compensate_qp
Definition: ni_device_api.h:2437
_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:1358
_ni_frame::total_start_len
uint32_t total_start_len
Definition: ni_device_api.h:2705
_ni_session_context::last_bitrate
int32_t last_bitrate
Definition: ni_device_api.h:1674
_ni_input_frame::video_width
uint32_t video_width
Definition: ni_device_api.h:1400
_ni_session_context::pts_correction_num_faulty_dts
int pts_correction_num_faulty_dts
Definition: ni_device_api.h:1442
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:3169
GC620_ARGB8888
#define GC620_ARGB8888
Definition: ni_device_api.h:203
_ni_session_context::actual_video_width
uint32_t actual_video_width
Definition: ni_device_api.h:1553
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:7112
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:2328
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:1437
_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:270
_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:1375
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:6376
NI_EC_POLICY_LIMITED_ERROR
#define NI_EC_POLICY_LIMITED_ERROR
Definition: ni_device_api.h:322
_ni_xcoder_params::rootBufId
int rootBufId
Definition: ni_device_api.h:2775
_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:773
_ni_frame::p_start_buffer
uint8_t * p_start_buffer
Definition: ni_device_api.h:2702
_ni_session_context::event_handle
ni_event_handle_t event_handle
Definition: ni_device_api.h:1574
_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:735
QUERY_INSTANCE_ACQUIRE_BUF
#define QUERY_INSTANCE_ACQUIRE_BUF(sid, instance)
Definition: ni_nvme.h:742
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:8209
NI_XCODER_REVISION
#define NI_XCODER_REVISION
Definition: ni_defs.h:95
QUERY_DETAIL_GET_STATUS_R
#define QUERY_DETAIL_GET_STATUS_R(instance)
Definition: ni_nvme.h:786
_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:1156
_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:5325
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:2613
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:12446
_ni_encoder_cfg_params::enable_acq_limit
int enable_acq_limit
Definition: ni_device_api.h:2429
_ni_encoder_cfg_params::noMbtree
int noMbtree
Definition: ni_device_api.h:2401
_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:765
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:5837
_ni_session_context::last_dts
int64_t last_dts
Definition: ni_device_api.h:1438
_ni_session_context::blk_io_handle
ni_device_handle_t blk_io_handle
Definition: ni_device_api.h:1465
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:16468
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:333
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:2704
OPEN_SESSION_CODEC
#define OPEN_SESSION_CODEC(instance, codec, param)
Definition: ni_nvme.h:695
NI_HDR10P_SEI_HDR_HEVC_LEN
#define NI_HDR10P_SEI_HDR_HEVC_LEN
Definition: ni_device_api.h:462
NI_RETCODE_PARAM_ERROR_AREA_TOO_BIG
@ NI_RETCODE_PARAM_ERROR_AREA_TOO_BIG
Definition: ni_defs.h:502
NI_RETCODE_PARAM_ERROR_CUSIZE_MODE_32X32_EN
@ NI_RETCODE_PARAM_ERROR_CUSIZE_MODE_32X32_EN
Definition: ni_defs.h:488
_ni_encoder_cfg_params::roi_enable
int roi_enable
Definition: ni_device_api.h:2257
_ni_session_context::frame_pkt_offset
uint64_t frame_pkt_offset
Definition: ni_device_api.h:1647
NI_MAX_DEVICES_PER_HW_INSTANCE
#define NI_MAX_DEVICES_PER_HW_INSTANCE
Definition: ni_defs.h:239
_ni_encoder_cfg_params::intra_period
int intra_period
Definition: ni_device_api.h:2260
_ni_session_context::pts_correction_last_pts
int64_t pts_correction_last_pts
Definition: ni_device_api.h:1445
_ni_decoder_config_t::ui32ErrRatioThreshold
uint32_t ui32ErrRatioThreshold
Definition: ni_device_api_priv.h:740
_ni_encoder_cfg_params::EnableRdoQuant
int EnableRdoQuant
Definition: ni_device_api.h:2302
_ni_frame_config::rectangle_height
uint16_t rectangle_height
Definition: ni_device_api.h:2815
_ni_decoder_input_params_t::min_packets_delay
bool min_packets_delay
Definition: ni_device_api.h:2531
_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:2698
_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:2370
_ni_network_data::outset
ni_network_layer_offset_t * outset
Definition: ni_device_api.h:1371
_ni_network_perf_metrics
Definition: ni_device_api.h:1384
_ni_hw_capability::max_4k_fps
uint8_t max_4k_fps
Definition: ni_device_api.h:1138
_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:2346
_ni_thread_arg_struct_t::p_buffer
void * p_buffer
Definition: ni_device_api.h:1266
_ni_packet::frame_type
uint32_t frame_type
Definition: ni_device_api.h:2835
_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:846
_ni_hw_capability::min_video_height
uint16_t min_video_height
Definition: ni_device_api.h:1144
_ni_load_query::fw_share_mem_usage
uint32_t fw_share_mem_usage
Definition: ni_device_api.h:1210
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:6544
_ni_gop_params::pic_type
int pic_type
Definition: ni_device_api.h:1965
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:726
_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:2366
_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:2710
_ni_encoder_cfg_params::gop_preset_index
int gop_preset_index
Definition: ni_device_api.h:2251
_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:2644
_ni_encoder_cfg_params::max_qp
int max_qp
Definition: ni_device_api.h:2367
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:5478
_ni_frame_config
Definition: ni_device_api.h:2808
_ni_encoder_cfg_params::ipRatio
float ipRatio
Definition: ni_device_api.h:2404
_ni_session_statistic_t::ui32FramesCorrupted
uint32_t ui32FramesCorrupted
Definition: ni_defs.h:398
NI_SCALER_FLAG_IO
#define NI_SCALER_FLAG_IO
Definition: ni_device_api.h:291
ni_decoder_output_config_t::ui8Enabled
uint8_t ui8Enabled
Definition: ni_device_api_priv.h:705
_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:1542
_ni_encoder_cfg_params::blockRCSize
int blockRCSize
Definition: ni_device_api.h:2336
NI_SCALER_OPCODE_IPOVLY
@ NI_SCALER_OPCODE_IPOVLY
Definition: ni_defs.h:581
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:376
_ni_network_buffer::ui16FrameIdx
uint16_t ui16FrameIdx[4]
Definition: ni_device_api_priv.h:758
_ni_xcoder_params::roi_demo_mode
int roi_demo_mode
Definition: ni_device_api.h:2730
_ni_sei_header::status
uint8_t status
Definition: ni_device_api.h:344
_niFrameSurface1::src_cpu
int8_t src_cpu
Definition: ni_device_api.h:2804
_ni_network_perf_metrics::total_idle_cycles
uint32_t total_idle_cycles
Definition: ni_device_api.h:1387
_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:1493
_ni_session_context::low_delay_sync_mutex
ni_pthread_mutex_t low_delay_sync_mutex
Definition: ni_device_api.h:1665
_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:2261
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:684
_ni_frame::orignal_pts
long long orignal_pts
Definition: ni_device_api.h:2709
NI_RETCODE_PARAM_ERROR_CONF_WIN_TOP
@ NI_RETCODE_PARAM_ERROR_CONF_WIN_TOP
Definition: ni_defs.h:465
_ni_encoder_cfg_params::vbv_buffer_size
int vbv_buffer_size
Definition: ni_device_api.h:2377
_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:716
_ni_frame::sei_total_len
unsigned int sei_total_len
Definition: ni_device_api.h:2629
_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:1449
_ni_frame::p_buffer
uint8_t * p_buffer
Definition: ni_device_api.h:2666
NUM_OF_CORES
@ NUM_OF_CORES
Definition: ni_defs.h:371
_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:2402
_ni_session_statistic_t::ui32FramesCompleted
uint32_t ui32FramesCompleted
Definition: ni_defs.h:381
_ni_encoder_cfg_params::gdrDuration
int gdrDuration
Definition: ni_device_api.h:2284
_ni_packet::psnr_y
double psnr_y
Definition: ni_device_api.h:2855
NI_RETCODE_PARAM_ERROR_CONF_WIN_L
@ NI_RETCODE_PARAM_ERROR_CONF_WIN_L
Definition: ni_defs.h:469
_ni_xcoder_params::video_full_range_flag
int video_full_range_flag
Definition: ni_device_api.h:2761
NI_FIFO_SZ
#define NI_FIFO_SZ
Definition: ni_defs.h:291
_ni_network_buffer::ui8PoolSize
uint8_t ui8PoolSize
Definition: ni_device_api_priv.h:756
_ni_frame::dts
long long dts
Definition: ni_device_api.h:2606
_ni_decoder_input_params_t::force_low_delay
bool force_low_delay
Definition: ni_device_api.h:2518
_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:323
_ni_encoder_cfg_params::aspectRatioWidth
int aspectRatioWidth
Definition: ni_device_api.h:2240
NI_RETCODE_INVALID_PARAM
@ NI_RETCODE_INVALID_PARAM
Definition: ni_defs.h:429
_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:1760
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:16181
_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:2699
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:1717
_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:1167
_ni_frame::crop_left
uint32_t crop_left
Definition: ni_device_api.h:2615
_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:2859
_ni_encoder_cfg_params::maxFrameSizeRatio
int maxFrameSizeRatio
Definition: ni_device_api.h:2244
_ni_decoder_input_params_t::enable_advanced_ec
int enable_advanced_ec
Definition: ni_device_api.h:2524
_ni_t408_config_t::bgThrMeanDiff
uint32_t bgThrMeanDiff
Definition: ni_device_api_priv.h:449
_ni_encoder_cfg_params::HDR10AveLight
int HDR10AveLight
Definition: ni_device_api.h:2271
_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:2254
_ni_decoder_config_t::ui8HWFrame
uint8_t ui8HWFrame
Definition: ni_device_api_priv.h:723
_ni_encoder_cfg_params::cu_size_mode
int cu_size_mode
Definition: ni_device_api.h:2348
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:7042
_ni_decoder_config_t::ui16MaxSeiDataSize
uint16_t ui16MaxSeiDataSize
Definition: ni_device_api_priv.h:725
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:6200
_ni_encoder_cfg_params::hrdEnable
int hrdEnable
Definition: ni_device_api.h:2303
_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:2820
_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:275
_ni_session_context::bit_depth_factor
int bit_depth_factor
Definition: ni_device_api.h:1494
_ni_session_statistic_t::ui32FramesBuffered
uint32_t ui32FramesBuffered
Definition: ni_defs.h:380
_ni_uploader_config_t::ui32chromaLinesize
uint32_t ui32chromaLinesize
Definition: ni_device_api_priv.h:666
_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:2435
_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:858
ni_decoder_output_config_t::ui8ScaleEnabled
uint8_t ui8ScaleEnabled
Definition: ni_device_api_priv.h:708
_ni_frame::force_pic_qp
uint16_t force_pic_qp
Definition: ni_device_api.h:2659
_ni_encoder_cfg_params::high_tier
int high_tier
Definition: ni_device_api.h:2344
_ni_session_context::mutex
ni_pthread_mutex_t mutex
Definition: ni_device_api.h:1565
_ni_encoder_cfg_params::max_num_merge
int max_num_merge
Definition: ni_device_api.h:2349
_ni_gop_params::num_ref_pics
int num_ref_pics
Definition: ni_device_api.h:1966
_ni_decoder_input_params_t::crop_mode
int crop_mode[NI_MAX_NUM_OF_DECODER_OUTPUTS]
Definition: ni_device_api.h:2506
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:15324
_ni_encoder_cfg_params::crf
int crf
Definition: ni_device_api.h:2269
_ni_session_context::hw_id
int hw_id
Definition: ni_device_api.h:1478
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:1146
NI_MAX_DEC_SESSION_READ_QUERY_EOS_RETRIES
#define NI_MAX_DEC_SESSION_READ_QUERY_EOS_RETRIES
Definition: ni_device_api_priv.h:811
_ni_network_layer_params_t
Definition: ni_device_api.h:1326
_ni_encoder_config_t::ui8fixedframerate
uint8_t ui8fixedframerate
Definition: ni_device_api_priv.h:605
NVME_CORE
@ NVME_CORE
Definition: ni_defs.h:366
_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:1562
_ni_xcoder_params::fps_number
uint32_t fps_number
Definition: ni_device_api.h:2718
QUERY_INSTANCE_RBUFF_SIZE_R
#define QUERY_INSTANCE_RBUFF_SIZE_R(sid, instance)
Definition: ni_nvme.h:724
MOTION_CONSTRAINED_QUALITY_MODE
#define MOTION_CONSTRAINED_QUALITY_MODE
Definition: ni_device_api.h:484
_ni_session_context::ddr_config
uint8_t ddr_config
Definition: ni_device_api.h:1626
_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:2745
NI_HDR10P_SEI_BYTE5
#define NI_HDR10P_SEI_BYTE5
Definition: ni_device_api.h:457
_ni_load_query::fw_video_shared_mem_usage
uint32_t fw_video_shared_mem_usage
Definition: ni_device_api.h:1207
_ni_scaler_params_t
Definition: ni_device_api.h:2566
_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:455
_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:1169
CONFIG_GLOBAL_NAMESPACE_NUM
#define CONFIG_GLOBAL_NAMESPACE_NUM
Definition: ni_nvme.h:894
_ni_p2p_sgl_t::ui64DMAAddr
uint64_t ui64DMAAddr[NI_MAX_P2P_SGL_ENTRY]
Definition: ni_defs.h:407
NI_RETCODE_PARAM_ERROR_HEIGHT_TOO_BIG
@ NI_RETCODE_PARAM_ERROR_HEIGHT_TOO_BIG
Definition: ni_defs.h:498
_ni_timestamp_table_t::list
ni_queue_t list
Definition: ni_device_api.h:1323
_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:461
_ni_encoder_cfg_params::enable_timecode
int enable_timecode
Definition: ni_device_api.h:2439
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:431
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:425
_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:7865
_ni_load_query::fw_model_load
uint32_t fw_model_load
Definition: ni_device_api.h:1199
_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:808
SESSION_RUN_STATE_SEQ_CHANGE_DRAINING
@ SESSION_RUN_STATE_SEQ_CHANGE_DRAINING
Definition: ni_device_api.h:1178
NI_FW_META_DATA_SZ
#define NI_FW_META_DATA_SZ
Definition: ni_defs.h:306
NI_CC_SEI_HDR_H264_LEN
#define NI_CC_SEI_HDR_H264_LEN
Definition: ni_device_api.h:464
_ni_decoder_config_t::ui32SourceHeight
uint32_t ui32SourceHeight
Definition: ni_device_api_priv.h:739
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:634
_ni_session_context::fw_rev
uint8_t fw_rev[8]
Definition: ni_device_api.h:1625
nvme_cmd_xcoder_read
@ nvme_cmd_xcoder_read
Definition: ni_defs.h:568
_ni_network_data::output_num
uint32_t output_num
Definition: ni_device_api.h:1366
_ni_encoder_cfg_params::intra_mb_refresh_arg
int intra_mb_refresh_arg
Definition: ni_device_api.h:2262
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:1177
_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:567
_ni_session_context::decoder_last_drop_frame_num
uint32_t decoder_last_drop_frame_num
Definition: ni_device_api.h:1723
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:3713
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:1444
_ni_encoder_cfg_params::pps_init_qp
int pps_init_qp
Definition: ni_device_api.h:2407
_ni_session_context::pkt_pos
uint64_t pkt_pos[NI_FIFO_SZ]
Definition: ni_device_api.h:1453
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:17805
NI_RETCODE_PARAM_INVALID_VALUE
@ NI_RETCODE_PARAM_INVALID_VALUE
Definition: ni_defs.h:437
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:2635
_ni_frame::sei_cc_offset
unsigned int sei_cc_offset
Definition: ni_device_api.h:2632
_ni_packet::pkt_pos
uint64_t pkt_pos
Definition: ni_device_api.h:2830
_ni_encoder_cfg_params::gopSize
int gopSize
Definition: ni_device_api.h:2323
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:1713
ni_decode_cropping_rectangle::ui16X
uint16_t ui16X
Definition: ni_device_api_priv.h:693
_ni_frame_config::options
uint16_t options
Definition: ni_device_api.h:2813
_ni_session_context::framerate
ni_framerate_t framerate
Definition: ni_device_api.h:1604
_ni_nvme_identity::fw_build_time
uint8_t fw_build_time[26]
Definition: ni_nvme.h:221
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:1534
_ni_session_context::pext_mutex
ni_pthread_mutex_t * pext_mutex
Definition: ni_device_api.h:1670
_ni_frame_config::picture_format
uint16_t picture_format
Definition: ni_device_api.h:2812
_ni_p2p_sgl_t::ui32NumEntries
uint32_t ui32NumEntries
Definition: ni_defs.h:405
_ni_thread_arg_struct_t::device_handle
ni_device_handle_t device_handle
Definition: ni_device_api.h:1264
_ni_decoder_input_params_t::crop_whxy
int crop_whxy[NI_MAX_NUM_OF_DECODER_OUTPUTS][4]
Definition: ni_device_api.h:2507
NI_RETCODE_PARAM_ERROR_VBV_BUFFER_SIZE
@ NI_RETCODE_PARAM_ERROR_VBV_BUFFER_SIZE
Definition: ni_defs.h:444
_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:1403
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:2318
_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:236
_ni_session_statistic_t::ui32FramesInput
uint32_t ui32FramesInput
Definition: ni_defs.h:379
_ni_custom_gop_params::custom_gop_size
int custom_gop_size
Definition: ni_device_api.h:2052
XCODER_TEST_RECONF_OFF
@ XCODER_TEST_RECONF_OFF
Definition: ni_device_api.h:1741
_ni_session_statistic_t::ui32LastTransactionCompletionStatus
uint32_t ui32LastTransactionCompletionStatus
Definition: ni_defs.h:389
_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:2795
_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:3202
_ni_encoder_cfg_params::HDR10dx2
int HDR10dx2
Definition: ni_device_api.h:2278
_ni_session_statistic_t::ui32LastErrorStatus
uint32_t ui32LastErrorStatus
Definition: ni_defs.h:391
_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:715
_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:1712
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:1378
_ni_uploader_config_t::ui8PixelFormat
uint8_t ui8PixelFormat
Definition: ni_device_api_priv.h:662
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:15233
NI_AI_FLAG_IO
#define NI_AI_FLAG_IO
Definition: ni_device_api.h:300
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:873
DP_LOG_OFFSET_IN_4K
#define DP_LOG_OFFSET_IN_4K
Definition: ni_nvme.h:664
_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:842
_ni_encoder_cfg_params::HDR10wx
int HDR10wx
Definition: ni_device_api.h:2280
_ni_xcoder_params::source_height
int source_height
Definition: ni_device_api.h:2728
_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:844
_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:2340
_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:2411
_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:410
_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:1492
_ni_frame::ni_pict_type
ni_pic_type_t ni_pict_type
Definition: ni_device_api.h:2627
_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:698
DP_CORE
@ DP_CORE
Definition: ni_defs.h:368
_ni_packet::ssim_v
double ssim_v
Definition: ni_device_api.h:2861
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:5027
_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:804
_ni_encoder_cfg_params::HDR10maxluma
int HDR10maxluma
Definition: ni_device_api.h:2282
_ni_decoder_config_t::ui8ReduceDpbDelay
uint8_t ui8ReduceDpbDelay
Definition: ni_device_api_priv.h:737
_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:1367
_niFrameSurface1::ui16session_ID
uint16_t ui16session_ID
Definition: ni_device_api.h:2796
NI_SCALER_OPCODE_OVERLAY
@ NI_SCALER_OPCODE_OVERLAY
Definition: ni_defs.h:577
NI_RETCODE_ERROR_UNSUPPORTED_FW_VERSION
@ NI_RETCODE_ERROR_UNSUPPORTED_FW_VERSION
Definition: ni_defs.h:521
_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:2765
_ni_session_context::last_pkt_pos
uint64_t last_pkt_pos
Definition: ni_device_api.h:1454
_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:2378
_ni_gop_params::qp_factor
float qp_factor
Definition: ni_device_api.h:1963
_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:2742
_ni_gop_rps::ref_pic
int ref_pic
Definition: ni_device_api.h:1955
NI_PIX_FMT_NV12
@ NI_PIX_FMT_NV12
Definition: ni_device_api.h:264
_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:806
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:5967
_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:365
_ni_frame::data_len
uint32_t data_len[NI_MAX_NUM_DATA_POINTERS]
Definition: ni_device_api.h:2664
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:13323
NI_MAX_SLICE_SIZE
#define NI_MAX_SLICE_SIZE
Definition: ni_device_api.h:219
_ni_session_context::pixel_format_changed
int pixel_format_changed
Definition: ni_device_api.h:1705
_ni_frame::extra_data_len
unsigned int extra_data_len
Definition: ni_device_api.h:2657
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:2368
_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:384
_ni_encoder_cfg_params::enable_rate_control
int enable_rate_control
Definition: ni_device_api.h:2365
_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:875
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:2418
NI_PIXEL_PLANAR_MAX
@ NI_PIXEL_PLANAR_MAX
Definition: ni_device_api.h:923
_ni_decoder_input_params_t::semi_planar
int semi_planar[NI_MAX_NUM_OF_DECODER_OUTPUTS]
Definition: ni_device_api.h:2505
_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:2533
_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:2499
NI_POOL_TYPE_NORMAL
@ NI_POOL_TYPE_NORMAL
Definition: ni_device_api.h:492
NI_DEVICE_TYPE_AI
@ NI_DEVICE_TYPE_AI
Definition: ni_defs.h:349
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:13917
_ni_ai_config_t
Definition: ni_device_api_priv.h:744
NI_RETCODE_PARAM_ERROR_DECODING_REFRESH_TYPE
@ NI_RETCODE_PARAM_ERROR_DECODING_REFRESH_TYPE
Definition: ni_defs.h:484
_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:2790
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:5237
_ni_uploader_config_t::ui32lumaLinesize
uint32_t ui32lumaLinesize
Definition: ni_device_api_priv.h:665
NI_RETCODE_PARAM_ERROR_CUSIZE_MODE_16X16_EN
@ NI_RETCODE_PARAM_ERROR_CUSIZE_MODE_16X16_EN
Definition: ni_defs.h:487
_ni_encoder_cfg_params::multicoreJointMode
int multicoreJointMode
Definition: ni_device_api.h:2289
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:370
_ni_hw_capability::max_video_height
uint16_t max_video_height
Definition: ni_device_api.h:1142
_ni_xcoder_params::enable_ai_enhance
int enable_ai_enhance
Definition: ni_device_api.h:2781
NETINT_PCI_VENDOR_ID
#define NETINT_PCI_VENDOR_ID
Definition: ni_defs.h:126
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:432
_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:1263
NI_HDR10P_SEI_BYTE3
#define NI_HDR10P_SEI_BYTE3
Definition: ni_device_api.h:455
_ni_decoder_config_t::ui8MCMode
uint8_t ui8MCMode
Definition: ni_device_api_priv.h:728
_ni_decoder_input_params_t
Definition: ni_device_api.h:2445
_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:722
_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:2275
_ni_load_query::fw_video_mem_usage
uint32_t fw_video_mem_usage
Definition: ni_device_api.h:1203
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:2350
_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:2569
_ni_encoder_cfg_params::ctbRcMode
int ctbRcMode
Definition: ni_device_api.h:2322
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:1268
NI_MEM_PAGE_ALIGNMENT
#define NI_MEM_PAGE_ALIGNMENT
Definition: ni_defs.h:251
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:2291
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:1146
_ni_frame::end_of_stream
uint32_t end_of_stream
Definition: ni_device_api.h:2608
_ni_session_context::network_data
ni_network_data_t * network_data
Definition: ni_device_api.h:1678
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:2822
NI_HWDESC_UNIFIED_MEMBIN_SIZE
#define NI_HWDESC_UNIFIED_MEMBIN_SIZE
Definition: ni_device_api_priv.h:799
CONFIG_SESSION_SWVersion_W
#define CONFIG_SESSION_SWVersion_W(sid)
Definition: ni_nvme.h:888
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:1145
NI_CODEC_HW_DOWNLOAD
@ NI_CODEC_HW_DOWNLOAD
Definition: ni_device_api.h:941
_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
current_time
struct timeval current_time
Definition: ni_p2p_test.c:64
_niFrameSurface1::encoding_type
int8_t encoding_type
Definition: ni_device_api.h:2802
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:14923
_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:1377
_ni_xcoder_params::hwframes
int hwframes
Definition: ni_device_api.h:2774
_ni_session_context::prev_size
int prev_size
Definition: ni_device_api.h:1507
_ni_encoder_cfg_params::newRcEnable
int newRcEnable
Definition: ni_device_api.h:2391
_ni_session_context::device_handle
ni_device_handle_t device_handle
Definition: ni_device_api.h:1462
_ni_session_context::p_session_config
void * p_session_config
Definition: ni_device_api.h:1473
NI_CODEC_FORMAT_AV1
@ NI_CODEC_FORMAT_AV1
Definition: ni_device_api.h:915
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:2417
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:1310
_ni_frame::sei_hdr_plus_len
unsigned int sei_hdr_plus_len
Definition: ni_device_api.h:2641
_ni_xcoder_params::preset
int preset
Definition: ni_device_api.h:2716
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:820
_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:573
_ni_session_context::dec_fme_buf_pool
ni_buf_pool_t * dec_fme_buf_pool
Definition: ni_device_api.h:1559
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:1125
_ni_encoder_cfg_params::entropy_coding_mode
int entropy_coding_mode
Definition: ni_device_api.h:2307
_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:2837
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:1143
_ni_session_data_io
Definition: ni_device_api.h:2866
_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:10030
_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:13122
_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:547
QUERY_INSTANCE_RBUFF_SIZE_BUSY_R
#define QUERY_INSTANCE_RBUFF_SIZE_BUSY_R(sid, instance)
Definition: ni_nvme.h:730
_ni_encoder_cfg_params::ltrFirstGap
int ltrFirstGap
Definition: ni_device_api.h:2287
TP_CORE
@ TP_CORE
Definition: ni_defs.h:369
_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:1077
_ni_encoder_cfg_params::ltrRefQpOffset
int ltrRefQpOffset
Definition: ni_device_api.h:2286
_ni_t408_config_t::chromaCrQpOffset
int32_t chromaCrQpOffset
Definition: ni_device_api_priv.h:419
_ni_network_buffer::ui8MultiIn
uint8_t ui8MultiIn
Definition: ni_device_api_priv.h:757
NI_PIX_FMT_YUV420P10LE
@ NI_PIX_FMT_YUV420P10LE
Definition: ni_device_api.h:263
_ni_frame_config::rgba_color
uint32_t rgba_color
Definition: ni_device_api.h:2818
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:16899
_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:96
_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:2356
_ni_session_context::sender_handle
ni_device_handle_t sender_handle
Definition: ni_device_api.h:1468
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:15115
_ni_scaler_config::filterblit
uint8_t filterblit
Definition: ni_device_api_priv.h:763
CONFIG_INSTANCE_SetP2P_W
#define CONFIG_INSTANCE_SetP2P_W(sid, instance)
Definition: ni_nvme.h:864
_ni_session_statistic_t::ui32FramesOutput
uint32_t ui32FramesOutput
Definition: ni_defs.h:382
_ni_dec_mastering_display_colour_volume_bytes::min_display_mastering_luminance
uint32_t min_display_mastering_luminance
Definition: ni_device_api.h:1067
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:481
_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:6647
_ni_encoder_cfg_params::enable_smooth_crf
int enable_smooth_crf
Definition: ni_device_api.h:2436
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
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:7413
_ni_session_context::keep_alive_timeout
uint32_t keep_alive_timeout
Definition: ni_device_api.h:1521
_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:396
NI_HDR10P_SEI_BYTE2
#define NI_HDR10P_SEI_BYTE2
Definition: ni_device_api.h:454
NI_RETCODE_PARAM_ERROR_CUSIZE_MODE_8X8_EN
@ NI_RETCODE_PARAM_ERROR_CUSIZE_MODE_8X8_EN
Definition: ni_defs.h:486
_ni_encoder_cfg_params::customize_roi_qp_level
int customize_roi_qp_level
Definition: ni_device_api.h:2431
_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:16332
NI_SCALER_OPCODE_STACK
@ NI_SCALER_OPCODE_STACK
Definition: ni_defs.h:578
_ni_packet::scene_change_detected
uint8_t scene_change_detected
Definition: ni_device_api.h:2863
_ni_encoder_cfg_params
Definition: ni_device_api.h:2060
GC620_BGRX8888
#define GC620_BGRX8888
Definition: ni_device_api.h:201
NI_RETCODE_PARAM_WARN
@ NI_RETCODE_PARAM_WARN
Definition: ni_defs.h:526
_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:2301
_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:1451
_ni_encoder_cfg_params::spatial_layers
int spatial_layers
Definition: ni_device_api.h:2438
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:794
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:2434
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:17000
ni_network_buffer_t
struct _ni_network_buffer ni_network_buffer_t
CONFIG_SESSION_KeepAlive_W
#define CONFIG_SESSION_KeepAlive_W(sid)
Definition: ni_nvme.h:871
_ni_session_context::device_type
uint32_t device_type
Definition: ni_device_api.h:1484
_ni_decoder_config_t::ui32MaxPktSize
uint32_t ui32MaxPktSize
Definition: ni_device_api_priv.h:732
niFrameSurface1_t
struct _niFrameSurface1 niFrameSurface1_t
NI_RETCODE_PARAM_ERROR_MX_DELTA_QP
@ NI_RETCODE_PARAM_ERROR_MX_DELTA_QP
Definition: ni_defs.h:463
_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:9648
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:713
NI_AI_HW_ALIGN_SIZE
#define NI_AI_HW_ALIGN_SIZE
Definition: ni_device_api_priv.h:1232
ni_decoder_output_config_t::ui8SemiPlanarEnabled
uint8_t ui8SemiPlanarEnabled
Definition: ni_device_api_priv.h:709
_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:754
GC620_RGBA8888
#define GC620_RGBA8888
Definition: ni_device_api.h:199
_ni_frame::force_key_frame
int force_key_frame
Definition: ni_device_api.h:2624
NI_GET_MAX_HWDESC_FRAME_INDEX
#define NI_GET_MAX_HWDESC_FRAME_INDEX(x)
Definition: ni_defs.h:278
_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:2570
LRETURN
#define LRETURN
Definition: ni_defs.h:323
NI_PIXEL_PLANAR_FORMAT_SEMIPLANAR
@ NI_PIXEL_PLANAR_FORMAT_SEMIPLANAR
Definition: ni_device_api.h:920
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:2272
_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:2572
_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:1379
_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:452
_ni_content_light_level_info_bytes::max_content_light_level
uint16_t max_content_light_level
Definition: ni_device_api.h:1076
_ni_packet::video_height
uint32_t video_height
Definition: ni_device_api.h:2834
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:8178
_ni_session_context::buffer_pool
ni_queue_buffer_pool_t * buffer_pool
Definition: ni_device_api.h:1558
_ni_xcoder_params::chroma_linesize
int chroma_linesize
Definition: ni_device_api.h:2785
NI_MINIMUM_CROPPED_LENGTH
#define NI_MINIMUM_CROPPED_LENGTH
Definition: ni_device_api_priv.h:685
_ni_xcoder_params::minFramesDelay
int minFramesDelay
Definition: ni_device_api.h:2788
_ni_xcoder_params::sar_num
int sar_num
Definition: ni_device_api.h:2759
_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:457
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:6925
_ni_packet
Definition: ni_device_api.h:2825
NI_DATA_BUFFER_LEN
#define NI_DATA_BUFFER_LEN
Definition: ni_nvme.h:625
_ni_session_statistic_t::ui32RdBufAvailSize
uint32_t ui32RdBufAvailSize
Definition: ni_defs.h:377
_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:808
_ni_session_context::frame_num
uint64_t frame_num
Definition: ni_device_api.h:1531
_ni_frame::sei_cc_len
unsigned int sei_cc_len
Definition: ni_device_api.h:2633
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:2273
CONFIG_INSTANCE_SetEncFramePara_W
#define CONFIG_INSTANCE_SetEncFramePara_W(sid, instance)
Definition: ni_nvme.h:840
_ni_queue_t::count
uint32_t count
Definition: ni_device_api.h:1316
_ni_encoder_cfg_params::intra_qp_delta
int intra_qp_delta
Definition: ni_device_api.h:2369
_ni_session_context::ori_luma_linesize
int ori_luma_linesize
Definition: ni_device_api.h:1680
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:13364
_ni_xcoder_params::ai_enhance_level
int ai_enhance_level
Definition: ni_device_api.h:2786
_ni_network_layer_params_t::sizes
uint32_t sizes[6]
Definition: ni_device_api.h:1329
_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:1489
_ni_network_layer_offset::offset
int32_t offset
Definition: ni_device_api.h:1360
_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:2311
_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:836
_ni_overall_load_query::overall_fw_model_load
uint32_t overall_fw_model_load
Definition: ni_device_api.h:1225
_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:726
_ni_session_context::last_gop_size
int last_gop_size
Definition: ni_device_api.h:1691
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:2649
_ni_encoder_cfg_params::tolCtbRcInter
float tolCtbRcInter
Definition: ni_device_api.h:2329
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:18139
_ni_frame::p_data
uint8_t * p_data[NI_MAX_NUM_DATA_POINTERS]
Definition: ni_device_api.h:2663
_ni_encoder_cfg_params::custom_gop_params
ni_custom_gop_params_t custom_gop_params
Definition: ni_device_api.h:2255
_ni_encoder_cfg_params::HDR10dx0
int HDR10dx0
Definition: ni_device_api.h:2274
NVME_LOG_OFFSET_IN_4K
#define NVME_LOG_OFFSET_IN_4K
Definition: ni_nvme.h:662
_ni_xcoder_params::zerocopy_mode
int zerocopy_mode
Definition: ni_device_api.h:2783
NI_EC_POLICY_DEFAULT
#define NI_EC_POLICY_DEFAULT
Definition: ni_device_api.h:324
_ni_xcoder_params::sar_denom
int sar_denom
Definition: ni_device_api.h:2760
_ni_t408_config_t::cu32IntraDeltaRate
int32_t cu32IntraDeltaRate
Definition: ni_device_api_priv.h:477
ni_decoder_output_config_t::sCroppingRectable
ni_decode_cropping_rectangle sCroppingRectable
Definition: ni_device_api_priv.h:717
_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:2527
NI_RETCODE_PARAM_ERROR_MAXNUMMERGE
@ NI_RETCODE_PARAM_ERROR_MAXNUMMERGE
Definition: ni_defs.h:477
_ni_device_capability::fw_build_time
uint8_t fw_build_time[26]
Definition: ni_device_api.h:1168
_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:733
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:12731
_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:1450
_ni_encoder_cfg_params::disableBframeRdoq
int disableBframeRdoq
Definition: ni_device_api.h:2426
NI_CODEC_FORMAT_VP9
@ NI_CODEC_FORMAT_VP9
Definition: ni_device_api.h:913
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:363
NI_DEVICE_TYPE_SCALER
@ NI_DEVICE_TYPE_SCALER
Definition: ni_defs.h:348
_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:2330
_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:390
_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_DDR_PRIORITY_RESET
@ NI_DDR_PRIORITY_RESET
Definition: ni_device_api.h:1825
_ni_frame::roi_len
unsigned int roi_len
Definition: ni_device_api.h:2653
_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:1640
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:3761
NI_RETCODE_PARAM_ERROR_CONF_WIN_R
@ NI_RETCODE_PARAM_ERROR_CONF_WIN_R
Definition: ni_defs.h:471
_ni_metadata_common::crop_top
uint16_t crop_top
Definition: ni_device_api_priv.h:212
NI_ERRNO
#define NI_ERRNO
Definition: ni_defs.h:217
_ni_frame_config::picture_height
uint16_t picture_height
Definition: ni_device_api.h:2811
NI_RETCODE_PARAM_ERROR_WIDTH_TOO_BIG
@ NI_RETCODE_PARAM_ERROR_WIDTH_TOO_BIG
Definition: ni_defs.h:494
_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:2640
_ni_encoder_cfg_params::enable_dynamic_16x16_merge
int enable_dynamic_16x16_merge
Definition: ni_device_api.h:2351
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:517
_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:1609
_ni_input_frame::video_height
uint32_t video_height
Definition: ni_device_api.h:1401
_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:1408
_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:1417
QUERY_DETAIL_GET_STATUS_V1_R
#define QUERY_DETAIL_GET_STATUS_V1_R(instance)
Definition: ni_nvme.h:788
_ni_encoder_cfg_params::iframe_size_ratio
int iframe_size_ratio
Definition: ni_device_api.h:2422
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:15702
NI_PIX_FMT_P010LE
@ NI_PIX_FMT_P010LE
Definition: ni_device_api.h:265
ni_memfree
#define ni_memfree(p_memptr)
Definition: ni_util.h:408
_ni_session_context::bus
unsigned short bus
Definition: ni_device_api.h:1620
_ni_thread_arg_struct_t::hw_id
int hw_id
Definition: ni_device_api.h:1259
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:10332
NI_CODEC_HW_UPLOAD
@ NI_CODEC_HW_UPLOAD
Definition: ni_device_api.h:942
_niFrameSurface1
Definition: ni_device_api.h:2793
NI_EC_ERR_THRESHOLD_DEFAULT
#define NI_EC_ERR_THRESHOLD_DEFAULT
Definition: ni_device_api.h:325
NI_CODEC_HW_ENABLE
@ NI_CODEC_HW_ENABLE
Definition: ni_device_api.h:940
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:696
_ni_session_context::last_dts_interval
int64_t last_dts_interval
Definition: ni_device_api.h:1639
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:528
_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:771
_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:1490
_ni_frame
Definition: ni_device_api.h:2601
_ni_decoder_config_t::ui8DisablePictureReordering
uint8_t ui8DisablePictureReordering
Definition: ni_device_api_priv.h:729
_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:1365
_ni_packet::sent_size
int sent_size
Definition: ni_device_api.h:2839
_ni_encoder_cfg_params::HDR10dy1
int HDR10dy1
Definition: ni_device_api.h:2277
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:818
CONFIG_INSTANCE_SetScalerPara_W
#define CONFIG_INSTANCE_SetScalerPara_W(sid, instance)
Definition: ni_nvme.h:813
NI_PIX_FMT_RGBA
@ NI_PIX_FMT_RGBA
Definition: ni_device_api.h:266
_ni_encoder_cfg_params::conf_win_bottom
int conf_win_bottom
Definition: ni_device_api.h:2298
_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:2535
_ni_overall_load_query::overall_instance_count
uint32_t overall_instance_count
Definition: ni_device_api.h:1226
_ni_session_context::roi_len
uint32_t roi_len
Definition: ni_device_api.h:1496
NI_DDR_PRIORITY_NONE
@ NI_DDR_PRIORITY_NONE
Definition: ni_device_api.h:1824
_ni_encoder_cfg_params::profile
int profile
Definition: ni_device_api.h:2248
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:663
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:2314
_ni_session_context::max_frame_delay
int max_frame_delay
Definition: ni_device_api.h:1694
_ni_gop_params::rps
ni_gop_rps_t rps[NI_MAX_REF_PIC]
Definition: ni_device_api.h:1967
CONFIG_INSTANCE_SetEOS_W
#define CONFIG_INSTANCE_SetEOS_W(sid, instance)
Definition: ni_nvme.h:806
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:930
_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:473
NI_RETCODE_NVME_SC_INVALID_PARAMETER
@ NI_RETCODE_NVME_SC_INVALID_PARAMETER
Definition: ni_defs.h:544
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:818
_ni_packet::average_psnr
double average_psnr
Definition: ni_device_api.h:2858
_ni_encoder_cfg_params::bitrateWindow
int bitrateWindow
Definition: ni_device_api.h:2331
NI_RETCODE_ERROR_INVALID_SESSION
@ NI_RETCODE_ERROR_INVALID_SESSION
Definition: ni_defs.h:433
_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:1452
_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:13561
NI_PIX_FMT_ABGR
@ NI_PIX_FMT_ABGR
Definition: ni_device_api.h:269
_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:709
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:1217
QUERY_GENERAL_GET_STATUS_R
#define QUERY_GENERAL_GET_STATUS_R(instance)
Definition: ni_nvme.h:783
NI_PIX_FMT_ARGB
@ NI_PIX_FMT_ARGB
Definition: ni_device_api.h:268
_ni_encoder_config_t::i32pbRatio
int32_t i32pbRatio
Definition: ni_device_api_priv.h:628
_ni_xcoder_params
Definition: ni_device_api.h:2713
TP_LOG_OFFSET_IN_4K
#define TP_LOG_OFFSET_IN_4K
Definition: ni_nvme.h:665
NI_RETRY_INTERVAL_100US
#define NI_RETRY_INTERVAL_100US
Definition: ni_device_api_priv.h:813
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:2638
_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:367
NI_RETCODE_PARAM_ERROR_WIDTH_TOO_SMALL
@ NI_RETCODE_PARAM_ERROR_WIDTH_TOO_SMALL
Definition: ni_defs.h:496
_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:2540
_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:2382
_ni_xcoder_params::staticMmapThreshold
int staticMmapThreshold
Definition: ni_device_api.h:2780
_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:2390
_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:848
_ni_session_context::low_delay_sync_cond
ni_pthread_cond_t low_delay_sync_cond
Definition: ni_device_api.h:1666
_ni_frame::sei_hdr_mastering_display_color_vol_len
unsigned int sei_hdr_mastering_display_color_vol_len
Definition: ni_device_api.h:2636
_ni_encoder_cfg_params::motionConstrainedMode
int motionConstrainedMode
Definition: ni_device_api.h:2432
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:13456
NI_SESSION_CLOSE_RETRY_MAX
#define NI_SESSION_CLOSE_RETRY_MAX
Definition: ni_device_api_priv.h:795
_ni_session_context::ready_to_close
uint32_t ready_to_close
Definition: ni_device_api.h:1544
_ni_session_context::overall_load_query
ni_overall_load_query_t overall_load_query
Definition: ni_device_api.h:1503
OPEN_ADD_CODEC
#define OPEN_ADD_CODEC(instance, codec, param)
Definition: ni_nvme.h:697
_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:2360
_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:548
_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:1157
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:3077
_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:3923
_ni_load_query::fw_load
uint32_t fw_load
Definition: ni_device_api.h:1200
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:17752
_ni_device_capability::fw_rev
uint8_t fw_rev[8]
Definition: ni_device_api.h:1164
_ni_encoder_cfg_params::crop_width
int crop_width
Definition: ni_device_api.h:2395
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:1257
_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:2357
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:359
_ni_encoder_cfg_params::HDR10minluma
int HDR10minluma
Definition: ni_device_api.h:2283
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:17665
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:15297
_ni_session_context::codec_format
uint32_t codec_format
Definition: ni_device_api.h:1486
_ni_device_capability
device capability type
Definition: ni_device_api.h:1153
NI_RETRY_INTERVAL_200US
#define NI_RETRY_INTERVAL_200US
Definition: ni_device_api_priv.h:812
NI_MAX_NUM_OF_DECODER_OUTPUTS
#define NI_MAX_NUM_OF_DECODER_OUTPUTS
Definition: ni_defs.h:243
_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:736
_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:365
_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:1610
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:1608
_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:522
_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:1419
_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:771
_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:1710
NI_RETCODE_PARAM_ERROR_PIC_HEIGHT
@ NI_RETCODE_PARAM_ERROR_PIC_HEIGHT
Definition: ni_defs.h:482
_ni_encoder_cfg_params::colorPrimaries
int colorPrimaries
Definition: ni_device_api.h:2312
_ni_decoder_input_params_t::scale_wh
int scale_wh[NI_MAX_NUM_OF_DECODER_OUTPUTS][2]
Definition: ni_device_api.h:2511
CONFIG_INSTANCE_SetDecPara_W
#define CONFIG_INSTANCE_SetDecPara_W(sid, instance)
Definition: ni_nvme.h:811
_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:440
_ni_encoder_cfg_params::forceBframeQpfactor
float forceBframeQpfactor
Definition: ni_device_api.h:2427
_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:1333
_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:1196
_ni_session_context::is_auto_dl
uint8_t is_auto_dl
Definition: ni_device_api.h:1470
FP_LOG_OFFSET_IN_4K
#define FP_LOG_OFFSET_IN_4K
Definition: ni_nvme.h:666
_ni_thread_arg_struct_t::session_id
uint32_t session_id
Definition: ni_device_api.h:1260
_ni_encoder_cfg_params::crfFloat
float crfFloat
Definition: ni_device_api.h:2410
_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:2292
_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:461
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:6035
_niFrameSurface1::ui16width
uint16_t ui16width
Definition: ni_device_api.h:2797
_ni_session_context::flags_array
int flags_array[NI_FIFO_SZ]
Definition: ni_device_api.h:1644
_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:759
_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:1265
_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:1500
_ni_session_statistic_t::ui32FramesDropped
uint32_t ui32FramesDropped
Definition: ni_defs.h:383
_ni_session_data_io::frame
ni_frame_t frame
Definition: ni_device_api.h:2870
_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:2501
_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:2249
_ni_scaler_params_t::filterblit
int filterblit
Definition: ni_device_api.h:2568
_ni_encoder_cfg_params::HDR10dy2
int HDR10dy2
Definition: ni_device_api.h:2279
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:660
_niFrameSurface1::output_idx
int8_t output_idx
Definition: ni_device_api.h:2803
NI_H265_USER_DATA_FLAG_CONTENT_LIGHT_LEVEL_INFO
@ NI_H265_USER_DATA_FLAG_CONTENT_LIGHT_LEVEL_INFO
Definition: ni_device_api.h:374
NI_SCALER_FLAG_P2
#define NI_SCALER_FLAG_P2
Definition: ni_device_api.h:294
_ni_thread_arg_struct_t::session_timestamp
uint64_t session_timestamp
Definition: ni_device_api.h:1261
_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:1515
_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:234
CONFIG_SESSION_FRAME_COPY_W
#define CONFIG_SESSION_FRAME_COPY_W(sid)
Definition: ni_nvme.h:905
NI_RETCODE_PARAM_ERROR_CUSTOM_GOP
@ NI_RETCODE_PARAM_ERROR_CUSTOM_GOP
Definition: ni_defs.h:478
CONFIG_INSTANCE_SetAiHVSPlus_W
#define CONFIG_INSTANCE_SetAiHVSPlus_W(sid, instance)
Definition: ni_nvme.h:853
_ni_frameclone_desc::ui32Size
uint32_t ui32Size
Definition: ni_device_api.h:1380
_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::maxFrameSize
int maxFrameSize
Definition: ni_device_api.h:2243
_ni_encoder_cfg_params::hvs_qp_scale
int hvs_qp_scale
Definition: ni_device_api.h:2375
_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:2406
_ni_packet::av1_show_frame
int av1_show_frame
Definition: ni_device_api.h:2849
_ni_instance_mgr_general_status
Definition: ni_device_api_priv.h:79
atoi
#define atoi(p_str)
Definition: ni_device_api.c:7178
_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:659
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:2643
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:324
_ni_encoder_cfg_params::enable_hvs_qp
int enable_hvs_qp
Definition: ni_device_api.h:2374
_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:2611
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:15427
_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:1369
_ni_decoder_input_params_t::force_8_bit
int force_8_bit[NI_MAX_NUM_OF_DECODER_OUTPUTS]
Definition: ni_device_api.h:2504
_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:2400
_ni_session_context::low_delay_sync_flag
int low_delay_sync_flag
Definition: ni_device_api.h:1664
NI_CUSTOMIZE_ROI_QPOFFSET_LEVEL
#define NI_CUSTOMIZE_ROI_QPOFFSET_LEVEL
Max number of lines supported for qpoffset level.
Definition: ni_device_api.h:479
_ni_packet::pos
long long pos
Definition: ni_device_api.h:2829
_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:2821
_ni_encoder_cfg_params::avcc_hvcc
int avcc_hvcc
Definition: ni_device_api.h:2440
_ni_network_buffer::ui16Width
uint16_t ui16Width
Definition: ni_device_api_priv.h:753
_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:447
_ni_input_frame::p_input_buffer
uint8_t * p_input_buffer
Definition: ni_device_api.h:1398
_ni_xcoder_params::ddr_priority_mode
ni_ddr_priority_mode_t ddr_priority_mode
Definition: ni_device_api.h:2787
_ni_encoder_cfg_params::colorTrc
int colorTrc
Definition: ni_device_api.h:2313
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:3716
_ni_session_context::hvsplus_level
int hvsplus_level
encoder:calculate PSNR end
Definition: ni_device_api.h:1715
_ni_session_context::active_video_height
uint32_t active_video_height
Definition: ni_device_api.h:1551
_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:18069
NI_MAX_SEI_DATA
#define NI_MAX_SEI_DATA
Definition: ni_device_api.h:426
_ni_session_context::pkt_delay_cnt
uint32_t pkt_delay_cnt
Definition: ni_device_api.h:1702
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:2388
NI_CC_SEI_BYTE6
#define NI_CC_SEI_BYTE6
Definition: ni_device_api.h:449
_ni_session_context::enc_pts_r_idx
int64_t enc_pts_r_idx
Definition: ni_device_api.h:1440
_ni_encoder_cfg_params::tune_bframe_visual
int tune_bframe_visual
Definition: ni_device_api.h:2428
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:12908
ni_instance_mgr_stream_complete_t
struct _ni_instance_mgr_stream_complete ni_instance_mgr_stream_complete_t
_ni_network_buffer
Definition: ni_device_api_priv.h:750
_ni_encoder_cfg_params::conf_win_left
int conf_win_left
Definition: ni_device_api.h:2299
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:2409
_ni_session_context::rc_error_count
int rc_error_count
Definition: ni_device_api.h:1533
_ni_encoder_cfg_params::conf_win_right
int conf_win_right
Definition: ni_device_api.h:2300
_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:911
_ni_session_context::blk_dev_name
char blk_dev_name[NI_MAX_DEVICE_NAME_LEN]
Definition: ni_device_api.h:1659
_ni_decoder_input_params_t::enable_low_delay_check
int enable_low_delay_check
Definition: ni_device_api.h:2519
NI_POOL_TYPE_NONE
@ NI_POOL_TYPE_NONE
Definition: ni_device_api.h:491
_ni_session_context::biggest_bitstream_buffer_allocated
uint32_t biggest_bitstream_buffer_allocated
Definition: ni_device_api.h:1555
_ni_session_context::session_run_state
ni_session_run_state_t session_run_state
Definition: ni_device_api.h:1547
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:6117
_ni_session_context::enable_low_delay_check
int enable_low_delay_check
Definition: ni_device_api.h:1595
_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:1508
_niFrameSurface1::device_handle
int32_t device_handle
Definition: ni_device_api.h:2800
_ni_encoder_cfg_params::crfMax
int crfMax
Definition: ni_device_api.h:2399
NI_CC_SEI_TRAILER_LEN
#define NI_CC_SEI_TRAILER_LEN
Definition: ni_device_api.h:465
_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:2801
_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:727
_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:406
_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:2403
_ni_network_layer_info::out_param
ni_network_layer_params_t * out_param
Definition: ni_device_api.h:1355
_ni_encoder_cfg_params::enableipRatio
int enableipRatio
Definition: ni_device_api.h:2420
NI_CODEC_HW_PAYLOAD_OFFSET
@ NI_CODEC_HW_PAYLOAD_OFFSET
Definition: ni_device_api.h:944
_ni_decoder_input_params_t::scale_resolution_ceil
int scale_resolution_ceil[NI_MAX_NUM_OF_DECODER_OUTPUTS]
Definition: ni_device_api.h:2534
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:1161
_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:1638
_ni_encoder_cfg_params::vbv_min_rate
int vbv_min_rate
Definition: ni_device_api.h:2424
NI_DEVICE_TYPE_XCODER_MAX
@ NI_DEVICE_TYPE_XCODER_MAX
Definition: ni_defs.h:350
CONFIG_INSTANCE_SetSOS_W
#define CONFIG_INSTANCE_SetSOS_W(sid, instance)
Definition: ni_nvme.h:804
_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:17893
_ni_encoder_cfg_params::HDR10MaxLight
int HDR10MaxLight
Definition: ni_device_api.h:2270
_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:2747
_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:1482
_ni_session_context::psnr_u
double psnr_u
Definition: ni_device_api.h:1711
_ni_dec_mastering_display_colour_volume_bytes::white_point_x
uint16_t white_point_x
Definition: ni_device_api.h:1064
_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:2433
_ni_encoder_cfg_params::crop_height
int crop_height
Definition: ni_device_api.h:2396
_ni_hw_capability::codec_format
uint8_t codec_format
Definition: ni_device_api.h:1139
_ni_frame_config::rectangle_width
uint16_t rectangle_width
Definition: ni_device_api.h:2814
_ni_session_context::max_nvme_io_size
uint32_t max_nvme_io_size
Definition: ni_device_api.h:1476
_ni_session_context::enable_user_data_sei_passthru
int enable_user_data_sei_passthru
Definition: ni_device_api.h:1614
_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:2241
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:7344
NI_RETCODE_EAGAIN
@ NI_RETCODE_EAGAIN
Definition: ni_defs.h:529
_ni_timestamp_table_t
Definition: ni_device_api.h:1321
_ni_dec_mastering_display_colour_volume_bytes::max_display_mastering_luminance
uint32_t max_display_mastering_luminance
Definition: ni_device_api.h:1066
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:1227
_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:428
_ni_encoder_cfg_params::HDR10dx1
int HDR10dx1
Definition: ni_device_api.h:2276
_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:1436
_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:456
_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:17231
_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:1141
_ni_decoder_config_t::ui32SourceWidth
uint32_t ui32SourceWidth
Definition: ni_device_api_priv.h:738
_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:2242
_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:2609
_ni_packet::buffer_size
uint32_t buffer_size
Definition: ni_device_api.h:2842
_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:448
_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:500
_ni_sei_header::type
uint8_t type
Definition: ni_device_api.h:346
_ni_session_context::dts_queue
ni_timestamp_table_t * dts_queue
Definition: ni_device_api.h:1518
_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:2607
_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:2778
_ni_decoder_config_t::ui8EnablelowDelayCheck
uint8_t ui8EnablelowDelayCheck
Definition: ni_device_api_priv.h:730
_ni_frame::inconsecutive_transfer
uint8_t inconsecutive_transfer
Definition: ni_device_api.h:2708
_ni_overall_load_query::overall_current_load
uint32_t overall_current_load
Definition: ni_device_api.h:1224
NI_RETCODE_PARAM_ERROR_FRATE
@ NI_RETCODE_PARAM_ERROR_FRATE
Definition: ni_defs.h:438
_ni_frame::crop_right
uint32_t crop_right
Definition: ni_device_api.h:2616
_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:1675
NI_SCALER_FLAG_PC
#define NI_SCALER_FLAG_PC
Definition: ni_device_api.h:292
NI_RETCODE_PARAM_ERROR_CU_SIZE_MODE
@ NI_RETCODE_PARAM_ERROR_CU_SIZE_MODE
Definition: ni_defs.h:450
_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:2268
_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:2259
_ni_frame::pixel_format
int pixel_format
Definition: ni_device_api.h:2672
_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:490
_ni_decoder_config_t::ui8SurviveStreamErr
uint8_t ui8SurviveStreamErr
Definition: ni_device_api_priv.h:741
_ni_encoder_cfg_params::get_psnr_mode
int get_psnr_mode
Definition: ni_device_api.h:2430
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:720
_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:1695
ni_decoder_output_config_t::ui8EnablePpuScaleAdapt
uint8_t ui8EnablePpuScaleAdapt
Definition: ni_device_api_priv.h:714
_ni_encoder_cfg_params::ltrNextInterval
int ltrNextInterval
Definition: ni_device_api.h:2288
_ni_decoder_input_params_t::survive_stream_err
int survive_stream_err
Definition: ni_device_api.h:2539
_ni_encoder_cfg_params::videoFullRange
int videoFullRange
Definition: ni_device_api.h:2290
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:1437
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:8210
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:15739
_ni_encoder_cfg_params::intra_reset_refresh
int intra_reset_refresh
Definition: ni_device_api.h:2389
_ni_uploader_config_t::ui8poolSize
uint8_t ui8poolSize
Definition: ni_device_api_priv.h:661
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:694
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:17602
NI_PIX_FMT_10_TILED4X4
@ NI_PIX_FMT_10_TILED4X4
Definition: ni_device_api.h:276
_ni_encoder_cfg_params::slice_arg
int slice_arg
Definition: ni_device_api.h:2358
NI_H265_USERDATA_FLAG_UNREGISTERED_SUF
@ NI_H265_USERDATA_FLAG_UNREGISTERED_SUF
Definition: ni_device_api.h:367
_ni_frame::p_metadata_buffer
uint8_t * p_metadata_buffer
Definition: ni_device_api.h:2695
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:677
lower_pixel_rate
int lower_pixel_rate(const ni_load_query_t *pQuery, uint32_t ui32CurrentLowest)
Definition: ni_device_api_priv.c:18211
NI_GET_MIN_HWDESC_P2P_BUF_ID
#define NI_GET_MIN_HWDESC_P2P_BUF_ID(x)
Definition: ni_defs.h:270
_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:2267
NI_MAX_AI_NETWORK_BINARY_BUFFER_QUERY_RETRIES
#define NI_MAX_AI_NETWORK_BINARY_BUFFER_QUERY_RETRIES
Definition: ni_device_api_priv.h:810
NI_RETCODE_ERROR_INVALID_HANDLE
@ NI_RETCODE_ERROR_INVALID_HANDLE
Definition: ni_defs.h:511
NI_RETCODE_PARAM_ERROR_INTRA_QP
@ NI_RETCODE_PARAM_ERROR_INTRA_QP
Definition: ni_defs.h:447
_ni_packet::still_image_detected
uint8_t still_image_detected
Definition: ni_device_api.h:2862
_ni_frame_config::rectangle_y
int16_t rectangle_y
Definition: ni_device_api.h:2817
NI_RETCODE_NVME_SC_WRITE_BUFFER_FULL
@ NI_RETCODE_NVME_SC_WRITE_BUFFER_FULL
Definition: ni_defs.h:534
_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:17952
_ni_custom_gop_params::pic_param
ni_gop_params_t pic_param[NI_MAX_GOP_NUM]
Definition: ni_device_api.h:2053
_ni_session_context::pic_reorder_delay
int pic_reorder_delay
Definition: ni_device_api.h:1641
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:4548
_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:1621
_ni_xcoder_params::low_delay_mode
int low_delay_mode
Definition: ni_device_api.h:2733
_ni_decoder_input_params_t::decoder_low_delay
int decoder_low_delay
Definition: ni_device_api.h:2517
_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:2503
_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:2833
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:777
_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:1532
_ni_encoder_cfg_params::enable_cu_level_rate_control
int enable_cu_level_rate_control
Definition: ni_device_api.h:2373
_ni_decoder_input_params_t::disable_adaptive_buffers
int disable_adaptive_buffers
Definition: ni_device_api.h:2537
NI_SCALER_OPCODE_ROTATE
@ NI_SCALER_OPCODE_ROTATE
Definition: ni_defs.h:579
NI_CODEC_FORMAT_H264
@ NI_CODEC_FORMAT_H264
Definition: ni_device_api.h:911
_ni_frame::use_cur_src_as_long_term_pic
uint8_t use_cur_src_as_long_term_pic
Definition: ni_device_api.h:2621
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:17425
_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:1269
_ni_frame::start_buffer_size
uint32_t start_buffer_size
Definition: ni_device_api.h:2703
_ni_decoder_config_t::ui8UduSeiEnabled
uint8_t ui8UduSeiEnabled
Definition: ni_device_api_priv.h:724
_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:493
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:2610
_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:802
_ni_session_context::initial_frame_delay
int initial_frame_delay
Definition: ni_device_api.h:1692
_ni_xcoder_params::bitrate
int bitrate
Definition: ni_device_api.h:2729
_ni_encoder_cfg_params::HDR10wy
int HDR10wy
Definition: ni_device_api.h:2281
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:2265
_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:1616
_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:2736
_ni_frame::flags
int flags
Definition: ni_device_api.h:2692
_ni_encoder_cfg_params::conf_win_top
int conf_win_top
Definition: ni_device_api.h:2297
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:12785
_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:16126
_ni_session_context::sei_trailer
uint8_t sei_trailer[NI_CC_SEI_TRAILER_LEN]
Definition: ni_device_api.h:1422
_ni_encoder_config_t::ui8noMbtree
uint8_t ui8noMbtree
Definition: ni_device_api_priv.h:623
QUADRA
#define QUADRA
Definition: ni_defs.h:120
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:2408
_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:1456
NI_PIXEL_PLANAR_FORMAT_TILED4X4
@ NI_PIXEL_PLANAR_FORMAT_TILED4X4
Definition: ni_device_api.h:922
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:448
_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:2614
NI_ENC_REPEAT_HEADERS_ALL_I_FRAMES
#define NI_ENC_REPEAT_HEADERS_ALL_I_FRAMES
Definition: ni_device_api.h:2057
_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:2766
_ni_network_perf_metrics::total_cycles
uint32_t total_cycles
Definition: ni_device_api.h:1386
_ni_decoder_config_t::ui8EnableAdvancedEc
uint8_t ui8EnableAdvancedEc
Definition: ni_device_api_priv.h:735
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:343
_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:1964
_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:17923
NI_PIXEL_PLANAR_FORMAT_PLANAR
@ NI_PIXEL_PLANAR_FORMAT_PLANAR
Definition: ni_device_api.h:921
_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:2674
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:4984
_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:2843
_ni_encoder_config_t::ui8spatialLayersMinusOne
uint8_t ui8spatialLayersMinusOne
Definition: ni_device_api_priv.h:650
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:3644
_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:1497
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:1579
_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:1155
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:1700
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:434
NI_RETCODE_PARAM_ERROR_MN_QP
@ NI_RETCODE_PARAM_ERROR_MN_QP
Definition: ni_defs.h:459
_ni_session_context::last_frame_offset
uint64_t last_frame_offset
Definition: ni_device_api.h:1455
_ni_encoder_cfg_params::forced_header_enable
int forced_header_enable
Definition: ni_device_api.h:2258
_ni_hw_capability::max_number_of_contexts
uint8_t max_number_of_contexts
Definition: ni_device_api.h:1137
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:15673
_ni_session_context::session_statistic
ni_session_statistic_t session_statistic
Definition: ni_device_api.h:1650
_ni_encoder_cfg_params::spatial_layers_ref_base_layer
int spatial_layers_ref_base_layer
Definition: ni_device_api.h:2441
_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:1663
IDENTIFY_DEVICE_R
#define IDENTIFY_DEVICE_R
Definition: ni_nvme.h:688
_ni_scaler_params_t::scaler_param_c
double scaler_param_c
Definition: ni_device_api.h:2571
ni_assert
#define ni_assert(expression)
Definition: ni_defs.h:327
_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:912
_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:1213
_ni_frame::use_long_term_ref
uint8_t use_long_term_ref
Definition: ni_device_api.h:2622
NI_RETCODE_PARAM_ERROR_INTRA_PERIOD
@ NI_RETCODE_PARAM_ERROR_INTRA_PERIOD
Definition: ni_defs.h:445
_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:1683
QUERY_INSTANCE_NL_SIZE_V2_R
#define QUERY_INSTANCE_NL_SIZE_V2_R(sid, instance)
Definition: ni_nvme.h:765
_ni_packet::data_len
uint32_t data_len
Definition: ni_device_api.h:2838