libxcoder  5.5.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 #if defined(__linux__) && defined(XCODER_ENABLE_CPU_AFFINITY)
78 #include <numa.h>
79 #endif
80 
81 //Map the gopPresetIdx to gopSize,
82 //gopPresetIdx -1: Adaptive Gop, using the biggest gopsize 8
83 //gopPresetIdx 0 : Custom Gop as defined by -xcoder-gop
84 //g_map_preset_to_gopsize[lookAheadEnable][gopPresetIdx + 1];
85 //for the not support gopPresetIdx value, such as gopPresetIdx == 2, set g_map_preset_to_gopsize to -1
86 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},
87  {8, 0, -1, -1, -1, 2, 4, -1, -1, 8, 1, -1, -1, -1, -1, -1, -1}};
88 #define MAGIC_P2P_VALUE "p2p"
89 #define AI_MODEL_TYPE_HVSPLUS_FILTER 3
90 
92 {
96 
97 typedef enum
98 {
99  OPT_1 = 1, // is a combination of OPT_3 and OPT_2 in that order
100  OPT_2 = 2, // returns NI_RETCODE_FAILURE after NI_XCODER_FAILURES_MAX attempts, and LRETURNs for failures
101  OPT_3 = 3 // queries session statistics
103 
104 // Preset configuration lookup table
105 typedef struct {
106  int rdoLevel;
113 } PresetConfig;
114 
115 static const PresetConfig preset_configs[] = {
116  [NI_VQ_VERYFAST] = {1, 0, GOP_PRESET_IDX_SP, 1000, 1, 0.0f, -1.0f},
117  [NI_VQ_FASTER] = {2, 0, GOP_PRESET_IDX_SP, 1000, 1, 0.0f, -1.0f},
118  [NI_VQ_FAST] = {3, 0, GOP_PRESET_IDX_SP, 1000, 1, 0.0f, -1.0f},
119  [NI_VQ_MEDIUM] = {1, 16, GOP_PRESET_IDX_DEFAULT, 3000, 0, 0.0f, 0.0f},
120  [NI_VQ_SLOW] = {2, 16, GOP_PRESET_IDX_DEFAULT, 3000, 0, 0.0f, 0.0f},
121  [NI_VQ_SLOWER] = {3, 16, GOP_PRESET_IDX_DEFAULT, 3000, 0, 0.0f, 0.0f},
122  [NI_VQ_VERYSLOW] = {3, 40, GOP_PRESET_IDX_DEFAULT, 3000, 0, 0.0f, 0.0f}
123 };
124 
125 static uint8_t g_itu_t_t35_cc_sei_hdr_hevc[NI_CC_SEI_HDR_HEVC_LEN] = {
126  0x00, 0x00, 0x00, 0x01, // NAL start code 00 00 00 01
127  0x4e,
128  0x01, // nal_unit_header() {forbidden bit=0 nal_unit_type=39,
129  // nuh_layer_id=0 nuh_temporal_id_plus1=1)
130  0x04, // payloadType= 4 (user_data_registered_itu_t_t35)
131  0 + 11, // payLoadSize= ui16Len + 11; to be set (index 7)
132  0xb5, // itu_t_t35_country_code =181 (North America)
133  0x00,
134  0x31, // itu_t_t35_provider_code = 49
135  0x47, 0x41, 0x39,
136  0x34, // ATSC_user_identifier = "GA94"
137  0x03, // ATSC1_data_user_data_type_code=3
138  0 | 0xc0, // (ui16Len/3) | 0xc0 (to be set; index 16) (each CC character
139  //is 3 bytes)
140  0xFF // em_data = 255
141 };
142 
143 static uint8_t g_itu_t_t35_hdr10p_sei_hdr_hevc[NI_HDR10P_SEI_HDR_HEVC_LEN] = {
144  0x00, 0x00, 0x00, 0x01, // NAL start code 00 00 00 01
145  0x4e,
146  0x01, // nal_unit_header() {forbidden bit=0 nal_unit_type=39,
147  // nuh_layer_id=0 nuh_temporal_id_plus1=1)
148  0x04, // payloadType= 4 (user_data_registered_itu_t_t35)
149  0x00, // payLoadSize; to be set (index 7)
150  0xb5, // u8 itu_t_t35_country_code =181 (North America)
151  //0x00,
152  //0x3c, // u16 itu_t_t35_provider_code = 0x003c
153  //0x00,
154  //0x01, // u16 itu_t_t35_provider_oriented_code = 0x0001
155  // payLoadSize count starts from itu_t_t35_provider_code and goes until
156  // and including trailer
157 };
158 
159 static uint8_t g_itu_t_t35_cc_sei_hdr_h264[NI_CC_SEI_HDR_H264_LEN] = {
160  0x00, 0x00, 0x00, 0x01, // NAL start code 00 00 00 01
161  0x06, // nal_unit_header() {forbidden bit=0 nal_ref_idc=0, nal_unit_type=6
162  0x04, // payloadType= 4 (user_data_registered_itu_t_t35)
163  0 + 11, // payLoadSize= ui16Len + 11; to be set (index 6)
164  0xb5, // itu_t_t35_country_code =181 (North America)
165  0x00,
166  0x31, // itu_t_t35_provider_code = 49
167  0x47, 0x41, 0x39,
168  0x34, // ATSC_user_identifier = "GA94"
169  0x03, // ATSC1_data_user_data_type_code=3
170  0 | 0xc0, // (ui16Len/3) | 0xc0 (to be set; index 15) (each CC character
171  //is 3 bytes)
172  0xFF // em_data = 255
173 };
174 
175 static uint8_t g_itu_t_t35_hdr10p_sei_hdr_h264[NI_HDR10P_SEI_HDR_H264_LEN] = {
176  0x00, 0x00, 0x00, 0x01, // NAL start code 00 00 00 01
177  0x06, // nal_unit_header() {forbidden bit=0 nal_ref_idc=0, nal_unit_type=6
178  0x04, // payloadType= 4 (user_data_registered_itu_t_t35)
179  0x00, // payLoadSize; to be set (index 6)
180  0xb5, // itu_t_t35_country_code =181 (North America)
181  //0x00,
182  //0x3c, // u16 itu_t_t35_provider_code = 0x003c
183  //0x00,
184  //0x01, // u16 itu_t_t35_provider_oriented_code = 0x0001
185  // payLoadSize count starts from itu_t_t35_provider_code and goes until
186  // and including trailer
187 };
188 
189 static uint8_t g_sei_trailer[NI_CC_SEI_TRAILER_LEN] = {
190  0xFF, // marker_bits = 255
191  0x80 // RBSP trailing bits - rbsp_stop_one_bit and 7 rbsp_alignment_zero_bit
192 };
193 
194 #define NI_XCODER_FAILURES_MAX 25
195 #define DP_IPC_PASSTHRU 0xFFFFFFFF
196 
197 #ifdef _WIN32
198 #ifdef XCODER_SELF_KILL_ERR
199 #undef XCODER_SELF_KILL_ERR
200 #endif
201 #endif
202 
203 void SwapSWBytes(uint8_t *buf, uint32_t bytes)
204 {
205  uint32_t i;
206  for (i = 0; i < bytes; i += 2)
207  {
208  uint8_t val = buf[i];
209  buf[i] = buf[i + 1];
210  buf[i + 1] = val;
211  }
212 }
213 
214 void SwapSW16(uint16_t *buf, uint32_t bytes)
215 {
216  uint32_t i, words = (bytes + 1) / 2;
217 
218  for (i = 0; i < words; i += 2)
219  {
220  uint16_t val = buf[i];
221  buf[i] = buf[i + 1];
222  buf[i + 1] = val;
223  }
224 }
225 
226 void SwapSW32(uint32_t *buf, uint32_t bytes)
227 {
228  uint32_t i, words = (bytes + 3) / 4;
229  for (i = 0; i < words; i += 2)
230  {
231  uint32_t val = buf[i];
232  buf[i] = buf[i + 1];
233  buf[i + 1] = val;
234  }
235 }
236 
237 void SwapSW64(uint64_t *buf, uint64_t bytes)
238 {
239  uint64_t i, words = (bytes + 7) / 8;
240  for (i = 0; i < words; i += 2)
241  {
242  uint64_t val = buf[i];
243  buf[i] = buf[i + 1];
244  buf[i + 1] = val;
245  }
246 }
247 
248 static void memcpyToPelByBits (uint8_t *dst_u8, uint8_t *src, int width, int height, int dst_stride, int src_stride, int srcValidBitsDepth)
249 {
250  int i, j;
251  uint8_t bitsDepth = srcValidBitsDepth;
252  uint32_t mask = (1<<bitsDepth)-1;
253  uint16_t *dst = (uint16_t *)dst_u8;
254 
255  //fast for byte write
256  if (bitsDepth == 8)
257  {
258  for (j = 0; j < height; j ++)
259  {
260  for (i = 0; i< width; i ++)
261  dst_u8[i] = src[i] & 0xff;
262 
263  src += src_stride;
264  dst_u8 += dst_stride;
265  }
266  return;
267  }
268 
269  //for arbitrary bits
270  for (j = 0; j < height; j ++)
271  {
272  uint8_t *src_line = src;
273  uint32_t cache = 0, cache_bits = 0;
274  for (i = 0; i< width; i ++)
275  {
276  while(cache_bits < bitsDepth)
277  {
278  cache |= ((*src_line++) << cache_bits);
279  cache_bits += 8;
280  }
281 
282  if (cache_bits >= bitsDepth)
283  {
284  dst[i] = cache & mask;
285  cache >>= bitsDepth;
286  cache_bits -= bitsDepth;
287  }
288  }
289 
290  src += src_stride;
291  dst += dst_stride;
292  }
293 }
294 
295 /*
296 static void write_(FILE *fp, uint8_t *data, int32_t width, int32_t height, int32_t stripe)
297 {
298  int32_t i;
299 
300  for (i = 0; i < height; i++)
301  {
302  if (fwrite(data, sizeof(uint8_t ), width, fp) < (size_t)width)
303  {
304  return;
305  }
306  data += stripe;
307  }
308 }
309 */
310 
311 static void trace_recon_tile2raster_y(FILE *fRecon, uint8_t *mem, int width, int height,
312  int leftOffset, int topOffset,
313  int src_stride, int pixDepth, uint8_t *luma_mem_temp)
314 {
315  int i;
316  int write_data_len = 0;
317  int bytesPerPix = (pixDepth == 8 ? 1 : 2);
318  uint8_t *tmp_mem = (uint8_t *)malloc(bytesPerPix*src_stride);
319  int num_tiles = (leftOffset + width + 3) / 4 - leftOffset/4;
320  if(tmp_mem == NULL)
321  return;
322  mem += topOffset/4*4*src_stride + leftOffset/4*16*pixDepth/8;
323  for(i = topOffset/4*4; i < topOffset + height; i += 4) {
324  memcpyToPelByBits(tmp_mem, mem, 4, num_tiles, 4, 16*pixDepth/8, pixDepth);
325  if(i >= topOffset && i < topOffset + height){
326  // write_(fRecon, tmp_mem + (leftOffset & 3)*bytesPerPix, width*bytesPerPix, 1, src_stride);
327  memcpy(luma_mem_temp + write_data_len, tmp_mem + (leftOffset & 3) * bytesPerPix, width * bytesPerPix);
328  write_data_len += width * bytesPerPix;
329  }
330  memcpyToPelByBits(tmp_mem, mem+4*pixDepth/8, 4, num_tiles, 4, 16*pixDepth/8, pixDepth);
331  if(i+1 >= topOffset && i+1 < topOffset + height)
332  {
333  // write_(fRecon, tmp_mem + (leftOffset & 3)*bytesPerPix, width*bytesPerPix, 1, src_stride);
334  memcpy(luma_mem_temp + write_data_len, tmp_mem + (leftOffset & 3) * bytesPerPix, width * bytesPerPix);
335  write_data_len += width * bytesPerPix;
336  }
337  memcpyToPelByBits(tmp_mem, mem+8*pixDepth/8, 4, num_tiles, 4, 16*pixDepth/8, pixDepth);
338  if(i+2 >= topOffset && i+2 < topOffset + height)
339  {
340  // write_(fRecon, tmp_mem + (leftOffset & 3)*bytesPerPix, width*bytesPerPix, 1, src_stride);
341  memcpy(luma_mem_temp + write_data_len, tmp_mem + (leftOffset & 3) * bytesPerPix, width * bytesPerPix);
342  write_data_len += width * bytesPerPix;
343  }
344  memcpyToPelByBits(tmp_mem, mem+12*pixDepth/8, 4, num_tiles, 4, 16*pixDepth/8, pixDepth);
345  if(i+3 >= topOffset && i+3 < topOffset + height)
346  {
347  // write_(fRecon, tmp_mem + (leftOffset & 3)*bytesPerPix, width*bytesPerPix, 1, src_stride);
348  memcpy(luma_mem_temp + write_data_len, tmp_mem + (leftOffset & 3) * bytesPerPix, width * bytesPerPix);
349  write_data_len += width * bytesPerPix;
350  }
351  mem += 4*src_stride;
352  }
353  free(tmp_mem);
354 }
355 
356 static void trace_recon_tile2raster_uv(FILE *fRecon, uint8_t *mem_uv, int width, int height,
357  int leftOffset, int topOffset,
358  int src_stride, int pixDepth, uint8_t *ch_mem_temp)
359 {
360  int i, j, k;
361  int bytesPerPix = (pixDepth == 8 ? 1 : 2);
362  uint8_t *tmp_mem = (uint8_t *)malloc(bytesPerPix*src_stride*2);
363  int num_tiles = 2*((leftOffset + width + 3) / 4 - leftOffset/4);
364  if(tmp_mem == NULL)
365  return;
366  uint8_t *mem;
367  int uv;
368  int write_data_len = 0;
369  memset(tmp_mem, 0x00, bytesPerPix * src_stride * 2);
370  for(uv = 0; uv <= 1; uv ++) {
371  mem = mem_uv + topOffset/4*8*src_stride + leftOffset/4*32*pixDepth/8;
372  for(i = topOffset/4*4; i < topOffset + height; i += 4) {
373  if(i >= topOffset && i < topOffset + height) {
374  memcpyToPelByBits(tmp_mem, mem, 4, num_tiles, 4, 16*pixDepth/8, pixDepth);
375  for(j = uv, k = 0; j < 4 * num_tiles; j += 2, k++) {
376  if(bytesPerPix == 1)
377  tmp_mem[k] = tmp_mem[j];
378  else
379  ((uint16_t*)tmp_mem)[k] = ((uint16_t*)tmp_mem)[j];
380  }
381  // write_(fRecon, tmp_mem + (leftOffset & 3)*bytesPerPix, width*bytesPerPix, 1, src_stride);
382  memcpy(ch_mem_temp + write_data_len, tmp_mem + (leftOffset & 3) * bytesPerPix, width * bytesPerPix);
383  write_data_len += width * bytesPerPix;
384  }
385  if(i+1 >= topOffset && i+1 < topOffset + height) {
386  memcpyToPelByBits(tmp_mem, mem+4*pixDepth/8, 4, num_tiles, 4, 16*pixDepth/8, pixDepth);
387  for(j = uv, k = 0; j < 4 * num_tiles; j += 2, k++) {
388  if(bytesPerPix == 1)
389  tmp_mem[k] = tmp_mem[j];
390  else
391  ((uint16_t*)tmp_mem)[k] = ((uint16_t*)tmp_mem)[j];
392  }
393  // write_(fRecon, tmp_mem + (leftOffset & 3)*bytesPerPix, width*bytesPerPix, 1, src_stride);
394  memcpy(ch_mem_temp + write_data_len, tmp_mem + (leftOffset & 3) * bytesPerPix, width * bytesPerPix);
395  write_data_len += width * bytesPerPix;
396  }
397  if(i+2 >= topOffset && i+2 < topOffset + height) {
398  memcpyToPelByBits(tmp_mem, mem+8*pixDepth/8, 4, num_tiles, 4, 16*pixDepth/8, pixDepth);
399  for(j = uv, k = 0; j < 4 * num_tiles; j += 2, k++) {
400  if(bytesPerPix == 1)
401  tmp_mem[k] = tmp_mem[j];
402  else
403  ((uint16_t*)tmp_mem)[k] = ((uint16_t*)tmp_mem)[j];
404  }
405  // write_(fRecon, tmp_mem + (leftOffset & 3)*bytesPerPix, width*bytesPerPix, 1, src_stride);
406  memcpy(ch_mem_temp + write_data_len, tmp_mem + (leftOffset & 3) * bytesPerPix, width * bytesPerPix);
407  write_data_len += width * bytesPerPix;
408  }
409  memcpyToPelByBits(tmp_mem, mem+12*pixDepth/8, 4, num_tiles, 4, 16*pixDepth/8, pixDepth);
410  if(i+3 >= topOffset && i+3 < topOffset + height) {
411  for(j = uv, k = 0; j < 4 * num_tiles; j += 2, k++) {
412  if(bytesPerPix == 1)
413  tmp_mem[k] = tmp_mem[j];
414  else
415  ((uint16_t*)tmp_mem)[k] = ((uint16_t*)tmp_mem)[j];
416  }
417  // write_(fRecon, tmp_mem + (leftOffset & 3)*bytesPerPix, width*bytesPerPix, 1, src_stride);
418  memcpy(ch_mem_temp + write_data_len, tmp_mem + (leftOffset & 3) * bytesPerPix, width * bytesPerPix);
419  write_data_len += width * bytesPerPix;
420  }
421  mem += 8*src_stride;
422  }
423  }
424  free(tmp_mem);
425 }
426 
427 static double calc_noise(uint8_t *p1, uint8_t *p2, int size)
428 {
429  double sum;
430  double diff;
431  int i;
432 
433  sum = 0;
434  for (i = 0; i < size; i++)
435  {
436  diff = p1[i] - p2[i];
437  sum += diff * diff;
438  }
439 
440  return sum;
441 }
442 
443 /*!*****************************************************************************
444  * \brief Download hw frames by HwDesc.
445  *
446  * \param
447  *
448  * \return 0 if successful, < 0 otherwise
449  ******************************************************************************/
450 static int hwdl_frame(ni_session_context_t *p_ctx,
451  ni_session_data_io_t *p_session_data, ni_frame_t *p_src_frame,
452  int pixel_format)
453 {
454  niFrameSurface1_t *src_surf = (niFrameSurface1_t *)(p_src_frame->p_data[3]);
455  int ret = 0;
456 
457  ret = ni_frame_buffer_alloc_dl(&(p_session_data->data.frame),
458  src_surf->ui16width, src_surf->ui16height,
459  pixel_format);
460 
461  if (ret != NI_RETCODE_SUCCESS)
462  {
464  }
465 
466  p_ctx->is_auto_dl = false;
467  ret = ni_device_session_hwdl(p_ctx, p_session_data, src_surf);
468  if (ret <= 0)
469  {
470  ni_frame_buffer_free(&p_session_data->data.frame);
471  return ret;
472  }
473  return ret;
474 }
475 
477 {
478  // char dump_file_tile[256];
479  // char dump_file_raster[256];
480  //FILE *fout_tile = NULL;
481  FILE *fout_raster = NULL;
482  // snprintf(dump_file_tile, sizeof(dump_file_tile), "output-%04ld-tile.yuv", (long)p_ctx->pkt_num - 1);
483  // snprintf(dump_file_raster, sizeof(dump_file_raster), "output-%04ld-raster.yuv", (long)p_ctx->pkt_num - 1);
484  // fout_tile = fopen(dump_file_tile, "wb");
485  // fout_raster = fopen(dump_file_raster, "wb");
486  // FILE *f_out = fopen("out.yuv", "a");
487  int bit_depth = (p_ctx->pixel_format == NI_PIX_FMT_YUV420P10LE || p_ctx->pixel_format == NI_PIX_FMT_P010LE) ? 10 : 8;
488 
490 
491  // only h.265 psnr_y is supported by HW
492  p_ctx->psnr_y = p_packet->psnr_y = (float)(p_meta->ui16psnr_y / 1000.0);
493 
494  if (((ni_xcoder_params_t *)(p_ctx->p_session_config))->cfg_enc_params.get_psnr_mode != 3 &&
495  (p_meta->reconLumaSize != 0 || p_meta->reconChromaSize != 0))
496  {
497  uint32_t lum_sz = 0;
498  uint32_t chr_sz = 0;
499  uint8_t *luma_mem_temp = NULL;
500  uint8_t *ch_mem_temp = NULL;
501  int width = p_ctx->actual_video_width;
502  int height = p_ctx->active_video_height;//p_meta->reconLumaSize / p_meta->reconLumaWidth;
503 
504  uint8_t *luma_mem = (uint8_t *)p_packet->p_data + p_packet->data_len - p_meta->reconChromaSize - p_meta->reconLumaSize;
505  uint8_t *ch_mem = (uint8_t *)p_packet->p_data + p_packet->data_len - p_meta->reconChromaSize;
506 
507  int bit_scale = (p_ctx->pixel_format == NI_PIX_FMT_YUV420P10LE || p_ctx->pixel_format == NI_PIX_FMT_P010LE) ? 5 : 4;
508  lum_sz = (2 * p_meta->frameCropTopOffset + height + 3) / 4 * p_meta->reconLumaWidth * bit_scale;
509  chr_sz = (p_meta->frameCropTopOffset + height / 2 + 1) / 2 * p_meta->reconChromaWidth * bit_scale;
510  if (p_meta->reconLumaSize)
511  {
512  uint32_t luma_size = (bit_depth == 8) ? p_meta->reconLumaSize : p_meta->reconLumaSize * 2;
513  luma_mem_temp = (uint8_t *)malloc(luma_size);
514  memset(luma_mem_temp, 0x00, luma_size);
515  }
516  if (p_meta->reconChromaSize)
517  {
518  uint32_t ch_size = (bit_depth == 8) ? p_meta->reconChromaSize : p_meta->reconChromaSize * 2;
519  ch_mem_temp = (uint8_t *)malloc(ch_size);
520  memset(ch_mem_temp, 0x00, ch_size);
521  }
525  if (p_meta->reconLumaSize != 0)
526  {
527  SwapSWBytes(luma_mem, lum_sz);
528  SwapSW16((uint16_t *)luma_mem, lum_sz);
529  SwapSW32((uint32_t *)luma_mem, lum_sz);
530  SwapSW64((uint64_t *)luma_mem, lum_sz);
531  trace_recon_tile2raster_y (fout_raster, luma_mem, width, height,
532  2 * p_meta->frameCropLeftOffset, 2 * p_meta->frameCropTopOffset,
533  p_meta->reconLumaWidth, bit_depth, luma_mem_temp);
534  ni_log2(p_ctx, NI_LOG_DEBUG, "lum_sz %d reconLumaSize %d width %d reconLumaWidth %d\n",
535  lum_sz, p_meta->reconLumaSize, width, p_meta->reconLumaWidth);
536  // fflush(fout_raster);
537  }
538 
539  if (p_meta->reconChromaSize != 0)
540  {
541  SwapSWBytes(ch_mem, chr_sz);
542  SwapSW16((uint16_t *)ch_mem, chr_sz);
543  SwapSW32((uint32_t *)ch_mem, chr_sz);
544  SwapSW64((uint64_t *)ch_mem, chr_sz);
545  trace_recon_tile2raster_uv(fout_raster, ch_mem, width / 2, height / 2,
546  p_meta->frameCropLeftOffset, p_meta->frameCropTopOffset,
547  p_meta->reconChromaWidth, bit_depth, ch_mem_temp);
548  ni_log2(p_ctx, NI_LOG_DEBUG, "chr_sz %d reconChromaSize %d reconChromaWidth %d\n",
549  chr_sz, p_meta->reconChromaSize, p_meta->reconChromaWidth);
550  // fflush(fout_raster);
551  }
552 
553  // fwrite((uint8_t *)p_packet->p_data + sizeof(ni_metadata_enc_bstream_t) + (p_packet->data_len - p_meta->reconLumaSize - p_meta->reconChromaSize),
554  // p_meta->reconLumaSize + p_meta->reconChromaSize, 1, fout_tile);
555 
559  if (((ni_xcoder_params_t *)(p_ctx->p_session_config))->cfg_enc_params.get_psnr_mode < 3)
560  {
561  do
562  {
563  int max;
564  double noise_y = 0.0, noise_u = 0.0, noise_v = 0.0, noise_yuv = 0.0;
565  double power_y = 0.0, power_u = 0.0, power_v = 0.0, power_yuv = 0.0;
566  double psnr_y = 0.0, psnr_u = 0.0, psnr_v = 0.0, psnr_yuv = 0.0;
567 
569  // FILE *f_out_raster = (FILE *)fopen(dump_file_raster, "rb");
570  // fread(luma_mem_temp, width * height , 1, f_out_raster);
571  // fread(ch_mem_temp, width * height / 2, 1, f_out_raster);
572  // fflush(f_out_raster);
573  // fclose(f_out_raster);
574 
575  max = (1 << 8) - 1;
576  power_y = 1.0 * max * max * width * height;
577  power_u = power_y / 4.0;
578  power_v = power_y / 4.0;
579  power_yuv = power_y * 3.0 / 2.0;
580 
581  uint8_t *p_y_src_buf = NULL;
582  uint8_t *p_u_src_buf = NULL;
583  uint8_t *p_v_src_buf = NULL;
584  for (int i = 0; i < 120; i++)
585  {
586  if (p_ctx->input_frame_fifo[i].p_input_buffer != NULL &&
587  p_ctx->input_frame_fifo[i].pts == p_meta->frame_tstamp)
588  {
589  p_y_src_buf = p_ctx->input_frame_fifo[i].p_input_buffer;
590  p_u_src_buf = p_y_src_buf + p_ctx->input_frame_fifo[i].video_width * p_ctx->input_frame_fifo[i].video_height;
591  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);
592 
593  p_ctx->input_frame_fifo[i].usable = 1;
594  ni_log2(p_ctx, NI_LOG_DEBUG, "%s %d i %d pkt_num %ld frame_num %ld frame_tstamp %lld\n",
595  __FUNCTION__, __LINE__, i, p_ctx->pkt_num, p_ctx->frame_num, p_meta->frame_tstamp);
596  break;
597  }
598  }
599  if (p_y_src_buf != NULL)
600  {
601  if (p_meta->reconLumaSize != 0)
602  {
603  noise_y = calc_noise(p_y_src_buf, luma_mem_temp, width * height);
604  }
605  else if (p_meta->ui16psnr_y != 0)
606  {
607  noise_y = power_y / pow(10, p_ctx->psnr_y / 10); // This is used for calculation noise_yuv for psnr_yuv
608  }
609 
610  if (p_meta->reconChromaSize != 0)
611  {
612  noise_u = calc_noise(p_u_src_buf, ch_mem_temp, (width / 2) * (height / 2));
613  noise_v = calc_noise(p_v_src_buf, ch_mem_temp + (width / 2) * (height / 2), (width / 2) * (height / 2));
614  }
615  noise_yuv = noise_y + noise_u + noise_v;
616 
617  //This is for the case when encoding some very simple picture
618  //this case the quality is very good. sometimes the noise_y may be zero
619  noise_y = noise_y < 1 ? 1 : noise_y;
620  noise_u = noise_u < 1 ? 1 : noise_u;
621  noise_v = noise_v < 1 ? 1 : noise_v;
622  noise_yuv = noise_yuv < 1 ? 1 : noise_yuv;
623 
624  if (p_meta->reconLumaSize != 0)
625  {
626  psnr_y = 10 * log10(power_y / noise_y);
627  }
628  else if (p_meta->ui16psnr_y != 0)
629  {
630  psnr_y = p_ctx->psnr_y;
631  }
632 
633  if (p_meta->reconChromaSize != 0)
634  {
635  psnr_u = 10 * log10(power_u / noise_u);
636  psnr_v = 10 * log10(power_v / noise_v);
637  }
638  psnr_yuv = 10 * log10(power_yuv / noise_yuv);
639  p_ctx->psnr_y = p_packet->psnr_y = psnr_y;
640  p_ctx->psnr_u = p_packet->psnr_u = psnr_u;
641  p_ctx->psnr_v = p_packet->psnr_v = psnr_v;
642  p_ctx->average_psnr = p_packet->average_psnr = psnr_yuv;
643  }
644  } while(0);
645  }
646  else
647  {
648  // get recontructed frame here
649  // int h = 0;
650  // luma
651  // for (h = 0; h < height; h++)
652  // {
653  // fwrite(luma_mem_temp + (width * h), 1, width, f_out);
654  // }
655  //chroma
656  // for (h = 0; h < height; h++)
657  // {
658  // fwrite(ch_mem_temp + ((width / 2) * h), 1, width / 2, f_out);
659  // }
660  // fclose(f_out);
661  }
662 
663  if (luma_mem_temp)
664  {
665  free(luma_mem_temp);
666  luma_mem_temp = NULL;
667  }
668  if (ch_mem_temp)
669  {
670  free(ch_mem_temp);
671  ch_mem_temp = NULL;
672  }
673  // fflush(fout_tile);
674  // fclose(fout_tile);
675  // fflush(fout_raster);
676  // fclose(fout_raster);
677  //}
678  }
679 
680  if(p_meta->reconChromaSize == 0)
681  {
682  p_ctx->psnr_u = p_ctx->psnr_v = 0;
683  p_ctx->average_psnr = p_ctx->psnr_y;
684  }
685  if (p_meta->reconLumaSize == 0 && p_meta->ui16psnr_y == 0)
686  {
687  p_ctx->psnr_y = 0;
688  p_ctx->average_psnr = (p_ctx->psnr_u + p_ctx->psnr_v) / 2;
689  }
690 
691 
692  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",
693  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);
694 
695  p_packet->data_len -= p_meta->reconLumaSize + p_meta->reconChromaSize;
696 }
697 
698 #if 0 // CU info parsing (currently not used)
699 static void parse_cu_info(ni_session_context_t *p_ctx, uint8_t *cuInfoBuf, ni_encoder_cu_info *pEncCuInfo)
700 {
701  int data;
702 
703  if (!p_ctx || !cuInfoBuf || !pEncCuInfo)
704  {
705  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): passed parameters are null!, return\n",
706  __func__);
707  return;
708  }
709 
710  // AVC
711  if (p_ctx->codec_format == NI_CODEC_FORMAT_H264)
712  {
713  pEncCuInfo->cuSize = 16;
714  pEncCuInfo->cuMode = (cuInfoBuf[AVC_MB_MODE_BYTE_OFFSET] >> AVC_MB_MODE_BIT_POSITION) & ((1 << AVC_MB_MODE_BITS) - 1);
715  if (pEncCuInfo->cuMode == 1) // intra
716  {
718  pEncCuInfo->costIntraSatd = (cuInfoBuf[INTRACOST_BYTE_OFFSET] >> INTRACOST_BIT_POSITION) & ((1 << INTRACOST_BITS) - 1);
719  pEncCuInfo->costInterSatd = (cuInfoBuf[INTERCOST_BYTE_OFFSET] >> INTERCOST_BIT_POSITION) & ((1 << INTERCOST_BITS) - 1);
720  pEncCuInfo->cost = (cuInfoBuf[AVC_RDCOST_BYTE_OFFSET] >> AVC_RDCOST_BIT_POSITION) & ((1 << AVC_RDCOST_BITS) - 1);
721  pEncCuInfo->costOfOtherMode = (cuInfoBuf[RDCOST_OTHER_BYTE_OFFSET] >> RDCOST_OTHER_BIT_POSITION) & ((1 << RDCOST_OTHER_BITS) - 1);
722  }
723  else
725  }
726  // HEVC & AV1
727  else
728  {
729  uint8_t cuSizeId = pEncCuInfo->cuSize = (1 << (((cuInfoBuf[CU_SIZE_BYTE_OFFSET] >> CU_SIZE_BIT_POSITION) & ((1 << CU_SIZE_BITS) - 1)) + 3));
730  pEncCuInfo->cuSize = 8 * (1 << cuSizeId);
731  pEncCuInfo->cuMode = (cuInfoBuf[CU_MODE_BYTE_OFFSET] >> CU_MODE_BIT_POSITION) & ((1 << CU_MODE_BITS) - 1);
732  if (pEncCuInfo->cuMode == 1) // intra
734  else
735  pEncCuInfo->interPredIdc = (cuInfoBuf[INTER_PRED_IDC_BYTE_OFFSET] >> INTER_PRED_IDC_BIT_POSITION) & ((1 << INTER_PRED_IDC_BITS) - 1);
736  }
737 
738  data = cuInfoBuf[QP_BYTE_OFFSET] | (cuInfoBuf[QP_BYTE_OFFSET+1] << 8);
739  pEncCuInfo->qp = (data >> QP_BIT_POSITION) & ((1 << QP_BITS) - 1);
740 }
741 #endif
742 
743 // Check for critical failures.
744 // Invalid parameters or resource busy do not account for failures that cause error count to be incremented so they can be retried indefinitely
745 static ni_retcode_t check_err_rc(
746  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)
747 {
748  ni_retcode_t retval = rc;
749  ni_session_stats_t stat = {0};
750  uint16_t ui16ErrorCount = 0;
751  uint32_t ui32LastErrorStatus = 0;
752 
753  if(opt == OPT_1 || opt == OPT_3)
754  {
755  retval = ni_query_session_stats(ctx, type, &stat, rc, opcode);
756  ui16ErrorCount = stat.ui16ErrorCount;
757  ui32LastErrorStatus = stat.ui32LastErrorStatus;
758  }
759 
760  if (retval != NI_RETCODE_SUCCESS)
761  {
762  ni_log2(ctx, NI_LOG_ERROR, "Query for statistics failed with %d!\n", retval);
763  return NI_RETCODE_FAILURE;
764  }
765 
766  if(opt == OPT_1)
767  {
768  retval = ni_nvme_check_error_code(stat.ui32LastTransactionCompletionStatus, opcode, type, hw_id, inst_id);
769 
770  if(!retval)
771  retval = ni_nvme_check_error_code(ui32LastErrorStatus, opcode, type, hw_id, inst_id);
772  }
773  else if(opt == OPT_2){
774  if(!stat_full){
775  ni_log2(ctx, NI_LOG_ERROR, "ERROR %s(): passed parameters are null!, return\n", __func__);
777  }
778  retval = ni_nvme_check_error_code(stat_full->ui32LastTransactionCompletionStatus, opcode, type, hw_id, inst_id);
779  ui16ErrorCount = stat_full->ui16ErrorCount;
780  ui32LastErrorStatus = stat_full->ui32LastErrorStatus;
781  /* re-query error status for transactionId 0xFFFF */
782  if(stat_full->ui32LastErrorTransactionId == 0xFFFF &&
783  ui16ErrorCount > 0 && !retval &&
784  type == NI_DEVICE_TYPE_DECODER)
785  {
786  retval = ni_nvme_check_error_code(ui32LastErrorStatus, opcode, type, hw_id, inst_id);
787  }
788  }
789  else if(opt == OPT_3)
790  {
791  retval = ni_nvme_check_error_code(stat.ui32LastTransactionCompletionStatus, opcode, type, hw_id, inst_id);
792  }
793 
794  if (retval)
795  {
796  ni_log2(ctx,
797  NI_LOG_ERROR,
798  "Persistent failures detected, %s() line-%d: session_no 0x%x "
799  "sess_err_count %u inst_err_no %u\n",
800  func, line, *(inst_id), ui16ErrorCount,
801  ui32LastErrorStatus);
802 #ifdef XCODER_SELF_KILL_ERR
803  if(opt != OPT_3)
804  {
805  kill(getpid(), SIGTERM);
806  }
807 #endif
808  }
809 
810  return retval;
811 }
812 
813 //Following macros will only check for critical failures.
814 //After the macro runs, rc should contain the status of the last command sent
815 //For non-critical failures, it is assumed in the code that eventually after enough retries, a command will succeed.
816 //Invalid parameters or resource busy do not account for failures that cause error count to be incremented so they can be retried indefinitely
817 #define CHECK_ERR_RC(ctx, rc, info, opcode, type, hw_id, inst_id, opt) \
818  { \
819  (rc) = check_err_rc(ctx, rc, info, opcode, type, hw_id, inst_id, opt, __func__, __LINE__); \
820  if((rc) && ((opt) != OPT_3)) LRETURN; \
821  }
822 
823 #define CHECK_VPU_RECOVERY(ret) \
824  { \
825  if (NI_RETCODE_NVME_SC_VPU_RECOVERY == (ret)) \
826  { \
827  ni_log(NI_LOG_ERROR, "Error, vpu reset.\n"); \
828  (ret) = NI_RETCODE_ERROR_VPU_RECOVERY; \
829  LRETURN; \
830  } \
831  }
832 
833 // ctx->decoder_low_delay is used as condition wait timeout for both decoder
834 // and encoder send/recv multi-thread in low delay mode.
835 static void low_delay_wait(ni_session_context_t* p_ctx)
836 {
837  const char *name = p_ctx->device_type == NI_DEVICE_TYPE_DECODER ? \
838  "decoder" : "encoder";
839  if (p_ctx->async_mode && p_ctx->decoder_low_delay > 0)
840  {
841  int ret;
842  uint64_t abs_time_ns;
843  struct timespec ts;
844 
845  ni_log2(p_ctx, NI_LOG_DEBUG, "%s waiting for %s recv thread\n", __FUNCTION__, name);
846 
847  abs_time_ns = ni_gettime_ns();
848  abs_time_ns += p_ctx->decoder_low_delay * 1000000LL;
849  ts.tv_sec = abs_time_ns / 1000000000LL;
850  ts.tv_nsec = abs_time_ns % 1000000000LL;
851 
853  while (p_ctx->low_delay_sync_flag)
854  {
855  // In case of dead lock on waiting for notification from recv thread.
858  &p_ctx->low_delay_sync_mutex, &ts);
859  ni_pthread_mutex_lock(&p_ctx->mutex);
860  if (ret == ETIMEDOUT)
861  {
862  p_ctx->low_delay_sync_flag = 0;
863  }
864  }
866  }
867 }
868 
869 static void low_delay_signal(ni_session_context_t* p_ctx)
870 {
871  const char *name = p_ctx->device_type == NI_DEVICE_TYPE_DECODER ? \
872  "decoder" : "encoder";
873  if (p_ctx->async_mode && p_ctx->decoder_low_delay > 0)
874  {
875  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: wake up %s send thread\n", __FUNCTION__, name);
877  p_ctx->low_delay_sync_flag = 0;
880  }
881 }
882 
883 static void query_sleep(ni_session_context_t* p_ctx)
884 {
885  if (p_ctx->async_mode)
886  {
887  // To avoid IO spam on NP core from queries and high volumens on latency.
890  ni_pthread_mutex_lock(&p_ctx->mutex);
891  }
892 }
893 
894 // create folder bearing the card name (nvmeX) if not existing
895 // start working inside this folder: nvmeX
896 // find the earliest saved and/or non-existing stream folder and use it as
897 // the pkt saving destination; at most 128 such folders to be checked/created;
898 // folder name is in the format of: streamY, where Y is [1, 128]
899 static void decoder_dump_dir_open(ni_session_context_t *p_ctx)
900 {
901 #ifdef _WIN32
902 #elif __linux__ || __APPLE__
903  FILE *fp = NULL;
904  char dir_name[128] = {0};
905  char file_name[512] = {0};
906  ni_device_context_t *p_device_context;
907  DIR *dir;
908  struct dirent *stream_folder;
909  int curr_stream_idx = 0;
910  int earliest_stream_idx = 0;
911  int max_exist_idx = 0;
912  time_t earliest_time = 0;
913  struct stat file_stat;
914 
915  p_device_context =
917  if (!p_device_context)
918  {
919  ni_log2(p_ctx, NI_LOG_ERROR, "Error retrieve device context for decoder guid %d\n",
920  p_ctx->hw_id);
921  return;
922  }
923 
924  flock(p_device_context->lock, LOCK_EX);
925 
926  ni_strcpy(dir_name, sizeof(dir_name), &p_ctx->dev_xcoder_name[5]);
927  if (0 != access(dir_name, F_OK))
928  {
929  if (0 != mkdir(dir_name, S_IRWXU | S_IRWXG | S_IRWXO))
930  {
931  ni_log2(p_ctx, NI_LOG_ERROR, "Error create folder %s, errno %d\n", dir_name,
932  NI_ERRNO);
933  } else
934  {
935  ni_log2(p_ctx, NI_LOG_DEBUG, "Created pkt folder for: %s\n", dir_name);
936  }
937  }
938 
939  if (NULL == (dir = opendir(dir_name)))
940  {
941  ni_log2(p_ctx, NI_LOG_ERROR, "Error %d: failed to open directory %s\n",
942  NI_ERRNO, dir_name);
943  } else
944  {
945  // have a quick first pass of streamY folders, and if existing Y < 128,
946  // create a new folder Y+1 directly without checking existing ones'
947  // content
948  while ((stream_folder = readdir(dir)))
949  {
950  if (!strncmp(stream_folder->d_name, "stream", strlen("stream")))
951  {
952  curr_stream_idx =
953  atoi(&(stream_folder->d_name[strlen("stream")]));
954  if (curr_stream_idx > 0)
955  {
956  if (curr_stream_idx > max_exist_idx)
957  {
958  max_exist_idx = curr_stream_idx;
959  }
960  if (NI_MAX_CONTEXTS_PER_HW_INSTANCE == curr_stream_idx)
961  {
962  break;
963  }
964  }
965  }
966  }
967 
968  // if less than 128 streams created then create a new one, otherwise have
969  // to pick the stream folder that has the earliest modified file which
970  // is most likely done by finished session.
971  if (max_exist_idx < NI_MAX_CONTEXTS_PER_HW_INSTANCE)
972  {
973  curr_stream_idx = max_exist_idx + 1;
974  } else
975  {
976  rewinddir(dir);
977  while ((stream_folder = readdir(dir)))
978  {
979  // go through each of these streamY folders and get modified
980  // time of the first pkt-* file to simplify the searching
981  if (!strncmp(stream_folder->d_name, "stream", strlen("stream")))
982  {
983  snprintf(file_name, sizeof(file_name), "%s/%s/pkt-0001.bin",
984  dir_name, stream_folder->d_name);
985 
986  curr_stream_idx =
987  atoi(&(stream_folder->d_name[strlen("stream")]));
988 
989  if (curr_stream_idx > 0 && 0 == access(file_name, F_OK))
990  {
991  // just take pkt-0001 file timestamp to simplify search
992  if (stat(file_name, &file_stat))
993  {
994  ni_log2(p_ctx, NI_LOG_ERROR, "Error %d: failed to stat file %s\n",
995  NI_ERRNO,
996  file_name);
997  } else
998  {
999  if (0 == earliest_stream_idx ||
1000  file_stat.st_mtime < earliest_time)
1001  {
1002  earliest_stream_idx = curr_stream_idx;
1003  earliest_time = file_stat.st_mtime;
1004  }
1005  }
1006  } // check first file in streamX
1007  } // go through each streamX folder
1008  } // read all files in nvmeY
1009 
1010  curr_stream_idx = earliest_stream_idx;
1011 
1012  // set the access/modified time of chosen pkt file to NOW so its
1013  // stream folder won't be taken by other sessions.
1014  snprintf(file_name, sizeof(file_name), "%s/stream%03d/pkt-0001.bin",
1015  dir_name, curr_stream_idx);
1016  if (utime(file_name, NULL))
1017  {
1018  ni_log2(p_ctx, NI_LOG_ERROR, "Error utime %s\n", file_name);
1019  }
1020  } // 128 streams in nvmeY already
1021  closedir(dir);
1022  }
1023 
1024  snprintf(p_ctx->stream_dir_name, sizeof(p_ctx->stream_dir_name),
1025  "%s/stream%03d", dir_name, curr_stream_idx);
1026 
1027  if (0 != access(p_ctx->stream_dir_name, F_OK))
1028  {
1029  if (0 != mkdir(p_ctx->stream_dir_name, S_IRWXU | S_IRWXG | S_IRWXO))
1030  {
1031  ni_log2(p_ctx, NI_LOG_ERROR, "Error create stream folder %s, errno %d\n",
1032  p_ctx->stream_dir_name, NI_ERRNO);
1033  } else
1034  {
1035  ni_log2(p_ctx, NI_LOG_DEBUG, "Created stream sub folder: %s\n",
1036  p_ctx->stream_dir_name);
1037  }
1038  } else
1039  {
1040  ni_log2(p_ctx, NI_LOG_DEBUG, "Reusing stream sub folder: %s\n",
1041  p_ctx->stream_dir_name);
1042  }
1043 
1044  flock(p_device_context->lock, LOCK_UN);
1045  ni_rsrc_free_device_context(p_device_context);
1046 
1047  snprintf(file_name, sizeof(file_name), "%s/process_session_id.txt",
1048  p_ctx->stream_dir_name);
1049 
1050  ni_fopen(&fp, file_name, "wb");
1051  if (fp)
1052  {
1053  char number[64] = {'\0'};
1054  ni_log2(p_ctx, NI_LOG_DEBUG, "Decoder pkt dump log created: %s\n", file_name);
1055  snprintf(number, sizeof(number), "proc id: %ld\nsession id: %u\n",
1056  (long)getpid(), p_ctx->session_id);
1057  fwrite(number, strlen(number), 1, fp);
1058  fclose(fp);
1059  } else
1060  {
1061  ni_log2(p_ctx, NI_LOG_ERROR, "Error create decoder pkt dump log: %s\n", file_name);
1062  }
1063 #endif
1064 }
1065 
1066 #if !defined(_WIN32) && !defined(__APPLE__) && !defined(__OPEN_HARMONY__) && !defined(_ANDROID)
1067 #define MAX_BDF_LEN 16
1068 
1069 // dev_name pattern: nvmeXnY
1070 // pci_addr pattern: dddd:bb:dd.f
1071 static int find_pci_address(const char *dev_name, char *pci_addr) {
1072  char sys_block_path[256];
1073 
1074  snprintf(sys_block_path, sizeof(sys_block_path), "/sys/class/block/%s/device", dev_name);
1075 
1076  char *real_sysfs_path = realpath(sys_block_path, NULL);
1077  if (!real_sysfs_path) {
1078  perror("realpath failed");
1079  return -1;
1080  }
1081 
1082  // Walk backward in the resolved path to find a segment that matches PCI BDF format
1083  char *p = real_sysfs_path + strlen(real_sysfs_path);
1084 
1085  while (p > real_sysfs_path) {
1086  p = strrchr(real_sysfs_path, '/');
1087  if (!p) break;
1088 
1089  char segment[MAX_BDF_LEN];
1090  ni_strncpy(segment, MAX_BDF_LEN, p + 1, (MAX_BDF_LEN-1));
1091  segment[MAX_BDF_LEN - 1] = '\0';
1092 
1093  unsigned domain, bus, dev, func;
1094  if (sscanf(segment, "%x:%x:%x.%x", &domain, &bus, &dev, &func) == 4) {
1095  ni_strncpy(pci_addr, MAX_BDF_LEN, segment, MAX_BDF_LEN - 1);
1096  pci_addr[MAX_BDF_LEN - 1] = '\0';
1097  free(real_sysfs_path);
1098  return 0;
1099  }
1100 
1101  *p = '\0'; // truncate path to move to the previous segment
1102  }
1103 
1104  free(real_sysfs_path);
1105  return -1;
1106 }
1107 
1108 // open netint p2p driver and fill the pcie address to p_ctx
1109 static ni_retcode_t p2p_fill_pcie_address(ni_session_context_t *p_ctx)
1110 {
1111 #if defined(_WIN32) || defined(__APPLE__) || defined(__OPEN_HARMONY__)
1112  (void)p_ctx;
1113  return NI_RETCODE_FAILURE;
1114 #else
1115  int ret = 0;
1116  char pci_addr[16];
1117  struct stat bstat;
1118  char *p_dev;
1119  char *dom, *bus, *dev, *fnc;
1120 #ifdef _ANDROID
1121  char syspath[256];
1122  FILE *fp;
1123 #endif
1124 
1125  if(!p_ctx)
1126  {
1127  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() parameter is NULL\n",__func__);
1128  return NI_RETCODE_FAILURE;
1129  }
1130 
1131  p_ctx->netint_fd = open("/dev/netint", O_RDWR);
1132  if (p_ctx->netint_fd < 0)
1133  {
1134  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: Can't open device /dev/netint\n");
1135  return NI_RETCODE_FAILURE;
1136  }
1137 
1138  p_dev = &p_ctx->dev_xcoder_name[0];
1139  if (stat(p_dev, &bstat) < 0)
1140  {
1141  ni_log2(p_ctx, NI_LOG_ERROR, "failed to get stat of file %s\n", p_dev);
1142  return NI_RETCODE_FAILURE;
1143  }
1144 
1145  if ((bstat.st_mode & S_IFMT) != S_IFBLK)
1146  {
1147  ni_log2(p_ctx, NI_LOG_ERROR, "%s is not a block device\n", p_dev);
1148  return NI_RETCODE_FAILURE;
1149  }
1150 
1151 #ifdef _ANDROID
1152  ret = snprintf(syspath, sizeof(syspath) - 1,
1153  "/sys/block/%s/device/address",
1154  p_dev + 5);
1155  syspath[ret] = '\0';
1156 
1157  FILE *fp = NULL;
1158  ni_fopen(&fp, syspath, "r");
1159  if (fp == NULL)
1160  {
1161  ni_log2(p_ctx, NI_LOG_ERROR, "Failed to read address\n");
1162  return NI_RETCODE_FAILURE;
1163  }
1164 
1165  if (fgets(pci_addr, sizeof(pci_addr), fp) == NULL)
1166  {
1167  ni_log2(p_ctx, NI_LOG_ERROR, "Failed to read line from address\n");
1168  fclose(fp);
1169  return NI_RETCODE_FAILURE;
1170  }
1171 
1172  fclose(fp);
1173 #else
1174  ret = find_pci_address(p_dev + 5, pci_addr);
1175  if (ret != 0)
1176  {
1177  return NI_RETCODE_FAILURE;
1178  }
1179 #endif
1180 
1181  // pci_addr pattern: 0000:04:00.0
1182  ni_log2(p_ctx, NI_LOG_INFO, "PCI address string = %12.12s\n", pci_addr);
1183 
1184  errno = 0;
1185  p_ctx->domain = strtoul(pci_addr, &dom, 16);
1186  if (errno < 0)
1187  {
1188  ni_log2(p_ctx, NI_LOG_ERROR, "Failed to read PCI domain\n");
1189  return NI_RETCODE_FAILURE;
1190  }
1191 
1192  errno = 0;
1193  p_ctx->bus = strtoul(dom + 1, &bus, 16);
1194  if (errno < 0)
1195  {
1196  ni_log2(p_ctx, NI_LOG_ERROR, "Failed to read PCI bus\n");
1197  return NI_RETCODE_FAILURE;
1198  }
1199 
1200  errno = 0;
1201  p_ctx->dev = strtoul(bus + 1, &dev, 16);
1202 
1203  if (errno < 0)
1204  {
1205  ni_log2(p_ctx, NI_LOG_ERROR, "Failed to read PCI device\n");
1206  return NI_RETCODE_FAILURE;
1207  }
1208 
1209  errno = 0;
1210  p_ctx->fn = strtoul(dev + 1, &fnc, 16);
1211 
1212  if (errno < 0)
1213  {
1214  ni_log2(p_ctx, NI_LOG_ERROR, "Falied to read PCI function\n");
1215  return NI_RETCODE_FAILURE;
1216  }
1217 
1218  ni_log2(p_ctx, NI_LOG_DEBUG, "PCI address parsed = %04x:%02x:%02x.%x\n",
1219  p_ctx->domain, p_ctx->bus, p_ctx->dev, p_ctx->fn);
1220 
1221  return NI_RETCODE_SUCCESS;
1222 #endif
1223 }
1224 #endif
1225 
1226 #if __linux__ || __APPLE__
1227 #if !defined(_ANDROID) && !defined(__OPENHARMONY__)
1228 #ifndef DISABLE_BACKTRACE_PRINT
1229 void ni_print_backtrace() {
1230  void* callstack[128];
1231  int frames = backtrace(callstack, 128);
1232  char** strs = backtrace_symbols(callstack, frames);
1233 
1234  ni_log(NI_LOG_ERROR, "Call stack:\n");
1235  for (int i = 0; i < frames; ++i) {
1236  ni_log(NI_LOG_ERROR, "%s\n", strs[i]);
1237  }
1238 
1239  free(strs);
1240 }
1241 #endif
1242 #endif
1243 #endif
1244 
1245 /*!******************************************************************************
1246  * \brief Open a xcoder decoder instance
1247  *
1248  * \param
1249  *
1250  * \return
1251 *******************************************************************************/
1253 {
1255  ni_xcoder_params_t *p_param = NULL;
1256  void* p_buffer = NULL;
1257  uint32_t ui32LBA = 0;
1258  char fmt_fw_api_ver1[5], fmt_fw_api_ver2[5];
1259 
1260  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
1261 
1262  if (!p_ctx)
1263  {
1264  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): passed parameters are null!, return\n",
1265  __func__);
1266  retval = NI_RETCODE_INVALID_PARAM;
1267  LRETURN;
1268  }
1269 
1270  if (p_ctx->p_session_config)
1271  {
1272  p_param = (ni_xcoder_params_t *)p_ctx->p_session_config;
1273  ni_params_print(p_param);
1274 
1275  // Select the CPU based on the NUMA node.
1276  if (p_param->enableCpuAffinity)
1277  {
1278 #if defined(__linux__) && defined(XCODER_ENABLE_CPU_AFFINITY)
1279  retval = ni_set_cpu_affinity(p_ctx);
1280  if (NI_RETCODE_SUCCESS != retval)
1281  {
1282  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): ni_set_cpu_affinity failed, ret=%d\n",
1283  __func__, retval);
1284  LRETURN;
1285  }
1286 #else
1287  ni_log2(p_ctx, NI_LOG_INFO, "Warning %s(): not support enableCpuAffinity parameter, "
1288  "enable with [./build.sh -c] on linux\n", __func__);
1289 #endif
1290  }
1291  }
1292 
1293  //Create the session if the create session flag is set
1294  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
1295  {
1297  p_ctx->pts_table = NULL;
1298  p_ctx->dts_queue = NULL;
1299  p_ctx->p_leftover = NULL;
1300  p_ctx->buffer_pool = NULL;
1301  p_ctx->dec_fme_buf_pool = NULL;
1302  p_ctx->prev_size = 0;
1303  p_ctx->sent_size = 0;
1304  p_ctx->status = 0;
1305  p_ctx->key_frame_type = 0;
1306  p_ctx->ready_to_close = 0;
1307  p_ctx->max_retry_fail_count[0] = p_ctx->max_retry_fail_count[1] = 0;
1308  p_ctx->rc_error_count = 0;
1309  p_ctx->frame_num = 0;
1310  p_ctx->pkt_num = 0;
1311  p_ctx->pkt_index = 0;
1312  p_ctx->session_timestamp = 0;
1313  p_ctx->is_dec_pkt_512_aligned = 0;
1314  p_ctx->p_all_zero_buf = NULL;
1315  p_ctx->last_pkt_pos = 0;
1316  p_ctx->last_frame_offset = 0;
1317  p_ctx->last_frame_dropped = 0;
1318  memset(p_ctx->pkt_custom_sei_set, 0, NI_FIFO_SZ * sizeof(ni_custom_sei_set_t *));
1319 
1320  //malloc zero data buffer
1321  if (ni_posix_memalign(&p_ctx->p_all_zero_buf, sysconf(_SC_PAGESIZE),
1323  {
1324  ni_log2(p_ctx, NI_LOG_ERROR,
1325  "ERROR %d: %s() alloc decoder all zero buffer failed\n",
1326  NI_ERRNO, __func__);
1327  retval = NI_RETCODE_ERROR_MEM_ALOC;
1328  LRETURN;
1329  }
1330  memset(p_ctx->p_all_zero_buf, 0, NI_DATA_BUFFER_LEN);
1331 
1332  //malloc data buffer
1333  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), NI_DATA_BUFFER_LEN))
1334  {
1335  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() alloc data buffer failed\n",
1336  NI_ERRNO, __func__);
1338  retval = NI_RETCODE_ERROR_MEM_ALOC;
1339  LRETURN;
1340  }
1341  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
1342 
1343  //Set session ID to be invalid. In case we cannot open session, the session id wold remain invalid.
1344  //In case we can open sesison, the session id would become valid.
1345  ((ni_session_stats_t *)p_buffer)->ui16SessionId =
1346  (uint16_t)NI_INVALID_SESSION_ID;
1347 
1348  // First uint32_t is either an invaild session ID or a valid session ID, depending on if session could be opened
1349  ui32LBA = OPEN_SESSION_CODEC(NI_DEVICE_TYPE_DECODER, ni_htonl(p_ctx->codec_format), p_ctx->hw_action);
1350  retval = ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
1351  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
1352  if (retval != NI_RETCODE_SUCCESS)
1353  {
1354  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR ni_nvme_send_read_cmd\n");
1355  LRETURN;
1356  }
1357  //Open will return a session status structure with a valid session id if it worked.
1358  //Otherwise the invalid session id set before the open command will stay
1359  p_ctx->session_id = ni_ntohs(((ni_session_stats_t *)p_buffer)->ui16SessionId);
1360  p_ctx->session_timestamp = ni_htonl(((ni_session_stats_t *)p_buffer)->ui32Session_timestamp_high);
1361  p_ctx->session_timestamp = (p_ctx->session_timestamp << 32) |
1362  ni_htonl(((ni_session_stats_t *)p_buffer)->ui32Session_timestamp_low);
1363  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
1364  {
1365  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): p_ctx->device_handle=0x%" PRIx64 ", "
1366  "p_ctx->hw_id=%d, p_ctx->session_id=%d\n", __func__,
1367  (int64_t)p_ctx->device_handle, p_ctx->hw_id, p_ctx->session_id);
1369  LRETURN;
1370  }
1371  ni_log2(p_ctx, NI_LOG_DEBUG, "Decoder open session ID:0x%x, timestamp:%" PRIu64 "\n",
1372  p_ctx->session_id, p_ctx->session_timestamp);
1373 
1374  //Send keep alive timeout Info
1375  uint64_t keep_alive_timeout =
1376  p_ctx->keep_alive_timeout * 1000000; //send us to FW
1377  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
1378  memcpy(p_buffer, &keep_alive_timeout, sizeof(keep_alive_timeout));
1379  ni_log2(p_ctx, NI_LOG_DEBUG, "%s keep_alive_timeout %" PRIx64 "\n", __func__,
1380  keep_alive_timeout);
1382  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
1383  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
1384  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
1385  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
1386  CHECK_VPU_RECOVERY(retval);
1387 
1388  if (NI_RETCODE_SUCCESS != retval)
1389  {
1390  ni_log2(p_ctx, NI_LOG_ERROR,
1391  "ERROR %s(): nvme write keep_alive_timeout command "
1392  "failed, blk_io_handle: %" PRIx64 ", hw_id, %d\n",
1393  __func__, (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id);
1395  LRETURN;
1396  }
1397 
1398  // Send SW version to FW if FW API version is >= 6.2
1400  "62") >= 0)
1401  {
1402  // Send SW version to session manager
1403  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
1404  memcpy(p_buffer, NI_XCODER_REVISION, sizeof(uint64_t));
1406  ni_fmt_fw_api_ver_str((char*) &p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], &fmt_fw_api_ver2[0]);
1407  ni_log2(p_ctx, NI_LOG_DEBUG, "%s libxcoder FW API ver %s, FW FW API ver %s\n",
1408  __func__, fmt_fw_api_ver1, fmt_fw_api_ver2);
1409  ui32LBA = CONFIG_SESSION_SWVersion_W(p_ctx->session_id);
1410  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
1411  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
1412  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config, p_ctx->device_type,
1413  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
1414  CHECK_VPU_RECOVERY(retval);
1415 
1416  if (NI_RETCODE_SUCCESS != retval)
1417  {
1418  ni_log2(p_ctx, NI_LOG_ERROR,
1419  "ERROR %s(): nvme write sw_version command "
1420  "failed, blk_io_handle: %" PRIx64 ", hw_id, %d\n",
1421  __func__, (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id);
1423  LRETURN;
1424  }
1425  }
1426 
1427  //VP9 requires a scaler session to be opened internally and attached as
1428  //well
1429  if(p_ctx->codec_format == NI_CODEC_FORMAT_VP9)
1430  {
1431  ni_log2(p_ctx, NI_LOG_DEBUG, "Adding scaling session to Vp9 decoder\n");
1432  ui32LBA = OPEN_ADD_CODEC(NI_DEVICE_TYPE_SCALER, ni_htonl(NI_SCALER_OPCODE_SCALE), ni_htons(p_ctx->session_id));
1433  retval = ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
1434  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
1435 
1436  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_open,
1437  p_ctx->device_type, p_ctx->hw_id,
1438  &(p_ctx->session_id), OPT_1);
1439  if (NI_RETCODE_SUCCESS != retval)
1440  {
1441  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR couldn't add vp9 scaler to decoding session\n");
1442  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s():p_ctx->device_handle=0x%" PRIx64 ", "
1443  "p_ctx->hw_id=%d, p_ctx->session_id=%d\n", __func__,
1444  (int64_t)p_ctx->device_handle,p_ctx->hw_id, p_ctx->session_id);
1445  ni_decoder_session_close(p_ctx, 0);
1446  LRETURN;
1447  }
1448  }
1449 
1450  ni_log2(p_ctx, NI_LOG_DEBUG,
1451  "%s(): p_ctx->device_handle=0x%" PRIx64 ", p_ctx->hw_id=%d, "
1452  "p_ctx->session_id=%d\n",
1453  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
1454  p_ctx->session_id);
1455  }
1456 
1457  //start dec config
1458  retval = ni_config_instance_set_decoder_params(p_ctx, 0);
1459  if (NI_RETCODE_SUCCESS != retval)
1460  {
1461  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);
1462  //ni_decoder_session_close(p_ctx, 0); //close happens on above
1464  LRETURN;
1465  }
1466  //end dec config
1467 
1468  // init for frame pts calculation
1469  p_ctx->is_first_frame = 1;
1470  p_ctx->last_pts = NI_NOPTS_VALUE;
1471  p_ctx->last_dts = NI_NOPTS_VALUE;
1472  p_ctx->last_dts_interval = 0;
1473  p_ctx->last_pts_interval = 0;
1474  p_ctx->pts_correction_last_dts = INT64_MIN;
1475  p_ctx->pts_correction_last_pts = INT64_MIN;
1476 
1477  //p_ctx->p_leftover = malloc(NI_MAX_PACKET_SZ * 2);
1478  p_ctx->p_leftover = malloc(p_ctx->max_nvme_io_size * 2);
1479  if (!p_ctx->p_leftover)
1480  {
1481  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Cannot allocate leftover buffer.\n",
1482  __func__);
1483  retval = NI_RETCODE_ERROR_MEM_ALOC;
1484  //ni_decoder_session_close(p_ctx, 0);
1485  LRETURN;
1486  }
1487 
1488  ni_timestamp_init(p_ctx, &p_ctx->pts_table, "dec_pts");
1489  ni_timestamp_init(p_ctx, &p_ctx->dts_queue, "dec_dts");
1490 
1491  if (p_param && p_param->ddr_priority_mode > NI_DDR_PRIORITY_NONE)
1492  {
1493  retval = ni_device_set_ddr_configuration(p_ctx, p_param->ddr_priority_mode);
1494  if (NI_RETCODE_SUCCESS != retval)
1495  {
1496  char errmsg[NI_ERRNO_LEN] = {0};
1497  ni_strerror(errmsg, NI_ERRNO_LEN, NI_ERRNO);
1498  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): ddr priority setting failure for %s\n",
1499  __func__, errmsg);
1500  LRETURN;
1501  }
1502  }
1503 
1504  if (p_ctx->force_low_delay)
1505  {
1506  if (ni_cmp_fw_api_ver(
1508  "6r3") < 0)
1509  {
1510  p_ctx->force_low_delay = false; // forceLowDelay not available for fw < 6r3
1511  ni_log2(p_ctx, NI_LOG_INFO, "Warn %s(): forceLowDelay is not available for fw < 6r3\n",
1512  __func__);
1513  }
1514  }
1515 
1516  p_ctx->active_video_width = 0;
1517  p_ctx->active_video_height = 0;
1518  p_ctx->actual_video_width = 0;
1519  p_ctx->pixel_format_changed = 0;
1520 
1521  ni_log2(p_ctx, NI_LOG_DEBUG,
1522  "%s(): p_ctx->device_handle=%" PRIx64 ", p_ctx->hw_id=%d, "
1523  "p_ctx->session_id=%d\n",
1524  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
1525  p_ctx->session_id);
1526 
1527  if (p_param && p_param->dec_input_params.nb_save_pkt)
1528  {
1529  decoder_dump_dir_open(p_ctx);
1530  }
1531 
1532 #ifdef XCODER_DUMP_DATA
1533  char dir_name[256] = {0};
1534 
1535  snprintf(dir_name, sizeof(dir_name), "%ld-%u-dec-fme", (long)getpid(),
1536  p_ctx->session_id);
1537  DIR *dir = opendir(dir_name);
1538  if (!dir && ENOENT == NI_ERRNO)
1539  {
1540  mkdir(dir_name, S_IRWXU | S_IRWXG | S_IRWXO);
1541  ni_log2(p_ctx, NI_LOG_DEBUG, "Decoder frame dump dir created: %s\n", dir_name);
1542  }
1543  if(dir){
1544  closedir(dir);
1545  }
1546 #endif
1547 
1548 END:
1549 
1550  ni_aligned_free(p_buffer);
1551  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
1552 
1553  return retval;
1554 }
1555 
1556 /*!******************************************************************************
1557  * \brief send a keep alive message to firmware
1558  *
1559  * \param
1560  *
1561  * \return
1562  *******************************************************************************/
1563 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)
1564 {
1565  ni_retcode_t retval;
1566  uint32_t ui32LBA = 0;
1567 
1568  ni_log(NI_LOG_TRACE, "%s(): enter\n", __func__);
1569  if (NI_INVALID_SESSION_ID == session_id)
1570  {
1571  ni_log(NI_LOG_ERROR, "ERROR: %s(): Invalid session ID!, return\n",
1572  __func__);
1574  LRETURN;
1575  }
1576 
1577  if (NI_INVALID_DEVICE_HANDLE == device_handle)
1578  {
1579  ni_log(NI_LOG_ERROR, "ERROR: %s(): xcoder instance id < 0, return\n",
1580  __func__);
1581  retval = NI_RETCODE_INVALID_PARAM;
1582  LRETURN;
1583  }
1584 
1585  ui32LBA = CONFIG_SESSION_KeepAlive_W(session_id);
1586  if (ni_nvme_send_write_cmd(device_handle, event_handle, p_data,
1587  NI_DATA_BUFFER_LEN, ui32LBA) < 0)
1588  {
1589  ni_log(NI_LOG_ERROR, "ERROR: %s(): device_handle=%" PRIx64 " , "
1590  "session_id=%d\n", __func__, (int64_t)device_handle, session_id);
1591  retval = NI_RETCODE_FAILURE;
1592  }
1593  else
1594  {
1596  "SUCCESS %s(): device_handle=%" PRIx64 " , "
1597  "session_id=%d\n", __func__, (int64_t)device_handle, session_id);
1598  retval = NI_RETCODE_SUCCESS;
1599  }
1600 
1601 END:
1602 
1603  ni_log(NI_LOG_TRACE, "%s(): exit\n", __func__);
1604 
1605  return retval;
1606 }
1607 
1608 /*!******************************************************************************
1609  * \brief Send end of stream signal to the decoder
1610  *
1611  * \param
1612  *
1613  * \return
1614  *******************************************************************************/
1616 {
1617  ni_retcode_t retval;
1618  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
1619  if (!p_ctx)
1620  {
1621  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
1622  __func__);
1623  retval = NI_RETCODE_INVALID_PARAM;
1624  LRETURN;
1625  }
1626 
1627  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
1628  {
1629  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
1630  __func__);
1632  LRETURN;
1633  }
1634 
1636  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
1637  p_ctx->device_type, p_ctx->hw_id,
1638  &(p_ctx->session_id), OPT_1);
1639  CHECK_VPU_RECOVERY(retval);
1640 
1641 END:
1642 
1643  if (NI_RETCODE_SUCCESS != retval)
1644  {
1645  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s(): %d, return\n", __func__, retval);
1646  }
1647 
1648  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
1649 
1650  return retval;
1651 }
1652 
1653 /*!******************************************************************************
1654  * \brief Flush decoder output
1655  *
1656  * \param
1657  *
1658  * \return
1659  *******************************************************************************/
1661 {
1662  ni_retcode_t retval;
1663  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
1664 
1665  if (!p_ctx)
1666  {
1667  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s(): passed parameters are null!, return\n",
1668  __func__);
1669  retval = NI_RETCODE_INVALID_PARAM;
1670  LRETURN;
1671  }
1672 
1673  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
1674  {
1675  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s(): xcoder instance id < 0, return\n",
1676  __func__);
1678  LRETURN;
1679  }
1680 
1682  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
1683  p_ctx->device_type, p_ctx->hw_id,
1684  &(p_ctx->session_id), OPT_1);
1685 
1686 END:
1687 
1688  if (NI_RETCODE_SUCCESS != retval)
1689  {
1690  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s(): %d, return\n", __func__, retval);
1691  }
1692 
1693  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
1694 
1695  return retval;
1696 }
1697 
1698 /*!******************************************************************************
1699  * \brief Close a xcoder decoder instance
1700  *
1701  * \param
1702  *
1703  * \return
1704  *******************************************************************************/
1706 {
1708  void* p_buffer = NULL;
1709  uint32_t ui32LBA = 0;
1710  int i;
1711  ni_xcoder_params_t *p_param = NULL;
1712 
1713  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
1714 
1715  if (!p_ctx)
1716  {
1717  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
1718  __func__);
1719  return NI_RETCODE_INVALID_PARAM;
1720  }
1721 
1722  ni_pthread_mutex_lock(&p_ctx->mutex);
1723 
1724  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
1725  {
1726  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): Invalid session ID, return.\n", __func__);
1727  retval = NI_RETCODE_SUCCESS;
1728  LRETURN;
1729  }
1730 
1731  if (NI_CODEC_HW_ENABLE == p_ctx->hw_action)
1732  {
1733  ni_session_statistic_t sessionStatistic = {0};
1734  ni_query_session_statistic_info(p_ctx, NI_DEVICE_TYPE_DECODER, &sessionStatistic);
1735  }
1736 
1737  if (ni_cmp_fw_api_ver(
1738  (char *) &p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6rg") >= 0)
1739  {
1740  ni_log2(p_ctx, NI_LOG_INFO,
1741  "Decoder_complete_info:session_id 0x%x, total frames input:%u "
1742  "buffered: %u completed: %u output: %u dropped: %u error: %u corrupted: %u\n",
1750  } else
1751  {
1752  ni_log2(p_ctx, NI_LOG_INFO,
1753  "Decoder_complete_info:session_id 0x%x, total frames input:%u "
1754  "buffered: %u completed: %u output: %u dropped: %u error: %u\n",
1761  }
1762 
1763  if (p_ctx->session_id == NI_INVALID_SESSION_ID)
1764  {
1765  ni_log2(p_ctx, NI_LOG_ERROR, "%s: Invalid session ID, return.\n", __func__);
1766  retval = NI_RETCODE_SUCCESS;
1767  LRETURN;
1768  }
1769 
1770  //malloc data buffer
1771  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), NI_DATA_BUFFER_LEN))
1772  {
1773  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: malloc decoder close data buffer failed\n", NI_ERRNO);
1774  retval = NI_RETCODE_ERROR_MEM_ALOC;
1775  LRETURN;
1776  }
1777 
1778  if (p_ctx->p_session_config)
1779  {
1780  p_param = (ni_xcoder_params_t *)p_ctx->p_session_config;
1781  if (p_param->ddr_priority_mode > NI_DDR_PRIORITY_NONE)
1782  {
1784  if (NI_RETCODE_SUCCESS != retval)
1785  {
1786  char errmsg[NI_ERRNO_LEN] = {0};
1787  ni_strerror(errmsg, NI_ERRNO_LEN, NI_ERRNO);
1788  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): ddr priority setting failure for %s\n",
1789  __func__, errmsg);
1790  LRETURN;
1791  }
1792  }
1793  }
1794 
1795  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
1796 
1798 
1799  int retry = 0;
1800  while (retry < NI_SESSION_CLOSE_RETRY_MAX)
1801  {
1802  ni_log2(p_ctx, NI_LOG_DEBUG,
1803  "%s(): p_ctx->blk_io_handle=%" PRIx64 ", p_ctx->hw_id=%d, "
1804  "p_ctx->session_id=%d, close_mode=1\n",
1805  __func__, (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id,
1806  p_ctx->session_id);
1807 
1809  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA) < 0)
1810  {
1811  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): command failed!\n", __func__);
1814  break;
1815  } else
1816  {
1817  //Close should always succeed
1818  retval = NI_RETCODE_SUCCESS;
1820  break;
1821  }
1822  /*
1823  else if (*((ni_retcode_t *)p_buffer) == RETCODE_SUCCESS)
1824  {
1825  retval = NI_RETCODE_SUCCESS;
1826  p_ctx->session_id = NI_INVALID_SESSION_ID;
1827  break;
1828  }
1829  else
1830  {
1831  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): wait for close\n", __func__);
1832  ni_usleep(NI_SESSION_CLOSE_RETRY_INTERVAL_US);
1833  retval = NI_RETCODE_ERROR_NVME_CMD_FAILED;
1834  }
1835  */
1836  retry++;
1837  }
1838 
1839 END:
1840 
1841  ni_aligned_free(p_buffer);
1843  ni_memfree(p_ctx->p_leftover);
1844 
1845  if (p_ctx->pts_table)
1846  {
1847  ni_queue_free(&p_ctx->pts_table->list, p_ctx->buffer_pool);
1848  ni_memfree(p_ctx->pts_table);
1849  ni_log2(p_ctx, NI_LOG_DEBUG, "ni_timestamp_done: success\n");
1850  }
1851 
1852  if (p_ctx->dts_queue)
1853  {
1854  ni_queue_free(&p_ctx->dts_queue->list, p_ctx->buffer_pool);
1855  ni_memfree(p_ctx->dts_queue);
1856  ni_log2(p_ctx, NI_LOG_DEBUG, "ni_timestamp_done: success\n");
1857  }
1858 
1861  p_ctx->buffer_pool = NULL;
1862  p_ctx->dec_fme_buf_pool = NULL;
1863 
1864  for (i = 0; i < NI_FIFO_SZ; i++)
1865  {
1866  ni_memfree(p_ctx->pkt_custom_sei_set[i]);
1867  }
1868 
1869  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): CTX[Card:%" PRIx64 " / HW:%d / INST:%d]\n",
1870  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
1871  p_ctx->session_id);
1872 
1873  low_delay_signal(p_ctx);
1874  ni_pthread_mutex_unlock(&p_ctx->mutex);
1875 
1876  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
1877 
1878  return retval;
1879 }
1880 
1881 /*!******************************************************************************
1882  * \brief Send a video p_packet to decoder
1883  *
1884  * \param
1885  *
1886  * \return
1887  *******************************************************************************/
1889 {
1890  uint32_t sent_size = 0;
1891  uint32_t packet_size = 0;
1892  int current_pkt_size;
1893  int retval = NI_RETCODE_SUCCESS;
1894  ni_xcoder_params_t *p_param;
1895  ni_instance_buf_info_t buf_info = { 0 };
1896  ni_session_statistic_t sessionStatistic = {0};
1897  int query_retry = 0;
1898  uint32_t ui32LBA = 0;
1899 
1900  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
1901 
1902  if ((!p_ctx) || (!p_packet))
1903  {
1904  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
1905  __func__);
1906  return NI_RETCODE_INVALID_PARAM;
1907  }
1908 
1909  ni_pthread_mutex_lock(&p_ctx->mutex);
1910 
1911  if ((NI_INVALID_SESSION_ID == p_ctx->session_id))
1912  {
1913  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
1914  __func__);
1916  LRETURN;
1917  }
1918 
1919  low_delay_wait(p_ctx);
1920 
1921 #ifdef MEASURE_LATENCY
1922  if ((p_packet->dts != NI_NOPTS_VALUE) && (p_ctx->frame_time_q != NULL))
1923  {
1924  uint64_t abs_time_ns = ni_gettime_ns();
1926  abs_time_ns, p_packet->dts);
1927  }
1928 #endif
1929 
1930  p_param = (ni_xcoder_params_t *)p_ctx->p_session_config;
1931  packet_size = p_packet->data_len;
1932  current_pkt_size = packet_size;
1933 
1934 #ifdef XCODER_311
1935  int max_retry = 200; // 20ms
1936  if (p_packet->video_width * p_packet->video_height >= NI_NUM_OF_PIXELS_1440P)
1937  {
1938  max_retry = NI_MAX_TX_RETRIES; // 100ms
1939  }
1940 #endif
1941 
1942  for (;;)
1943  {
1944  query_sleep(p_ctx);
1945 
1946  query_retry++;
1947 
1948 
1950  "65") >= 0)
1951  {
1953  &sessionStatistic);
1954  CHECK_ERR_RC(p_ctx, retval, &sessionStatistic,
1956  p_ctx->hw_id, &(p_ctx->session_id), OPT_2);
1957  CHECK_VPU_RECOVERY(retval);
1958 
1959  buf_info.buf_avail_size = sessionStatistic.ui32WrBufAvailSize;
1960  } else
1961  {
1963  NI_DEVICE_TYPE_DECODER, &buf_info);
1964  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
1965  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
1966  CHECK_VPU_RECOVERY(retval);
1967  }
1968 
1969  if (buf_info.buf_avail_size == DP_IPC_PASSTHRU)
1970  {
1971  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): Bad available buffer size %u\n", __FUNCTION__, buf_info.buf_avail_size);
1972  retval = NI_RETCODE_FAILURE;
1973  LRETURN;
1974  }
1975 
1976  if (p_ctx->biggest_bitstream_buffer_allocated < buf_info.buf_avail_size)
1977  {
1979  }
1980  if (p_ctx->biggest_bitstream_buffer_allocated < packet_size &&
1982  {
1983  // Reallocate decoder bitstream buffers to accomodate
1985  "66") >= 0)
1986  {
1987  retval = ni_config_instance_set_write_len(p_ctx,
1989  // packet buffer aligned to NI_MAX_PACKET_SZ(128k)
1990  (packet_size / NI_MAX_PACKET_SZ + 1) * NI_MAX_PACKET_SZ);
1992  p_ctx->device_type, p_ctx->hw_id,
1993  &(p_ctx->session_id), OPT_3);
1994  CHECK_VPU_RECOVERY(retval);
1995  }
1996  else
1997  {
1998  retval = ni_config_instance_set_decoder_params(p_ctx, packet_size);
1999  }
2000  if (NI_RETCODE_SUCCESS != retval)
2001  {
2002  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): failed to reallocate bitstream\n", __FUNCTION__);
2003  LRETURN;
2004  }
2005  query_retry--;
2006  continue;
2007  }
2008 
2009  if (NI_RETCODE_SUCCESS != retval ||
2010  buf_info.buf_avail_size < packet_size)
2011  {
2012  ni_log2(p_ctx, NI_LOG_TRACE,
2013  "Warning: dec write query fail rc %d or available buf size %u < "
2014  "pkt size %u , retry: %d max_retry_fail_count %d\n",
2015  retval, buf_info.buf_avail_size, packet_size, query_retry, p_ctx->max_retry_fail_count[0]);
2016 #ifdef XCODER_311
2017  if (query_retry > max_retry ||
2018  (sessionStatistic.ui32RdBufAvailSize > 0 && !p_ctx->required_buf_size))
2019 #else
2020  if (query_retry > NI_MAX_TX_RETRIES ||
2021  (sessionStatistic.ui32RdBufAvailSize > 0 && !p_ctx->required_buf_size))
2022 #endif
2023  {
2025 #ifdef XCODER_311
2026  if (query_retry > max_retry)
2027 #else
2028  if (query_retry > NI_MAX_TX_RETRIES)
2029 #endif
2030  {
2031  p_ctx->max_retry_fail_count[0]++;
2032  }
2033  p_ctx->required_buf_size = packet_size;
2035  LRETURN;
2036  }
2037  ni_pthread_mutex_unlock(&p_ctx->mutex);
2039  ni_pthread_mutex_lock(&p_ctx->mutex);
2040  }
2041  else
2042  {
2043  p_ctx->max_retry_fail_count[0] = 0;
2044  ni_log2(p_ctx, NI_LOG_DEBUG, "Info dec write query success, available buf "
2045  "size %u >= pkt size %u !\n",
2046  buf_info.buf_avail_size, packet_size);
2047  break;
2048  }
2049  }
2050 
2051  //Configure write size for the buffer
2053  packet_size);
2055  p_ctx->device_type, p_ctx->hw_id,
2056  &(p_ctx->session_id), OPT_1);
2057  CHECK_VPU_RECOVERY(retval);
2058  if (retval < 0)
2059  {
2060  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): config pkt size command failed\n",
2061  __func__);
2063  LRETURN;
2064  }
2065 
2067 
2068  //check for start of stream flag
2069  if (p_packet->start_of_stream)
2070  {
2072  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
2073  p_ctx->device_type, p_ctx->hw_id,
2074  &(p_ctx->session_id), OPT_1);
2075  CHECK_VPU_RECOVERY(retval);
2076  if (NI_RETCODE_SUCCESS != retval)
2077  {
2078  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Failed to send SOS.\n", __func__);
2079  LRETURN;
2080  }
2081 
2082  p_packet->start_of_stream = 0;
2083  }
2084 
2085  if (p_packet->p_data)
2086  {
2087  ni_log2(p_ctx, NI_LOG_DEBUG,
2088  "%s() had data to send: packet_size=%u, "
2089  "p_packet->sent_size=%d, p_packet->data_len=%u, "
2090  "p_packet->start_of_stream=%u, p_packet->end_of_stream=%u, "
2091  "p_packet->video_width=%u, p_packet->video_height=%u\n",
2092  __func__, packet_size, p_packet->sent_size, p_packet->data_len,
2093  p_packet->start_of_stream, p_packet->end_of_stream,
2094  p_packet->video_width, p_packet->video_height);
2095 
2096  uint8_t *p_data = (uint8_t *)p_packet->p_data;
2097  // Note: session status is NOT reset but tracked between send
2098  // and recv to catch and recover from a loop condition
2099  // p_ctx->status = 0;
2100 
2101  if (packet_size % NI_MEM_PAGE_ALIGNMENT) //packet size, already aligned
2102  {
2103  packet_size = ( (packet_size / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT) + NI_MEM_PAGE_ALIGNMENT;
2104  }
2105 
2106  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
2107  p_data, packet_size, ui32LBA);
2108  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_write,
2109  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
2110  CHECK_VPU_RECOVERY(retval);
2111  if (retval < 0)
2112  {
2113  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
2115  LRETURN;
2116  }
2117 
2118  // reset session status after successful send
2119  p_ctx->status = 0;
2120  p_ctx->required_buf_size = 0;
2121 
2122  sent_size = p_packet->data_len;
2123  p_packet->data_len = 0;
2124 
2125  if (p_param->dec_input_params.nb_save_pkt)
2126  {
2127  char dump_file[512] = {0};
2128  long curr_pkt_num =
2129  ((long)p_ctx->pkt_num % p_param->dec_input_params.nb_save_pkt) + 1;
2130  snprintf(dump_file, sizeof(dump_file), "%s/pkt-%04ld.bin",
2131  p_ctx->stream_dir_name, curr_pkt_num);
2132 
2133  FILE *f = NULL;
2134  ni_fopen(&f, dump_file, "wb");
2135  if (f)
2136  {
2137  fwrite(p_packet->p_data, sent_size, 1, f);
2138  fflush(f);
2139  fclose(f);
2140  }
2141  }
2142 
2143  p_ctx->pkt_num++;
2144  p_ctx->low_delay_sync_flag = 1;
2145  }
2146 
2147  //Handle end of stream flag
2148  if (p_packet->end_of_stream)
2149  {
2151  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
2152  p_ctx->device_type, p_ctx->hw_id,
2153  &(p_ctx->session_id), OPT_1);
2154  CHECK_VPU_RECOVERY(retval);
2155 
2156  if (NI_RETCODE_SUCCESS != retval)
2157  {
2158  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Failed to send EOS.\n", __func__);
2159  LRETURN;
2160  }
2161 
2162  p_packet->end_of_stream = 0;
2163  p_ctx->ready_to_close = 1;
2164  }
2165 
2166  {
2167  p_ctx->pts_offsets[p_ctx->pkt_index % NI_FIFO_SZ] = p_packet->pts;
2168  p_ctx->flags_array[p_ctx->pkt_index % NI_FIFO_SZ] = p_packet->flags;
2169  p_ctx->pkt_pos[p_ctx->pkt_index % NI_FIFO_SZ] = p_packet->pkt_pos;
2170  if (p_ctx->pkt_index == 0)
2171  {
2172  p_ctx->pkt_offsets_index_min[p_ctx->pkt_index % NI_FIFO_SZ] = 0;
2173  /* minus 1 here. ffmpeg parses the msb 0 of long start code as the last packet's payload for hevc bitstream (hevc_parse).
2174  * move 1 byte forward on all the pkt_offset so that frame_offset coming from fw can fall into the correct range. */
2175  //p_ctx->pkt_offsets_index[p_ctx->pkt_index % NI_FIFO_SZ] = current_pkt_size - 1;
2176  p_ctx->pkt_offsets_index[p_ctx->pkt_index % NI_FIFO_SZ] = current_pkt_size;
2177  ni_log2(p_ctx, NI_LOG_DEBUG,
2178  "%s: (first packet) pkt_index %d i %u "
2179  "pkt_offsets_index_min %" PRIu64 " pkt_offsets_index %" PRIu64
2180  " pts_offsets %" PRId64 "\n",
2181  __func__, p_ctx->pkt_index, p_ctx->pkt_index % NI_FIFO_SZ,
2182  p_ctx->pkt_offsets_index_min[p_ctx->pkt_index % NI_FIFO_SZ],
2183  p_ctx->pkt_offsets_index[p_ctx->pkt_index % NI_FIFO_SZ],
2184  p_ctx->pts_offsets[p_ctx->pkt_index % NI_FIFO_SZ]);
2185  }
2186  else
2187  {
2188  // cumulate sizes to correspond to FW offsets
2189  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];
2190  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;
2191  ni_log2(p_ctx, NI_LOG_DEBUG,
2192  "%s: pkt_index %d i %u pkt_offsets_index_min "
2193  "%" PRIu64 " pkt_offsets_index %" PRIu64 " pts_offsets %" PRId64
2194  "\n",
2195  __func__, p_ctx->pkt_index, p_ctx->pkt_index % NI_FIFO_SZ,
2196  p_ctx->pkt_offsets_index_min[p_ctx->pkt_index % NI_FIFO_SZ],
2197  p_ctx->pkt_offsets_index[p_ctx->pkt_index % NI_FIFO_SZ],
2198  p_ctx->pts_offsets[p_ctx->pkt_index % NI_FIFO_SZ]);
2199 
2200  //Wrapping 32 bits since FW send u32 wrapped values
2201  if (p_ctx->pkt_offsets_index_min[p_ctx->pkt_index % NI_FIFO_SZ] > 0xFFFFFFFF)
2202  {
2203  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);
2204  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;
2205  ni_log2(p_ctx, NI_LOG_DEBUG,
2206  "%s: (wrap) pkt_index %d i %u "
2207  "pkt_offsets_index_min %" PRIu64 " pkt_offsets_index %" PRIu64
2208  " pts_offsets %" PRId64 "\n",
2209  __func__, p_ctx->pkt_index, p_ctx->pkt_index % NI_FIFO_SZ,
2210  p_ctx->pkt_offsets_index_min[p_ctx->pkt_index % NI_FIFO_SZ],
2211  p_ctx->pkt_offsets_index[p_ctx->pkt_index % NI_FIFO_SZ],
2212  p_ctx->pts_offsets[p_ctx->pkt_index % NI_FIFO_SZ]);
2213  }
2214  }
2215 
2216  /* if this wrap-around pkt_offset_index spot is about to be overwritten, free the previous one. */
2217  free(p_ctx->pkt_custom_sei_set[p_ctx->pkt_index % NI_FIFO_SZ]);
2218 
2219  if (p_packet->p_custom_sei_set)
2220  {
2221  p_ctx->pkt_custom_sei_set[p_ctx->pkt_index % NI_FIFO_SZ] = malloc(sizeof(ni_custom_sei_set_t));
2222  if (p_ctx->pkt_custom_sei_set[p_ctx->pkt_index % NI_FIFO_SZ])
2223  {
2224  ni_custom_sei_set_t *p_custom_sei_set = p_ctx->pkt_custom_sei_set[p_ctx->pkt_index % NI_FIFO_SZ];
2225  memcpy(p_custom_sei_set, p_packet->p_custom_sei_set, sizeof(ni_custom_sei_set_t));
2226  }
2227  else
2228  {
2229  /* warn and lose the sei data. */
2230  ni_log2(p_ctx, NI_LOG_ERROR,
2231  "Error %s: failed to allocate custom SEI buffer for pkt.\n",
2232  __func__);
2233  }
2234  }
2235  else
2236  {
2237  p_ctx->pkt_custom_sei_set[p_ctx->pkt_index % NI_FIFO_SZ] = NULL;
2238  }
2239 
2240  p_ctx->pkt_index++;
2241  }
2242 
2243  retval = ni_timestamp_register(p_ctx->buffer_pool, p_ctx->dts_queue, p_packet->dts, 0);
2244  if (NI_RETCODE_SUCCESS != retval)
2245  {
2246  ni_log2(p_ctx, NI_LOG_ERROR,
2247  "ERROR %s(): ni_timestamp_register() for dts returned %d\n",
2248  __func__, retval);
2249  }
2250 END:
2251 
2252  ni_pthread_mutex_unlock(&p_ctx->mutex);
2253 
2254  if (NI_RETCODE_SUCCESS == retval)
2255  {
2256  ni_log2(p_ctx, NI_LOG_TRACE,
2257  "%s(): exit: packets: %" PRIu64 " offset %" PRIx64 ""
2258  " sent_size = %u, status=%d\n",
2259  __func__, p_ctx->pkt_num, (uint64_t)p_packet->pos, sent_size,
2260  p_ctx->status);
2261  return sent_size;
2262  } else
2263  {
2264  ni_log2(p_ctx, NI_LOG_ERROR,
2265  "ERROR %s(): exit: returnErr: %d, p_ctx->status: %d\n", __func__,
2266  retval, p_ctx->status);
2267  return retval;
2268  }
2269 }
2270 
2271 static int64_t guess_correct_pts(ni_session_context_t* p_ctx, int64_t reordered_pts, int64_t dts)
2272 {
2273  int64_t pts = NI_NOPTS_VALUE;
2274  if (dts != NI_NOPTS_VALUE)
2275  {
2277  p_ctx->pts_correction_last_dts = dts;
2278  ni_log2(p_ctx, NI_LOG_DEBUG,
2279  "%s: pts_correction_last_dts %" PRId64 " "
2280  "pts_correction_num_faulty_dts %d\n",
2281  __func__, p_ctx->pts_correction_last_dts,
2283  }
2284  else if (reordered_pts != NI_NOPTS_VALUE)
2285  {
2286  p_ctx->pts_correction_last_dts = reordered_pts;
2287  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: pts_correction_last_dts %" PRId64 "\n", __func__,
2288  p_ctx->pts_correction_last_dts);
2289  }
2290  if (reordered_pts != NI_NOPTS_VALUE)
2291  {
2292  p_ctx->pts_correction_num_faulty_pts += reordered_pts <= p_ctx->pts_correction_last_pts;
2293  p_ctx->pts_correction_last_pts = reordered_pts;
2294  ni_log2(p_ctx, NI_LOG_DEBUG,
2295  "%s: pts_correction_last_pts %" PRId64 " "
2296  "pts_correction_num_faulty_pts %d\n",
2297  __func__, p_ctx->pts_correction_last_pts,
2299  }
2300  else if (dts != NI_NOPTS_VALUE)
2301  {
2302  p_ctx->pts_correction_last_pts = dts;
2303  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: pts_correction_last_pts %" PRId64 "\n", __func__,
2304  p_ctx->pts_correction_last_pts);
2305  }
2307  && reordered_pts != NI_NOPTS_VALUE)
2308  {
2309  pts = reordered_pts;
2310  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: (reordered_pts) pts %" PRId64 "\n", __func__,
2311  pts);
2312  }
2313  else
2314  {
2315  if ((NI_NOPTS_VALUE == reordered_pts) ||
2316  (NI_NOPTS_VALUE == p_ctx->last_pts) || (dts >= p_ctx->last_pts))
2317  {
2318  pts = dts;
2319  } else
2320  {
2321  pts = reordered_pts;
2322  }
2323 
2324  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: (dts) pts %" PRId64 "\n", __func__, pts);
2325  }
2326  return pts;
2327 }
2328 
2329 static int rotated_array_binary_search(uint64_t *lefts, uint64_t *rights,
2330  int32_t size, uint64_t target)
2331 {
2332  int lo = 0;
2333  int hi = size - 1;
2334  while (lo <= hi)
2335  {
2336  int mid = lo + (hi - lo) / 2;
2337  if (lefts[mid] <= target && target < rights[mid])
2338  {
2339  return mid;
2340  }
2341 
2342  if (rights[mid] == 0)
2343  {
2344  // empty in (mid, hi)
2345  hi = mid - 1;
2346  continue;
2347  }
2348 
2349  if (rights[lo] <= rights[mid])
2350  {
2351  if (lefts[lo] <= target && target < lefts[mid])
2352  {
2353  // Elements are all monotonous in (lo, mid)
2354  hi = mid - 1;
2355  } else
2356  {
2357  // Rotation in (mid, hi)
2358  lo = mid + 1;
2359  }
2360  } else
2361  {
2362  if (rights[mid] <= target && target < rights[hi])
2363  {
2364  // Elements are all monotonous in (mid, hi)
2365  lo = mid + 1;
2366  } else
2367  {
2368  // Rotation in (lo, mid)
2369  hi = mid - 1;
2370  }
2371  }
2372  }
2373 
2374  return -1;
2375 }
2376 
2377 /*!******************************************************************************
2378  * \brief Retrieve a YUV p_frame from decoder
2379  *
2380  * \param
2381  *
2382  * \return
2383  *******************************************************************************/
2385 {
2386  ni_instance_mgr_stream_info_t data = { 0 };
2387  int rx_size = 0;
2388  uint64_t frame_offset = 0;
2389  uint32_t frame_dropped = 0;
2390  uint8_t *p_data_buffer = NULL;
2391  uint32_t i = 0;
2392  int is_planar;
2393  int retval = NI_RETCODE_SUCCESS;
2394  int metadata_hdr_size = NI_FW_META_DATA_SZ - NI_MAX_NUM_OF_DECODER_OUTPUTS * sizeof(niFrameSurface1_t);
2395  int sei_size = 0;
2396  uint32_t total_bytes_to_read = 0;
2397  uint32_t read_size_bytes = 0;
2398  ni_instance_buf_info_t buf_info = { 0 };
2399  ni_session_statistic_t sessionStatistic = {0};
2400  ni_xcoder_params_t *p_param;
2401 
2402  int query_retry = 0;
2403  uint32_t ui32LBA = 0;
2404  unsigned int bytes_read_so_far = 0;
2405  int query_type = INST_BUF_INFO_RW_READ;
2406  int low_delay_notify = 0;
2407  uint32_t frames_dropped = 0;
2408  uint8_t get_first_metadata = 0;
2409  uint8_t sequence_change = 0;
2410 
2411  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
2412 
2413  if ((!p_ctx) || (!p_frame))
2414  {
2415  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: passed parameters are null!, return\n");
2416  return NI_RETCODE_INVALID_PARAM;
2417  }
2418 
2419  ni_pthread_mutex_lock(&p_ctx->mutex);
2420 
2421 start:
2422  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
2423  {
2424  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): xcoder instance id < 0, return\n",
2425  __func__);
2427  LRETURN;
2428  }
2429 
2430  p_param = (ni_xcoder_params_t *)p_ctx->p_session_config;
2431  p_data_buffer = (uint8_t *)p_frame->p_buffer;
2432 
2433  // p_frame->p_data[] can be NULL before actual resolution is returned by
2434  // decoder and buffer pool is allocated, so no checking here.
2435  total_bytes_to_read = p_frame->data_len[0] + p_frame->data_len[1] +
2436  p_frame->data_len[2] + metadata_hdr_size;
2437  ni_log2(p_ctx, NI_LOG_DEBUG, "Total bytes to read %u, low_delay %u\n",
2438  total_bytes_to_read, p_ctx->decoder_low_delay);
2439 
2440  if (p_ctx->decoder_low_delay > 0 && !p_ctx->ready_to_close)
2441  {
2442  ni_log2(p_ctx, NI_LOG_DEBUG, "frame_num = %" PRIu64 ", pkt_num = %" PRIu64 "\n",
2443  p_ctx->frame_num, p_ctx->pkt_num);
2444  frames_dropped = p_ctx->session_statistic.ui32FramesDropped;
2445  if (p_ctx->force_low_delay && (p_ctx->force_low_delay_cnt < frames_dropped)) {
2446  p_ctx->force_low_delay_cnt = frames_dropped;
2447  }
2448  if (p_ctx->frame_num + p_ctx->force_low_delay_cnt >= p_ctx->pkt_num)
2449  {
2450  //nothing to query, leave
2451  retval = NI_RETCODE_SUCCESS;
2452  LRETURN;
2453  }
2454  query_type = INST_BUF_INFO_RW_READ_BUSY;
2455  }
2456  for (;;)
2457  {
2458  query_sleep(p_ctx);
2459 
2460  query_retry++;
2461 
2463  "65") >= 0)
2464  {
2466  &sessionStatistic);
2467  CHECK_ERR_RC(p_ctx, retval, &sessionStatistic,
2469  p_ctx->hw_id, &(p_ctx->session_id), OPT_2);
2470  CHECK_VPU_RECOVERY(retval);
2471 
2472  buf_info.buf_avail_size = sessionStatistic.ui32RdBufAvailSize;
2473  } else
2474  {
2475  retval = ni_query_instance_buf_info(p_ctx, query_type,
2476  NI_DEVICE_TYPE_DECODER, &buf_info);
2477  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
2478  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
2479  CHECK_VPU_RECOVERY(retval);
2480  }
2481 
2482  ni_log2(p_ctx, NI_LOG_TRACE, "Info query buf_info.size = %u\n",
2483  buf_info.buf_avail_size);
2484 
2485  if (NI_RETCODE_SUCCESS != retval)
2486  {
2487  ni_log2(p_ctx, NI_LOG_TRACE, "Warning: dec read query fail rc %d retry %d max_retry_fail_count %d\n",
2488  retval, query_retry, p_ctx->max_retry_fail_count[1]);
2489 
2490  if (query_retry >= 1000)
2491  {
2492  p_ctx->max_retry_fail_count[1]++;
2493  low_delay_notify = 1;
2495  LRETURN;
2496  }
2497  ni_pthread_mutex_unlock(&p_ctx->mutex);
2499  ni_pthread_mutex_lock(&p_ctx->mutex);
2500  }
2501  else if (buf_info.buf_avail_size == DP_IPC_PASSTHRU)
2502  {
2503  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): Bad available buffer size %u\n", __FUNCTION__, buf_info.buf_avail_size);
2504  retval = NI_RETCODE_FAILURE;
2505  LRETURN;
2506  }
2507  else if (buf_info.buf_avail_size == metadata_hdr_size)
2508  {
2509  ni_log2(p_ctx, NI_LOG_DEBUG, "Info only metadata hdr is available, seq change?\n");
2510  total_bytes_to_read = metadata_hdr_size;
2511  sequence_change = 1;
2512  break;
2513  }
2514  else if (0 == buf_info.buf_avail_size)
2515  {
2516  // query to see if it is eos now, if we have sent it
2517  if (p_ctx->ready_to_close)
2518  {
2519  ni_log2(p_ctx, NI_LOG_TRACE, "Info dec query, ready_to_close %u, query eos\n",
2520  p_ctx->ready_to_close);
2521  retval = ni_query_stream_info(p_ctx, NI_DEVICE_TYPE_DECODER, &data);
2522  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
2523  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
2524  CHECK_VPU_RECOVERY(retval);
2525 
2526  if (data.is_flushed ||
2527  query_retry >=
2529  {
2530  if (query_retry >=
2532  {
2533  ni_log2(p_ctx, NI_LOG_ERROR,
2534  "WARNING: Dec eos reached but also exceeded max dec read query "
2535  "retries. is_flushed=%u try=%d.\n",
2536  data.is_flushed, query_retry);
2537  } else
2538  {
2539  ni_log2(p_ctx, NI_LOG_DEBUG,
2540  "Dec eos reached. is_flushed=%u try=%d.\n",
2541  data.is_flushed, query_retry);
2542  }
2543  p_frame->end_of_stream = 1;
2544  low_delay_notify = 1;
2545  retval = NI_RETCODE_SUCCESS;
2546  LRETURN;
2547  } else
2548  {
2549  ni_log2(p_ctx, NI_LOG_TRACE, "Dec read available buf size == 0, query try %d,"
2550  " retrying ..\n", query_retry);
2551  ni_pthread_mutex_unlock(&p_ctx->mutex);
2553  ni_pthread_mutex_lock(&p_ctx->mutex);
2554  continue;
2555  }
2556  }
2557 
2558  ni_log2(p_ctx, NI_LOG_TRACE, "Dec read available buf size == 0. retry=%d, eos=%d"
2559  "\n", query_retry, p_frame->end_of_stream);
2561  (p_ctx->decoder_low_delay > 0 &&
2562  ((p_ctx->frame_num + p_ctx->force_low_delay_cnt)
2563  < p_ctx->pkt_num))) &&
2564  query_retry < 1000 / 2)
2565  {
2566  if (p_ctx->decoder_low_delay && p_ctx->force_low_delay) {
2567  if (p_ctx->session_statistic.ui32FramesDropped > frames_dropped) {
2568  // last pkt sent to decoder marked as dropped, no output,
2569  // so just stop query and return
2570  p_ctx->force_low_delay_cnt++;
2571  low_delay_signal(p_ctx);
2572  retval = NI_RETCODE_SUCCESS;
2573  LRETURN;
2574  }
2575  } else if (NI_RETCODE_NVME_SC_WRITE_BUFFER_FULL == p_ctx->status &&
2576  sessionStatistic.ui32WrBufAvailSize > p_ctx->required_buf_size)
2577  {
2578  ni_log2(p_ctx, NI_LOG_TRACE, "Info dec write buffer is enough, available buf "
2579  "size %u >= required size %u !\n",
2580  sessionStatistic.ui32WrBufAvailSize, p_ctx->required_buf_size);
2581  p_ctx->status = 0;
2582  p_ctx->required_buf_size = 0;
2583  retval = NI_RETCODE_SUCCESS;
2584  LRETURN;
2585  }
2586  ni_pthread_mutex_unlock(&p_ctx->mutex);
2587  ni_usleep(25);
2588  ni_pthread_mutex_lock(&p_ctx->mutex);
2589  continue;
2590  } else
2591  {
2592  if(p_ctx->decoder_low_delay > 0)
2593  {
2594  if (p_ctx->force_low_delay) {
2595  p_ctx->force_low_delay_cnt++;
2596  low_delay_signal(p_ctx);
2597  } else {
2598  ni_log2(p_ctx, NI_LOG_ERROR,
2599  "Warning: ceased using low delay decoding mode after "
2600  "excessively long decoder read query.\n");
2601  // Here it should be the last signal to release the send thread
2602  // holding the low delay mutex.
2603  low_delay_signal(p_ctx);
2604  p_ctx->decoder_low_delay = 0;
2605  }
2606  }
2607 
2608  if ((p_param->dec_input_params.min_packets_delay && p_ctx->pkt_delay_cnt))
2609  {
2610  if(p_ctx->pkt_num >= (p_ctx->frame_num + p_ctx->pkt_delay_cnt +
2612  {
2613  if(query_retry <= 2000)
2614  {
2615  ni_pthread_mutex_unlock(&p_ctx->mutex);
2616  ni_usleep(25);
2617  ni_pthread_mutex_lock(&p_ctx->mutex);
2618  continue;
2619  } else {
2620  p_ctx->pkt_delay_cnt++;
2621  ni_log2(p_ctx, NI_LOG_ERROR,
2622  "Warning: decoder pkt_num %u frame_num %u "
2623  "timeout, increasing pkt_delay_cnt to %u\n",
2624  p_ctx->pkt_num, p_ctx->frame_num,
2625  p_ctx->pkt_delay_cnt);
2626  }
2627  }
2628  }
2629 
2630  ni_log2(p_ctx, NI_LOG_DEBUG, "Warning: dec read failed %d retries. rc=%d; eos=%d\n",
2631  query_retry, p_ctx->status, p_frame->end_of_stream);
2632  }
2633  retval = NI_RETCODE_SUCCESS;
2634  LRETURN;
2635  }
2636  else
2637  {
2638  // We have to ensure there are adequate number of DTS for picture
2639  // reorder delay otherwise wait for more packets to be sent to decoder.
2640  ni_timestamp_table_t *p_dts_queue = p_ctx->dts_queue;
2641  if ((int)p_dts_queue->list.count < p_ctx->pic_reorder_delay + 1 &&
2642  !p_ctx->ready_to_close &&
2644  {
2645  retval = NI_RETCODE_SUCCESS;
2646  ni_log2(p_ctx, NI_LOG_DEBUG,
2647  "At least %d packets should be sent before reading the "
2648  "first frame!\n",
2649  p_ctx->pic_reorder_delay + 1);
2650  LRETURN;
2651  }
2652  p_ctx->max_retry_fail_count[1] = 0;
2653 
2654  // get actual YUV transfer size if this is the stream's very first read
2655  if (0 == p_ctx->active_video_width || 0 == p_ctx->active_video_height)
2656  {
2657  retval = ni_query_stream_info(p_ctx, NI_DEVICE_TYPE_DECODER, &data);
2658  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
2659  p_ctx->device_type, p_ctx->hw_id,
2660  &(p_ctx->session_id), OPT_1);
2661  CHECK_VPU_RECOVERY(retval);
2662 
2663  ni_log2(p_ctx, NI_LOG_DEBUG, "Info dec YUV query, pic size %ux%u xfer frame size "
2664  "%ux%u frame-rate %u is_flushed %u\n",
2665  data.picture_width, data.picture_height,
2667  data.frame_rate, data.is_flushed);
2670  p_ctx->actual_video_width = data.picture_width;
2671  p_ctx->pixel_format = data.pix_format;
2672  is_planar = (p_ctx->pixel_format == NI_PIX_FMT_YUV420P) ||
2675  //p_ctx->bit_depth_factor = data.transfer_frame_stride / data.picture_width;
2676  p_ctx->is_first_frame = 1;
2677  p_ctx->pixel_format_changed = 0;
2678 
2679  ni_log2(p_ctx, NI_LOG_DEBUG, "Info dec YUV, adjust frame size from %ux%u to "
2680  "%ux%u format = %d\n", p_frame->video_width, p_frame->video_height,
2681  p_ctx->active_video_width, p_ctx->active_video_height, p_ctx->pixel_format);
2682 
2684 
2685  // set up decoder YUV frame buffer pool
2688  p_ctx->active_video_height,
2690  p_ctx->bit_depth_factor))
2691  {
2692  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Cannot allocate fme buf pool.\n",
2693  __func__);
2694  retval = NI_RETCODE_ERROR_MEM_ALOC;
2695 
2696  ni_pthread_mutex_unlock(&p_ctx->mutex);
2697  ni_decoder_session_close(p_ctx, 0);
2698 #ifdef XCODER_SELF_KILL_ERR
2699  // if need to terminate at such occasion when continuing is not
2700  // possible, trigger a codec closure
2701  ni_log2(p_ctx, NI_LOG_ERROR, "Terminating due to unable to allocate fme buf "
2702  "pool.\n");
2703  kill(getpid(), SIGTERM);
2704 #endif
2705  ni_pthread_mutex_lock(&p_ctx->mutex);
2706  LRETURN;
2707  }
2708 
2710  p_ctx->dec_fme_buf_pool, p_frame, 1, // get mem buffer
2711  p_ctx->actual_video_width, p_ctx->active_video_height,
2713  p_ctx->bit_depth_factor, is_planar);
2714 
2715  if (NI_RETCODE_SUCCESS != retval)
2716  {
2717  LRETURN;
2718  }
2719  total_bytes_to_read = p_frame->data_len[0] + p_frame->data_len[1] +
2720  p_frame->data_len[2] + metadata_hdr_size;
2721  p_data_buffer = (uint8_t*) p_frame->p_buffer;
2722 
2723  // make sure we don't read more than available
2724  ni_log2(p_ctx, NI_LOG_DEBUG, "Info dec buf size: %u YUV frame + meta-hdr size: %u "
2725  "available: %u\n", p_frame->buffer_size,
2726  total_bytes_to_read, buf_info.buf_avail_size);
2727  }
2728  break;
2729  }
2730  }
2731 
2732  ni_log2(p_ctx, NI_LOG_DEBUG, "total_bytes_to_read %u max_nvme_io_size %u ylen %u cr len "
2733  "%u cb len %u hdr %d\n",
2734  total_bytes_to_read, p_ctx->max_nvme_io_size,
2735  p_frame->data_len[0], p_frame->data_len[1],
2736  p_frame->data_len[2], metadata_hdr_size);
2737 
2738  if (buf_info.buf_avail_size < total_bytes_to_read)
2739  {
2740  ni_pthread_mutex_unlock(&p_ctx->mutex);
2741  ni_log2(p_ctx, NI_LOG_ERROR,
2742  "ERROR %s() avaliable size(%u)"
2743  "less than needed (%u)\n",
2744  __func__, buf_info.buf_avail_size, total_bytes_to_read);
2745  abort();
2746  }
2747 
2748  if (buf_info.buf_avail_size == metadata_hdr_size && (!p_ctx->frame_num || !p_data_buffer))
2749  {
2750  if (ni_cmp_fw_api_ver(
2752  "6rE") >= 0)
2753  {
2754  // allocate p_data_buffer to read the first metadata
2755  void *p_metadata_buffer = NULL;
2756  int buffer_size = ((metadata_hdr_size + (NI_MEM_PAGE_ALIGNMENT - 1)) /
2758  if (ni_posix_memalign(&p_metadata_buffer, sysconf(_SC_PAGESIZE), buffer_size))
2759  {
2760  ni_log2(p_ctx, NI_LOG_ERROR,
2761  "ERROR %d: %s() Cannot allocate metadata buffer.\n",
2762  NI_ERRNO, __func__);
2763  retval = NI_RETCODE_ERROR_MEM_ALOC;
2764  LRETURN;
2765  }
2766  p_data_buffer = (uint8_t *)p_metadata_buffer;
2767  get_first_metadata = 1;
2768  if (!p_ctx->frame_num)
2769  sequence_change = 0;
2770  }
2771  }
2772 
2773  if (!p_data_buffer)
2774  {
2775  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: data buffer is null!, return\n");
2776  retval = NI_RETCODE_INVALID_PARAM;
2777  LRETURN;
2778  }
2779 
2780  read_size_bytes = buf_info.buf_avail_size;
2782  if (read_size_bytes % NI_MEM_PAGE_ALIGNMENT)
2783  {
2784  read_size_bytes = ((read_size_bytes / NI_MEM_PAGE_ALIGNMENT) *
2787  }
2788 
2789  retval = ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
2790  p_data_buffer, read_size_bytes, ui32LBA);
2791  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_read, p_ctx->device_type,
2792  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
2793  CHECK_VPU_RECOVERY(retval);
2794  if (retval < 0)
2795  {
2796  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
2798  LRETURN;
2799  } else if (get_first_metadata) {
2800  // got first metadata alone
2801  ni_metadata_dec_frame_t *p_meta =
2802  (ni_metadata_dec_frame_t *)((uint8_t *)p_data_buffer);
2803  ni_log2(p_ctx, NI_LOG_DEBUG, "Got first pkt_delay_cnt %u\n",
2804  p_meta->metadata_common.pkt_delay_cnt);
2805  if (p_ctx->pkt_delay_cnt < p_meta->metadata_common.pkt_delay_cnt)
2806  p_ctx->pkt_delay_cnt = p_meta->metadata_common.pkt_delay_cnt;
2807  get_first_metadata = 0;
2808  ni_aligned_free(p_data_buffer);
2809  goto start;
2810  } else {
2811  // command issued successfully, now exit
2812  ni_metadata_dec_frame_t *p_meta;
2813  p_meta =
2814  (ni_metadata_dec_frame_t *)((uint8_t *)p_frame->p_buffer +
2815  p_frame->data_len[0] +
2816  p_frame->data_len[1] +
2817  p_frame->data_len[2]);
2818 
2819  if (buf_info.buf_avail_size != metadata_hdr_size)
2820  {
2821  low_delay_notify = 1;
2822  sei_size = p_meta->sei_size;
2823  } else if (ni_cmp_fw_api_ver(
2825  "6rE") >= 0)
2826  {
2827  p_meta =
2828  (ni_metadata_dec_frame_t *)((uint8_t *)p_frame->p_buffer);
2829  ni_log2(p_ctx, NI_LOG_DEBUG, "Got pkt_delay_cnt %u\n",
2830  p_meta->metadata_common.pkt_delay_cnt);
2831  if (p_ctx->pkt_delay_cnt < p_meta->metadata_common.pkt_delay_cnt)
2832  p_ctx->pkt_delay_cnt = p_meta->metadata_common.pkt_delay_cnt;
2833  }
2834  total_bytes_to_read = total_bytes_to_read + sei_size;
2835  ni_log2(p_ctx, NI_LOG_DEBUG, "decoder read success, size %d total_bytes_to_read "
2836  "include sei %u sei_size %d\n",
2837  retval, total_bytes_to_read, sei_size);
2838  }
2839 
2840  bytes_read_so_far = total_bytes_to_read ;
2841  // Note: session status is NOT reset but tracked between send
2842  // and recv to catch and recover from a loop condition
2843 
2845  "6sP") >= 0)
2846  {
2847  rx_size = ni_create_frame(p_frame, bytes_read_so_far, &frame_offset, &frame_dropped, false);
2848  }
2849  else
2850  {
2851  rx_size = ni_create_frame(p_frame, bytes_read_so_far, &frame_offset, 0, false);
2852  }
2853  p_ctx->frame_pkt_offset = frame_offset;
2854  if (p_ctx->decoder_low_delay > 0 && buf_info.buf_avail_size == metadata_hdr_size &&
2855  p_ctx->enable_low_delay_check)
2856  {
2857  ni_log2(p_ctx, NI_LOG_TRACE, "Low delay mode amd check header if has b frame\n");
2858 
2859  ni_metadata_dec_frame_t *p_meta =
2860  (ni_metadata_dec_frame_t *)((uint8_t *)p_frame->p_buffer +
2861  p_frame->data_len[0] +
2862  p_frame->data_len[1] +
2863  p_frame->data_len[2]);
2864  if (p_meta->metadata_common.has_b_frame == 1)
2865  {
2866  ni_log2(p_ctx, NI_LOG_ERROR,"Warning: session 0x%x decoder lowDelay mode "
2867  "is cancelled due to has_b_frames, frame_num %u\n",
2868  p_ctx->session_id, p_ctx->frame_num);
2869  p_ctx->decoder_low_delay = 0;
2870  }
2871  }
2872 
2873  if (rx_size > 0)
2874  {
2875  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): s-state %d first_frame %d\n", __func__,
2876  p_ctx->session_run_state, p_ctx->is_first_frame);
2877 
2878  int64_t tmp_dts, prev_dts = INT64_MIN, ts_diff = 0;
2879  int nb_diff = 0;
2880 
2882  "6sP") >= 0)
2883  {
2884  if(p_ctx->last_frame_dropped + frame_dropped != p_ctx->session_statistic.ui32FramesDropped)
2885  {
2886  ni_log2(p_ctx, NI_LOG_DEBUG,
2887  "### %s(): Warning: ui32FramesDropped %u should be %u + %u\n",
2888  __func__, p_ctx->session_statistic.ui32FramesDropped,
2889  p_ctx->last_frame_dropped, frame_dropped);
2890 
2891  p_ctx->session_statistic.ui32FramesDropped = p_ctx->last_frame_dropped + frame_dropped;
2892  }
2893 
2895  }
2896 
2897  // at stream start, if there are already frames dropped at decoding, drop
2898  // equal number of dts from dts queue to try to align with the first
2899  // returned frame, and calculate the average of dts difference to be used
2900  // for locating the first frame's dts
2901  if (p_ctx->is_first_frame)
2902  {
2903  ni_log2(p_ctx, NI_LOG_DEBUG,
2904  "%s(): First frame : session_id 0x%x, pic_reorder_delay: %d "
2905  "total frames input:%u buffered: %u completed: %u output: %u "
2906  "dropped: %u (%u %u) error: %u\n",
2907  __func__, p_ctx->session_id, p_ctx->pic_reorder_delay,
2913  p_ctx->last_frame_dropped, frame_dropped,
2915 
2916  if (p_ctx->session_statistic.ui32FramesDropped > 0)
2917  {
2918  for (i = 0; i < p_ctx->session_statistic.ui32FramesDropped; i++)
2919  {
2921  p_ctx->dts_queue, 0, &tmp_dts,
2923  p_ctx->buffer_pool) != NI_RETCODE_SUCCESS)
2924  {
2925  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): FramesDropped pop "
2926  "decoder dts queue %d %ld failed !\n",
2927  __func__, i, tmp_dts);
2928  break;
2929  } else
2930  {
2931  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): FramesDropped pop "
2932  "decoder dts queue %d %ld success !\n",
2933  __func__, i, tmp_dts);
2934  if (prev_dts != INT64_MIN) {
2935  ts_diff += llabs(tmp_dts - prev_dts);
2936  nb_diff++;
2937  }
2938  prev_dts = tmp_dts;
2939  }
2940  }
2941  if (nb_diff > 1)
2942  {
2943  ts_diff = ts_diff / nb_diff;
2944  }
2946  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): FramesDropped pop dts "
2947  "average diff: %ld\n", __func__, ts_diff);
2948  }
2949  }
2950  else
2951  {
2953  {
2954  for(i = p_ctx->decoder_last_drop_frame_num; i < p_ctx->session_statistic.ui32FramesDropped; i++)
2955  {
2957  p_ctx->dts_queue, 0, &tmp_dts,
2959  p_ctx->buffer_pool) != NI_RETCODE_SUCCESS)
2960  {
2961  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): FramesDropped pop "
2962  "decoder dts queue %d %ld failed !\n",
2963  __func__, i, tmp_dts);
2964  break;
2965  }
2966  }
2968  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): FramesDropped pop dts %d\n", __func__, p_ctx->decoder_last_drop_frame_num);
2969  }
2970  }
2971 
2973  p_ctx->dts_queue, 0, (int64_t *)&p_frame->dts,
2975  p_ctx->buffer_pool) != NI_RETCODE_SUCCESS)
2976  {
2977  if (p_ctx->last_dts != NI_NOPTS_VALUE && !p_ctx->ready_to_close)
2978  {
2979  p_ctx->pic_reorder_delay++;
2980  p_frame->dts = p_ctx->last_dts + p_ctx->last_dts_interval;
2981  ni_log2(p_ctx, NI_LOG_DEBUG, "Padding DTS: %" PRId64 "\n", p_frame->dts);
2982  } else
2983  {
2984  p_frame->dts = NI_NOPTS_VALUE;
2985  }
2986  }
2987 
2988  if (p_ctx->is_first_frame)
2989  {
2990  for (i = 0; (int)i < p_ctx->pic_reorder_delay; i++)
2991  {
2992  if (p_ctx->last_pts == NI_NOPTS_VALUE &&
2993  p_ctx->last_dts == NI_NOPTS_VALUE)
2994  {
2995  // If the p_frame->pts is unknown in the very beginning we assume
2996  // p_frame->pts == 0 as well as DTS less than PTS by 1000 * 1/timebase
2997  if (p_frame->pts >= p_frame->dts &&
2998  p_frame->pts - p_frame->dts < 1000)
2999  {
3000  break;
3001  }
3002  }
3003 
3005  p_ctx->dts_queue, 0, (int64_t *)&p_frame->dts,
3007  p_ctx->frame_num % 500 == 0,
3008  p_ctx->buffer_pool) != NI_RETCODE_SUCCESS)
3009  {
3010  p_frame->dts = NI_NOPTS_VALUE;
3011  } else
3012  {
3013  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): pic_reorder_delay pop "
3014  "decoder dts queue %d %ld success !\n",
3015  __func__, i, p_frame->dts);
3016  }
3017  }
3018  // Reset for DTS padding counting
3019  p_ctx->pic_reorder_delay = 0;
3020  }
3021  if (p_ctx->codec_format == NI_CODEC_FORMAT_JPEG)//fw won't save frameoffset when decoding jpeg.
3022  {
3023  if (p_ctx->is_first_frame)
3024  {
3025  p_ctx->is_first_frame = 0;
3026  }
3027  p_frame->pts = p_ctx->pts_offsets[p_ctx->frame_num % NI_FIFO_SZ];
3028  p_frame->flags = p_ctx->flags_array[p_ctx->frame_num % NI_FIFO_SZ];
3029  p_frame->pkt_pos = p_ctx->pkt_pos[p_ctx->frame_num % NI_FIFO_SZ];
3030  ni_log2(p_ctx, NI_LOG_DEBUG, "p_frame->pts = %u, frame_num = %d, p_frame->dts = %u\n",
3031  p_frame->pts, p_ctx->frame_num, p_frame->dts);
3032  }
3033  else
3034  {
3035  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: frame_offset %" PRIu64 "\n", __func__,
3036  frame_offset);
3037 
3038  // search for the pkt_offsets of received frame according to frame_offset.
3039  // 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)
3040  // i = -1 if not found
3041  int j = 0;
3042  j = rotated_array_binary_search(p_ctx->pkt_offsets_index_min,
3043  p_ctx->pkt_offsets_index, NI_FIFO_SZ,
3044  frame_offset);
3045  if (j >= 0)
3046  {
3047  p_frame->pts = p_ctx->pts_offsets[j];
3048  p_frame->flags = p_ctx->flags_array[j];
3049  p_frame->pkt_pos = p_ctx->pkt_pos[j];
3050  ni_log2(p_ctx, NI_LOG_DEBUG,
3051  "%s: (found pts) dts %" PRId64 " pts "
3052  "%" PRId64 " frame_offset %" PRIu64 " j %d "
3053  "pkt_offsets_index_min %" PRIu64 " pkt_offsets_index "
3054  "%" PRIu64 " pkt_pos %" PRIu64 " \n",
3055  __func__, p_frame->dts, p_frame->pts, frame_offset, j,
3056  p_ctx->pkt_offsets_index_min[j],
3057  p_ctx->pkt_offsets_index[j],
3058  p_ctx->pkt_pos[j]);
3059 
3060  if (p_ctx->is_first_frame)
3061  {
3062  // if the first frame is I frame and there are dropped frames,
3063  // find the dts closest to its pts using the average of dts diff
3064  if (p_frame->dts != NI_NOPTS_VALUE &&
3065  p_frame->pts != NI_NOPTS_VALUE &&
3066  PIC_TYPE_I == p_frame->ni_pict_type &&
3067  p_ctx->session_statistic.ui32FramesDropped > 0 &&
3068  ts_diff > 0)
3069  {
3070  while (p_frame->dts < p_frame->pts &&
3071  llabs(p_frame->pts - p_frame->dts) > ts_diff)
3072  {
3073  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: First I frame pts %lld "
3074  "dts %lld diff. %lld > ts_diff %lld\n",
3075  __func__, p_frame->pts, p_frame->dts,
3076  llabs(p_frame->pts - p_frame->dts), ts_diff);
3077 
3079  p_ctx->dts_queue, 0, (int64_t *)&p_frame->dts,
3081  {
3082  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): First I frame "
3083  "pop decoder dts queue error.\n", __func__);
3084  break;
3085  }
3086  }
3087  }
3088  }
3089 
3090  p_frame->p_custom_sei_set = p_ctx->pkt_custom_sei_set[j];
3091  p_ctx->pkt_custom_sei_set[j] = NULL;
3092  } else
3093  {
3094  // backup solution pts
3095  if (p_param->dec_input_params.skip_pts_guess && p_ctx->last_pts != NI_NOPTS_VALUE)
3096  {
3097  // if skip guess_correct_pts, use pts interval to get the correct pts
3098  p_frame->pts = p_ctx->last_pts + (p_ctx->last_pts_interval > 0 ? p_ctx->last_pts_interval : 1);
3099  }
3100  else
3101  {
3102  p_frame->pts = p_ctx->last_pts + (p_frame->dts - p_ctx->last_dts);
3103  }
3104  p_frame->pkt_pos = p_ctx->last_pkt_pos + (frame_offset - p_ctx->last_frame_offset);
3105  ni_log2(p_ctx, NI_LOG_ERROR,
3106  "ERROR: Frame pts %" PRId64 " not found for offset "
3107  "%" PRIu64 "\n", p_frame->pts, frame_offset);
3108  ni_log2(p_ctx, NI_LOG_DEBUG,
3109  "%s: (not found use default) dts %" PRId64 " pts %" PRId64
3110  "\n",
3111  __func__, p_frame->dts, p_frame->pts);
3112  }
3113 
3114  if (p_ctx->is_first_frame)
3115  {
3116  p_ctx->is_first_frame = 0;
3117  }
3118  }
3119  p_frame->orignal_pts = p_frame->pts;
3120  p_ctx->last_pkt_pos = p_frame->pkt_pos;
3121  p_ctx->last_frame_offset = frame_offset;
3122  if (!p_param->dec_input_params.skip_pts_guess)
3123  p_frame->pts = guess_correct_pts(p_ctx, p_frame->pts, p_frame->dts);
3124  if (p_frame->pts != NI_NOPTS_VALUE && p_ctx->last_pts != NI_NOPTS_VALUE)
3125  p_ctx->last_pts_interval = p_frame->pts - p_ctx->last_pts;
3126  p_ctx->last_pts = p_frame->pts;
3127  if (p_frame->dts != NI_NOPTS_VALUE && p_ctx->last_dts != NI_NOPTS_VALUE)
3128  p_ctx->last_dts_interval = p_frame->dts - p_ctx->last_dts;
3129  p_ctx->last_dts = p_frame->dts;
3130  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: (best_effort_timestamp) pts %" PRId64 "\n",
3131  __func__, p_frame->pts);
3132  p_ctx->frame_num++;
3133 
3135  if (p_frame->error_ratio > 0)
3136  {
3137  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: frame number = %d, error_ratio = %u\n",
3138  __func__, p_ctx->frame_num, p_frame->error_ratio);
3139  }
3140 
3141 #ifdef MEASURE_LATENCY
3142 #ifndef XCODER_311
3143  ni_log2(p_ctx, NI_LOG_INFO, "DEC pkt_num %d, fme_num %d, latecy is %d\n",
3144  p_ctx->pkt_num, p_ctx->frame_num, p_ctx->pkt_num - p_ctx->frame_num);
3145 #endif
3146 #endif
3147 
3148 #ifdef XCODER_DUMP_DATA
3149  char dump_file[256];
3150  snprintf(dump_file, sizeof(dump_file), "%ld-%u-dec-fme/fme-%04ld.yuv",
3151  (long)getpid(), p_ctx->session_id, (long)p_ctx->frame_num);
3152  FILE *f = NULL;
3153  ni_fopen(&f, dump_file, "wb");
3154  fwrite(p_frame->p_buffer,
3155  p_frame->data_len[0] + p_frame->data_len[1] + p_frame->data_len[2],
3156  1, f);
3157  fflush(f);
3158  fclose(f);
3159 #endif
3160  }
3161 
3162  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): received data: [0x%08x]\n", __func__, rx_size);
3163  ni_log2(p_ctx, NI_LOG_DEBUG,
3164  "%s(): p_frame->start_of_stream=%u, "
3165  "p_frame->end_of_stream=%u, p_frame->video_width=%u, "
3166  "p_frame->video_height=%u\n",
3167  __func__, p_frame->start_of_stream, p_frame->end_of_stream,
3168  p_frame->video_width, p_frame->video_height);
3169  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): p_frame->data_len[0/1/2]=%u/%u/%u\n", __func__,
3170  p_frame->data_len[0], p_frame->data_len[1], p_frame->data_len[2]);
3171 
3172  if (p_ctx->frame_num % 500 == 0)
3173  {
3174  ni_log2(p_ctx, NI_LOG_DEBUG,
3175  "Decoder pts queue size = %u dts queue size = %u\n\n",
3176  p_ctx->pts_table->list.count, p_ctx->dts_queue->list.count);
3177  // scan and clean up
3179  p_ctx->buffer_pool);
3180  }
3181 
3182 #ifdef MEASURE_LATENCY
3183  if ((p_frame->dts != NI_NOPTS_VALUE) && (p_ctx->frame_time_q != NULL))
3184  {
3185  uint64_t abs_time_ns = ni_gettime_ns();
3187  ni_log2(p_ctx, NI_LOG_INFO, "DTS:%" PRId64 ",DELTA:%" PRId64 ",dLAT:%" PRIu64 ";\n",
3188  p_frame->dts, abs_time_ns - q->last_benchmark_time,
3189  ni_lat_meas_q_check_latency(q, abs_time_ns, p_frame->dts));
3190  q->last_benchmark_time = abs_time_ns;
3191  }
3192 #endif
3193 
3194 END:
3195 
3196  ni_pthread_mutex_unlock(&p_ctx->mutex);
3197 
3198  if (get_first_metadata && p_data_buffer)
3199  ni_aligned_free(p_data_buffer);
3200  if (sequence_change && p_ctx->frame_num)
3201  {
3202  if (p_ctx->actual_video_width == p_frame->video_width &&
3203  p_ctx->active_video_height == p_frame->video_height)
3204  {
3205  p_ctx->pixel_format_changed = 1;
3206  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): format changed\n", __func__);
3207  }
3208  }
3209 
3210  if (NI_RETCODE_SUCCESS != retval)
3211  {
3212  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): bad exit, retval = %d\n", __func__, retval);
3213  if (retval == NI_RETCODE_ERROR_VPU_RECOVERY)
3214  {
3215  low_delay_signal(p_ctx);
3216  }
3217  return retval;
3218  } else
3219  {
3220  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit, rx_size = %d\n", __func__, rx_size);
3221  if (low_delay_notify)
3222  {
3223  low_delay_signal(p_ctx);
3224  }
3225  return rx_size;
3226  }
3227 }
3228 
3229 /*!******************************************************************************
3230  * \brief Query current xcoder status
3231  *
3232  * \param
3233  *
3234  * \return
3235  *******************************************************************************/
3237  ni_device_type_t device_type)
3238 {
3240  int retval = NI_RETCODE_SUCCESS;
3241 
3242  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): device_type %d:%s; enter\n", __func__,
3243  device_type, g_device_type_str[device_type]);
3244 
3245  if (!p_ctx)
3246  {
3247  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
3248  __func__);
3249  retval = NI_RETCODE_INVALID_PARAM;
3250  LRETURN;
3251  }
3252 
3253  retval = ni_query_general_status(p_ctx, device_type, &data);
3254  if (NI_RETCODE_SUCCESS == retval)
3255  {
3259  p_ctx->overall_load_query.admin_queried = (uint32_t)data.admin_nsid;
3260  p_ctx->load_query.current_load =
3261  (uint32_t)data.process_load_percent | (uint32_t)(data.process_load_percent_upper << 8);
3262  p_ctx->load_query.fw_model_load = (uint32_t)data.fw_model_load;
3263  p_ctx->load_query.fw_load = (uint32_t)data.fw_load;
3265 
3266  switch (device_type)
3267  {
3270  case NI_DEVICE_TYPE_UPLOAD:
3271  if (p_ctx->load_query.fw_load == 1 &&
3273  {
3274  // ignore negligible non-video traffic
3275  p_ctx->load_query.fw_load -= 1;
3276  }
3277  break;
3278  default:
3279  break;
3280  }
3281  p_ctx->load_query.total_contexts = (uint32_t)data.active_sub_instances_cnt;
3282  p_ctx->load_query.fw_video_mem_usage = (uint32_t)data.fw_video_mem_usage;
3284  p_ctx->load_query.fw_share_mem_usage = (uint32_t)data.fw_share_mem_usage;
3285  p_ctx->load_query.fw_p2p_mem_usage = (uint32_t)data.fw_p2p_mem_usage;
3287  (uint32_t)data.active_hwupload_sub_inst_cnt;
3288  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 "
3289  "total_contexts:%u fw_video_mem_usage:%u "
3290  "fw_video_shared_mem_usage:%u fw_share_mem_usage:%u "
3291  "fw_p2p_mem_usage:%u active_hwuploaders:%u\n", __func__,
3292  p_ctx->blk_dev_name,
3293  p_ctx->blk_xcoder_name,
3294  p_ctx->dev_xcoder_name,
3295  p_ctx->load_query.current_load,
3296  p_ctx->load_query.fw_model_load,
3297  p_ctx->load_query.fw_load,
3298  p_ctx->load_query.total_contexts,
3304  if (p_ctx->overall_load_query.admin_queried)
3305  {
3306  ni_log2(p_ctx, NI_LOG_DEBUG,
3307  "Overall load %u, model load %u, instance count %u\n",
3311  }
3312  }
3313 
3314 END:
3315 
3316  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
3317 
3318  return retval;
3319 }
3320 
3321 /*!******************************************************************************
3322  * \brief Query current xcoder status
3323  *
3324  * \param
3325  *
3326  * \return
3327  *******************************************************************************/
3329  ni_device_type_t device_type, void *detail_data, int ver)
3330 {
3331  int retval = NI_RETCODE_SUCCESS;
3332 
3333  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): device_type %d:%s; enter\n", __func__,
3334  device_type, g_device_type_str[device_type]);
3335 
3336  if (!p_ctx)
3337  {
3338  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
3339  __func__);
3340  retval = NI_RETCODE_INVALID_PARAM;
3341  LRETURN;
3342  }
3343 
3344  retval = ni_query_detail_status(p_ctx, device_type, detail_data, ver);
3345 
3346 END:
3347 
3348  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
3349 
3350  return retval;
3351 }
3352 
3353 /*!******************************************************************************
3354  * \brief Open a xcoder encoder instance
3355  *
3356  * \param
3357  *
3358  * \return
3359  *******************************************************************************/
3360 
3362 {
3364  ni_xcoder_params_t *p_param;
3365  void *p_buffer = NULL;
3366  ni_instance_buf_info_t buf_info = {0};
3367  uint32_t ui32LBA = 0;
3368  uint32_t max_cu_size;
3369  uint32_t block_size;
3370  int32_t width, height;
3371  int m_threshold = 0;
3372  char fmt_fw_api_ver1[5], fmt_fw_api_ver2[5];
3373 
3374  if (!p_ctx || !p_ctx->p_session_config)
3375  {
3376  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
3377  __func__);
3378  retval = NI_RETCODE_INVALID_PARAM;
3379  LRETURN;
3380  }
3381 
3382  p_param = (ni_xcoder_params_t *)p_ctx->p_session_config;
3383  bool isrgba = (p_ctx->pixel_format == NI_PIX_FMT_ABGR || p_ctx->pixel_format == NI_PIX_FMT_ARGB
3384  || p_ctx->pixel_format == NI_PIX_FMT_RGBA || p_ctx->pixel_format == NI_PIX_FMT_BGRA);
3385  if (isrgba)
3386  {
3388  || p_param->source_width < NI_MIN_WIDTH || p_param->source_height < NI_MIN_HEIGHT)
3389  {
3390  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() RGBA / BGRA / ARGB / AGBR resolution invalid, return\n",
3391  __func__);
3392  retval = NI_RETCODE_INVALID_PARAM;
3393  LRETURN;
3394  }
3395  }
3396 
3397  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter hwframes = %d\n", __func__,
3398  p_param->hwframes);
3399 
3400  // Select the CPU based on the NUMA node.
3401  if (p_param->enableCpuAffinity)
3402  {
3403 #if defined(__linux__) && defined(XCODER_ENABLE_CPU_AFFINITY)
3404  retval = ni_set_cpu_affinity(p_ctx);
3405  if (NI_RETCODE_SUCCESS != retval)
3406  {
3407  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): ni_set_cpu_affinity failed, ret=%d\n",
3408  __func__, retval);
3409  LRETURN;
3410  }
3411 #else
3412  ni_log2(p_ctx, NI_LOG_INFO, "Warning %s(): not support enableCpuAffinity parameter, "
3413  "enable with [./build.sh -c] on linux\n", __func__);
3414 #endif
3415  }
3416 
3417  // calculate encoder ROI map size: each QP info takes 8-bit, represent 8 x 8
3418  // pixel block
3419  max_cu_size = (NI_CODEC_FORMAT_H264 == p_ctx->codec_format) ? 16 : 64;
3420 
3421  width = p_param->source_width;
3422  height = p_param->source_height;
3423  // AV1 non-8x8-aligned resolution is implicitly cropped due to Quadra HW limitation
3424  if (NI_CODEC_FORMAT_AV1 == p_ctx->codec_format)
3425  {
3426  width = (width / 8) * 8;
3427  height = (height / 8) * 8;
3428  }
3429 
3430  block_size =
3431  ((width + max_cu_size - 1) & (~(max_cu_size - 1))) *
3432  ((height + max_cu_size - 1) & (~(max_cu_size - 1))) /
3433  (8 * 8);
3434  p_ctx->roi_len = ((block_size + 63) & (~63)); // align to 64 bytes
3435 
3436  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): finish init\n", __func__);
3437 
3438  //Check if there is an instance or we need a new one
3439  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
3440  {
3442  p_ctx->pts_table = NULL;
3443  p_ctx->dts_queue = NULL;
3444  p_ctx->buffer_pool = NULL;
3445  p_ctx->status = 0;
3446  p_ctx->key_frame_type = 0;
3447  p_ctx->keyframe_factor = 1;
3448  p_ctx->frame_num = 0;
3449  p_ctx->pkt_num = 0;
3450  p_ctx->av1_pkt_num = 0;
3451  p_ctx->rc_error_count = 0;
3452  p_ctx->force_frame_type = 0;
3453  p_ctx->ready_to_close = 0;
3454  p_ctx->auto_dl_handle = 0;
3455  //Sequence change tracking related stuff
3456  p_ctx->active_video_width = 0;
3457  p_ctx->active_video_height = 0;
3458  p_ctx->p_all_zero_buf = NULL;
3459  p_ctx->actual_video_width = 0;
3460  p_ctx->enc_pts_w_idx = 0;
3461  p_ctx->enc_pts_r_idx = 0;
3462  p_ctx->session_timestamp = 0;
3463  memset(p_ctx->pkt_custom_sei_set, 0, NI_FIFO_SZ * sizeof(ni_custom_sei_set_t *));
3464  memset(&(p_ctx->param_err_msg[0]), 0, sizeof(p_ctx->param_err_msg));
3466  {
3468  }
3469 
3470  //malloc zero data buffer
3471  if (ni_posix_memalign(&p_ctx->p_all_zero_buf, sysconf(_SC_PAGESIZE),
3473  {
3474  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() alloc all zero buffer failed\n",
3475  NI_ERRNO, __func__);
3476  retval = NI_RETCODE_ERROR_MEM_ALOC;
3477  LRETURN;
3478  }
3479  memset(p_ctx->p_all_zero_buf, 0, NI_DATA_BUFFER_LEN);
3480 
3481  //malloc data buffer
3482  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), NI_DATA_BUFFER_LEN))
3483  {
3484  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() alloc data buffer failed\n",
3485  NI_ERRNO, __func__);
3487  retval = NI_RETCODE_ERROR_MEM_ALOC;
3488  LRETURN;
3489  }
3490  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
3491 
3492  //Set session ID to be invalid. In case we cannot open session, the session id wold remain invalid.
3493  //In case we can open sesison, the session id would become valid.
3494  ((ni_session_stats_t *)p_buffer)->ui16SessionId =
3495  (uint16_t)NI_INVALID_SESSION_ID;
3496 
3497  // First uint32_t is either an invaild session ID or a valid session ID, depending on if session could be opened
3499  ni_htonl(p_ctx->codec_format), 0);
3500  ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_buffer,
3501  NI_DATA_BUFFER_LEN, ui32LBA);
3502  //Open will return a session status structure with a valid session id if it worked.
3503  //Otherwise the invalid session id set before the open command will stay
3504  p_ctx->session_id =
3505  ni_ntohs(((ni_session_stats_t *)p_buffer)->ui16SessionId);
3506  p_ctx->session_timestamp = ni_htonl(((ni_session_stats_t *)p_buffer)->ui32Session_timestamp_high);
3507  p_ctx->session_timestamp = (p_ctx->session_timestamp << 32) |
3508  ni_htonl(((ni_session_stats_t *)p_buffer)->ui32Session_timestamp_low);
3509  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
3510  {
3511  ni_log2(p_ctx, NI_LOG_ERROR,
3512  "ERROR %s(): p_ctx->device_handle=%" PRIx64 ", "
3513  "p_ctx->hw_id=%d, p_ctx->session_id=%d\n",
3514  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
3515  p_ctx->session_id);
3516  ni_encoder_session_close(p_ctx, 0);
3518  LRETURN;
3519  }
3520  ni_log2(p_ctx, NI_LOG_DEBUG, "Encoder open session ID:0x%x timestamp:%" PRIu64 "\n",
3521  p_ctx->session_id, p_ctx->session_timestamp);
3522 
3523  //Send keep alive timeout Info
3524  uint64_t keep_alive_timeout =
3525  p_ctx->keep_alive_timeout * 1000000; //send us to FW
3526  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
3527  memcpy(p_buffer, &keep_alive_timeout, sizeof(keep_alive_timeout));
3528  ni_log2(p_ctx, NI_LOG_DEBUG, "%s keep_alive_timeout %" PRIx64 "\n", __func__,
3529  keep_alive_timeout);
3531  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
3532  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
3533  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config, p_ctx->device_type,
3534  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
3535  CHECK_VPU_RECOVERY(retval);
3536 
3537  if (NI_RETCODE_SUCCESS != retval)
3538  {
3539  ni_log2(p_ctx, NI_LOG_ERROR,
3540  "ERROR %s(): nvme write keep_alive_timeout command "
3541  "failed, blk_io_handle: %" PRIx64 ", hw_id, %d\n",
3542  __func__, (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id);
3544  LRETURN;
3545  }
3546 
3547  // Send SW version to FW if FW API version is >= 6.2
3549  "62") >= 0)
3550  {
3551  // Send SW version to session manager
3552  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
3553  memcpy(p_buffer, NI_XCODER_REVISION, sizeof(uint64_t));
3555  ni_fmt_fw_api_ver_str((char*) &p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], &fmt_fw_api_ver2[0]);
3556  ni_log2(p_ctx, NI_LOG_DEBUG, "%s libxcoder FW API ver %s, FW FW API ver %s\n",
3557  __func__, fmt_fw_api_ver1, fmt_fw_api_ver2);
3558  ui32LBA = CONFIG_SESSION_SWVersion_W(p_ctx->session_id);
3559  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
3560  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
3561  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config, p_ctx->device_type,
3562  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
3563  CHECK_VPU_RECOVERY(retval);
3564 
3565  if (NI_RETCODE_SUCCESS != retval)
3566  {
3567  ni_log2(p_ctx, NI_LOG_ERROR,
3568  "ERROR %s(): nvme write sw_version command "
3569  "failed, blk_io_handle: %" PRIx64 ", hw_id, %d\n",
3570  __func__, (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id);
3572  LRETURN;
3573  }
3574 
3575  if (ni_cmp_fw_api_ver((char*) &p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6sM") >= 0)
3576  {
3577  // For FW API ver 6sM or newer, initialize with the most current size
3578  p_ctx->meta_size = sizeof(ni_metadata_enc_bstream_t);
3579  }
3580  else if (ni_cmp_fw_api_ver((char*) &p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6rc") >= 0)
3581  {
3582  // For FW API ver 6rc or newer, initialize with the most current size
3584  }
3586  "6p") >= 0)
3587  // For FW API ver 6.p or newer, initialize with the most current size
3589  else
3590  // For FW API ver 6.2 or newer, initialize with the most current size
3592  }
3593  else
3594  {
3595  // For FW API ver 6.1 or older, initialize with metadata size 32
3597  }
3598 
3599  // Open AI session for AI Enhance
3600  if(p_param->enable_ai_enhance)
3601  {
3602  // Check if the bit depth & resoultion
3603  if ((p_ctx->bit_depth_factor != 1) ||
3604  !((p_param->source_width == 1280 && p_param->source_height == 720) ||
3605  (p_param->source_width == 1920 && p_param->source_height == 1080) ||
3606  (p_param->source_width == 3840 && p_param->source_height == 2160) ||
3607  (p_param->source_width == 720 && p_param->source_height == 1280) ||
3608  (p_param->source_width == 768 && p_param->source_height == 1280) ||
3609  (p_param->source_width == 1080 && p_param->source_height == 1920) ||
3610  (p_param->source_width == 2496 && p_param->source_height == 1080)))
3611  {
3612  p_param->enable_ai_enhance = 0;
3613  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);
3614  ni_log2(p_ctx, NI_LOG_ERROR, "The bit depth or resoultion is not supported on ai enhance mode\n");
3615  }else{
3616  ni_log2(p_ctx, NI_LOG_DEBUG, "Adding ai enhance session to encoder\n");
3617  ui32LBA = OPEN_ADD_CODEC(NI_DEVICE_TYPE_AI, 0, ni_htons(p_ctx->session_id));
3618  retval = ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
3619  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
3620 
3621  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_open,
3622  p_ctx->device_type, p_ctx->hw_id,
3623  &(p_ctx->session_id), OPT_1);
3624  if (NI_RETCODE_SUCCESS != retval)
3625  {
3626  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR couldn't add ai to encoder session\n");
3627  ni_log2(p_ctx, NI_LOG_ERROR,
3628  "ERROR %s(): p_ctx->device_handle=%" PRIx64 ", "
3629  "p_ctx->hw_id=%d, p_ctx->session_id=%d\n",
3630  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
3631  p_ctx->session_id);
3632  ni_encoder_session_close(p_ctx, 0);
3633  LRETURN;
3634  }
3635  }
3636  }
3637 
3638  if (p_param->ddr_priority_mode > NI_DDR_PRIORITY_NONE)
3639  {
3640  retval = ni_device_set_ddr_configuration(p_ctx, p_param->ddr_priority_mode);
3641  if (NI_RETCODE_SUCCESS != retval)
3642  {
3643  char errmsg[NI_ERRNO_LEN] = {0};
3644  ni_strerror(errmsg, NI_ERRNO_LEN, NI_ERRNO);
3645  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): ddr priority setting failure for %s\n",
3646  __func__, errmsg);
3647  LRETURN;
3648  }
3649  }
3650 
3651  ni_log2(p_ctx, NI_LOG_DEBUG, "Open session completed\n");
3652  }
3653 
3654  /* Modify mmap_threshold and trim_threshold of malloc */
3655  // calculate the default threshold which equals to the resolution + padding size.
3656  // 64 is AV_INPUT_BUFFER_PADDING_SIZE
3657  m_threshold = p_param->source_width * p_param->source_height * 3 / 2 *
3658  p_ctx->bit_depth_factor + 64;
3659  m_threshold = ((m_threshold + (NI_MEM_PAGE_ALIGNMENT - 1)) /
3661 
3662  // The upper limit is DEFAULT_MMAP_THRESHOLD_MAX: 512*1024 on 32-bit systems,
3663  // or 4*1024*1024*sizeof(long) on 64-bit systems.
3664  // This macro is not defined in header file.
3665  // So, assume that is running on 64-bit systems
3666  if (m_threshold > 4 * 1024 * 1024 * sizeof(long))
3667  {
3668  ni_log2(p_ctx, NI_LOG_INFO, "Warning: m_threshold (%d) is bigger than "
3669  "DEFAULT_MMAP_THRESHOLD_MAX, use default value (%d)\n",
3670  m_threshold, 4 * 1024 * 1024 * sizeof(long));
3671  m_threshold = 4 * 1024 * 1024 * sizeof(long);
3672  }
3673 
3674  if (p_param->staticMmapThreshold) // Set Static Mmap Threshold
3675  {
3676 #if defined(__linux__) && !defined(_ANDROID) && !defined(__OPENHARMONY__)
3677  // If the malloc buffer is larger than the threshold,
3678  // glibc will use mmap to malloc the memory, which is a low speed method.
3679  // So, set the M_MMAP_THRESHOLD >= 1 yuv buffer size here.
3680  if (mallopt(M_MMAP_THRESHOLD, m_threshold) == 0)
3681  {
3682  ni_log2(p_ctx, NI_LOG_ERROR, "Error: could not set M_MMAP_THRESHOLD to %d\n",
3683  m_threshold);
3684  retval = NI_RETCODE_ERROR_MEM_ALOC;
3685  LRETURN;
3686  }
3687 
3688  // When the amount of contiguous free memory at the top of the heap
3689  // grows larger than M_TRIM_THRESHOLD,
3690  // free(3) employs sbrk(2) to release this memory back to the system.
3691  // So, set the M_TRIM_THRESHOLD to 3*m_threshold,
3692  // to avoid an inefficient case, frequently releasing and requesting physical memory.
3693  if (mallopt(M_TRIM_THRESHOLD, 3 * m_threshold) == 0)
3694  {
3695  ni_log2(p_ctx, NI_LOG_ERROR, "Error: could not set M_TRIM_THRESHOLD to %ds\n",
3696  3 * m_threshold);
3697  retval = NI_RETCODE_ERROR_MEM_ALOC;
3698  LRETURN;
3699  }
3700 #else
3701  ni_log2(p_ctx, NI_LOG_INFO,
3702  "Warning: this platform does not support staticMmapThreshold\n");
3703 #endif
3704  }
3705  else // default
3706  {
3707  // a way to dynamically trigger a pre-mature increase in mmap&trim threshold
3708  // make the mmap_threshold larger than frame size to avoid frequent page faults
3709  m_threshold = (int) (m_threshold * 1.25); // An experience value
3710  void *temp_buf = malloc((size_t) m_threshold);
3711  ni_log2(p_ctx, NI_LOG_TRACE, "trigger a pre-mature increase in mmap&trim threshold: 0x%p = malloc(%d)\n",
3712  temp_buf, m_threshold);
3713  free(temp_buf);
3714  }
3715 
3716  if (p_ctx->hw_action == NI_CODEC_HW_ENABLE)
3717  {
3718  ni_device_capability_t sender_cap, receiver_cap;
3719  bool device_in_ctxt = true;
3720  retval = ni_device_capability_query2(p_ctx->sender_handle, &sender_cap, device_in_ctxt);
3721  if (retval != NI_RETCODE_SUCCESS)
3722  {
3723  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: ni_device_capability_query2 returned %d\n",
3724  retval);
3725  LRETURN;
3726  }
3727  retval = ni_device_capability_query2(p_ctx->blk_io_handle, &receiver_cap, device_in_ctxt);
3728  if (retval != NI_RETCODE_SUCCESS)
3729  {
3730  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: ni_device_capability_query2 returned %d\n",
3731  retval);
3732  LRETURN;
3733  }
3734 
3735  for (uint8_t ui8Index = 0; ui8Index < 20; ui8Index++)
3736  {
3737  if (sender_cap.serial_number[ui8Index] !=
3738  receiver_cap.serial_number[ui8Index])
3739  {
3740  // QDFW-315 Autodownload
3741  p_ctx->auto_dl_handle = p_ctx->sender_handle;
3742  ni_log2(p_ctx, NI_LOG_DEBUG, "Autodownload device handle set %p!\n",
3743  p_ctx->auto_dl_handle);
3744  p_ctx->hw_action = NI_CODEC_HW_NONE;
3745  break;
3746  }
3747  else
3748  {
3750  "6m") >= 0)
3751  {
3752  ni_device_vf_ns_id_t sender_vf_ns_id = {0};
3753  ni_device_vf_ns_id_t curr_vf_ns_id = {0};
3754  retval = ni_query_vf_ns_id(p_ctx->sender_handle, &sender_vf_ns_id, p_ctx->fw_rev);
3755  if (retval != NI_RETCODE_SUCCESS)
3756  {
3757  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: calling ni_query_vf_ns_id(): "
3758  "p_ctx->sender_handle=%" PRIx64 ", p_ctx->hw_id=%d, p_ctx->session_id=%u\n",
3759  (int64_t)p_ctx->sender_handle, p_ctx->hw_id, p_ctx->session_id);
3760  ni_encoder_session_close(p_ctx, 0);
3761  LRETURN;
3762  }
3763  retval = ni_query_vf_ns_id(p_ctx->blk_io_handle, &curr_vf_ns_id, p_ctx->fw_rev);
3764  if (retval != NI_RETCODE_SUCCESS)
3765  {
3766  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: calling ni_query_vf_ns_id(): "
3767  "p_ctx->blk_io_handle=%" PRIx64 ", p_ctx->hw_id=%d, p_ctx->session_id=%u\n",
3768  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
3769  ni_encoder_session_close(p_ctx, 0);
3770  LRETURN;
3771  }
3772 
3773  if ((sender_vf_ns_id.ns_id != curr_vf_ns_id.ns_id) ||
3774  (sender_vf_ns_id.vf_id != curr_vf_ns_id.vf_id))
3775  {
3776  p_ctx->auto_dl_handle = p_ctx->sender_handle;
3777  ni_log2(p_ctx, NI_LOG_DEBUG, "Autodownload device handle set %p!\n",
3778  p_ctx->auto_dl_handle);
3779  p_ctx->hw_action = NI_CODEC_HW_NONE;
3780  break;
3781  }
3782  }
3783  }
3784  }
3785  }
3786 
3787  retval = ni_config_instance_set_encoder_params(p_ctx);
3788  if (NI_RETCODE_SUCCESS != retval)
3789  {
3790  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: calling ni_config_instance_set_encoder_params(): "
3791  "p_ctx->device_handle=%" PRIx64 ", p_ctx->hw_id=%d, p_ctx->session_id=%d\n",
3792  (int64_t)p_ctx->device_handle, p_ctx->hw_id, p_ctx->session_id);
3793  ni_encoder_session_close(p_ctx, 0);
3794  LRETURN;
3795  }
3796  if(p_param->enable_ai_enhance)
3797  {
3798  retval = ni_ai_query_network_ready(p_ctx);
3799  if(retval != NI_RETCODE_SUCCESS)
3800  {
3801  ni_log2(p_ctx, NI_LOG_ERROR,
3802  "ERROR: ni_ai_query_network_ready returned %d\n", retval);
3803  LRETURN;
3804  }
3805  }
3806 
3807  ni_timestamp_init(p_ctx, &p_ctx->pts_table, "enc_pts");
3808  ni_timestamp_init(p_ctx, &p_ctx->dts_queue, "enc_dts");
3809 
3810  // init close caption SEI header and trailer
3811  memcpy(p_ctx->itu_t_t35_cc_sei_hdr_hevc, g_itu_t_t35_cc_sei_hdr_hevc,
3813  memcpy(p_ctx->itu_t_t35_cc_sei_hdr_h264, g_itu_t_t35_cc_sei_hdr_h264,
3815  memcpy(p_ctx->sei_trailer, g_sei_trailer, NI_CC_SEI_TRAILER_LEN);
3816  // init hdr10+ SEI header
3817  memcpy(p_ctx->itu_t_t35_hdr10p_sei_hdr_hevc, g_itu_t_t35_hdr10p_sei_hdr_hevc,
3819  memcpy(p_ctx->itu_t_t35_hdr10p_sei_hdr_h264, g_itu_t_t35_hdr10p_sei_hdr_h264,
3821 
3822  // query to check the final encoder config status
3823  for (;;)
3824  {
3826  NI_DEVICE_TYPE_ENCODER, &buf_info);
3827 
3828  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
3829  p_ctx->device_type, p_ctx->hw_id,
3830  &(p_ctx->session_id), OPT_1);
3831 
3833  || NI_RETCODE_FAILURE == retval)
3834  {
3835  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() line-%d return %d\n", __func__, __LINE__, retval);
3836  LRETURN;
3837  }
3838  else if (buf_info.buf_avail_size > 0)
3839  {
3840  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): buf_avail_size %u\n", __func__,
3841  buf_info.buf_avail_size);
3842  break;
3843  }
3844  else
3845  {
3846  ni_usleep(1000);
3847  }
3848  }
3849 
3850  ni_log2(p_ctx, NI_LOG_DEBUG,
3851  "%s(): p_ctx->device_handle=%" PRIx64 ", p_ctx->hw_id=%d, "
3852  "p_ctx->session_id=%d\n",
3853  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
3854  p_ctx->session_id);
3855 
3856 #ifdef XCODER_DUMP_DATA
3857  char dir_name[256] = {0};
3858  snprintf(dir_name, sizeof(dir_name), "%ld-%u-enc-pkt", (long)getpid(),
3859  p_ctx->session_id);
3860  DIR *dir = opendir(dir_name);
3861  if (!dir && ENOENT == NI_ERRNO)
3862  {
3863  mkdir(dir_name, S_IRWXU | S_IRWXG | S_IRWXO);
3864  ni_log2(p_ctx, NI_LOG_DEBUG, "Encoder pkt dump dir created: %s\n", dir_name);
3865  }
3866 
3867  if(dir){
3868  closedir(dir);
3869  }
3870 
3871  snprintf(dir_name, sizeof(dir_name), "%ld-%u-enc-fme", (long)getpid(),
3872  p_ctx->session_id);
3873  dir = opendir(dir_name);
3874  if (!dir && ENOENT == NI_ERRNO)
3875  {
3876  mkdir(dir_name, S_IRWXU | S_IRWXG | S_IRWXO);
3877  ni_log2(p_ctx, NI_LOG_DEBUG, "Encoder frame dump dir created: %s\n", dir_name);
3878  }
3879  if(dir){
3880  closedir(dir);
3881  }
3882 #endif
3883 
3884 END:
3885 
3886  ni_aligned_free(p_buffer);
3887  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
3888 
3889  return retval;
3890 }
3891 
3892 /*!******************************************************************************
3893  * \brief Flush encoder output
3894  *
3895  * \param
3896  *
3897  * \return
3898  *******************************************************************************/
3900 {
3901  ni_retcode_t retval;
3902  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
3903  if (!p_ctx)
3904  {
3905  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
3906  __func__);
3907  retval = NI_RETCODE_INVALID_PARAM;
3908  LRETURN;
3909  }
3910 
3911  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
3912  {
3913  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
3914  __func__);
3916  LRETURN;
3917  }
3918 
3920  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
3921  p_ctx->device_type, p_ctx->hw_id,
3922  &(p_ctx->session_id), OPT_1);
3923  CHECK_VPU_RECOVERY(retval);
3924 
3925 END:
3926 
3927  if (NI_RETCODE_SUCCESS != retval)
3928  {
3929  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s(): %d, return\n", __func__, retval);
3930  }
3931 
3932  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
3933 
3934  return retval;
3935 }
3936 
3937 /*!******************************************************************************
3938  * \brief Close a xcoder encoder instance
3939  *
3940  * \param
3941  *
3942  * \return
3943  *******************************************************************************/
3945 {
3947  void* p_buffer = NULL;
3948  uint32_t ui32LBA = 0;
3949  int i = 0;
3950  ni_xcoder_params_t *p_param = NULL;
3951 
3952  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
3953 
3954  if (!p_ctx)
3955  {
3956  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
3957  __func__);
3958  return NI_RETCODE_INVALID_PARAM;
3959  }
3960 
3961  ni_pthread_mutex_lock(&p_ctx->mutex);
3962 
3963  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
3964  {
3965  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): Invalid session ID, return.\n", __func__);
3966  retval = NI_RETCODE_SUCCESS;
3967  LRETURN;
3968  }
3969 
3970  if (p_ctx->device_type == NI_DEVICE_TYPE_ENCODER)
3971  {
3972  ni_log2(p_ctx, NI_LOG_INFO,
3973  "Encoder_complete_info:session_id 0x%x, total frames input:%u "
3974  "buffered: %u completed: %u output: %u dropped: %u error: %u\n",
3981  }
3982 
3983  //malloc data buffer
3984  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), NI_DATA_BUFFER_LEN))
3985  {
3986  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() alloc data buffer failed\n",
3987  NI_ERRNO, __func__);
3988  retval = NI_RETCODE_ERROR_MEM_ALOC;
3989  LRETURN;
3990  }
3991  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
3992 
3993  if (p_ctx->p_session_config)
3994  {
3995  p_param = (ni_xcoder_params_t *)p_ctx->p_session_config;
3996  if (p_param->ddr_priority_mode > NI_DDR_PRIORITY_NONE)
3997  {
3999  if (NI_RETCODE_SUCCESS != retval)
4000  {
4001  char errmsg[NI_ERRNO_LEN] = {0};
4002  ni_strerror(errmsg, NI_ERRNO_LEN, NI_ERRNO);
4003  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): ddr priority setting failure for %s\n",
4004  __func__, errmsg);
4005  LRETURN;
4006  }
4007  }
4008  }
4009 
4011 
4012  int retry = 0;
4013  while (retry < NI_SESSION_CLOSE_RETRY_MAX)
4014  {
4015  ni_log2(p_ctx, NI_LOG_DEBUG,
4016  "%s(): p_ctx->blk_io_handle=%" PRIx64 ", p_ctx->hw_id=%d, "
4017  "p_ctx->session_id=%d, close_mode=1\n",
4018  __func__, (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id,
4019  p_ctx->session_id);
4020 
4022  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA) < 0)
4023  {
4024  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): command failed\n", __func__);
4027  break;
4028  } else
4029  {
4030  //Close should always succeed
4031  retval = NI_RETCODE_SUCCESS;
4033  break;
4034  }
4035  /*
4036  else if(((ni_session_closed_status_t *)p_buffer)->session_closed)
4037  {
4038  retval = NI_RETCODE_SUCCESS;
4039  p_ctx->session_id = NI_INVALID_SESSION_ID;
4040  break;
4041  }
4042  else
4043  {
4044  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): wait for close\n");
4045  ni_usleep(NI_SESSION_CLOSE_RETRY_INTERVAL_US);
4046  retval = NI_RETCODE_ERROR_NVME_CMD_FAILED;
4047  }
4048  */
4049  retry++;
4050  }
4051 
4052 END:
4053 
4054  ni_aligned_free(p_buffer);
4056 
4057  //Sequence change related stuff cleanup here
4058  p_ctx->active_video_width = 0;
4059  p_ctx->active_video_height = 0;
4060  p_ctx->actual_video_width = 0;
4061  //End of sequence change related stuff cleanup
4062 
4063  if (p_ctx->pts_table)
4064  {
4065  ni_queue_free(&p_ctx->pts_table->list, p_ctx->buffer_pool);
4066  ni_memfree(p_ctx->pts_table);
4067  ni_log2(p_ctx, NI_LOG_DEBUG, "ni_timestamp_done: success\n");
4068  }
4069 
4070  if (p_ctx->dts_queue)
4071  {
4072  ni_queue_free(&p_ctx->dts_queue->list, p_ctx->buffer_pool);
4073  ni_memfree(p_ctx->dts_queue);
4074  ni_log2(p_ctx, NI_LOG_DEBUG, "ni_timestamp_done: success\n");
4075  }
4076 
4078  p_ctx->buffer_pool = NULL;
4079 
4080  for (i = 0; i < NI_FIFO_SZ; i++)
4081  {
4082  ni_memfree(p_ctx->pkt_custom_sei_set[i]);
4083  }
4084 
4085  for (i = 0; i < 120 ; i++)
4086  {
4087  if (p_ctx->input_frame_fifo[i].p_input_buffer != NULL)
4088  {
4089  free(p_ctx->input_frame_fifo[i].p_input_buffer);
4090  p_ctx->input_frame_fifo[i].p_input_buffer = NULL;
4091  }
4092  }
4093 
4094  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): CTX[Card:%" PRIx64 " / HW:%d / INST:%d]\n",
4095  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
4096  p_ctx->session_id);
4097  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
4098 
4099  low_delay_signal(p_ctx);
4100  ni_pthread_mutex_unlock(&p_ctx->mutex);
4101 
4102  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
4103 
4104  return retval;
4105 }
4106 
4107 
4108 void enqueue_ni_frame(ni_session_context_t *p_ctx, ni_frame_t *ni_frame, int32_t source_width, int32_t source_height)
4109 {
4110  int32_t width_stride[4] = {0};
4111  int32_t height_stride[4] = {0};
4112 
4113  if (((ni_xcoder_params_t *)(p_ctx->p_session_config))->zerocopy_mode &&
4114  ((ni_xcoder_params_t *)(p_ctx->p_session_config))->luma_linesize != 0 &&
4115  ((ni_xcoder_params_t *)(p_ctx->p_session_config))->chroma_linesize != 0)
4116  {
4117  //zero copy happend
4118  width_stride[0] = ((ni_xcoder_params_t *)(p_ctx->p_session_config))->luma_linesize;
4119  width_stride[1] = width_stride[2] = ((ni_xcoder_params_t *)(p_ctx->p_session_config))->chroma_linesize;
4120  }
4121  else
4122  {
4123  int is_semiplanar = !ni_get_planar_from_pixfmt(p_ctx->pixel_format);
4124  ni_get_hw_yuv420p_dim(ni_frame->video_width, ni_frame->video_height,
4125  p_ctx->bit_depth_factor, is_semiplanar,
4126  width_stride, height_stride);
4127  }
4128 
4129  for (int i = 0; i < 120; i++)
4130  {
4131  // 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",
4132  // __FUNCTION__, __LINE__, i, p_ctx->frame_num, p_ctx->input_frame_fifo[i].p_input_buffer,
4133  // ni_frame->data_len[0], ni_frame->data_len[1], ni_frame->data_len[2],
4134  // ni_frame->p_data[0], ni_frame->p_data[1], ni_frame->p_data[2],
4135  // ni_frame->video_width, ni_frame->video_height);
4136  if ((source_width != p_ctx->input_frame_fifo[i].video_width ||
4137  source_height != p_ctx->input_frame_fifo[i].video_height) &&
4138  p_ctx->input_frame_fifo[i].p_input_buffer != NULL &&
4139  p_ctx->input_frame_fifo[i].usable == 1)
4140  {
4141  free(p_ctx->input_frame_fifo[i].p_input_buffer);
4142  p_ctx->input_frame_fifo[i].p_input_buffer = NULL;
4143  p_ctx->input_frame_fifo[i].usable = -1;
4144  }
4145 
4146  if (p_ctx->input_frame_fifo[i].p_input_buffer == NULL)
4147  {
4148  p_ctx->input_frame_fifo[i].p_input_buffer = (uint8_t *)malloc(source_width * p_ctx->bit_depth_factor * source_height * 3 / 2);
4149 
4150  for (int j = 0; j < source_height; j++)
4151  {
4152  memcpy(p_ctx->input_frame_fifo[i].p_input_buffer + source_width * p_ctx->bit_depth_factor * j,
4153  ni_frame->p_data[0] + j * width_stride[0],
4154  source_width * p_ctx->bit_depth_factor);
4155  }
4156  for (int j = 0; j < (source_height + 1) / 2; j++)
4157  {
4158  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,
4159  ni_frame->p_data[1] + j * width_stride[1],
4160  source_width / 2 * p_ctx->bit_depth_factor);
4161  }
4162  for (int j = 0; j < (source_height + 1) / 2; j++)
4163  {
4164  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,
4165  ni_frame->p_data[2] + j * width_stride[2],
4166  source_width / 2 * p_ctx->bit_depth_factor);
4167  }
4168 
4169  p_ctx->input_frame_fifo[i].video_width = source_width;
4170  p_ctx->input_frame_fifo[i].video_height = source_height;
4171 
4172  p_ctx->input_frame_fifo[i].usable = -1;
4173  p_ctx->input_frame_fifo[i].pts = ni_frame->pts;
4174  // 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",
4175  // __FUNCTION__, __LINE__, i, p_ctx->frame_num, ni_frame->buffer_size, ni_frame->video_width, ni_frame->video_height,
4176  // p_ctx->input_frame_fifo[i].pts);
4177  break;
4178  }
4179  else if (p_ctx->input_frame_fifo[i].usable == 1)
4180  {
4181  for (int j = 0; j < source_height; j++)
4182  {
4183  memcpy(p_ctx->input_frame_fifo[i].p_input_buffer + source_width * p_ctx->bit_depth_factor * j,
4184  ni_frame->p_data[0] + j * width_stride[0],
4185  source_width * p_ctx->bit_depth_factor);
4186  }
4187  for (int j = 0; j < (source_height + 1) / 2; j++)
4188  {
4189  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,
4190  ni_frame->p_data[1] + j * width_stride[1],
4191  source_width / 2 * p_ctx->bit_depth_factor);
4192  }
4193  for (int j = 0; j < (source_height + 1) / 2; j++)
4194  {
4195  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,
4196  ni_frame->p_data[2] + j * width_stride[2],
4197  source_width / 2 * p_ctx->bit_depth_factor);
4198  }
4199 
4200  p_ctx->input_frame_fifo[i].video_width = source_width;
4201  p_ctx->input_frame_fifo[i].video_height = source_height;
4202 
4203  p_ctx->input_frame_fifo[i].usable = -1;
4204  p_ctx->input_frame_fifo[i].pts = ni_frame->pts;
4205  break;
4206  }
4207  }
4208 }
4209 
4210 /*!******************************************************************************
4211  * \brief Send a YUV p_frame to encoder
4212  *
4213  * \param
4214  *
4215  * \return
4216  *******************************************************************************/
4218 {
4219  bool ishwframe = false;
4220  uint32_t size = 0;
4221  uint32_t send_count = 0;
4222  uint32_t i = 0;
4223  uint32_t sent_size = 0;
4224  uint32_t frame_size_bytes = 0;
4225  int retval = 0;
4226  ni_instance_buf_info_t buf_info = { 0 };
4227  ni_session_statistic_t sessionStatistic = {0};
4228 
4229  if (!p_ctx || !p_frame)
4230  {
4231  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
4232  __func__);
4233  return NI_RETCODE_INVALID_PARAM;
4234  }
4235  ishwframe = p_ctx->hw_action & NI_CODEC_HW_ENABLE;
4236  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter hw=%d\n", __func__, ishwframe);
4237 
4238  if (ishwframe && !p_frame->end_of_stream)
4239  {
4240  // check if the hw input frame is valid
4241  if (!p_frame->p_data[3])
4242  {
4243  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() hw input frame is null!, return\n",
4244  __func__);
4245  return NI_RETCODE_INVALID_PARAM;
4246  }
4247  uint16_t input_frame_idx = ((niFrameSurface1_t*)(p_frame->p_data[3]))->ui16FrameIdx;
4248  if (!((input_frame_idx > 0 &&
4249  input_frame_idx < NI_GET_MAX_HWDESC_FRAME_INDEX(p_ctx->ddr_config)) ||
4250  (input_frame_idx > NI_GET_MIN_HWDESC_P2P_BUF_ID(p_ctx->ddr_config) &&
4251  input_frame_idx <= NI_GET_MAX_HWDESC_P2P_BUF_ID(p_ctx->ddr_config))))
4252  {
4253  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): got invalid frameIdx [%u]\n",
4254  __func__, input_frame_idx);
4255  return NI_RETCODE_INVALID_PARAM;
4256  }
4257  }
4258 
4259  ni_pthread_mutex_lock(&p_ctx->mutex);
4260 
4261  uint8_t separate_metadata = p_frame->separate_metadata;
4262 
4263  uint8_t separate_start = (p_frame->separate_start && p_frame->total_start_len) ? 1 : 0;
4264 
4265  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
4266  {
4267  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
4268  __func__);
4270  LRETURN;
4271  }
4272 
4273  low_delay_wait(p_ctx);
4274 
4275 #ifdef MEASURE_LATENCY
4276  if ((p_frame->dts != NI_NOPTS_VALUE) && (p_ctx->frame_time_q != NULL))
4277  {
4278  uint64_t abs_time_ns = ni_gettime_ns();
4279  ni_lat_meas_q_t *frame_time_q = (ni_lat_meas_q_t *)p_ctx->frame_time_q;
4280  ni_lat_meas_q_add_entry(frame_time_q, abs_time_ns, p_frame->dts);
4281  }
4282 #endif
4283 
4284  /*!********************************************************************/
4285  /*!************ Sequence Change related stuff *************************/
4286  //First check squence changed related stuff.
4287  //We need to record the current hight/width params if we didn't do it before:
4288 
4289  if( p_frame->video_height)
4290  {
4291  p_ctx->active_video_width = p_frame->data_len[0] / p_frame->video_height;
4292  p_ctx->active_video_height = p_frame->video_height;
4293  }
4294  else if (p_frame->video_width)
4295  {
4296  ni_log2(p_ctx, NI_LOG_DEBUG, "WARNING: passed video_height is not valid!, return\n");
4297  p_ctx->active_video_height = p_frame->data_len[0] / p_frame->video_width;
4298  p_ctx->active_video_width = p_frame->video_width;
4299  }
4300  else
4301  {
4302  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: passed video_height and video_width are not valid!, return\n");
4304  LRETURN;
4305  }
4306  // record actual width (in pixels / without padding) for sequnce change detection
4307  p_ctx->actual_video_width = p_frame->video_width;
4308 
4309  /*!************ Sequence Change related stuff end*************************/
4310  /*!********************************************************************/
4311 
4312  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;
4313  ni_log2(p_ctx, NI_LOG_DEBUG,
4314  "%s: data_len[0] %u data_len[1] %u "
4315  "data_len[2] %u extra_data_len %u frame_size_bytes %u\n",
4316  __func__, p_frame->data_len[0], p_frame->data_len[1],
4317  p_frame->data_len[2], p_frame->extra_data_len, frame_size_bytes);
4318 
4319  // skip query write buffer because we just send EOS
4320  if (!p_frame->end_of_stream)
4321  {
4322  for (;;)
4323  {
4324  query_sleep(p_ctx);
4325 
4327  "65") >= 0)
4328  {
4330  p_ctx, NI_DEVICE_TYPE_ENCODER, &sessionStatistic);
4331  CHECK_ERR_RC(p_ctx, retval, &sessionStatistic,
4333  p_ctx->hw_id, &(p_ctx->session_id), OPT_2);
4334  CHECK_VPU_RECOVERY(retval);
4335  buf_info.buf_avail_size = sessionStatistic.ui32WrBufAvailSize;
4336  } else
4337  {
4338  retval =
4340  NI_DEVICE_TYPE_ENCODER, &buf_info);
4341  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
4342  p_ctx->device_type, p_ctx->hw_id,
4343  &(p_ctx->session_id), OPT_1);
4344  CHECK_VPU_RECOVERY(retval);
4345  }
4346 
4347  if (NI_RETCODE_FAILURE == retval)
4348  {
4349  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() line-%d retrun %d\n", __func__, __LINE__, retval);
4350  LRETURN;
4351  }
4352  else if (NI_RETCODE_SUCCESS != retval ||
4353  buf_info.buf_avail_size < frame_size_bytes)
4354  {
4355  ni_log2(p_ctx, NI_LOG_TRACE,
4356  "Enc write query retry %d. rc=%d. Available buf size %u < "
4357  "frame size %u\n", retval, send_count,
4358  buf_info.buf_avail_size, frame_size_bytes);
4359  if (send_count >= NI_MAX_ENCODER_QUERY_RETRIES)
4360  {
4361  int retval_backup = retval;
4362  retval = ni_query_instance_buf_info(
4365  {
4366  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
4367  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
4368  CHECK_VPU_RECOVERY(retval);
4369  }
4370  else
4371  retval = NI_RETCODE_SUCCESS;
4372 
4373  ni_log2(p_ctx, NI_LOG_DEBUG,
4374  "Enc write query buf info exceeded max retries: "
4375  "%d, rc=%d. Available buf size %u < frame size %u\n",
4376  NI_MAX_ENCODER_QUERY_RETRIES, retval_backup,
4377  buf_info.buf_avail_size, frame_size_bytes);
4379 
4380  LRETURN;
4381  }
4382  send_count++;
4383  ni_pthread_mutex_unlock(&p_ctx->mutex);
4385  ni_pthread_mutex_lock(&p_ctx->mutex);
4386  } else
4387  {
4388  ni_log2(p_ctx, NI_LOG_DEBUG,
4389  "Info enc write query success, available buf "
4390  "size %u >= frame size %u !\n",
4391  buf_info.buf_avail_size, frame_size_bytes);
4392  break;
4393  }
4394  }
4395  }
4396 
4397  // fill in metadata such as timestamp
4398  ni_metadata_enc_frame_t *p_meta;
4399  if (separate_metadata)
4400  {
4401  p_meta = (ni_metadata_enc_frame_t *)p_frame->p_metadata_buffer;
4402  } else
4403  {
4404  p_meta = (ni_metadata_enc_frame_t *)((uint8_t *)
4405  p_frame->p_data[2 + ishwframe] +
4406  p_frame->data_len[2 + ishwframe]);
4407  }
4408 
4409  if (p_meta) //When hwframe xcoding reaches eos, frame looks like swframe but no allocation for p_meta
4410  {
4411  p_meta->metadata_common.ui64_data.frame_tstamp = (uint64_t)p_frame->pts;
4412 
4413  p_meta->force_headers = 0; // p_frame->force_headers not implemented/used
4416  p_meta->use_long_term_ref = p_frame->use_long_term_ref;
4417 
4418  ni_log2(p_ctx, NI_LOG_DEBUG,
4419  "%s: p_meta "
4420  "use_cur_src_as_long_term_pic %d use_long_term_ref %d\n",
4421  __func__, p_meta->use_cur_src_as_long_term_pic,
4422  p_meta->use_long_term_ref);
4423 
4424  p_meta->frame_force_type_enable = p_meta->frame_force_type = 0;
4425  // frame type to be forced to is supposed to be set correctly
4426  // in p_frame->ni_pict_type
4427  if (1 == p_ctx->force_frame_type || p_frame->force_key_frame)
4428  {
4429  if (p_frame->ni_pict_type)
4430  {
4431  p_meta->frame_force_type_enable = 1;
4432  p_meta->frame_force_type = p_frame->ni_pict_type;
4433  }
4434  ni_log2(p_ctx, NI_LOG_DEBUG,
4435  "%s(): ctx->force_frame_type"
4436  " %d frame->force_key_frame %d force frame_num %" PRIu64 ""
4437  " type to %d\n",
4438  __func__, p_ctx->force_frame_type, p_frame->force_key_frame,
4439  p_ctx->frame_num, p_frame->ni_pict_type);
4440  }
4441 
4442  // force pic qp if specified
4443  p_meta->force_pic_qp_enable = p_meta->force_pic_qp_i =
4444  p_meta->force_pic_qp_p = p_meta->force_pic_qp_b = 0;
4445  if (p_frame->force_pic_qp)
4446  {
4447  p_meta->force_pic_qp_enable = 1;
4448  p_meta->force_pic_qp_i = p_meta->force_pic_qp_p =
4449  p_meta->force_pic_qp_b = p_frame->force_pic_qp;
4450  }
4451  p_meta->frame_sei_data_size = p_frame->sei_total_len;
4452  p_meta->frame_roi_map_size = p_frame->roi_len;
4453  p_meta->frame_roi_avg_qp = p_ctx->roi_avg_qp;
4454  p_meta->enc_reconfig_data_size = p_frame->reconf_len;
4455 
4457  "6Q") >= 0)
4458  {
4459  if (separate_start)
4460  {
4461  for (i = 0; i < NI_MAX_NUM_SW_FRAME_DATA_POINTERS; i++)
4462  p_meta->start_len[i] = p_frame->start_len[i];
4463  }
4464  else
4465  {
4466  memset(p_meta->start_len, 0, sizeof(p_meta->start_len));
4467  }
4469  }
4470 
4471  if (p_ctx->frame_num % ((ni_xcoder_params_t *)(p_ctx->p_session_config))->interval_of_psnr == 0)
4472  p_meta->get_recon_frame_mode = ((ni_xcoder_params_t *)(p_ctx->p_session_config))->cfg_enc_params.get_psnr_mode;
4473  else
4474  p_meta->get_recon_frame_mode = 3;
4475 
4476  ni_log2(p_ctx,
4477  NI_LOG_DEBUG,
4478  "%s(): %d.%u p_ctx->frame_num=%" PRIu64 ", "
4479  "p_frame->start_of_stream=%u, p_frame->end_of_stream=%u, "
4480  "p_frame->video_width=%u, p_frame->video_height=%u, pts=0x%08x 0x%08x, "
4481  "dts=0x%08x 0x%08x, sei_len=%u, roi size=%u avg_qp=%u reconf_len=%u "
4482  "force_pic_qp=%u use_cur_src_as_long_term_pic %u use_long_term_ref "
4483  "%u start_len [%u,%u,%u] inconsecutive_transfer %u "
4484  "get_recon_frame_mode %u\n",
4485  __func__, p_ctx->hw_id, p_ctx->session_id, p_ctx->frame_num,
4486  p_frame->start_of_stream, p_frame->end_of_stream, p_frame->video_width,
4487  p_frame->video_height, (uint32_t)((p_frame->pts >> 32) & 0xFFFFFFFF),
4488  (uint32_t)(p_frame->pts & 0xFFFFFFFF),
4489  (uint32_t)((p_frame->dts >> 32) & 0xFFFFFFFF),
4490  (uint32_t)(p_frame->dts & 0xFFFFFFFF), p_meta->frame_sei_data_size,
4491  p_meta->frame_roi_map_size, p_meta->frame_roi_avg_qp,
4492  p_meta->enc_reconfig_data_size, p_meta->force_pic_qp_i,
4494  p_meta->start_len[0], p_meta->start_len[1], p_meta->start_len[2],
4495  p_meta->inconsecutive_transfer,
4496  p_meta->get_recon_frame_mode);
4497  }
4498  if (p_frame->start_of_stream)
4499  {
4500  //Send Start of stream p_config command here
4502  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
4503  p_ctx->device_type, p_ctx->hw_id,
4504  &(p_ctx->session_id), OPT_1);
4505  CHECK_VPU_RECOVERY(retval);
4506  if (NI_RETCODE_SUCCESS != retval)
4507  {
4508  LRETURN;
4509  }
4510 
4511  p_frame->start_of_stream = 0;
4512  }
4513 
4514  // skip direct to send eos without sending the passed in p_frame as it's been sent already
4515  if (p_frame->end_of_stream)
4516  {
4518  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
4519  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
4520  CHECK_VPU_RECOVERY(retval);
4521  if (NI_RETCODE_SUCCESS != retval)
4522  {
4523  LRETURN;
4524  }
4525 
4526  p_frame->end_of_stream = 0;
4527  p_ctx->ready_to_close = 1;
4528  } else //handle regular frame sending
4529  {
4530  retval = ni_timestamp_register(p_ctx->buffer_pool, p_ctx->dts_queue,
4531  p_frame->dts, 0);
4532  if (NI_RETCODE_SUCCESS != retval)
4533  {
4534  ni_log2(p_ctx, NI_LOG_ERROR,
4535  "ERROR %s(): "
4536  "ni_timestamp_register() for dts returned: %d\n",
4537  __func__, retval);
4538  }
4539 
4540  if (separate_metadata)
4541  {
4542  uint32_t ui32LBA_metadata =
4544  ni_log2(p_ctx, NI_LOG_DEBUG,
4545  "%s: p_metadata_buffer = %p, metadata_buffer_size "
4546  "= %u, p_ctx->frame_num = %" PRIu64 ", LBA = 0x%x\n",
4547  __func__, p_frame->p_metadata_buffer,
4548  p_frame->metadata_buffer_size, p_ctx->frame_num,
4549  ui32LBA_metadata);
4550 
4551  sent_size =
4553 
4554  retval = ni_nvme_send_write_cmd(
4555  p_ctx->blk_io_handle, p_ctx->event_handle,
4556  p_frame->p_metadata_buffer, sent_size,
4557  ui32LBA_metadata);
4558  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_write, p_ctx->device_type,
4559  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
4560  CHECK_VPU_RECOVERY(retval);
4561  if (retval < 0)
4562  {
4563  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n",
4564  __func__);
4566  LRETURN;
4567  }
4568  }
4569 
4570  if (separate_start)
4571  {
4572  uint32_t ui32LBA =
4574  ni_log2(p_ctx, NI_LOG_DEBUG,
4575  "%s: p_start_buffer = %p, p_frame->start_buffer_size "
4576  "= %u, p_ctx->frame_num = %" PRIu64 ", LBA = 0x%x\n",
4577  __func__, p_frame->p_start_buffer, p_frame->start_buffer_size, p_ctx->frame_num,
4578  ui32LBA);
4579 
4580  sent_size =
4582 
4583  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
4584  p_frame->p_start_buffer, sent_size, ui32LBA);
4585  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_write, p_ctx->device_type,
4586  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
4587  CHECK_VPU_RECOVERY(retval);
4588  if (retval < 0)
4589  {
4590  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
4592  LRETURN;
4593  }
4594  }
4595 
4596  if (p_frame->inconsecutive_transfer)
4597  {
4598  uint32_t ui32LBA =
4600 
4601  for (i = 0; i < NI_MAX_NUM_SW_FRAME_DATA_POINTERS; i++)
4602  {
4603  ni_log2(p_ctx, NI_LOG_DEBUG,
4604  "%s: p_data = %p, p_frame->buffer_size "
4605  "= %u, p_ctx->frame_num = %" PRIu64 ", LBA = 0x%x\n",
4606  __func__, p_frame->p_data, p_frame->buffer_size, p_ctx->frame_num,
4607  ui32LBA);
4608 
4609  if (p_frame->data_len[i])
4610  {
4611  sent_size = p_frame->data_len[i];
4612  if (separate_start)
4613  sent_size -= p_frame->start_len[i];
4614 
4615  sent_size =
4617 
4618  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
4619  p_frame->p_data[i]+p_frame->start_len[i], sent_size, ui32LBA);
4620  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_write, p_ctx->device_type,
4621  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
4622  CHECK_VPU_RECOVERY(retval);
4623  if (retval < 0)
4624  {
4625  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
4627  LRETURN;
4628  }
4629  }
4630  }
4631  }
4632  else
4633  {
4634  uint32_t ui32LBA =
4636  ni_log2(p_ctx, NI_LOG_DEBUG,
4637  "%s: p_data = %p, p_frame->buffer_size "
4638  "= %u, p_ctx->frame_num = %" PRIu64 ", LBA = 0x%x\n",
4639  __func__, p_frame->p_data, p_frame->buffer_size, p_ctx->frame_num,
4640  ui32LBA);
4641 
4642  sent_size = frame_size_bytes;
4643  if (separate_metadata)
4644  sent_size -= p_frame->extra_data_len;
4645  if (separate_start)
4646  sent_size -= p_frame->total_start_len;
4647 
4648  sent_size =
4650 
4651  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
4652  p_frame->p_buffer+p_frame->total_start_len, sent_size, ui32LBA);
4653  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_write, p_ctx->device_type,
4654  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
4655  CHECK_VPU_RECOVERY(retval);
4656  if (retval < 0)
4657  {
4658  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
4660  LRETURN;
4661  }
4662  }
4663 
4664  //Save input frame data used for calculate PSNR
4665  if ((ni_cmp_fw_api_ver((char*) &p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6rc") >= 0) &&
4666  (p_ctx->frame_num == 0 || ((p_ctx->frame_num % ((ni_xcoder_params_t *)(p_ctx->p_session_config))->interval_of_psnr) == 0)) &&
4667  (((ni_xcoder_params_t *)(p_ctx->p_session_config))->cfg_enc_params.get_psnr_mode < 3) &&
4668  (!(((ni_xcoder_params_t *)(p_ctx->p_session_config))->cfg_enc_params.get_psnr_mode == 2 && p_ctx->codec_format == NI_CODEC_FORMAT_H265)))
4669  {
4670  if (ishwframe && !p_frame->end_of_stream)
4671  {
4672  // uint16_t input_frame_idx = ((niFrameSurface1_t*)(p_frame->p_data[3]))->ui16FrameIdx;
4673  // use hwdownload to download hw frame, but donot recycle hwframe buffer
4674  ni_session_data_io_t hwdl_session_data = {0};
4675  // ni_log2(p_ctx, NI_LOG_ERROR, "pixel_format %d\n", p_ctx->pixel_format);
4676  int ret = hwdl_frame(p_ctx, &hwdl_session_data, p_frame,
4677  p_frame->pixel_format);
4678  if (ret <= 0)
4679  {
4680  ni_frame_buffer_free(&(hwdl_session_data.data.frame));
4681  return ret;
4682  }
4683 
4684  hwdl_session_data.data.frame.pts = p_frame->pts;
4685  enqueue_ni_frame(p_ctx, &hwdl_session_data.data.frame, p_frame->video_width, p_frame->video_height);
4686  ni_frame_buffer_free(&hwdl_session_data.data.frame);
4687  }
4688  else
4689  {
4690  enqueue_ni_frame(p_ctx, p_frame, p_frame->video_width, p_frame->video_height);
4691  }
4692  }
4693 
4694  if (ishwframe)
4695  {
4696  ni_log2(p_ctx, NI_LOG_DEBUG,
4697  "%s(): session=0x%x ui16FrameIdx=%u\n",
4698  __func__,
4699  p_ctx->session_id,
4700  ((niFrameSurface1_t*)(p_frame->p_data[3]))->ui16FrameIdx);
4701  }
4702 
4703  p_ctx->status = 0;
4704  p_ctx->frame_num++;
4705  size = frame_size_bytes;
4706  p_ctx->low_delay_sync_flag = 1;
4707 
4708 #ifdef XCODER_DUMP_DATA
4709  char dump_file[256];
4710  snprintf(dump_file, sizeof(dump_file), "%ld-%u-enc-fme/fme-%04ld.yuv",
4711  (long)getpid(), p_ctx->session_id, (long)p_ctx->frame_num);
4712 
4713  FILE *f = NULL;
4714  ni_fopen(&f, dump_file, "wb");
4715  fwrite(p_frame->p_buffer,
4716  p_frame->data_len[0] + p_frame->data_len[1] + p_frame->data_len[2],
4717  1, f);
4718  fflush(f);
4719  fclose(f);
4720 #endif
4721  }
4722 
4723  retval = size;
4724 
4725 END:
4726 
4727  ni_pthread_mutex_unlock(&p_ctx->mutex);
4728 
4729  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
4730  return retval;
4731 }
4732 
4734 {
4735  ni_instance_mgr_stream_info_t data = { 0 };
4736  uint32_t actual_read_size = 0;
4737  uint32_t to_read_size = 0;
4738  int size = 0;
4739  static long long encq_count = 0LL;
4740  int retval = NI_RETCODE_SUCCESS;
4741  int query_retry = 0;
4742  int query_type = INST_BUF_INFO_RW_READ;
4743  uint32_t ui32LBA = 0;
4744  ni_metadata_enc_bstream_t *p_meta = NULL;
4745  ni_metadata_enc_bstream_rev61_t *p_meta_rev61 = NULL;
4746  ni_instance_buf_info_t buf_info = { 0 };
4747  ni_session_statistic_t sessionStatistic = {0};
4748  int low_delay_notify = 0;
4749  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
4750 
4751  if (!p_ctx || !p_packet || !p_packet->p_data)
4752  {
4753  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
4754  __func__);
4755  retval = NI_RETCODE_INVALID_PARAM;
4756  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
4757 
4758  return retval;
4759  }
4760 
4761  ni_pthread_mutex_lock(&p_ctx->mutex);
4762 
4763  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
4764  {
4765  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
4766  __func__);
4768  LRETURN;
4769  }
4770 
4771  ni_log2(p_ctx, NI_LOG_DEBUG, "frame_num=%" PRIu64 ", pkt_num=%" PRIu64 ", av1_pkt_num=%" PRIu64 "\n",
4772  p_ctx->frame_num, p_ctx->pkt_num, p_ctx->av1_pkt_num);
4773  if (((ni_xcoder_params_t *)p_ctx->p_session_config)->low_delay_mode)
4774  {
4775  query_type = INST_BUF_INFO_RW_READ_BUSY;
4776  if (!p_packet->end_of_stream && p_ctx->frame_num < p_ctx->pkt_num)
4777  {
4778  if (p_ctx->ready_to_close)
4779  {
4780  query_type = INST_BUF_INFO_RW_READ;
4781  } else
4782  { //nothing to query, leave
4783  retval = NI_RETCODE_SUCCESS;
4784  low_delay_notify = 1;
4785  LRETURN;
4786  }
4787  }
4788  }
4789  for (;;)
4790  {
4791  query_sleep(p_ctx);
4792 
4793  query_retry++;
4794 
4796  "65") >= 0)
4797  {
4799  p_ctx, NI_DEVICE_TYPE_ENCODER, &sessionStatistic);
4800  CHECK_ERR_RC(p_ctx, retval, &sessionStatistic,
4802  p_ctx->hw_id, &(p_ctx->session_id), OPT_2);
4803  CHECK_VPU_RECOVERY(retval);
4804 
4805  buf_info.buf_avail_size = sessionStatistic.ui32RdBufAvailSize;
4806 
4807  if (((ni_xcoder_params_t *)p_ctx->p_session_config)->cfg_enc_params.lookAheadDepth)
4808  {
4810  "6rX") >= 0)
4811  {
4812  if (p_ctx->current_frame_delay < (int)sessionStatistic.ui8AdditionalFramesDelay + p_ctx->initial_frame_delay)
4813  {
4814  p_ctx->current_frame_delay = (int)sessionStatistic.ui8AdditionalFramesDelay + p_ctx->initial_frame_delay; // extend frames delay by FW estimated additional number of frames
4815  }
4816  if(query_retry == 0)
4817  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: initial_frame_delay %d max_frame_delay %d ui8AdditionalFramesDelay %u current_frame_delay %d\n",
4818  __FUNCTION__, p_ctx->initial_frame_delay, p_ctx->max_frame_delay, sessionStatistic.ui8AdditionalFramesDelay, p_ctx->current_frame_delay);
4819  }
4820  }
4821  } else
4822  {
4823  retval = ni_query_instance_buf_info(
4824  p_ctx, query_type, NI_DEVICE_TYPE_ENCODER, &buf_info);
4825  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
4826  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
4827  CHECK_VPU_RECOVERY(retval);
4828  }
4829 
4830  ni_log2(p_ctx, NI_LOG_TRACE,
4831  "Info enc read query rc %d, available buf size %u, "
4832  "frame_num=%" PRIu64 ", pkt_num=%" PRIu64 "\n",
4833  retval, buf_info.buf_avail_size, p_ctx->frame_num, p_ctx->pkt_num);
4834 
4835  if (NI_RETCODE_SUCCESS != retval)
4836  {
4837  ni_log2(p_ctx, NI_LOG_ERROR, "Buffer info query failed in encoder read!!!!\n");
4838  LRETURN;
4839  } else if (0 == buf_info.buf_avail_size)
4840  {
4841  // query to see if it is eos now, if we have sent it
4842  if (p_ctx->ready_to_close)
4843  {
4844  retval = ni_query_stream_info(p_ctx, NI_DEVICE_TYPE_ENCODER, &data);
4845  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
4846  p_ctx->device_type, p_ctx->hw_id,
4847  &(p_ctx->session_id), OPT_1);
4848  CHECK_VPU_RECOVERY(retval);
4849 
4850  if (NI_RETCODE_SUCCESS != retval)
4851  {
4852  ni_log2(p_ctx, NI_LOG_ERROR, "Stream info query failed in encoder read!!!!\n");
4853  LRETURN;
4854  }
4855 
4856  if (data.is_flushed)
4857  {
4858  p_packet->end_of_stream = 1;
4859  }
4860  }
4861  ni_log2(p_ctx, NI_LOG_DEBUG, "Info enc read available buf size %u, eos %u !\n",
4862  buf_info.buf_avail_size, p_packet->end_of_stream);
4863 
4864  if ((((ni_xcoder_params_t *)p_ctx->p_session_config)->low_delay_mode ||
4866  !p_packet->end_of_stream && p_ctx->frame_num >= p_ctx->pkt_num)
4867  {
4868  ni_log2(p_ctx, NI_LOG_DEBUG, "Encoder low latency mode, eos not sent, frame_num "
4869  "%" PRIu64 " >= %" PRIu64 " pkt_num, keep querying p_ctx->status %d\n",
4870  p_ctx->frame_num, p_ctx->pkt_num, p_ctx->status);
4871  ni_pthread_mutex_unlock(&p_ctx->mutex);
4873  ni_pthread_mutex_lock(&p_ctx->mutex);
4874  if (query_retry >= NI_MAX_ENCODER_QUERY_RETRIES &&
4876  {
4877  low_delay_notify = 1;
4879  LRETURN;
4880  }
4881  else
4882  {
4883  continue;
4884  }
4885  }
4886  else if (((ni_xcoder_params_t *)p_ctx->p_session_config)->minFramesDelay)
4887  {
4888  if (p_ctx->pkt_num) // do not busy read until header is received
4889  {
4890  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
4891  if (p_ctx->frame_num - pkt_num >= p_ctx->current_frame_delay &&
4892  !p_packet->end_of_stream)
4893  {
4894  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",
4895  __FUNCTION__, p_ctx->frame_num, pkt_num,
4896  p_ctx->last_gop_size, p_ctx->current_frame_delay);
4897  continue;
4898  }
4899  }
4900  }
4901  retval = NI_RETCODE_SUCCESS;
4902  LRETURN;
4903  } else
4904  {
4905  break;
4906  }
4907  }
4908  ni_log2(p_ctx, NI_LOG_DEBUG, "Encoder read buf_avail_size %u\n", buf_info.buf_avail_size);
4909 
4910  to_read_size = buf_info.buf_avail_size;
4911 
4912  p_packet->data_len = 0;
4913  p_packet->pts = NI_NOPTS_VALUE;
4914  p_packet->dts = 0;
4915 
4916  p_packet->psnr_y = 0;
4917  p_packet->psnr_u = 0;
4918  p_packet->psnr_v = 0;
4919  p_packet->average_psnr = 0;
4920  p_packet->ssim_y = 0;
4921  p_packet->ssim_u = 0;
4922  p_packet->ssim_v = 0;
4923 
4925  actual_read_size = to_read_size;
4926  if (actual_read_size % NI_MEM_PAGE_ALIGNMENT)
4927  {
4928  actual_read_size =
4929  ((actual_read_size / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT) +
4931  }
4932 
4933  if (p_packet->buffer_size < actual_read_size)
4934  {
4935  if (ni_packet_buffer_alloc(p_packet, actual_read_size))
4936  {
4937  ni_log2(p_ctx, NI_LOG_ERROR,
4938  "ERROR %s(): packet buffer size %u allocation "
4939  "failed\n",
4940  __func__, actual_read_size);
4941  retval = NI_RETCODE_ERROR_MEM_ALOC;
4942  LRETURN;
4943  }
4944  }
4945 
4946  retval = ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
4947  p_packet->p_data, actual_read_size, ui32LBA);
4948  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_read, p_ctx->device_type,
4949  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
4950  CHECK_VPU_RECOVERY(retval);
4951  if (retval < 0)
4952  {
4953  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
4955  LRETURN;
4956  }
4957 
4958  // SSIM is supported if fw_rev is >= 6.2
4960  "62") >= 0)
4961  {
4962  p_meta = (ni_metadata_enc_bstream_t *)p_packet->p_data;
4963  p_packet->pts = (int64_t)(p_meta->frame_tstamp);
4964  p_packet->frame_type = p_meta->frame_type;
4965  p_packet->avg_frame_qp = p_meta->avg_frame_qp;
4966  p_packet->recycle_index = p_meta->recycle_index;
4967  p_packet->av1_show_frame = p_meta->av1_show_frame;
4968  ni_log2(p_ctx, NI_LOG_DEBUG, "%s RECYCLE INDEX = %u!!!\n", __FUNCTION__, p_meta->recycle_index);
4969  ni_log2(p_ctx, NI_LOG_DEBUG, "%s MetaDataSize %d FrameType %d AvgFrameQp %d ssim %d %d %d\n",
4970  __FUNCTION__, p_meta->metadata_size, p_meta->frame_type, p_meta->avg_frame_qp, p_meta->ssimY, p_meta->ssimU, p_meta->ssimV);
4971 
4973  "6r2") >= 0)
4974  {
4975  if (((ni_xcoder_params_t *)p_ctx->p_session_config)->cfg_enc_params.lookAheadDepth)
4976  {
4978  "6rX") < 0)
4979  {
4980  if (p_meta->gop_size) // ignore frame 0 gop size 0 (other I-frame gop size 1)
4981  {
4982  if ((int)p_meta->gop_size < p_ctx->last_gop_size)
4983  {
4984  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
4985  }
4986  }
4987  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",
4988  __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);
4989  }
4990  p_ctx->last_gop_size = p_meta->gop_size;
4991  }
4992  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: current gop_size %u\n",
4993  __FUNCTION__, p_meta->gop_size);
4994  }
4995 
4997  "6p") >= 0)
4998  {
4999  if (ni_cmp_fw_api_ver((char*)&p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6rm") >= 0)
5000  {
5001  p_packet->still_image_detected = p_meta->ui8StillImage;
5002  p_packet->scene_change_detected = p_meta->ui8SceneChange;
5003  }
5004  else
5005  {
5006  p_packet->still_image_detected = 0;
5007  p_packet->scene_change_detected = 0;
5008  }
5009 
5010  if(p_meta->frame_size > 0)
5011  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",
5012  p_ctx->pkt_num,
5013  p_meta->max_mv_x[0], p_meta->max_mv_x[1], p_meta->max_mv_y[0], p_meta->max_mv_y[1],
5014  p_meta->min_mv_x[0], p_meta->min_mv_x[1], p_meta->min_mv_y[0], p_meta->min_mv_y[1],
5015  p_meta->frame_size, p_meta->inter_total_count, p_meta->intra_total_count,
5016  p_meta->ui8StillImage, p_meta->ui8SceneChange);
5017  }
5018 
5020  "6sC") >= 0)
5021  {
5022  if (p_meta->pass1Cost)
5023  ni_log2(p_ctx, NI_LOG_DEBUG, "pkt_num %d pass1Cost %f\n", p_ctx->pkt_num, p_meta->pass1Cost);
5024  }
5025 
5026  p_ctx->meta_size = p_meta->metadata_size;
5027 
5028  if (p_meta->ssimY != 0)
5029  {
5030  p_packet->ssim_y = (float)p_meta->ssimY/10000;
5031  p_packet->ssim_u = (float)p_meta->ssimU/10000;
5032  p_packet->ssim_v = (float)p_meta->ssimV/10000;
5033  // The SSIM Y, U, V values returned by FW are 4 decimal places multiplied by 10000.
5034  //Divide by 10000 to get the original value.
5035  ni_log2(p_ctx,
5036 #ifdef NI_LOG_SSIM_AT_INFO
5037  NI_LOG_INFO,
5038 #else
5039  NI_LOG_DEBUG,
5040 #endif
5041  "%s: pkt #%" PRId64 " pts %" PRId64 " ssim "
5042  "Y %.4f U %.4f V %.4f\n", __FUNCTION__, p_ctx->pkt_num,
5043  p_packet->pts, (float)p_meta->ssimY/10000,
5044  (float)p_meta->ssimU/10000, (float)p_meta->ssimV/10000);
5045  }
5046  }
5047  else
5048  {
5049  // Up to fw_rev major 6 and minor 1, use the old meta data structure
5050  p_meta_rev61 = (ni_metadata_enc_bstream_rev61_t *)p_packet->p_data;
5051  p_packet->pts = (int64_t)(p_meta_rev61->frame_tstamp);
5052  p_packet->frame_type = p_meta_rev61->frame_type;
5053  p_packet->avg_frame_qp = p_meta_rev61->avg_frame_qp;
5054  p_packet->recycle_index = p_meta_rev61->recycle_index;
5055  p_packet->av1_show_frame = p_meta_rev61->av1_show_frame;
5056  ni_log2(p_ctx, NI_LOG_DEBUG, "%s RECYCLE INDEX = %u!!!\n", __FUNCTION__, p_meta_rev61->recycle_index);
5057 
5058  }
5059 
5060  p_packet->data_len = to_read_size;
5061 
5062  size = p_packet->data_len;
5063 
5064  if (size > 0)
5065  {
5066  if (p_ctx->pkt_num >= 1 &&
5068  "6sM") >= 0)
5069  {
5070  p_meta = (ni_metadata_enc_bstream_t *)p_packet->p_data;
5071 
5072  // CU info parsing
5073  // Add CU info parsing here
5074 
5075  if (p_meta && p_meta->cuInfoSize != 0)
5076  {
5077  p_packet->data_len -= p_meta->cuInfoSize;
5078  }
5079  }
5080 
5081  if (p_ctx->pkt_num >= 1 &&
5083  "6rc") >= 0)
5084  {
5085  calculate_psnr(p_ctx, p_packet);
5086  }
5087 
5088  if (p_ctx->pkt_num < 1)
5089  {
5090  p_ctx->headers_length += (p_packet->data_len - p_ctx->meta_size);
5091  }
5092 
5093 
5094  if (p_ctx->pkt_num >= 1)
5095  {
5096  if (NI_CODEC_FORMAT_AV1 != p_ctx->codec_format
5097  || p_packet->av1_show_frame)
5098  {
5099  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)
5100  {
5101  p_packet->dts = NI_NOPTS_VALUE;
5102  }
5103  }
5104 
5105  p_ctx->pkt_num++;
5106  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
5107  {
5108  p_ctx->av1_pkt_num++;
5109  }
5110  low_delay_notify = 1;
5111  }
5112 
5113  encq_count++;
5114 
5115 #ifdef MEASURE_LATENCY
5116 #ifndef XCODER_311
5117  ni_log2(p_ctx, NI_LOG_INFO, "ENC fme_num %d, pkt_num %d, latency is %d\n",
5118  p_ctx->frame_num, p_ctx->pkt_num, p_ctx->frame_num - p_ctx->pkt_num);
5119 #endif
5120 #endif
5121 
5122 #ifdef XCODER_DUMP_DATA
5123  char dump_file[256];
5124  snprintf(dump_file, sizeof(dump_file), "%ld-%u-enc-pkt/pkt-%04ld.bin",
5125  (long)getpid(), p_ctx->session_id, (long)p_ctx->pkt_num);
5126 
5127  FILE *f = NULL;
5128  ni_fopen(&f, dump_file, "wb");
5129  fwrite((uint8_t *)p_packet->p_data + sizeof(ni_metadata_enc_bstream_t),
5130  p_packet->data_len - sizeof(ni_metadata_enc_bstream_t), 1, f);
5131  fflush(f);
5132  fclose(f);
5133 #endif
5134  }
5135 
5136  ni_log2(p_ctx,
5137  NI_LOG_DEBUG,
5138  "%s(): %d.%u p_packet->start_of_stream=%u, "
5139  "p_packet->end_of_stream=%u, p_packet->video_width=%u, "
5140  "p_packet->video_height=%u, p_packet->dts=0x%08x 0x%08x, "
5141  "p_packet->pts=0x%08x 0x%08x, type=%u, avg_frame_qp=%u, show_frame=%d\n",
5142  __func__, p_ctx->hw_id, p_ctx->session_id, p_packet->start_of_stream,
5143  p_packet->end_of_stream, p_packet->video_width, p_packet->video_height,
5144  (uint32_t)((p_packet->dts >> 32) & 0xFFFFFFFF),
5145  (uint32_t)(p_packet->dts & 0xFFFFFFFF),
5146  (uint32_t)((p_packet->pts >> 32) & 0xFFFFFFFF),
5147  (uint32_t)(p_packet->pts & 0xFFFFFFFF), p_packet->frame_type,
5148  p_packet->avg_frame_qp, p_packet->av1_show_frame);
5149 
5150  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): p_packet->data_len=%u, size=%d\n", __func__,
5151  p_packet->data_len, size);
5152 
5153  if (encq_count % 500 == 0)
5154  {
5155  ni_log2(p_ctx, NI_LOG_DEBUG,
5156  "Encoder pts queue size = %u dts queue size = %u\n\n",
5157  p_ctx->pts_table->list.count, p_ctx->dts_queue->list.count);
5158  }
5159 
5160  retval = size;
5161 
5162 #ifdef MEASURE_LATENCY
5163  if ((p_packet->dts != NI_NOPTS_VALUE) && (p_ctx->frame_time_q != NULL) && (p_ctx->pkt_num > 0))
5164  {
5165  if (NI_CODEC_FORMAT_AV1 != p_ctx->codec_format
5166  || p_packet->av1_show_frame)
5167  {
5168  uint64_t abs_time_ns = ni_gettime_ns();
5170  ni_log2(p_ctx, NI_LOG_INFO, "DTS:%" PRId64 ",DELTA:%" PRId64 ",eLAT:%" PRIu64 ";\n",
5171  p_packet->dts, abs_time_ns - q->last_benchmark_time,
5172  ni_lat_meas_q_check_latency(q, abs_time_ns, p_packet->dts));
5173  q->last_benchmark_time = abs_time_ns;
5174  }
5175  }
5176 #endif
5177 
5178 END:
5179 
5180  ni_pthread_mutex_unlock(&p_ctx->mutex);
5181 
5182  if (low_delay_notify)
5183  {
5184  low_delay_signal(p_ctx);
5185  }
5186 
5187  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
5188 
5189  return retval;
5190 }
5191 
5192 /*!******************************************************************************
5193  * \brief Send sequnce change to a xcoder encoder instance
5194  *
5195  * \param
5196  *
5197  * \return
5198  *******************************************************************************/
5200 {
5202 
5203  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
5204 
5205  // re-init last_gop_size and av1_pkt_num for minFramesDelay frame delay estimiation
5206  int lookAheadEnable = !!(((ni_xcoder_params_t *)p_ctx->p_session_config)->cfg_enc_params.lookAheadDepth);
5207  int gop_preset_index = ((ni_xcoder_params_t *)p_ctx->p_session_config)->cfg_enc_params.gop_preset_index;
5208  p_ctx->last_gop_size = g_map_preset_to_gopsize[lookAheadEnable][gop_preset_index + 1];
5209  p_ctx->av1_pkt_num = 0;
5210 
5211  //Configure encoder sequence change
5213  p_resolution);
5215  p_ctx->device_type, p_ctx->hw_id,
5216  &(p_ctx->session_id), OPT_1);
5217  CHECK_VPU_RECOVERY(retval);
5218  if (retval < 0)
5219  {
5220  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): config encoder sequence change command failed\n",
5221  __func__);
5223  }
5224 
5225 END:
5226 
5227  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
5228 
5229  return retval;
5230 }
5231 
5232 /*!******************************************************************************
5233  * \brief Open a xcoder scaler instance
5234  *
5235  * \param[in] p_ctx pointer to session context
5236  *
5237  * \return NI_RETCODE_INVALID_PARAM
5238  * NI_RETCODE_ERROR_NVME_CMD_FAILED
5239  * NI_RETCODE_ERROR_INVALID_SESSION
5240  * NI_RETCODE_ERROR_MEM_ALOC
5241  *******************************************************************************/
5243 {
5245  void* p_buffer = NULL;
5246  uint32_t ui32LBA = 0;
5247  char fmt_fw_api_ver1[5], fmt_fw_api_ver2[5];
5248 
5249  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
5250 
5251  if (!p_ctx)
5252  {
5253  retval = NI_RETCODE_INVALID_PARAM;
5254  LRETURN;
5255  }
5256 
5258  {
5260  "64") < 0)
5261  {
5262  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: Cannot use stack filter on device with FW API version < 6.4\n");
5264  }
5265  }
5266 
5268  {
5270  "67") < 0)
5271  {
5272  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: Cannot use rotate filter on device with FW API version < 6.7\n");
5274  }
5275  }
5276 
5278  {
5280  "6L") < 0)
5281  {
5282  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: Cannot use in-place overlay filter on device with FW API version < 6.L\n");
5284  }
5285  }
5286 
5288  {
5290  "6sI") < 0)
5291  {
5292  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: Cannot use ai align filter on device with FW API version < 6.sI\n");
5294  }
5295  }
5296 
5297  if (p_ctx->session_id == NI_INVALID_SESSION_ID)
5298  {
5300  p_ctx->pts_table = NULL;
5301  p_ctx->dts_queue = NULL;
5302  p_ctx->p_leftover = NULL;
5303  p_ctx->buffer_pool = NULL;
5304  p_ctx->dec_fme_buf_pool = NULL;
5305  p_ctx->prev_size = 0;
5306  p_ctx->sent_size = 0;
5307  p_ctx->status = 0;
5308  p_ctx->key_frame_type = 0;
5309  p_ctx->ready_to_close = 0;
5310  p_ctx->rc_error_count = 0;
5311  p_ctx->frame_num = 0;
5312  p_ctx->pkt_num = 0;
5313  p_ctx->pkt_index = 0;
5314 
5315  //malloc zero data buffer
5316  if (ni_posix_memalign(&p_ctx->p_all_zero_buf, sysconf(_SC_PAGESIZE),
5318  {
5319  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() alloc all zero buffer failed\n",
5320  NI_ERRNO, __func__);
5321  retval = NI_RETCODE_ERROR_MEM_ALOC;
5322  LRETURN;
5323  }
5324  memset(p_ctx->p_all_zero_buf, 0, NI_DATA_BUFFER_LEN);
5325 
5326  //malloc data buffer
5327  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), NI_DATA_BUFFER_LEN))
5328  {
5329  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() alloc data buffer failed\n",
5330  NI_ERRNO, __func__);
5331  retval = NI_RETCODE_ERROR_MEM_ALOC;
5332  LRETURN;
5333  }
5334  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
5335 
5336  //Set session ID to be invalid. In case we cannot open session, the session id wold remain invalid.
5337  //In case we can open sesison, the session id would become valid.
5338  ((ni_session_stats_t *)p_buffer)->ui16SessionId =
5339  (uint16_t)NI_INVALID_SESSION_ID;
5340 
5341  // First uint32_t is either an invaild session ID or a valid session ID, depending on if session could be opened
5342  ui32LBA = OPEN_SESSION_CODEC(NI_DEVICE_TYPE_SCALER, ni_htonl(p_ctx->scaler_operation), 0);
5343  retval = ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
5344  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
5345  if (retval != NI_RETCODE_SUCCESS)
5346  {
5347  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR ni_nvme_send_read_cmd\n");
5348  LRETURN;
5349  }
5350  //Open will return a session status structure with a valid session id if it worked.
5351  //Otherwise the invalid session id set before the open command will stay
5352  p_ctx->session_id = ni_ntohs(((ni_session_stats_t *)p_buffer)->ui16SessionId);
5353  p_ctx->session_timestamp = ni_htonl(((ni_session_stats_t *)p_buffer)->ui32Session_timestamp_high);
5354  p_ctx->session_timestamp = (p_ctx->session_timestamp << 32) |
5355  ni_htonl(((ni_session_stats_t *)p_buffer)->ui32Session_timestamp_low);
5356  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
5357  {
5358  ni_log2(p_ctx, NI_LOG_ERROR,
5359  "ERROR %s(): p_ctx->device_handle=%" PRIx64
5360  ", p_ctx->hw_id=%d, p_ctx->session_id=%d\n",
5361  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
5362  p_ctx->session_id);
5364  LRETURN;
5365  }
5366  ni_log2(p_ctx, NI_LOG_DEBUG, "Scaler open session ID:0x%x\n",p_ctx->session_id);
5367  ni_log2(p_ctx, NI_LOG_DEBUG,
5368  "%s(): p_ctx->device_handle=%" PRIx64
5369  ", p_ctx->hw_id=%d, p_ctx->session_id=%d\n",
5370  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
5371  p_ctx->session_id);
5372 
5373  //Send keep alive timeout Info
5374  uint64_t keep_alive_timeout =
5375  p_ctx->keep_alive_timeout * 1000000; //send us to FW
5376  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
5377  memcpy(p_buffer, &keep_alive_timeout, sizeof(keep_alive_timeout));
5378  ni_log2(p_ctx, NI_LOG_DEBUG, "%s keep_alive_timeout %" PRIx64 "\n", __func__,
5379  keep_alive_timeout);
5381  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
5382  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
5383  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config, p_ctx->device_type,
5384  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
5385  CHECK_VPU_RECOVERY(retval);
5386 
5387  if (NI_RETCODE_SUCCESS != retval)
5388  {
5389  ni_log2(p_ctx, NI_LOG_ERROR,
5390  "ERROR %s(): nvme write keep_alive_timeout command "
5391  "failed, blk_io_handle: %" PRIx64 ", hw_id, %d\n",
5392  __func__, (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id);
5394  LRETURN;
5395  }
5396 
5397  // Send SW version to FW if FW API version is >= 6.2
5399  "62") >= 0)
5400  {
5401  // Send SW version to session manager
5402  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
5403  memcpy(p_buffer, NI_XCODER_REVISION, sizeof(uint64_t));
5405  ni_fmt_fw_api_ver_str((char*) &p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], &fmt_fw_api_ver2[0]);
5406  ni_log2(p_ctx, NI_LOG_DEBUG, "%s libxcoder FW API ver %s, FW FW API ver %s\n",
5407  __func__, fmt_fw_api_ver1, fmt_fw_api_ver2);
5408  ui32LBA = CONFIG_SESSION_SWVersion_W(p_ctx->session_id);
5409  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
5410  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
5411  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config, p_ctx->device_type,
5412  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
5413  CHECK_VPU_RECOVERY(retval);
5414 
5415  if (NI_RETCODE_SUCCESS != retval)
5416  {
5417  ni_log2(p_ctx, NI_LOG_ERROR,
5418  "ERROR %s(): nvme write sw_version command "
5419  "failed, blk_io_handle: %" PRIx64 ", hw_id, %d\n",
5420  __func__, (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id);
5422  LRETURN;
5423  }
5424  }
5425  }
5426 
5427  // init for frame pts calculation
5428  p_ctx->is_first_frame = 1;
5429  p_ctx->last_pts = 0;
5430  p_ctx->last_dts = 0;
5431  p_ctx->active_video_width = 0;
5432  p_ctx->active_video_height = 0;
5433  p_ctx->actual_video_width = 0;
5434 
5435 #if !defined(_WIN32) && !defined(__APPLE__) && !defined(__OPEN_HARMONY__) && !defined(_ANDROID)
5436  if (p_ctx->isP2P)
5437  {
5438  retval = p2p_fill_pcie_address(p_ctx);
5439  if(retval != NI_RETCODE_SUCCESS)
5440  {
5441  LRETURN;
5442  }
5443  }
5444 #endif
5445 
5446 END:
5447 
5448  ni_aligned_free(p_buffer);
5449  return retval;
5450 }
5451 
5452 /*!******************************************************************************
5453  * \brief close a scaler session
5454  *
5455  * \param[in] p_ctx pointer to session context
5456  * \param[in] eos_received (not used)
5457  *
5458  * \return NI_RETCODE_INVALID_PARAM
5459  * NI_RETCODE_ERROR_INVALID_SESSION
5460  * NI_RETCODE_ERROR_NVME_CMD_FAILED
5461  *******************************************************************************/
5463 {
5465  void * p_buffer = NULL;
5466  uint32_t ui32LBA = 0;
5467 
5468  if (!p_ctx)
5469  {
5470  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
5471  __func__);
5472  return NI_RETCODE_INVALID_PARAM;
5473  }
5474 
5475  ni_pthread_mutex_lock(&p_ctx->mutex);
5476 
5477  if (p_ctx->session_id == NI_INVALID_SESSION_ID)
5478  {
5479  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): Invalid session ID, return.\n", __func__);
5480  retval = NI_RETCODE_SUCCESS;
5481  LRETURN;
5482  }
5483 
5484  //malloc data buffer
5485  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), NI_DATA_BUFFER_LEN))
5486  {
5487  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() malloc data buffer failed\n",
5488  NI_ERRNO, __func__);
5489  retval = NI_RETCODE_ERROR_MEM_ALOC;
5490  LRETURN;
5491  }
5492  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
5493 
5495 
5496  int retry = 0;
5497  while (retry < NI_SESSION_CLOSE_RETRY_MAX)
5498  {
5499  ni_log2(p_ctx, NI_LOG_DEBUG,
5500  "%s(): p_ctx->blk_io_handle=%" PRIx64 ", p_ctx->hw_id=%d, "
5501  "p_ctx->session_id=%d, close_mode=1\n",
5502  __func__, (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id,
5503  p_ctx->session_id);
5504 
5506  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA) < 0)
5507  {
5508  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): command failed!\n", __func__);
5511  break;
5512  } else
5513  {
5514  //Close should always succeed
5515  retval = NI_RETCODE_SUCCESS;
5517  break;
5518  }
5519  retry++;
5520  }
5521 
5522 #ifndef _WIN32
5523  if (p_ctx->isP2P)
5524  {
5525  if (p_ctx->netint_fd)
5526  {
5527  close(p_ctx->netint_fd);
5528  }
5529  }
5530 #endif
5531 
5532 END:
5533 
5534  ni_aligned_free(p_buffer);
5536 
5537  ni_pthread_mutex_unlock(&p_ctx->mutex);
5538 
5539  return retval;
5540 }
5541 
5542 /*!******************************************************************************
5543  * \brief Send a p_config command to configure scaling parameters.
5544  *
5545  * \param ni_session_context_t p_ctx - xcoder Context
5546  * \param ni_scaler_params_t * params - pointer to the scaler ni_scaler_params_t struct
5547  *
5548  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION, NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
5549  *******************************************************************************/
5551  ni_scaler_params_t *p_params)
5552 {
5553  void *p_scaler_config = NULL;
5554  ni_scaler_config_t *p_cfg = NULL;
5555  uint32_t buffer_size = sizeof(ni_scaler_params_t);
5557  uint32_t ui32LBA = 0;
5558 
5559  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
5560 
5561  if (!p_ctx || !p_params)
5562  {
5563  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
5564  __func__);
5565  retval = NI_RETCODE_INVALID_PARAM;
5566  LRETURN;
5567  }
5568 
5569  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
5570  {
5571  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
5572  __func__);
5574  LRETURN;
5575  }
5576 
5577  buffer_size =
5578  ((buffer_size + (NI_MEM_PAGE_ALIGNMENT - 1)) / NI_MEM_PAGE_ALIGNMENT) *
5580  if (ni_posix_memalign(&p_scaler_config, sysconf(_SC_PAGESIZE), buffer_size))
5581  {
5582  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() malloc p_scaler_config buffer failed\n",
5583  NI_ERRNO, __func__);
5584  retval = NI_RETCODE_ERROR_MEM_ALOC;
5585  LRETURN;
5586  }
5587  memset(p_scaler_config, 0, buffer_size);
5588 
5589  //configure the session here
5592 
5593  //Flip the bytes!!
5594  p_cfg = (ni_scaler_config_t *)p_scaler_config;
5595  p_cfg->filterblit = p_params->filterblit;
5596  p_cfg->numInputs = p_params->nb_inputs;
5597  if (p_cfg->filterblit == 5)
5598  {
5599  // check fw revision
5600  if (ni_cmp_fw_api_ver(
5602  "6sL") < 0)
5603  {
5604  ni_log2(p_ctx, NI_LOG_ERROR, "%s: not supported config filterblit 5 "
5605  "on device with FW API version < 6rL\n", __func__);
5606  // Close the session since we can't configure it as per fw
5607  retval = ni_scaler_session_close(p_ctx, 0);
5608  if (NI_RETCODE_SUCCESS != retval)
5609  {
5610  ni_log2(p_ctx, NI_LOG_ERROR,
5611  "ERROR: %s failed: blk_io_handle: %" PRIx64 ","
5612  "hw_id, %d, xcoder_inst_id: %d\n",
5613  __func__,
5614  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id,
5615  p_ctx->session_id);
5616  }
5618  LRETURN;
5619  }
5620 
5621  // set filterblit 5 default parameter
5622  p_cfg->scaler_param_b = (uint16_t)(3.5 * 10000);
5623  p_cfg->scaler_param_c = (uint16_t)(1.25 * 10000);
5624  }
5625  else
5626  {
5627  p_cfg->scaler_param_b = (uint16_t)(0);
5628  p_cfg->scaler_param_c = (uint16_t)(0.75 * 10000);
5629  }
5630 
5631  if (p_params->enable_scaler_params)
5632  {
5633  // check fw revision
5634  if (ni_cmp_fw_api_ver(
5636  "6s2") < 0)
5637  {
5638  ni_log2(p_ctx, NI_LOG_ERROR, "%s: not supported config scaler params B and C "
5639  "on device with FW API version < 6s1\n", __func__);
5640  // Close the session since we can't configure it as per fw
5641  retval = ni_scaler_session_close(p_ctx, 0);
5642  if (NI_RETCODE_SUCCESS != retval)
5643  {
5644  ni_log2(p_ctx, NI_LOG_ERROR,
5645  "ERROR: %s failed: blk_io_handle: %" PRIx64 ","
5646  "hw_id, %d, xcoder_inst_id: %d\n",
5647  __func__,
5648  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id,
5649  p_ctx->session_id);
5650  }
5651 
5653  LRETURN;
5654  }
5655  if (p_params->scaler_param_b < 0 || p_params->scaler_param_c < 0 ||
5656  p_params->scaler_param_b > 5 || p_params->scaler_param_c > 5)
5657  {
5658  ni_log2(p_ctx, NI_LOG_ERROR, "%s: scaler_params_b and scaler_params_c must "
5659  "be in [0 , 5]. scaler_params_b is %lf, scaler_params_c is %lf\n",
5660  __func__, p_params->scaler_param_b, p_params->scaler_param_c);
5661  // Close the session since we can't configure it as per fw
5662  retval = ni_scaler_session_close(p_ctx, 0);
5663  if (NI_RETCODE_SUCCESS != retval)
5664  {
5665  ni_log2(p_ctx, NI_LOG_ERROR,
5666  "ERROR: %s failed: blk_io_handle: %" PRIx64 ","
5667  "hw_id, %d, xcoder_inst_id: %d\n",
5668  __func__,
5669  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id,
5670  p_ctx->session_id);
5671  }
5672 
5674  LRETURN;
5675  }
5676  p_cfg->scaler_param_b = (uint16_t)(p_params->scaler_param_b * 10000);
5677  p_cfg->scaler_param_c = (uint16_t)(p_params->scaler_param_c * 10000);
5678  }
5679 
5680  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
5681  p_scaler_config, buffer_size, ui32LBA);
5682  if ((int32_t)retval < 0)
5683  {
5684  ni_log2(p_ctx, NI_LOG_ERROR,
5685  "ERROR: ni_nvme_send_write_cmd failed: blk_io_handle: %" PRIx64
5686  ", hw_id, %d, xcoder_inst_id: %d\n",
5687  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
5688  // Close the session since we can't configure it as per fw
5689  retval = ni_scaler_session_close(p_ctx, 0);
5690  if (NI_RETCODE_SUCCESS != retval)
5691  {
5692  ni_log2(p_ctx, NI_LOG_ERROR,
5693  "ERROR: %s failed: blk_io_handle: %" PRIx64 ","
5694  "hw_id, %d, xcoder_inst_id: %d\n",
5695  __func__,
5696  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id,
5697  p_ctx->session_id);
5698  }
5699 
5701  LRETURN;
5702  }
5703  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
5704  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
5705 
5706 END:
5707 
5708  ni_aligned_free(p_scaler_config);
5709  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
5710 
5711  return retval;
5712 }
5713 
5714 /*!******************************************************************************
5715  * \brief allocate a frame in the scaler
5716  *
5717  * \param[in] p_ctx pointer to session context
5718  * \param[in] width width in pixels
5719  * \param[in] height height in pixels
5720  * \param[in] format pixel format
5721  * \param[in] options option flags
5722  * \param[in] rectangle_width clipping rectangle width in pixels
5723  * \param[in] rectangle_height clipping rectangle height in pixels
5724  * \param[in] rectangle_x clipping rectangle x position
5725  * \param[in] rectangle_y clipping rectangle y position
5726  * \param[in] rgba_color background colour (only used by pad filter)
5727  * \param[in] frame_index frame index (only for hardware frames)
5728  *
5729  * \return NI_RETCODE_INVALID_PARAM
5730  * NI_RETCODE_ERROR_INVALID_SESSION
5731  * NI_RETCODE_ERROR_NVME_CMD_FAILED
5732  * NI_RETCODE_ERROR_MEM_ALOC
5733  *******************************************************************************/
5735  int width,
5736  int height,
5737  int format,
5738  int options,
5739  int rectangle_width,
5740  int rectangle_height,
5741  int rectangle_x,
5742  int rectangle_y,
5743  int rgba_color,
5744  int frame_index)
5745 {
5748  uint32_t dataLen;
5749  uint32_t ui32LBA = 0;
5750  uint32_t query_retry = 0;
5751 
5752  /* Round up to nearest 4096 bytes */
5753  dataLen =
5755  dataLen = dataLen & 0xFFFFF000;
5756 
5757  if (!p_ctx)
5758  {
5759  return NI_RETCODE_INVALID_PARAM;
5760  }
5761 
5762  if (p_ctx->session_id == NI_INVALID_SESSION_ID)
5763  {
5764  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
5765  __func__);
5767  }
5768 
5769  if (((options & NI_SCALER_FLAG_IO) && (options & NI_SCALER_FLAG_PC)))
5770  {
5771  // this operation is to free/allocate scaler frame pool
5772  if (rgba_color == 0)
5773  {
5774  if (ni_cmp_fw_api_ver(
5776  "6r3") < 0)
5777  {
5778  ni_log2(p_ctx, NI_LOG_INFO,
5779  "WARNING: Allocate framepool size 0 for session 0x%x\n", p_ctx->session_id);
5780  return NI_RETCODE_SUCCESS;
5781  }
5782  else if (p_ctx->pool_type == NI_POOL_TYPE_NONE)
5783  {
5784  ni_log2(p_ctx, NI_LOG_ERROR,
5785  "ERROR: %s() try to free session 0x%x framepool while it's not allocated\n",
5786  __func__, p_ctx->session_id);
5787  return NI_RETCODE_INVALID_PARAM;
5788  }
5789  else if ((options & NI_SCALER_FLAG_P2) == p_ctx->pool_type)
5790  {
5791  ni_log2(p_ctx, NI_LOG_INFO, "Free framepool of scaler 0x%x\n", p_ctx->session_id);
5792  }
5793  else
5794  {
5795  ni_log2(p_ctx, NI_LOG_ERROR,
5796  "ERROR: %s() try to free session 0x%x framepool of type %u while "
5797  "passing type %u\n",
5798  __func__, p_ctx->session_id, p_ctx->pool_type,
5799  (options & NI_SCALER_FLAG_P2));
5800  }
5801  }
5802  else
5803  {
5804  if (p_ctx->pool_type != NI_POOL_TYPE_NONE)
5805  {
5806  // try to expand the framepool
5807  if (ni_cmp_fw_api_ver(
5809  "6r3") < 0)
5810  {
5811  ni_log2(p_ctx, NI_LOG_ERROR,
5812  "ERROR: allocate framepool multiple times for session 0x%x "
5813  "not supported in FW API version < 6r3\n",
5814  p_ctx->session_id);
5815  return NI_RETCODE_INVALID_PARAM;
5816  }
5817  if ((options & NI_SCALER_FLAG_P2) == p_ctx->pool_type)
5818  {
5819  ni_log2(p_ctx, NI_LOG_INFO,
5820  "Expand frame pool of scaler 0x%x with %u more frames\n",
5821  p_ctx->session_id, rgba_color);
5822  }
5823  else
5824  {
5825  ni_log2(p_ctx, NI_LOG_ERROR,
5826  "ERROR: try to expand session 0x%x framepool with type %u "
5827  "while pool type is %u\n",
5828  p_ctx->session_id, options & NI_SCALER_FLAG_P2, p_ctx->pool_type);
5829  return NI_RETCODE_INVALID_PARAM;
5830  }
5831  }
5832  }
5833  }
5834 
5835  if (ni_posix_memalign((void **)&p_data, sysconf(_SC_PAGESIZE), dataLen))
5836  {
5837  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
5838  NI_ERRNO, __func__);
5840  }
5841 
5842  memset(p_data, 0x00, dataLen);
5843 
5844  p_data->picture_width = width;
5845  p_data->picture_height = height;
5846  p_data->picture_format = format;
5847  p_data->options = options;
5848  p_data->rectangle_width = rectangle_width;
5849  p_data->rectangle_height = rectangle_height;
5850  p_data->rectangle_x = rectangle_x;
5851  p_data->rectangle_y = rectangle_y;
5852  p_data->rgba_color = rgba_color;
5853  p_data->frame_index = frame_index;
5854 
5855  ni_log2(p_ctx, NI_LOG_DEBUG, "%s() options %d frame_index %u buffered_frame_index %d\n",
5856  __func__, options, p_data->frame_index, p_ctx->buffered_frame_index);
5857 
5858  switch (p_ctx->scaler_operation)
5859  {
5862  if ((options & NI_SCALER_FLAG_IO) && !(options & NI_SCALER_FLAG_PC))
5863  {
5864  p_data->frame_index = frame_index; // background
5865  if (p_ctx->buffered_frame_index)
5866  {
5867  p_data->rgba_color = p_ctx->buffered_frame_index; // output frame
5868  }
5869  else
5870  {
5871  p_data->rgba_color = 0; // no output frame, fw must acquire
5872  }
5873  }
5874  break;
5875 
5876  default:
5877  // Send acquired buffer index in AdminWr SET_ALLOC_FRAME command to prevent FP
5878  // ConfigOutput from acquire buffer again
5879  if ((options & NI_SCALER_FLAG_IO) && p_ctx->buffered_frame_index)
5880  {
5881  p_data->frame_index = p_ctx->buffered_frame_index;
5882  }
5883  break;
5884  }
5885 
5886  bool isrgb = ((GC620_RGBA8888 == format) || (GC620_BGRX8888 == format) ||
5887  (GC620_ARGB8888 == format) || (GC620_ABGR8888 == format));
5888  if(width > NI_MAX_RESOLUTION_WIDTH || height > NI_MAX_RESOLUTION_HEIGHT ||
5890  ((width > NI_MAX_RESOLUTION_RGBA_WIDTH || height > NI_MAX_RESOLUTION_RGBA_HEIGHT) && isrgb))
5891  {
5892  if(width < NI_MIN_RESOLUTION_WIDTH_SCALER)
5893  ni_log2(p_ctx, NI_LOG_ERROR, "Invalid Picture Width: too small for scaler\n");
5894  if(height < NI_MIN_RESOLUTION_HEIGHT_SCALER)
5895  ni_log2(p_ctx, NI_LOG_ERROR, "Invalid Picture Height: too small for scaler\n");
5896  if(width > NI_MAX_RESOLUTION_WIDTH || ((width > NI_MAX_RESOLUTION_RGBA_WIDTH) && isrgb))
5897  ni_log2(p_ctx, NI_LOG_ERROR, "Invalid Picture Width: too big for scaler\n");
5898  if(height > NI_MAX_RESOLUTION_HEIGHT || ((height > NI_MAX_RESOLUTION_RGBA_HEIGHT) && isrgb))
5899  ni_log2(p_ctx, NI_LOG_ERROR, "Invalid Picture Height: too big for scaler\n");
5900 
5901  ni_log2(p_ctx, NI_LOG_ERROR, "Resolution %d x %d not supported for %d format!\n", width, height, format);
5902  ni_aligned_free(p_data);
5904  }
5905  ni_log2(p_ctx, NI_LOG_DEBUG,
5906  "Session=0x%x: Dev alloc frame: FrameIndex=%d; W=%d; H=%d; C=%d; RW=%d; RH=%d; RX=%d; RY=%d\n",
5907  p_ctx->session_id, p_data->frame_index,
5908  p_data->picture_width, p_data->picture_height,
5909  p_data->picture_format, p_data->rectangle_width,
5910  p_data->rectangle_height, p_data->rectangle_x, p_data->rectangle_y);
5911 
5914 
5915  for (;;)
5916  {
5917  query_retry++;
5918 
5919  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
5920  p_data, dataLen, ui32LBA);
5921 
5922  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
5923  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_3);
5924 
5926  {
5927  if (query_retry >= 1000 || (options & NI_SCALER_FLAG_PC))
5928  {
5929  ni_log2(p_ctx, NI_LOG_ERROR, "Error: 2D could not acquire frame\n");
5930  retval = NI_RETCODE_FAILURE;
5931  LRETURN;
5932  }
5934  continue;
5935  }
5936  else
5937  {
5938  p_ctx->pool_type = ((options & NI_SCALER_FLAG_IO) && (options & NI_SCALER_FLAG_PC)) ?
5939  (options & NI_SCALER_FLAG_P2) : (p_ctx->pool_type);
5940  break;
5941  }
5942  }
5943 
5944  if (NI_RETCODE_SUCCESS != retval)
5945  {
5946  ni_log2(p_ctx, NI_LOG_ERROR,
5947  "ERROR: ni_nvme_send_admin_cmd failed: "
5948  "blk_io_handle: %" PRIx64 ", hw_id, %u, xcoder_inst_id: %d\n",
5949  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
5950  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed!\n", __func__);
5952  }
5953 
5954 END:
5955 
5956  ni_aligned_free(p_data);
5957  return retval;
5958 }
5959 
5960 /*!******************************************************************************
5961  * \brief config a frame in the scaler
5962  *
5963  * \param[in] p_ctx pointer to session context
5964  * \param[in] p_cfg pointer to frame config
5965  *
5966  * \return NI_RETCODE_INVALID_PARAM
5967  * NI_RETCODE_ERROR_INVALID_SESSION
5968  * NI_RETCODE_ERROR_NVME_CMD_FAILED
5969  * NI_RETCODE_ERROR_MEM_ALOC
5970  *******************************************************************************/
5972  ni_frame_config_t *p_cfg)
5973 {
5976  uint32_t dataLen;
5977  uint32_t ui32LBA = 0;
5978 
5979  /* Round up to nearest 4096 bytes */
5980  dataLen =
5982  dataLen = dataLen & 0xFFFFF000;
5983 
5984  if (!p_ctx || !p_cfg)
5985  {
5986  return NI_RETCODE_INVALID_PARAM;
5987  }
5988 
5989  if (p_ctx->session_id == NI_INVALID_SESSION_ID)
5990  {
5991  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
5992  __func__);
5994  }
5995 
5996  if (ni_posix_memalign((void **)&p_data, sysconf(_SC_PAGESIZE), dataLen))
5997  {
5998  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
5999  NI_ERRNO, __func__);
6001  }
6002 
6003  memset(p_data, 0x00, dataLen);
6004 
6005  p_data->picture_width = p_cfg->picture_width;
6006  p_data->picture_height = p_cfg->picture_height;
6007  p_data->picture_format = p_cfg->picture_format;
6008  p_data->options = p_cfg->options;
6009 
6010  p_data->rectangle_width = p_cfg->rectangle_width;
6011  p_data->rectangle_height = p_cfg->rectangle_height;
6012  p_data->rectangle_x = p_cfg->rectangle_x;
6013  p_data->rectangle_y = p_cfg->rectangle_y;
6014  p_data->rgba_color = p_cfg->rgba_color;
6015  p_data->frame_index = p_cfg->frame_index;
6016  p_data->session_id = p_cfg->session_id;
6017  p_data->output_index = p_cfg->output_index;
6018  switch (p_cfg->orientation)
6019  {
6020  case 0:
6021  case 2:
6022  case 4:
6023  case 5:
6024  p_data->orientation = p_cfg->orientation;
6025  break;
6026  case 1:
6027  p_data->orientation = 3;
6028  break;
6029  case 3:
6030  p_data->orientation = 1;
6031  break;
6032  default:
6033  ni_log2(p_ctx, NI_LOG_ERROR, "Bad orientation: %u\n", p_cfg->orientation);
6035  }
6036  bool isrgb = ((GC620_RGBA8888 == p_data->picture_format) || (GC620_BGRX8888 == p_data->picture_format) ||
6037  (GC620_ARGB8888 == p_data->picture_format) || (GC620_ABGR8888 == p_data->picture_format));
6038  if(p_data->picture_width > NI_MAX_RESOLUTION_WIDTH || p_data->picture_height > NI_MAX_RESOLUTION_HEIGHT ||
6039  p_data->picture_width < NI_MIN_RESOLUTION_WIDTH_SCALER || p_data->picture_height < NI_MIN_RESOLUTION_HEIGHT_SCALER ||
6040  ((p_data->picture_width > NI_MAX_RESOLUTION_RGBA_WIDTH || p_data->picture_height > NI_MAX_RESOLUTION_RGBA_HEIGHT) && isrgb))
6041  {
6042  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);
6043  ni_aligned_free(p_data);
6045  }
6046 
6047  ni_log2(p_ctx, NI_LOG_DEBUG,
6048  "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",
6049  p_ctx->session_id, p_cfg->frame_index,
6050  p_data->picture_width, p_data->picture_height,
6051  p_data->picture_format, p_data->rectangle_width,
6052  p_data->rectangle_height, p_data->rectangle_x,
6053  p_data->rectangle_y, p_data->orientation);
6054 
6057 
6058  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
6059  p_data, dataLen, ui32LBA);
6060  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
6061  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
6062  if (NI_RETCODE_SUCCESS != retval)
6063  {
6064  ni_log2(p_ctx, NI_LOG_ERROR,
6065  "ERROR: ni_nvme_send_admin_cmd failed: blk_io_handle: %" PRIx64
6066  ", hw_id, %u, xcoder_inst_id: %d\n",
6067  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
6068 
6069  ni_log2(p_ctx, NI_LOG_ERROR,
6070  "ERROR ni_scaler_config(): nvme command failed!\n");
6072  }
6073 
6074 END:
6075 
6076  ni_aligned_free(p_data);
6077  return retval;
6078 }
6079 
6080 /*!******************************************************************************
6081  * \brief config multiple frames in the scaler
6082  *
6083  * \param[in] p_ctx pointer to session context
6084  * \param[in] p_cfg_in pointer to input frame config array
6085  * \param[in] numInCfgs number of input frame configs in the p_cfg array
6086  * \param[in] p_cfg_out pointer to output frame config
6087  *
6088  * \return NI_RETCODE_INVALID_PARAM
6089  * NI_RETCODE_ERROR_INVALID_SESSION
6090  * NI_RETCODE_ERROR_NVME_CMD_FAILED
6091  * NI_RETCODE_ERROR_MEM_ALOC
6092  *******************************************************************************/
6094  ni_frame_config_t p_cfg_in[],
6095  int numInCfgs,
6096  ni_frame_config_t *p_cfg_out)
6097 {
6099  ni_instance_mgr_allocation_info_t *p_data, *p_data_orig;
6100  uint32_t dataLen;
6101  uint32_t ui32LBA = 0;
6102  int i;
6103 
6104  /* Round up to nearest 4096 bytes */
6105  dataLen =
6106  sizeof(ni_instance_mgr_allocation_info_t) * (numInCfgs + 1) + NI_MEM_PAGE_ALIGNMENT - 1;
6107  dataLen = dataLen & 0xFFFFF000;
6108 
6109  if (!p_ctx || (!p_cfg_in && numInCfgs))
6110  {
6111  return NI_RETCODE_INVALID_PARAM;
6112  }
6113 
6114  if (p_ctx->session_id == NI_INVALID_SESSION_ID)
6115  {
6116  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
6117  __func__);
6119  }
6120 
6121  if (ni_posix_memalign((void **)&p_data, sysconf(_SC_PAGESIZE), dataLen))
6122  {
6123  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
6124  NI_ERRNO, __func__);
6126  }
6127 
6128  memset(p_data, 0x00, dataLen);
6129 
6130  p_data_orig = p_data;
6131 
6132  for (i = 0; i < numInCfgs; i++)
6133  {
6134  p_data->picture_width = p_cfg_in[i].picture_width;
6135  p_data->picture_height = p_cfg_in[i].picture_height;
6136  p_data->picture_format = p_cfg_in[i].picture_format;
6137  p_data->options = p_cfg_in[i].options & ~NI_SCALER_FLAG_IO;
6138 
6139  p_data->rectangle_width = p_cfg_in[i].rectangle_width;
6140  p_data->rectangle_height = p_cfg_in[i].rectangle_height;
6141  p_data->rectangle_x = p_cfg_in[i].rectangle_x;
6142  p_data->rectangle_y = p_cfg_in[i].rectangle_y;
6143  p_data->rgba_color = p_cfg_in[i].rgba_color;
6144  p_data->frame_index = p_cfg_in[i].frame_index;
6145  p_data->session_id = p_cfg_in[i].session_id;
6146  p_data->output_index = p_cfg_in[i].output_index;
6147 
6148  ni_log2(p_ctx, NI_LOG_DEBUG,
6149  "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",
6150  p_ctx->session_id, i,
6151  p_data->frame_index, p_data->session_id,
6152  p_data->picture_width, p_data->picture_height,
6153  p_data->picture_format, p_data->rectangle_width,
6154  p_data->rectangle_height, p_data->rectangle_x, p_data->rectangle_y);
6155 
6156  p_data++;
6157  }
6158 
6159  if (p_cfg_out)
6160  {
6161  p_data->picture_width = p_cfg_out->picture_width;
6162  p_data->picture_height = p_cfg_out->picture_height;
6163  p_data->picture_format = p_cfg_out->picture_format;
6164  p_data->options = p_cfg_out->options | NI_SCALER_FLAG_IO;
6165 
6166  p_data->rectangle_width = p_cfg_out->rectangle_width;
6167  p_data->rectangle_height = p_cfg_out->rectangle_height;
6168  p_data->rectangle_x = p_cfg_out->rectangle_x;
6169  p_data->rectangle_y = p_cfg_out->rectangle_y;
6170  p_data->rgba_color = p_cfg_out->rgba_color;
6171  p_data->frame_index = p_cfg_out->frame_index;
6172 
6173  ni_log2(p_ctx, NI_LOG_DEBUG,
6174  "Session=0x%x: Dev out config frame: FrameIndex=%u; W=%d; H=%d; C=%d; RW=%d; RH=%d; RX=%d; RY=%d\n",
6175  p_ctx->session_id, p_data->frame_index,
6176  p_data->picture_width, p_data->picture_height,
6177  p_data->picture_format, p_data->rectangle_width,
6178  p_data->rectangle_height, p_data->rectangle_x,
6179  p_data->rectangle_y);
6180  }
6181  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))))
6182  {
6183  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);
6184  ni_aligned_free(p_data);
6186  }
6187 
6190 
6191  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
6192  p_data_orig, dataLen, ui32LBA);
6193  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
6194  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
6195  if (NI_RETCODE_SUCCESS != retval)
6196  {
6197  ni_log2(p_ctx, NI_LOG_ERROR,
6198  "ERROR: ni_nvme_send_admin_cmd failed: blk_io_handle: %" PRIx64
6199  ", hw_id, %u, xcoder_inst_id: %d\n",
6200  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
6201 
6202  ni_log2(p_ctx, NI_LOG_ERROR,
6203  "ERROR ni_scaler_config(): nvme command failed!\n");
6205  }
6206 
6207 END:
6208 
6209  ni_aligned_free(p_data_orig);
6210  return retval;
6211 }
6212 
6213 /*!******************************************************************************
6214  * \brief Query a particular xcoder instance to get GeneralStatus data
6215  *
6216  * \param ni_session_context_t p_ctx - xcoder Context
6217  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
6218  * \param ni_instance_mgr_general_status_t *out - Struct preallocated from the caller where the
6219  * resulting data will be placed
6220  *
6221  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_MEM_ALOC or NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
6222  *******************************************************************************/
6224 {
6225  void* p_buffer = NULL;
6226  int retval = NI_RETCODE_SUCCESS;
6227  uint32_t ui32LBA = 0;
6229 
6230  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
6231 
6232  if ((!p_ctx) || (!p_gen_status))
6233  {
6234  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
6235  __func__);
6236  retval = NI_RETCODE_INVALID_PARAM;
6237  LRETURN;
6238  }
6239 
6240  if (!IS_XCODER_DEVICE_TYPE(device_type))
6241  {
6242  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown device type %d, return\n",
6243  __func__, device_type);
6244  retval = NI_RETCODE_INVALID_PARAM;
6245  LRETURN;
6246  }
6247 
6248  ui32LBA = QUERY_GENERAL_GET_STATUS_R(device_type);
6249 
6250  if (ni_posix_memalign((void **)&p_buffer, sysconf(_SC_PAGESIZE), dataLen))
6251  {
6252  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
6253  NI_ERRNO, __func__);
6254  retval = NI_RETCODE_ERROR_MEM_ALOC;
6255  LRETURN;
6256  }
6257 
6258  memset(p_buffer, 0, dataLen);
6259 
6260  if (ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_buffer, dataLen, ui32LBA) < 0)
6261  {
6262  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
6264  LRETURN;
6265  }
6266 
6267  //No need to flip the bytes since the datastruct has only uint8_t datatypes
6268  memcpy((void*)p_gen_status, p_buffer, sizeof(ni_instance_mgr_general_status_t));
6269 
6270  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): model_load:%u qc:%d percent:%d\n", __func__,
6271  p_gen_status->fw_model_load, p_gen_status->cmd_queue_count,
6272  p_gen_status->process_load_percent);
6273 END:
6274 
6275  ni_aligned_free(p_buffer);
6276  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
6277 
6278  return retval;
6279 }
6280 
6281 /*!******************************************************************************
6282  * \brief Query a particular xcoder instance to get DetailStatus data
6283  *
6284  * \param ni_session_context_t p_ctx - xcoder Context
6285  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
6286  * \param ni_instance_mgr_detail_status_t *out - Struct preallocated from the caller where the
6287  * resulting data will be placed
6288  *
6289  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_MEM_ALOC or NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
6290  *******************************************************************************/
6291 int ni_query_detail_status(ni_session_context_t* p_ctx, ni_device_type_t device_type, void* p_detail_status, int ver)
6292 {
6293  void* p_buffer = NULL;
6294  int retval = NI_RETCODE_SUCCESS;
6295  uint32_t ui32LBA = 0;
6296  uint32_t dataLen = 0;
6297  uint32_t copyLen = 0;
6298  if(ver == 0)
6299  {
6300  ui32LBA = QUERY_DETAIL_GET_STATUS_R(device_type);
6302  dataLen = ((copyLen + (NI_MEM_PAGE_ALIGNMENT - 1)) / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT;
6303  }
6304  else if(ver == 1)
6305  {
6306  ui32LBA = QUERY_DETAIL_GET_STATUS_V1_R(device_type);
6307  copyLen = sizeof(ni_instance_mgr_detail_status_v1_t);
6308  dataLen = (copyLen + (NI_MEM_PAGE_ALIGNMENT - 1)) / NI_MEM_PAGE_ALIGNMENT * NI_MEM_PAGE_ALIGNMENT;
6309  }
6310  else
6311  {
6312  retval = NI_RETCODE_INVALID_PARAM;
6313  LRETURN;
6314  }
6315 
6316  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
6317 
6318  if ((!p_ctx) || (!p_detail_status))
6319  {
6320  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
6321  __func__);
6322  retval = NI_RETCODE_INVALID_PARAM;
6323  LRETURN;
6324  }
6325 
6326  if (!IS_XCODER_DEVICE_TYPE(device_type))
6327  {
6328  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown device type %d, return\n",
6329  __func__, device_type);
6330  retval = NI_RETCODE_INVALID_PARAM;
6331  LRETURN;
6332  }
6333 
6334  if (ni_posix_memalign((void **)&p_buffer, sysconf(_SC_PAGESIZE), dataLen))
6335  {
6336  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
6337  NI_ERRNO, __func__);
6338  retval = NI_RETCODE_ERROR_MEM_ALOC;
6339  LRETURN;
6340  }
6341 
6342  memset(p_buffer, 0, dataLen);
6343 
6344  if (ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_buffer, dataLen, ui32LBA) < 0)
6345  {
6346  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
6348  LRETURN;
6349  }
6350 
6351  //No need to flip the bytes since the datastruct has only uint8_t datatypes
6352  memcpy(p_detail_status, p_buffer, copyLen);
6353 
6354 END:
6355 
6356  ni_aligned_free(p_buffer);
6357  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
6358 
6359  return retval;
6360 }
6361 
6362 /*!******************************************************************************
6363  * \brief Query a particular xcoder instance to get Stream Info data
6364  *
6365  * \param ni_session_context_t p_ctx - xcoder Context
6366  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
6367  * \param ni_instance_mgr_stream_info_t *out - Struct preallocated from the caller where the
6368  * resulting data will be placed
6369  *
6370  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION, NI_RETCODE_ERROR_MEM_ALOC
6371  * or NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
6372  *******************************************************************************/
6374 {
6375  void* p_buffer = NULL;
6377  uint32_t ui32LBA = 0;
6379 
6380  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
6381 
6382  if ((!p_ctx) || (!p_stream_info))
6383  {
6384  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
6385  __func__);
6386  retval = NI_RETCODE_INVALID_PARAM;
6387  LRETURN;
6388  }
6389 
6390  if (! (NI_DEVICE_TYPE_DECODER == device_type ||
6391  NI_DEVICE_TYPE_ENCODER == device_type))
6392  {
6393  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown device type %d, return\n",
6394  __func__, device_type);
6395  retval = NI_RETCODE_INVALID_PARAM;
6396  LRETURN;
6397  }
6398 
6399  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
6400  {
6401  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
6402  __func__);
6404  LRETURN;
6405  }
6406 
6407  ui32LBA = QUERY_INSTANCE_STREAM_INFO_R(p_ctx->session_id, device_type);
6408 
6409  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), dataLen))
6410  {
6411  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
6412  NI_ERRNO, __func__);
6413  retval = NI_RETCODE_ERROR_MEM_ALOC;
6414  LRETURN;
6415  }
6416  memset(p_buffer, 0, dataLen);
6417 
6418  if (ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_buffer, dataLen, ui32LBA) < 0)
6419  {
6420  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
6422  LRETURN;
6423  }
6424 
6425  memcpy((void*)p_stream_info, p_buffer, sizeof(ni_instance_mgr_stream_info_t));
6426 
6427  //flip the bytes to host order
6428  p_stream_info->picture_width = ni_htons(p_stream_info->picture_width);
6429  p_stream_info->picture_height = ni_htons(p_stream_info->picture_height);
6430  p_stream_info->frame_rate = ni_htons(p_stream_info->frame_rate);
6431  p_stream_info->is_flushed = ni_htons(p_stream_info->is_flushed);
6432  p_stream_info->transfer_frame_stride = ni_htons(p_stream_info->transfer_frame_stride);
6433  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): pix_format = %d\n", __func__,
6434  p_stream_info->pix_format); //temp
6435 
6436 END:
6437 
6438  ni_aligned_free(p_buffer);
6439  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
6440 
6441  return retval;
6442 }
6443 
6444 /*!*****************************************************************************
6445  * \brief Query a particular session to get the stats info
6446  *
6447  * \param ni_session_context_t p_ctx - xcoder Context
6448  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
6449  * \param ni_session_stats_t *out - Struct preallocated from the
6450  * caller where the resulting data will be placed
6451  *
6452  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION,
6453  * NI_RETCODE_ERROR_MEM_ALOC or NI_RETCODE_ERROR_NVME_CMD_FAILED
6454  * on failure
6455  ******************************************************************************/
6457  ni_device_type_t device_type,
6458  ni_session_stats_t *p_session_stats, int rc,
6459  int opcode)
6460 {
6462  void* p_buffer = NULL;
6463  uint64_t session_timestamp;
6464  uint32_t ui32LBA = 0;
6465  uint32_t dataLen = ((sizeof(ni_session_stats_t) + (NI_MEM_PAGE_ALIGNMENT - 1)) / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT;
6466 
6467  ni_device_type_t xc_device_type =
6468  (device_type != NI_DEVICE_TYPE_UPLOAD ? device_type :
6470 
6471  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
6472 
6473  if ((!p_ctx) || (!p_session_stats))
6474  {
6475  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
6476  __func__);
6477  retval = NI_RETCODE_INVALID_PARAM;
6478  LRETURN;
6479  }
6480 
6481  if (!IS_XCODER_DEVICE_TYPE(xc_device_type))
6482  {
6483  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown device type %d, return\n",
6484  __func__, device_type);
6485  retval = NI_RETCODE_INVALID_PARAM;
6486  LRETURN;
6487  }
6488 
6489  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
6490  {
6491  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
6492  __func__);
6494  LRETURN;
6495  }
6496 
6497  ui32LBA = QUERY_SESSION_STATS_R(p_ctx->session_id, xc_device_type);
6498 
6499  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), dataLen))
6500  {
6501  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
6502  NI_ERRNO, __func__);
6503  retval = NI_RETCODE_ERROR_MEM_ALOC;
6504  LRETURN;
6505  }
6506  memset(p_buffer, 0, dataLen);
6507 
6508  // Set session ID to be invalid. In case, the last command fails because the invalid session ID was submitted
6509  // with the command, the session id would remain invalid.
6510  // If the last command is processed successfully in session manager, the session id would become valid.
6511  ((ni_session_stats_t *)p_buffer)->ui16SessionId =
6512  (uint16_t)NI_INVALID_SESSION_ID;
6513 
6514  if (ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_buffer, dataLen, ui32LBA)
6515  < 0)
6516  {
6517  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): read command Failed\n", __func__);
6519  LRETURN;
6520  }
6521 
6522  memcpy((void*)p_session_stats, p_buffer, sizeof(ni_session_stats_t));
6523 
6524  // flip the bytes to host order
6525  // all query commands are guaranteed success once a session is opened
6526  p_session_stats->ui16SessionId = ni_htons(p_session_stats->ui16SessionId);
6527  p_session_stats->ui16ErrorCount = ni_htons(p_session_stats->ui16ErrorCount);
6528  p_session_stats->ui32LastTransactionId =
6529  ni_htonl(p_session_stats->ui32LastTransactionId);
6530  p_session_stats->ui32LastTransactionCompletionStatus =
6531  ni_htonl(p_session_stats->ui32LastTransactionCompletionStatus);
6532  p_session_stats->ui32LastErrorTransactionId = ni_htonl(p_session_stats->ui32LastErrorTransactionId);
6533  p_session_stats->ui32LastErrorStatus = ni_htonl(p_session_stats->ui32LastErrorStatus);
6534  p_session_stats->ui32Session_timestamp_high = ni_htonl(p_session_stats->ui32Session_timestamp_high);
6535  p_session_stats->ui32Session_timestamp_low = ni_htonl(p_session_stats->ui32Session_timestamp_low);
6536 
6537  session_timestamp = p_session_stats->ui32Session_timestamp_high;
6538  session_timestamp <<= 32;
6539  session_timestamp |= p_session_stats->ui32Session_timestamp_low;
6540 
6541  // get the session timestamp when open session
6542  // check the timestamp during transcoding
6543  if ((p_ctx->session_timestamp != session_timestamp) &&
6544  (ni_xcoder_resource_recovery != p_session_stats->ui32LastErrorStatus))
6545  // if VPU recovery, the session timestamp will be reset.
6546  {
6547  p_session_stats->ui32LastErrorStatus =
6549  ni_log2(p_ctx, NI_LOG_DEBUG, "instance id invalid:%u, timestamp:%" PRIu64 ", "
6550  "query timestamp:%" PRIu64 "\n", p_ctx->session_id,
6551  p_ctx->session_timestamp, session_timestamp);
6552  }
6553 
6554  // check rc here, if rc != NI_RETCODE_SUCCESS, it means that last read/write command failed
6555  // failures may be link layer errors, such as physical link errors or ERROR_WRITE_PROTECT in windows.
6556  if (NI_RETCODE_SUCCESS != rc)
6557  {
6558  ni_log2(p_ctx, NI_LOG_ERROR, "%s():last command Failed: rc %d\n", __func__, rc);
6559  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 "
6560  "ready_to_close %u session_run_state %d active_video_width %u active_video_height %u\n",
6561  __func__,
6562  p_ctx->session_id,
6563  p_ctx->session_timestamp,
6564  p_ctx->hw_id,
6565  p_ctx->device_type,
6566  p_ctx->codec_format,
6567  p_ctx->frame_num,
6568  p_ctx->pkt_num,
6569  p_ctx->ready_to_close,
6570  p_ctx->session_run_state,
6571  p_ctx->active_video_width,
6572  p_ctx->active_video_height);
6573 
6574 #if __linux__ || __APPLE__
6575 #if !defined(_ANDROID) && !defined(__OPENHARMONY__)
6576 #ifndef DISABLE_BACKTRACE_PRINT
6577  ni_print_backtrace(); // log backtrace
6578 #endif
6579 #endif
6580 #endif
6581 
6582  p_session_stats->ui32LastTransactionCompletionStatus =
6584  p_session_stats->ui32LastErrorStatus = NI_RETCODE_ERROR_NVME_CMD_FAILED;
6586  } else if (p_ctx->session_id != p_session_stats->ui16SessionId)
6587  {
6588  uint64_t ct = ni_gettime_ns();
6589  uint64_t dt = ct - p_ctx->last_access_time;
6590  ni_log2(p_ctx, NI_LOG_ERROR,
6591  "%s(): device 0x%" PRIx64 " last command Failed due to wrong "
6592  "session ID. Expected 0x%x, got 0x%x keep alive last access "
6593  "time %" PRIu64 ", current %" PRIu64 "\n", __func__,
6594  (int64_t)p_ctx->blk_io_handle, p_ctx->session_id,
6595  p_session_stats->ui16SessionId, p_ctx->last_access_time, ct);
6596  if (dt > 1000000000)
6597  {
6598  ni_log2(p_ctx, NI_LOG_ERROR,
6599  "%s():long delay between last command dt = %" PRId64 " ns, "
6600  "process was possibly blocked.\n", __func__, dt);
6601  }
6602  p_session_stats->ui32LastErrorStatus = NI_RETCODE_ERROR_INVALID_SESSION;
6603 
6604  // Mark session id to INVALID so that all commands afterward are blocked
6606  }
6607 
6608  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): error count %u last rc 0x%x inst_err_no 0x%x\n",
6609  __func__, p_session_stats->ui16ErrorCount,
6610  p_session_stats->ui32LastTransactionCompletionStatus,
6611  p_session_stats->ui32LastErrorStatus);
6612 
6613 END:
6614 
6615  ni_aligned_free(p_buffer);
6616  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
6617 
6618  return retval;
6619 }
6620 
6621 /*!******************************************************************************
6622  * \brief Query a particular xcoder instance to get End of Output data
6623  *
6624  * \param ni_session_context_t p_ctx - xcoder Context
6625  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
6626  * \param InstMgrStreamComp *out - Struct preallocated from the caller where the
6627  * resulting data will be placed
6628  *
6629  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION, NI_RETCODE_ERROR_MEM_ALOC
6630  * or NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
6631  *******************************************************************************/
6633 {
6634  void* p_buffer = NULL;
6636  uint32_t ui32LBA = 0;
6638 
6639  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
6640 
6641  if (!p_ctx || !p_stream_complete)
6642  {
6643  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
6644  __func__);
6645  retval = NI_RETCODE_INVALID_PARAM;
6646  LRETURN;
6647  }
6648 
6649  if (! (NI_DEVICE_TYPE_DECODER == device_type ||
6650  NI_DEVICE_TYPE_ENCODER == device_type))
6651  {
6652  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown device type %d, return\n",
6653  __func__, device_type);
6654  retval = NI_RETCODE_INVALID_PARAM;
6655  LRETURN;
6656  }
6657 
6658  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
6659  {
6660  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
6661  __func__);
6663  LRETURN;
6664  }
6665 
6666  ui32LBA = QUERY_INSTANCE_EOS_R(p_ctx->session_id, device_type);
6667 
6668  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), dataLen))
6669  {
6670  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: Cannot allocate buffer.\n");
6671  retval = NI_RETCODE_ERROR_MEM_ALOC;
6672  LRETURN;
6673  }
6674 
6675  memset(p_buffer, 0, dataLen);
6676 
6677  if (ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_buffer, dataLen, ui32LBA) < 0)
6678  {
6679  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
6681  LRETURN;
6682  }
6683 
6684  memcpy((void*)p_stream_complete, p_buffer, sizeof(ni_instance_mgr_stream_complete_t));
6685 
6686  //flip the bytes to host order
6687  p_stream_complete->is_flushed = ni_htons(p_stream_complete->is_flushed);
6688 
6689 END:
6690 
6691  ni_aligned_free(p_buffer);
6692  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
6693 
6694  return retval;
6695 }
6696 
6697 static const char* ni_get_device_type_str(int type)
6698 {
6699  if (type < NI_DEVICE_TYPE_DECODER || type > NI_DEVICE_TYPE_AI)
6700  {
6701  return "Invalid device type";
6702  }
6703  return g_device_type_str[type];
6704 }
6705 
6706 static void
6707 ni_parse_session_statistic_info(ni_session_context_t *p_ctx,
6708  ni_session_statistic_t *p_session_statistic,
6709  void *p_buffer)
6710 {
6711  memcpy((void *)p_session_statistic, p_buffer,
6712  sizeof(ni_session_statistic_t));
6713 
6714  //flip the bytes to host order
6715  p_session_statistic->ui32RdBufAvailSize =
6716  ni_htonl(p_session_statistic->ui32RdBufAvailSize);
6717  p_session_statistic->ui32WrBufAvailSize =
6718  ni_htonl(p_session_statistic->ui32WrBufAvailSize);
6719 
6720  p_session_statistic->ui32FramesInput =
6721  ni_htonl(p_session_statistic->ui32FramesInput);
6722  p_session_statistic->ui32FramesBuffered =
6723  ni_htonl(p_session_statistic->ui32FramesBuffered);
6724  p_session_statistic->ui32FramesCompleted =
6725  ni_htonl(p_session_statistic->ui32FramesCompleted);
6726  p_session_statistic->ui32FramesOutput =
6727  ni_htonl(p_session_statistic->ui32FramesOutput);
6728  p_session_statistic->ui32FramesDropped =
6729  ni_htonl(p_session_statistic->ui32FramesDropped);
6730  p_session_statistic->ui32InstErrors =
6731  ni_htonl(p_session_statistic->ui32InstErrors);
6732 
6733  p_session_statistic->ui16SessionId =
6734  ni_htons(p_session_statistic->ui16SessionId);
6735  p_session_statistic->ui16ErrorCount =
6736  ni_htons(p_session_statistic->ui16ErrorCount);
6737  p_session_statistic->ui32LastTransactionId =
6738  ni_htonl(p_session_statistic->ui32LastTransactionId);
6739  p_session_statistic->ui32LastTransactionCompletionStatus =
6740  ni_htonl(p_session_statistic->ui32LastTransactionCompletionStatus);
6741  p_session_statistic->ui32LastErrorTransactionId =
6742  ni_htonl(p_session_statistic->ui32LastErrorTransactionId);
6743  p_session_statistic->ui32LastErrorStatus =
6744  ni_htonl(p_session_statistic->ui32LastErrorStatus);
6745  p_session_statistic->ui32Session_timestamp_high =
6746  ni_htonl(p_session_statistic->ui32Session_timestamp_high);
6747  p_session_statistic->ui32Session_timestamp_low =
6748  ni_htonl(p_session_statistic->ui32Session_timestamp_low);
6749 
6750  // p_session_statistic->ui8AdditionalFramesDelay does not require endian conversion
6751 
6752  if (p_ctx->session_id != p_session_statistic->ui16SessionId)
6753  {
6754  uint64_t ct = ni_gettime_ns();
6755  uint64_t dt = ct - p_ctx->last_access_time;
6756  ni_log2(p_ctx, NI_LOG_ERROR,
6757  "%s(): %s device 0x%" PRIx64 " last command Failed due to wrong "
6758  "session ID. Expected 0x%x, got 0x%x w_r <%u %u> keep alive "
6759  "last access time %" PRIu64 ", current %" PRIu64 "\n", __func__,
6760  ni_get_device_type_str(p_ctx->device_type),
6761  (int64_t)p_ctx->device_handle, p_ctx->session_id,
6762  p_session_statistic->ui16SessionId,
6763  p_session_statistic->ui32WrBufAvailSize,
6764  p_session_statistic->ui32RdBufAvailSize, p_ctx->last_access_time,
6765  ct);
6766  if (dt > 1000000000)
6767  {
6768  ni_log2(p_ctx, NI_LOG_ERROR,
6769  "%s():long delay between last command dt = %" PRId64 " ns, "
6770  "process was possibly blocked.\n", __func__, dt);
6771  }
6772  p_session_statistic->ui32LastErrorStatus =
6774  //Mark session id to INVALID so that all commands afterward are blocked
6776  } else
6777  {
6778  //Acknowledge that total error count here
6779  }
6780 
6781  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): error count %u last rc 0x%x inst_err_no 0x%x\n",
6782  __func__, p_session_statistic->ui16ErrorCount,
6783  p_session_statistic->ui32LastTransactionCompletionStatus,
6784  p_session_statistic->ui32LastErrorStatus);
6785 }
6786 
6787 /*!*****************************************************************************
6788  * \brief Query a particular xcoder session to get session statistics
6789  *
6790  * \param ni_session_context_t p_ctx - xcoder Context
6791  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
6792  * \param ni_session_statistic_t*out - Struct preallocated from the caller
6793  * where the resulting data will be placed
6794  *
6795  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION,
6796  * NI_RETCODE_ERROR_MEM_ALOC or NI_RETCODE_ERROR_NVME_CMD_FAILED on
6797  * failure
6798  ******************************************************************************/
6801  ni_device_type_t device_type,
6802  ni_session_statistic_t *p_session_statistic)
6803 {
6804  void *p_buffer = NULL;
6806  uint32_t ui32LBA = 0;
6807  uint32_t dataLen =
6808  ((sizeof(ni_session_statistic_t) + (NI_MEM_PAGE_ALIGNMENT - 1)) /
6811 
6812  if (!p_ctx || !p_session_statistic)
6813  {
6814  ni_log2(p_ctx, NI_LOG_ERROR,
6815  "ERROR: %s() passed parameters are null!, return\n", __func__);
6816  retval = NI_RETCODE_INVALID_PARAM;
6817  LRETURN;
6818  }
6819 
6821  "65") < 0)
6822  {
6823  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() not supported on device with FW api version < 6.5\n", __func__);
6825  }
6826 
6827  if (!(NI_DEVICE_TYPE_DECODER == device_type ||
6828  NI_DEVICE_TYPE_ENCODER == device_type ||
6829  NI_DEVICE_TYPE_AI == device_type))
6830  {
6831  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown device type %d, return\n",
6832  __func__, device_type);
6833  retval = NI_RETCODE_INVALID_PARAM;
6834  LRETURN;
6835  }
6836 
6837  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
6838  {
6839  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
6840  __func__);
6842  LRETURN;
6843  }
6844 
6845  ui32LBA = QUERY_INSTANCE_CUR_STATUS_INFO_R(p_ctx->session_id, device_type);
6846 
6847  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), dataLen))
6848  {
6849  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
6850  NI_ERRNO, __func__);
6851  retval = NI_RETCODE_ERROR_MEM_ALOC;
6852  LRETURN;
6853  }
6854  memset(p_buffer, 0, dataLen);
6855 
6856  // Set session ID to be invalid. In case, the last command fails because the invalid session ID was submitted
6857  // with the command, the session id would remain invalid.
6858  // If the Last command is processed successfully in session manager, the session id would become valid.
6859  ((ni_session_statistic_t *)p_buffer)->ui16SessionId =
6860  (uint16_t)NI_INVALID_SESSION_ID;
6861 
6863  p_buffer, dataLen, ui32LBA) < 0)
6864  {
6865  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): NVME command Failed\n", __func__);
6866  p_session_statistic->ui32LastTransactionCompletionStatus =
6868  p_session_statistic->ui32LastErrorStatus =
6871  LRETURN;
6872  }
6873 
6874  ni_parse_session_statistic_info(p_ctx, p_session_statistic, p_buffer);
6875  if (p_ctx->session_id == NI_INVALID_SESSION_ID)
6876  {
6878  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, %s return.\n",
6879  __func__, ni_get_device_type_str(p_ctx->device_type));
6880  LRETURN;
6881  }
6882  p_ctx->session_statistic = *p_session_statistic;
6883 
6884 END:
6885  ni_aligned_free(p_buffer);
6886  ni_log2(p_ctx, NI_LOG_TRACE, "%s():exit\n", __func__);
6887 
6888  return retval;
6889 }
6890 /*!*****************************************************************************
6891  * \brief Query a particular xcoder instance to get buffer/data Info data
6892  *
6893  * \param ni_session_context_t p_ctx - xcoder Context
6894  * \param ni_instance_buf_info_rw_type_t rw_type
6895  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
6896  * \param ni_instance_buf_info_t *out - Struct preallocated from the caller
6897  * where the resulting data will be placed
6898  *
6899  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION,
6900  * NI_RETCODE_ERROR_MEM_ALOC or NI_RETCODE_ERROR_NVME_CMD_FAILED on
6901  * failure
6902  ******************************************************************************/
6905  ni_device_type_t device_type,
6906  ni_instance_buf_info_t *p_inst_buf_info)
6907 {
6908  void* p_buffer = NULL;
6910  uint32_t ui32LBA = 0;
6911  uint32_t dataLen =
6912  ((sizeof(ni_instance_buf_info_t) + (NI_MEM_PAGE_ALIGNMENT - 1)) /
6915 
6916  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
6917 
6918  if (!p_ctx || !p_inst_buf_info)
6919  {
6920  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
6921  __func__);
6922  retval = NI_RETCODE_INVALID_PARAM;
6923  LRETURN;
6924  }
6925 
6926  if (!(NI_DEVICE_TYPE_DECODER == device_type ||
6927  NI_DEVICE_TYPE_ENCODER == device_type ||
6928  NI_DEVICE_TYPE_SCALER == device_type ||
6929  NI_DEVICE_TYPE_AI == device_type))
6930  {
6931  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown device type %d, return\n",
6932  __func__, device_type);
6933  retval = NI_RETCODE_INVALID_PARAM;
6934  LRETURN;
6935  }
6936 
6937  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
6938  {
6939  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
6940  __func__);
6942  LRETURN;
6943  }
6944 
6945  if (INST_BUF_INFO_RW_READ == rw_type)
6946  {
6947  ui32LBA = QUERY_INSTANCE_RBUFF_SIZE_R(p_ctx->session_id, device_type);
6948  }
6949  else if (INST_BUF_INFO_RW_WRITE == rw_type)
6950  {
6951  ui32LBA = QUERY_INSTANCE_WBUFF_SIZE_R(p_ctx->session_id, device_type);
6952  }
6953  else if(INST_BUF_INFO_RW_WRITE_BY_EP == rw_type)
6954  {
6956  "65") >= 0)
6957  {
6958  ui32LBA = QUERY_INSTANCE_WBUFF_SIZE_R_BY_EP(p_ctx->session_id, device_type);
6959  }
6960  else
6961  {
6963  LRETURN;
6964  }
6965  }
6966  else if (INST_BUF_INFO_RW_UPLOAD == rw_type)
6967  {
6968  ui32LBA = QUERY_INSTANCE_UPLOAD_ID_R(p_ctx->session_id, device_type);
6969  } else if (INST_BUF_INFO_R_ACQUIRE == rw_type)
6970  {
6971  ui32LBA = QUERY_INSTANCE_ACQUIRE_BUF(p_ctx->session_id, device_type);
6972  } else if (INST_BUF_INFO_RW_READ_BUSY == rw_type)
6973  {
6974  ui32LBA =
6975  QUERY_INSTANCE_RBUFF_SIZE_BUSY_R(p_ctx->session_id, device_type);
6976  } else if (INST_BUF_INFO_RW_WRITE_BUSY == rw_type)
6977  {
6978  ui32LBA =
6979  QUERY_INSTANCE_WBUFF_SIZE_BUSY_R(p_ctx->session_id, device_type);
6980  } else if (INST_BUF_INFO_RW_READ_BY_AI == rw_type)
6981  {
6982  ui32LBA = QUERY_INSTANCE_AI_INFO_R(p_ctx->session_id, device_type);
6983  } else
6984  {
6985  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown query type %d, return\n",
6986  __func__, rw_type);
6987  retval = NI_RETCODE_INVALID_PARAM;
6988  LRETURN;
6989  }
6990 
6991  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), dataLen))
6992  {
6993  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
6994  NI_ERRNO, __func__);
6995  retval = NI_RETCODE_ERROR_MEM_ALOC;
6996  LRETURN;
6997  }
6998 
6999  memset(p_buffer, 0, dataLen);
7000 
7001  if (ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_buffer, dataLen, ui32LBA) < 0)
7002  {
7003  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
7005  LRETURN;
7006  }
7007 
7008  memcpy((void*)p_inst_buf_info, p_buffer, sizeof(ni_instance_buf_info_t));
7009 
7010  p_inst_buf_info->buf_avail_size = ni_htonl(p_inst_buf_info->buf_avail_size);
7011 
7012 END:
7013 
7014  ni_aligned_free(p_buffer);
7015  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
7016 
7017  return retval;
7018 }
7019 
7020 /*!*****************************************************************************
7021  * \brief Configure the read/write pipe for a session to control its behavior
7022  *
7023  * \param ni_session_context_t p_ctx - xcoder Context
7024  * \param ni_session_config_rw_t rw_type
7025  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
7026  * \param uint8_t enable
7027  * \param uint8_t hw_action
7028  * \param uint16_t frame_id
7029  *
7030  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION,
7031  * NI_RETCODE_ERROR_MEM_ALOC or NI_RETCODE_ERROR_NVME_CMD_FAILED on
7032  * failure
7033  ******************************************************************************/
7036  uint8_t enable, uint8_t hw_action,
7037  uint16_t frame_id)
7038 {
7040  uint32_t ui32LBA = 0;
7041  void * p_buffer = NULL;
7042  uint32_t buffer_size = 0;
7043  ni_session_config_rw_t * rw_config = NULL;
7044 
7045  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
7046 
7047  if (!p_ctx)
7048  {
7049  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
7050  __func__);
7051  retval = NI_RETCODE_INVALID_PARAM;
7052  LRETURN;
7053  }
7054 
7055  if (!((SESSION_READ_CONFIG == rw_type) || (SESSION_WRITE_CONFIG == rw_type)))
7056  {
7057  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown config type %d, return\n",
7058  __func__, rw_type);
7059  retval = NI_RETCODE_INVALID_PARAM;
7060  LRETURN;
7061  }
7062 
7064  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), buffer_size))
7065  {
7066  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
7067  NI_ERRNO, __func__);
7068  retval = NI_RETCODE_ERROR_MEM_ALOC;
7069  LRETURN;
7070  }
7071  memset(p_buffer, 0, buffer_size);
7072  rw_config = (ni_session_config_rw_t *)p_buffer;
7073  rw_config->ui8Enable = enable;
7074  rw_config->ui8HWAccess = hw_action;
7075  switch(rw_type)
7076  {
7077  case SESSION_READ_CONFIG:
7078  rw_config->uHWAccessField.ui16ReadFrameId = frame_id;
7079  break;
7080  case SESSION_WRITE_CONFIG:
7081  rw_config->uHWAccessField.ui16WriteFrameId = frame_id;
7082  break;
7083  default:
7084  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown config type %d, return\n",
7085  __func__, rw_type);
7086  retval = NI_RETCODE_INVALID_PARAM;
7087  LRETURN;
7088  }
7089 
7090  switch(rw_type)
7091  {
7092  case SESSION_READ_CONFIG:
7093  ui32LBA = CONFIG_SESSION_Read_W(p_ctx->session_id);
7094  break;
7095  case SESSION_WRITE_CONFIG:
7096  ui32LBA = CONFIG_SESSION_Write_W(p_ctx->session_id);
7097  break;
7098  default:
7099  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown config type %d, return\n",
7100  __func__, rw_type);
7101  retval = NI_RETCODE_INVALID_PARAM;
7102  LRETURN;
7103  }
7104  if (ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_buffer, buffer_size, ui32LBA) < 0)
7105  {
7106  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
7108  LRETURN;
7109  }
7110 
7111 END:
7112 
7113  ni_aligned_free(p_buffer);
7114  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
7115 
7116  return retval;
7117 }
7118 
7119 /*!******************************************************************************
7120  * \brief Send a p_config command for Start Of Stream
7121  *
7122  * \param ni_session_context_t p_ctx - xcoder Context
7123  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
7124  *
7125  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION. NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
7126  *******************************************************************************/
7128 {
7130  uint32_t ui32LBA = 0;
7131 
7132  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
7133 
7134  if (!p_ctx)
7135  {
7136  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
7137  __func__);
7138  retval = NI_RETCODE_INVALID_PARAM;
7139  LRETURN;
7140  }
7141 
7142  if (! (NI_DEVICE_TYPE_DECODER == device_type ||
7143  NI_DEVICE_TYPE_ENCODER == device_type))
7144  {
7145  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown device type %d, return\n",
7146  __func__, device_type);
7147  retval = NI_RETCODE_INVALID_PARAM;
7148  LRETURN;
7149  }
7150 
7151  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
7152  {
7153  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
7154  __func__);
7156  LRETURN;
7157  }
7158 
7159  ui32LBA = CONFIG_INSTANCE_SetSOS_W(p_ctx->session_id, device_type);
7160 
7161  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)
7162  {
7163  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
7165  }
7166 
7167 END:
7168 
7169  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
7170  return retval;
7171 }
7172 
7173 /*!******************************************************************************
7174  * \brief Send a p_config command for End Of Stream
7175  *
7176  * \param ni_session_context_t p_ctx - xcoder Context
7177  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
7178  *
7179  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION, NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
7180  *******************************************************************************/
7182 {
7184  uint32_t ui32LBA = 0;
7185 
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_DEVICE_TYPE_DECODER == device_type ||
7197  NI_DEVICE_TYPE_ENCODER == device_type))
7198  {
7199  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown device type %d, return\n",
7200  __func__, device_type);
7201  retval = NI_RETCODE_INVALID_PARAM;
7202  LRETURN;
7203  }
7204 
7205  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
7206  {
7207  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
7208  __func__);
7210  LRETURN;
7211  }
7212 
7213  ui32LBA = CONFIG_INSTANCE_SetEOS_W(p_ctx->session_id, device_type);
7214 
7215  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)
7216  {
7217  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
7219  }
7220 
7221 END:
7222 
7223  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
7224 
7225  return retval;
7226 }
7227 
7228 /*!******************************************************************************
7229  * \brief Send a p_config command to flush the stream
7230  *
7231  * \param ni_session_context_t p_ctx - xcoder Context
7232  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
7233  *
7234  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION, NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
7235  *******************************************************************************/
7237 {
7239  uint32_t ui32LBA = 0;
7240 
7241  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
7242 
7243  if (!p_ctx)
7244  {
7245  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
7246  __func__);
7247  retval = NI_RETCODE_INVALID_PARAM;
7248  LRETURN;
7249  }
7250 
7251  if (NI_DEVICE_TYPE_DECODER != device_type)
7252  {
7253  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown device type %d, return\n",
7254  __func__, device_type);
7255  retval = NI_RETCODE_INVALID_PARAM;
7256  LRETURN;
7257  }
7258 
7259  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
7260  {
7261  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
7262  __func__);
7264  LRETURN;
7265  }
7266 
7267  ui32LBA = CONFIG_INSTANCE_Flush_W(p_ctx->session_id, device_type);
7268  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
7270  ui32LBA);
7271  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
7272  p_ctx->device_type, p_ctx->hw_id,
7273  &(p_ctx->session_id), OPT_1);
7274  CHECK_VPU_RECOVERY(retval);
7275 
7276 END:
7277 
7278  if (NI_RETCODE_SUCCESS != retval)
7279  {
7281  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed with %d\n", __func__, retval);
7282  }
7283 
7284  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
7285 
7286  return retval;
7287 }
7288 
7289 /*!******************************************************************************
7290  * \brief Send a p_config command to set the length for the incoming write packet
7291  *
7292  * \param ni_session_context_t p_ctx - xcoder Context
7293  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
7294  * \param
7295  *
7296  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION, NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
7297  *******************************************************************************/
7299 {
7301  uint32_t ui32LBA = 0;
7302  void * p_buffer = NULL;
7303  uint32_t buffer_size = 0;
7304 
7305  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
7306 
7307  if (!p_ctx)
7308  {
7309  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
7310  __func__);
7311  retval = NI_RETCODE_INVALID_PARAM;
7312  LRETURN;
7313  }
7314 
7315  if (!(NI_DEVICE_TYPE_DECODER == device_type ||
7316  NI_DEVICE_TYPE_ENCODER == device_type))
7317  {
7318  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown device type %d, return\n",
7319  __func__, device_type);
7320  retval = NI_RETCODE_INVALID_PARAM;
7321  LRETURN;
7322  }
7323 
7324  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
7325  {
7326  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
7327  __func__);
7329  LRETURN;
7330  }
7331 
7332  buffer_size = ((sizeof(len) + (NI_MEM_PAGE_ALIGNMENT - 1)) / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT;
7333  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), buffer_size))
7334  {
7335  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
7336  NI_ERRNO, __func__);
7337  retval = NI_RETCODE_ERROR_MEM_ALOC;
7338  LRETURN;
7339  }
7340  memset(p_buffer, 0, buffer_size);
7341  memcpy(p_buffer, &len, sizeof(len));
7342 
7343  ui32LBA = CONFIG_INSTANCE_SetPktSize_W(p_ctx->session_id, device_type);
7344 
7345  if (ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_buffer, buffer_size, ui32LBA) < 0)
7346  {
7347  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
7349  }
7350 
7351 END:
7352 
7353  ni_aligned_free(p_buffer);
7354  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
7355 
7356  return retval;
7357 }
7358 
7359 /*!******************************************************************************
7360  * \brief Send a p_config command to inform encoder sequence change
7361  *
7362  * \param ni_session_context_t p_ctx - xcoder Context
7363  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
7364  * \param ni_resolution_t p_resolution - sequence change resolution
7365  *
7366  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION, NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
7367  *******************************************************************************/
7369 {
7371  uint32_t ui32LBA = 0;
7372  void * p_buffer = NULL;
7373  uint32_t buffer_size = 0;
7374 
7375  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
7376 
7377  if (!p_ctx)
7378  {
7379  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
7380  __func__);
7381  retval = NI_RETCODE_INVALID_PARAM;
7382  LRETURN;
7383  }
7384 
7385  if (!(NI_DEVICE_TYPE_ENCODER == device_type))
7386  {
7387  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: Seq Change not supported for device type %d, return\n", device_type);
7388  retval = NI_RETCODE_INVALID_PARAM;
7389  LRETURN;
7390  }
7391 
7392  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
7393  {
7394  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
7395  __func__);
7397  LRETURN;
7398  }
7399 
7401  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), buffer_size))
7402  {
7403  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
7404  NI_ERRNO, __func__);
7405  retval = NI_RETCODE_ERROR_MEM_ALOC;
7406  LRETURN;
7407  }
7408  memset(p_buffer, 0, buffer_size);
7409  memcpy(p_buffer, p_resolution, sizeof(ni_resolution_t));
7410 
7411  ui32LBA = CONFIG_INSTANCE_SetSeqChange_W(p_ctx->session_id, device_type);
7412 
7413  if (ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_buffer, buffer_size, ui32LBA) < 0)
7414  {
7415  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
7417  }
7418 
7419 END:
7420 
7421  ni_aligned_free(p_buffer);
7422  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
7423 
7424  return retval;
7425 }
7426 
7427 /*!******************************************************************************
7428  * \brief Send a p_config command to configure encoding parameters.
7429  *
7430  * \param ni_session_context_t p_ctx - xcoder Context
7431  *
7432  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION, NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
7433  *******************************************************************************/
7435 {
7436  void* p_encoder_config = NULL;
7437  void* p_encoder_roi_qp_map = NULL;
7438  ni_encoder_config_t* p_cfg = NULL;
7439  uint32_t buffer_size = 0;
7441  uint32_t ui32LBA = 0;
7442  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
7443 
7444  if (!p_ctx)
7445  {
7446  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
7447  __func__);
7448  retval = NI_RETCODE_INVALID_PARAM;
7449  LRETURN;
7450  }
7451 
7452  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
7453  {
7454  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
7455  __func__);
7457  LRETURN;
7458  }
7459 
7460  if (ni_cmp_fw_api_ver((char*) &p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6rh") >= 0)
7461  {
7463  ni_xcoder_params_t *session_config = (ni_xcoder_params_t *)p_ctx->p_session_config;
7464  if (session_config->cfg_enc_params.customize_roi_qp_level > 64) {
7466  buffer_size = ((buffer_size + (NI_MEM_PAGE_ALIGNMENT - 1)) / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT;
7467  if (ni_posix_memalign(&p_encoder_roi_qp_map, sysconf(_SC_PAGESIZE), buffer_size))
7468  {
7469  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
7470  NI_ERRNO, __func__);
7471  retval = NI_RETCODE_ERROR_MEM_ALOC;
7472  LRETURN;
7473  }
7474  memset(p_encoder_roi_qp_map, 0, buffer_size);
7475  memcpy(p_encoder_roi_qp_map, session_config->customize_roi_qp_map,
7477  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
7478  p_encoder_roi_qp_map, buffer_size, ui32LBA);
7479  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
7480  p_ctx->device_type, p_ctx->hw_id,
7481  &(p_ctx->session_id), OPT_1);
7482  if (NI_RETCODE_SUCCESS != retval)
7483  {
7484  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);
7485  //Close the session since we can't configure it
7486  retval = ni_encoder_session_close(p_ctx, 0);
7487  if (NI_RETCODE_SUCCESS != retval)
7488  {
7489  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);
7490  }
7492  LRETURN;
7493  }
7494  session_config->cfg_enc_params.customize_roi_qp_level -= 64;
7495  } else if (session_config->cfg_enc_params.customize_roi_qp_level == 64) {
7496  ni_log2(p_ctx, NI_LOG_INFO, "customizeQpMapFile is ignored since customizeQpLevel=0");
7497  session_config->cfg_enc_params.customize_roi_qp_level = 0;
7498  }
7499  }
7500 
7501  buffer_size = sizeof(ni_encoder_config_t);
7502  buffer_size = ((buffer_size + (NI_MEM_PAGE_ALIGNMENT - 1)) / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT;
7503  if (ni_posix_memalign(&p_encoder_config, sysconf(_SC_PAGESIZE), buffer_size))
7504  {
7505  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
7506  NI_ERRNO, __func__);
7507  retval = NI_RETCODE_ERROR_MEM_ALOC;
7508  LRETURN;
7509  }
7510  memset(p_encoder_config, 0, buffer_size);
7511 
7512  ni_set_custom_template(p_ctx, p_encoder_config, p_ctx->p_session_config);
7513  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));
7514  if (NI_RETCODE_PARAM_WARN == retval)
7515  {
7516  ni_log2(p_ctx, NI_LOG_INFO, "WARNING: ni_validate_custom_template() . %s\n", p_ctx->param_err_msg);
7517  fflush(stdout);
7518  }
7519  else if (NI_RETCODE_SUCCESS != retval)
7520  {
7521  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: ni_validate_custom_template() failed. %s\n", p_ctx->param_err_msg);
7522  fflush(stdout);
7523  retval = NI_RETCODE_INVALID_PARAM;
7524  LRETURN;
7525  }
7526  //configure the session here
7528 
7529  //Flip the bytes!! any param 16bits in size need ni_htons, 32bit need ni_htonl
7530  p_cfg = (ni_encoder_config_t*)p_encoder_config;
7531  p_cfg->i32picWidth = ni_htonl(p_cfg->i32picWidth);
7532  p_cfg->i32picHeight = ni_htonl(p_cfg->i32picHeight);
7533  p_cfg->i32meBlkMode = ni_htonl(p_cfg->i32meBlkMode);
7534  p_cfg->i32frameRateInfo = ni_htonl(p_cfg->i32frameRateInfo);
7535  p_cfg->i32vbvBufferSize = ni_htonl(p_cfg->i32vbvBufferSize);
7536  p_cfg->i32userQpMax = ni_htonl(p_cfg->i32userQpMax);
7537  p_cfg->i32maxIntraSize = ni_htonl(p_cfg->i32maxIntraSize);
7538  p_cfg->i32userMaxDeltaQp = ni_htonl(p_cfg->i32userMaxDeltaQp);
7539  p_cfg->i32userMinDeltaQp = ni_htonl(p_cfg->i32userMinDeltaQp);
7540  p_cfg->i32userQpMin = ni_htonl(p_cfg->i32userQpMin);
7541  p_cfg->i32bitRate = ni_htonl(p_cfg->i32bitRate);
7542  p_cfg->i32bitRateBL = ni_htonl(p_cfg->i32bitRateBL);
7543  p_cfg->i32srcBitDepth = ni_htonl(p_cfg->i32srcBitDepth);
7544  p_cfg->hdrEnableVUI = ni_htonl(p_cfg->hdrEnableVUI);
7545  p_cfg->ui32VuiDataSizeBits = ni_htonl(p_cfg->ui32VuiDataSizeBits);
7546  p_cfg->ui32VuiDataSizeBytes = ni_htonl(p_cfg->ui32VuiDataSizeBytes);
7547  p_cfg->i32hwframes = ni_htonl(p_cfg->i32hwframes);
7548  p_cfg->ui16HDR10MaxLight = ni_htons(p_cfg->ui16HDR10MaxLight);
7549  p_cfg->ui16HDR10AveLight = ni_htons(p_cfg->ui16HDR10AveLight);
7550  p_cfg->ui16gdrDuration = ni_htons(p_cfg->ui16gdrDuration);
7551  p_cfg->ui32ltrRefInterval = ni_htonl(p_cfg->ui32ltrRefInterval);
7552  p_cfg->i32ltrRefQpOffset = ni_htonl(p_cfg->i32ltrRefQpOffset);
7553  p_cfg->ui32ltrFirstGap = ni_htonl(p_cfg->ui32ltrFirstGap);
7554  p_cfg->i32tolCtbRcInter = ni_htonl(p_cfg->i32tolCtbRcInter);
7555  p_cfg->i32tolCtbRcIntra = ni_htonl(p_cfg->i32tolCtbRcIntra);
7556  p_cfg->i16bitrateWindow = ni_htons(p_cfg->i16bitrateWindow);
7557  p_cfg->ui16hdr10_dx0 = ni_htons(p_cfg->ui16hdr10_dx0);
7558  p_cfg->ui16hdr10_dy0 = ni_htons(p_cfg->ui16hdr10_dy0);
7559  p_cfg->ui16hdr10_dx1 = ni_htons(p_cfg->ui16hdr10_dx1);
7560  p_cfg->ui16hdr10_dy1 = ni_htons(p_cfg->ui16hdr10_dy1);
7561  p_cfg->ui16hdr10_dx2 = ni_htons(p_cfg->ui16hdr10_dx2);
7562  p_cfg->ui16hdr10_dy2 = ni_htons(p_cfg->ui16hdr10_dy2);
7563  p_cfg->ui16hdr10_wx = ni_htons(p_cfg->ui16hdr10_wx);
7564  p_cfg->ui16hdr10_wy = ni_htons(p_cfg->ui16hdr10_wy);
7565  p_cfg->ui32hdr10_maxluma = ni_htonl(p_cfg->ui32hdr10_maxluma);
7566  p_cfg->ui32hdr10_minluma = ni_htonl(p_cfg->ui32hdr10_minluma);
7567  p_cfg->ui32lumaLinesize = ni_htons(p_cfg->ui32lumaLinesize);
7568  p_cfg->ui32chromaLinesize = ni_htons(p_cfg->ui32chromaLinesize);
7569 
7570  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
7571  p_encoder_config, buffer_size, ui32LBA);
7572  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
7573  p_ctx->device_type, p_ctx->hw_id,
7574  &(p_ctx->session_id), OPT_1);
7575  if (NI_RETCODE_SUCCESS != retval)
7576  {
7577  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);
7578  //Close the session since we can't configure it
7579  retval = ni_encoder_session_close(p_ctx, 0);
7580  if (NI_RETCODE_SUCCESS != retval)
7581  {
7582  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);
7583  }
7584 
7586  }
7587 
7588 END:
7589 
7590  ni_aligned_free(p_encoder_config);
7591  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
7592  return retval;
7593 }
7594 
7595 /*!******************************************************************************
7596  * \brief Send a p_config command to configure encoding p_frame parameters.
7597  *
7598  * \param ni_session_context_t p_ctx - xcoder Context
7599  * \param ni_encoder_frame_params_t * params - pointer to the encoder ni_encoder_frame_params_t struct
7600  *
7601  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION, NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
7602  *******************************************************************************/
7604 {
7606  uint32_t buffer_size = sizeof(ni_encoder_frame_params_t);
7608  uint32_t ui32LBA = 0;
7609 
7610  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
7611 
7612  if (!p_ctx || !p_params)
7613  {
7614  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
7615  __func__);
7616  return NI_RETCODE_INVALID_PARAM;
7617  }
7618 
7619  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
7620  {
7621  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
7622  __func__);
7624  }
7625 
7626  buffer_size = ((buffer_size + (NI_MEM_PAGE_ALIGNMENT - 1)) / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT;
7627  if (ni_posix_memalign((void **)&p_cfg, sysconf(_SC_PAGESIZE), buffer_size))
7628  {
7629  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
7630  NI_ERRNO, __func__);
7632  }
7633 
7634  //configure the session here
7636 
7637  //Flip the bytes!!
7638  p_cfg->force_picture_type = ni_htons(p_params->force_picture_type);
7639  p_cfg->data_format = ni_htons(p_params->data_format);
7640  p_cfg->picture_type = ni_htons(p_params->picture_type);
7641  p_cfg->video_width = ni_htons(p_params->video_width);
7642  p_cfg->video_height = ni_htons(p_params->video_height);
7643  p_cfg->timestamp = ni_htonl(p_params->timestamp);
7644 
7645  if (ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_cfg,
7646  buffer_size, ui32LBA) < 0)
7647  {
7648  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);
7649  //Close the session since we can't configure it
7650  retval = ni_encoder_session_close(p_ctx, 0);
7651  if (NI_RETCODE_SUCCESS != retval)
7652  {
7653  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);
7654  }
7655 
7657  }
7658 
7659  ni_aligned_free(p_cfg);
7660  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
7661 
7662  return retval;
7663 }
7664 
7665 /*!******************************************************************************
7666  * \brief Get info from received p_frame
7667  *
7668  * \param
7669  *
7670  * \return
7671  *******************************************************************************/
7672 int ni_create_frame(ni_frame_t* p_frame, uint32_t read_length, uint64_t*
7673  p_frame_offset, uint32_t* p_frame_dropped, bool is_hw_frame)
7674 {
7675  uint32_t rx_size =
7676  read_length; //get the length since its the only thing in DW10 now
7677 
7678  if (!p_frame || !p_frame_offset)
7679  {
7680  ni_log(NI_LOG_ERROR, "ERROR: %s(): Null pointer parameters passed\n",
7681  __func__);
7682  return NI_RETCODE_INVALID_PARAM;
7683  }
7684 
7685  *p_frame_offset = 0;
7686  if (p_frame_dropped)
7687  *p_frame_dropped = 0;
7688 
7689  unsigned int metadata_size = NI_FW_META_DATA_SZ -
7691  unsigned int video_data_size = p_frame->data_len[0] + p_frame->data_len[1] +
7692  p_frame->data_len[2] + ((is_hw_frame) ? p_frame->data_len[3] : 0);
7693  ni_log(NI_LOG_DEBUG, "%s: rx_size = %d metadataSize = %d\n", __func__, rx_size,
7694  metadata_size);
7695 
7696  p_frame->p_custom_sei_set = NULL;
7697 
7698  if (rx_size == metadata_size)
7699  {
7700  video_data_size = 0;
7701  }
7702 
7703  if (rx_size > video_data_size)
7704  {
7705  ni_metadata_dec_frame_t *p_meta =
7706  (ni_metadata_dec_frame_t *)((uint8_t *)p_frame->p_buffer +
7707  video_data_size);
7708 
7709  *p_frame_offset = p_meta->metadata_common.ui64_data.frame_offset;
7710  rx_size -= metadata_size;
7711  p_frame->crop_top = p_meta->metadata_common.crop_top;
7712  p_frame->crop_bottom = p_meta->metadata_common.crop_bottom;
7713  p_frame->crop_left = p_meta->metadata_common.crop_left;
7714  p_frame->crop_right = p_meta->metadata_common.crop_right;
7715  p_frame->ni_pict_type = p_meta->metadata_common.frame_type;
7716 
7717  p_frame->video_width = p_meta->metadata_common.frame_width;
7718  p_frame->video_height = p_meta->metadata_common.frame_height;
7719 
7720  ni_log(
7721  NI_LOG_DEBUG,
7722  "%s: [metadata] cropRight=%u, cropLeft=%u, "
7723  "cropBottom=%u, cropTop=%u, frame_offset=%" PRIu64 ", pic=%ux%u, "
7724  "pict_type=%d, crop=%ux%u, sei header: 0x%0x number %u size %u num_frame_dropped %u\n",
7725  __func__, p_frame->crop_right, p_frame->crop_left,
7726  p_frame->crop_bottom, p_frame->crop_top,
7728  p_meta->metadata_common.frame_width,
7729  p_meta->metadata_common.frame_height, p_frame->ni_pict_type,
7730  p_frame->crop_right - p_frame->crop_left,
7731  p_frame->crop_bottom - p_frame->crop_top, p_meta->sei_header,
7732  p_meta->sei_number, p_meta->sei_size, p_meta->metadata_common.num_frame_dropped);
7733 
7734  p_frame->sei_total_len = 0;
7735  p_frame->sei_cc_offset = 0;
7736  p_frame->sei_cc_len = 0;
7741  p_frame->sei_hdr_plus_offset = 0;
7742  p_frame->sei_hdr_plus_len = 0;
7743  p_frame->sei_user_data_unreg_offset = 0;
7744  p_frame->sei_user_data_unreg_len = 0;
7745 
7746  if (p_frame_dropped)
7747  {
7748  *p_frame_dropped = p_meta->metadata_common.num_frame_dropped;
7749  }
7750 
7751  if (p_meta->sei_number)
7752  {
7753 #if 1 // QUADRA_SEI_FMT
7754  ni_log(NI_LOG_DEBUG, "ui32SeiHeader 0x%x ui16SeiNumber %d ui16SeiSize %d SEI 0x%02x%02x\n",
7755  p_meta->sei_header, p_meta->sei_number, p_meta->sei_size,
7756  *((uint8_t*)p_meta + metadata_size),
7757  *((uint8_t*)p_meta + metadata_size+1));
7758 
7759  { // retrieve sei from new format
7760  uint16_t ui16SeiProcessed = 0;
7761  ni_sei_header_t * pEntryHeader = &p_meta->first_sei_header;
7762  uint32_t ui32Offset = 0;
7763  uint32_t ui32Size;
7764 
7765  rx_size -= p_meta->sei_size;
7766 
7767  do
7768  {
7769  ui16SeiProcessed++;
7770 
7771  if (pEntryHeader->status)
7772  {
7773  ui32Size = pEntryHeader->size;
7774  }
7775  else
7776  {
7777  ui32Size = 0;
7778  }
7779 
7780  ni_log(NI_LOG_DEBUG, "SEI #%x st %d size %d ty %d sz/of %u/%u 0x%02x%02x\n",
7781  ui16SeiProcessed, pEntryHeader->status,
7782  pEntryHeader->size, pEntryHeader->type, ui32Size,
7783  ui32Offset,
7784  *((uint8_t *)p_meta + metadata_size + ui32Offset),
7785  *((uint8_t *)p_meta + metadata_size + ui32Offset + 1));
7786 
7787  // - if multiple entries with same SEI type/subtype, only the last entry is saved;
7788  // consider to use sei_offset[] array instead of explicit sei_*_offset
7789  // - user_data_unreg (UDU) and custom sei passthru via HW and SW respectively
7790  // thus custom SEI is not processed here as it is by SW.
7791 
7792  switch(pEntryHeader->type)
7793  {
7794  case 4: //HEVC_SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35
7795  if (ui32Size)
7796  {
7797  uint8_t *ptr = (uint8_t*)p_meta + metadata_size + ui32Offset;
7798  if(ptr[0] == NI_HDR10P_SEI_BYTE0 && ptr[1] == NI_HDR10P_SEI_BYTE1 &&
7799  ptr[2] == NI_HDR10P_SEI_BYTE2 && ptr[3] == NI_HDR10P_SEI_BYTE3 &&
7800  ptr[4] == NI_HDR10P_SEI_BYTE4 && ptr[5] == NI_HDR10P_SEI_BYTE5)
7801  {
7802  p_frame->sei_hdr_plus_len = ui32Size;
7803  p_frame->sei_hdr_plus_offset =
7804  video_data_size + metadata_size + ui32Offset;
7805 
7806  p_frame->sei_total_len += ui32Size;
7807 
7808  ni_log(NI_LOG_DEBUG, "%s: hdr10+ size=%u hdr10+ offset=%u\n",
7809  __func__, p_frame->sei_hdr_plus_len,
7810  p_frame->sei_hdr_plus_offset);
7811  }
7812  else if(ptr[0] == NI_CC_SEI_BYTE0 && ptr[1] == NI_CC_SEI_BYTE1 &&
7813  ptr[2] == NI_CC_SEI_BYTE2 && ptr[3] == NI_CC_SEI_BYTE3 &&
7814  ptr[4] == NI_CC_SEI_BYTE4 && ptr[5] == NI_CC_SEI_BYTE5 &&
7815  ptr[6] == NI_CC_SEI_BYTE6 && ptr[7] == NI_CC_SEI_BYTE7)
7816  {
7817  // Found CC data
7818  // number of 3 byte close captions is bottom 5 bits of
7819  // 9th byte of T35 payload
7820  // uint32_t ui32CCSize = (ptr[8] & 0x1F) * 3; // avoid overwriting ui32CCSize
7821 
7822  // return close caption data offset and length, and
7823  // skip past 10 header bytes to close caption data
7824  p_frame->sei_cc_len = (ptr[8] & 0x1F) * 3; // ui32CCSize;
7825  p_frame->sei_cc_offset = video_data_size + metadata_size
7826  + ui32Offset + 10;
7827 
7828  p_frame->sei_total_len += p_frame->sei_cc_len;
7829 
7831  "%s: close caption size %u ,"
7832  "offset %u = video size %u meta size %d off "
7833  " %u + 10\n",
7834  __func__, p_frame->sei_cc_len, p_frame->sei_cc_offset,
7835  video_data_size, metadata_size, ui32Offset);
7836  }
7837  else
7838  {
7840  "%s: unsupported T35; type %u size %u status %u "
7841  "offset %u\n",
7842  __func__, pEntryHeader->type, pEntryHeader->size,
7843  pEntryHeader->status, ui32Offset);
7844  }
7845  }
7846  else
7847  {
7849  "Error %s: T35 SEI dropped due to %s; type %u size %u status %u offset %u\n",
7850  __func__, pEntryHeader->status ? "missing payload" : "payload size exceeded SEI buffer size",
7851  pEntryHeader->type, pEntryHeader->size, pEntryHeader->status, ui32Offset);
7852  }
7853  break;
7854 
7855  case 5: // HEVC_SEI_TYPE_USER_DATA_UNREGISTERED
7856  // set offset now so len=0 will signify an error if this SEI is dropped
7857  p_frame->sei_user_data_unreg_offset = video_data_size + metadata_size + ui32Offset;
7858  if (ui32Size)
7859  {
7860  p_frame->sei_user_data_unreg_len = ui32Size;
7861  p_frame->sei_total_len += ui32Size;
7862  ni_log(NI_LOG_DEBUG, "User Data Unreg size = %u\n", ui32Size);
7863  }
7864  else
7865  {
7866  p_frame->sei_user_data_unreg_len = 0; // in case there are multiple UDU SEI entries
7868  "Error %s: User Data Unreg dropped due to %s; type %u size %u status %u offset %u\n",
7869  __func__, pEntryHeader->status ? "missing payload" : "payload size exceeded SEI buffer size",
7870  pEntryHeader->type, pEntryHeader->size, pEntryHeader->status, ui32Offset);
7871  }
7872  break;
7873 
7874  case 137: //HEVC_SEI_TYPE_MASTERING_DISPLAY_INFO
7875  if (ui32Size)
7876  {
7877  p_frame->sei_hdr_mastering_display_color_vol_len = ui32Size;
7879  video_data_size + metadata_size + ui32Offset;
7880 
7881  p_frame->sei_total_len += ui32Size;
7884  *)((uint8_t *)p_meta + metadata_size + ui32Offset);
7885 
7886  ni_log(NI_LOG_DEBUG, "Display Primaries x[0]=%u y[0]=%u\n",
7887  ni_ntohs(pColourVolume->display_primaries[0][0]),
7888  ni_ntohs(pColourVolume->display_primaries[0][1]));
7889  ni_log(NI_LOG_DEBUG, "Display Primaries x[1]=%u y[1]=%u\n",
7890  ni_ntohs(pColourVolume->display_primaries[1][0]),
7891  ni_ntohs(pColourVolume->display_primaries[1][1]));
7892  ni_log(NI_LOG_DEBUG, "Display Primaries x[2]=%u y[2]=%u\n",
7893  ni_ntohs(pColourVolume->display_primaries[2][0]),
7894  ni_ntohs(pColourVolume->display_primaries[2][1]));
7895 
7896  ni_log(NI_LOG_DEBUG, "White Point x=%u y=%u\n",
7897  ni_ntohs(pColourVolume->white_point_x),
7898  ni_ntohs(pColourVolume->white_point_y));
7899  ni_log(NI_LOG_DEBUG, "Display Mastering Lum, Max=%u Min=%u\n",
7900  ni_ntohl(pColourVolume->max_display_mastering_luminance),
7901  ni_ntohl(pColourVolume->min_display_mastering_luminance));
7902  }
7903  else
7904  {
7906  "Error %s: mastering display info dropped due to %s; type %u size %u status %u offset %u\n",
7907  __func__, pEntryHeader->status ? "missing payload" : "payload size exceeded SEI buffer size",
7908  pEntryHeader->type, pEntryHeader->size, pEntryHeader->status, ui32Offset);
7909  }
7910  break;
7911 
7912  case 144: //HEVC_SEI_TYPE_CONTENT_LIGHT_LEVEL_INFO
7913  if (ui32Size)
7914  {
7915  p_frame->sei_hdr_content_light_level_info_len = ui32Size;
7917  video_data_size + metadata_size + ui32Offset;
7918 
7919  p_frame->sei_total_len += ui32Offset;
7920 
7923  (uint8_t*)p_meta + metadata_size + ui32Offset);
7924 
7925  ni_log(NI_LOG_DEBUG, "Max Content Light level=%u Max Pic Avg Light Level=%u\n",
7926  ni_ntohs(pLightLevel->max_content_light_level),
7927  ni_ntohs(pLightLevel->max_pic_average_light_level));
7928  }
7929  else
7930  {
7932  "Error %s: content light level info dropped due to %s; type %u size %u status %u offset %u\n",
7933  __func__, pEntryHeader->status ? "missing payload" : "payload size exceeded SEI buffer size",
7934  pEntryHeader->type, pEntryHeader->size, pEntryHeader->status, ui32Offset);
7935  }
7936  break;
7937 
7938  case 200: // Custom SEI not included in HEVC_SEI_Type
7939  if (ui32Size)
7940  {
7941  p_frame->vui_len = ui32Size;
7942  p_frame->vui_offset =
7943  video_data_size + metadata_size + ui32Offset;
7944  p_frame->sei_total_len += ui32Size;
7945  }
7946  break;
7947  case 206: // Customer reset ppu resolution, need dropped
7948  ni_log(NI_LOG_DEBUG, "Custom SEI PPU_RECONFIG dropped", __func__);
7949  break;
7950  default:
7952  "Warning %s: SEI message dropped (unsupported - check "
7953  "decoder SEI bitmap settings);"
7954  " type %u size %u status %u offset %u payload bytes %u\n",
7955  __func__, pEntryHeader->type, pEntryHeader->size,
7956  pEntryHeader->status, ui32Offset, ui32Size);
7957  break;
7958  }
7959  ui32Offset += ui32Size;
7960  pEntryHeader = (ni_sei_header_t *)((uint8_t*)p_meta + metadata_size + ui32Offset);
7961  ui32Offset += sizeof(ni_sei_header_t);
7962  } while (ui32Offset <= p_meta->sei_size && ui16SeiProcessed < p_meta->sei_number);
7963 
7964  if (p_meta->sei_number != ui16SeiProcessed)
7965  {
7966  ni_log(
7967  NI_LOG_ERROR,
7968  "Error %s: number of SEI messages reported %u != processed %u\n",
7969  __func__, p_meta->sei_number, ui16SeiProcessed);
7970  }
7971  }
7972 
7973 #else // QUADRA_SEI_FMT
7974 
7975  // ni_assert(p_meta->sei_header);
7976  // else // backward compatibility
7977  {
7978  ni_sei_user_data_entry_t *pEntry;
7979  uint32_t ui32CCOffset = 0, ui32CCSize = 0;
7980 
7981  rx_size -= p_meta->sei_size;
7982 
7983  pEntry = (ni_sei_user_data_entry_t *)((uint8_t*)p_meta + metadata_size);
7984 
7985  if (find_t35_sei(p_meta->sei_header, NI_T35_SEI_HDR10_PLUS, pEntry,
7986  &ui32CCOffset, &ui32CCSize))
7987  {
7988  p_frame->sei_hdr_plus_len = ui32CCSize;
7989  p_frame->sei_hdr_plus_offset =
7990  video_data_size + metadata_size + ui32CCOffset;
7991 
7992  p_frame->sei_total_len += ui32CCSize;
7993 
7994  ni_log(NI_LOG_DEBUG, "%s: hdr10+ size=%u hdr10+ offset=%u\n", __func__,
7995  p_frame->sei_hdr_plus_len, p_frame->sei_hdr_plus_offset);
7996  }
7997  else
7998  {
7999  ni_log(NI_LOG_DEBUG, "%s: hdr+ NOT found in meta data!\n", __func__);
8000  }
8001 
8002  if (find_t35_sei(p_meta->sei_header, NI_T35_SEI_CLOSED_CAPTION, pEntry,
8003  &ui32CCOffset, &ui32CCSize))
8004  {
8005  uint8_t *ptr;
8006  // Found CC data at pEntry + ui32CCOffset
8007  ptr = (uint8_t*)pEntry + ui32CCOffset;
8008  // number of 3 byte close captions is bottom 5 bits of
8009  // 9th byte of T35 payload
8010  ui32CCSize = (ptr[8] & 0x1F) * 3;
8011 
8012  // return close caption data offset and length, and
8013  // skip past 10 header bytes to close caption data
8014  p_frame->sei_cc_len = ui32CCSize;
8015  p_frame->sei_cc_offset = video_data_size + metadata_size
8016  + ui32CCOffset + 10;
8017 
8018  p_frame->sei_total_len += p_frame->sei_cc_len;
8019 
8021  "%s: close caption size %u ,"
8022  "offset %u = video size %u meta size %u off "
8023  " %u + 10\n",
8024  __func__, p_frame->sei_cc_len, p_frame->sei_cc_offset,
8025  video_data_size, metadata_size, ui32CCOffset);
8026  }
8027  else
8028  {
8029  ni_log(NI_LOG_DEBUG, "%s: close caption NOT found in meta data!\n",
8030  __func__);
8031  }
8032 
8033  if (find_sei(p_meta->sei_header, pEntry,
8035  &ui32CCOffset, &ui32CCSize))
8036  {
8037  p_frame->sei_hdr_mastering_display_color_vol_len = ui32CCSize;
8039  video_data_size + metadata_size + ui32CCOffset;
8040 
8041  p_frame->sei_total_len += ui32CCSize;
8042 
8043  ni_dec_mastering_display_colour_volume_t* pColourVolume =
8044  (ni_dec_mastering_display_colour_volume_t*)((uint8_t*)pEntry + ui32CCOffset);
8045 
8046  ni_log(NI_LOG_DEBUG, "Display Primaries x[0]=%u y[0]=%u\n",
8047  pColourVolume->display_primaries_x[0],
8048  pColourVolume->display_primaries_y[0]);
8049  ni_log(NI_LOG_DEBUG, "Display Primaries x[1]=%u y[1]=%u\n",
8050  pColourVolume->display_primaries_x[1],
8051  pColourVolume->display_primaries_y[1]);
8052  ni_log(NI_LOG_DEBUG, "Display Primaries x[2]=%u y[2]=%u\n",
8053  pColourVolume->display_primaries_x[2],
8054  pColourVolume->display_primaries_y[2]);
8055 
8056  ni_log(NI_LOG_DEBUG, "White Point x=%u y=%u\n",
8057  pColourVolume->white_point_x,
8058  pColourVolume->white_point_y);
8059  ni_log(NI_LOG_DEBUG, "Display Mastering Lum, Max=%u Min=%u\n",
8060  pColourVolume->max_display_mastering_luminance, pColourVolume->min_display_mastering_luminance);
8061  }
8062  if (find_sei(p_meta->sei_header, pEntry,
8064  &ui32CCOffset, &ui32CCSize))
8065  {
8066  p_frame->sei_hdr_content_light_level_info_len = ui32CCSize;
8068  video_data_size + metadata_size + ui32CCOffset;
8069 
8070  p_frame->sei_total_len += ui32CCSize;
8071 
8072  ni_content_light_level_info_t* pLightLevel =
8073  (ni_content_light_level_info_t*)((uint8_t*)pEntry + ui32CCOffset);
8074  ni_log(NI_LOG_DEBUG, "Max Content Light level=%u Max Pic Avg Light Level=%u\n",
8075  pLightLevel->max_content_light_level, pLightLevel->max_pic_average_light_level);
8076  }
8077 
8078  if (find_sei(p_meta->sei_header, pEntry,
8080  &ui32CCOffset, &ui32CCSize) ||
8081  find_sei(p_meta->sei_header, pEntry,
8083  &ui32CCOffset, &ui32CCSize))
8084  {
8085  p_frame->sei_user_data_unreg_len = ui32CCSize;
8086  p_frame->sei_user_data_unreg_offset =
8087  video_data_size + metadata_size + ui32CCOffset;
8088 
8089  p_frame->sei_total_len += ui32CCSize;
8090 
8091  ni_log(NI_LOG_DEBUG, "User Data Unreg size = %u\n", ui32CCSize);
8092  }
8093  }
8094 #endif // QUADRA_SEI_FMT
8095  if (0 == p_frame->sei_total_len)
8096  {
8097  ni_log(NI_LOG_DEBUG, "Warning retrieved 0 supported SEI !\n");
8098  }
8099  }
8100  }
8101 
8102  p_frame->dts = NI_NOPTS_VALUE;
8103  p_frame->pts = NI_NOPTS_VALUE;
8104  //p_frame->end_of_stream = isEndOfStream;
8105  p_frame->start_of_stream = 0;
8106 
8107  if (rx_size == 0)
8108  {
8109  p_frame->data_len[0] = 0;
8110  p_frame->data_len[1] = 0;
8111  p_frame->data_len[2] = 0;
8112  p_frame->data_len[3] = 0;
8113  }
8114 
8115  ni_log(NI_LOG_DEBUG, "received [0x%08x] data size: %d, end of stream=%u\n",
8116  read_length, rx_size, p_frame->end_of_stream);
8117 
8118  return rx_size;
8119 }
8120 
8121 /*!******************************************************************************
8122  * \brief Get info from received xcoder capability
8123  *
8124  * \param
8125  *
8126  * \return
8127  *******************************************************************************/
8129  ni_device_handle_t device_handle, bool device_in_ctxt)
8130 {
8131  int i, total_types = 0, total_modules = 0;
8132  ni_nvme_identity_t *p_id_data = (ni_nvme_identity_t *)p_data;
8133 
8134  COMPILE_ASSERT(sizeof(p_cap->xcoder_cnt) <= sizeof(p_id_data->xcoder_cnt) &&
8136  sizeof(p_id_data->xcoder_cnt[0]) ==
8137  sizeof(p_cap->xcoder_cnt));
8138 
8139  if (!p_cap || !p_data)
8140  {
8141  ni_log(NI_LOG_ERROR, "ERROR: %s(): Null pointer parameters passed\n",
8142  __func__);
8143  LRETURN;
8144  }
8145 
8146  if ((p_id_data->ui16Vid != NETINT_PCI_VENDOR_ID) ||
8147  (p_id_data->ui16Ssvid != NETINT_PCI_VENDOR_ID))
8148  {
8149  if (device_in_ctxt)
8150  {
8152  "ERROR: Previously in context device got an invalid vendor ID 0x%X SSVID 0x%X. Netint "
8153  "ID 0x%X. Retrying\n",
8154  p_id_data->ui16Vid, p_id_data->ui16Ssvid, NETINT_PCI_VENDOR_ID);
8155  //print some other fields as a test to see if they are invalid too
8156  ni_log(NI_LOG_ERROR, "Model Number: %.*s\n",
8157  (int)sizeof(p_id_data->ai8Sn), p_id_data->ai8Sn);
8158  ni_log(NI_LOG_ERROR, "Serial Number: %.*s\n",
8159  (int)sizeof(p_id_data->ai8Mn), p_id_data->ai8Mn);
8160  ni_log(NI_LOG_ERROR, "Firmware Revision: %.*s\n",
8161  (int)sizeof(p_id_data->ai8Fr), p_id_data->ai8Fr);
8162  ni_log(NI_LOG_ERROR, "xcoder_num_elements: %d\n",
8163  p_id_data->xcoder_num_elements);
8164 
8165  ni_event_handle_t event_handle = NI_INVALID_EVENT_HANDLE;
8166  uint32_t ui32LBA = IDENTIFY_DEVICE_R;
8167  if (ni_nvme_send_read_cmd(device_handle, event_handle, p_data,
8168  NI_NVME_IDENTITY_CMD_DATA_SZ, ui32LBA) < 0)
8169  {
8170  LRETURN;
8171  }
8172  if ((p_id_data->ui16Vid != NETINT_PCI_VENDOR_ID) ||
8173  (p_id_data->ui16Ssvid != NETINT_PCI_VENDOR_ID))
8174  {
8176  "ERROR: %s(): Retry got an invalid vendor ID too 0x%X SSVID "
8177  "0x%X!\n",
8178  __func__, p_id_data->ui16Vid, p_id_data->ui16Ssvid);
8179  //print some other fields as a test to see if they are invalid too
8180  ni_log(NI_LOG_ERROR, "Model Number: %.*s\n",
8181  (int)sizeof(p_id_data->ai8Sn), p_id_data->ai8Sn);
8182  ni_log(NI_LOG_ERROR, "Serial Number: %.*s\n",
8183  (int)sizeof(p_id_data->ai8Mn), p_id_data->ai8Mn);
8184  ni_log(NI_LOG_ERROR, "Firmware Revision: %.*s\n",
8185  (int)sizeof(p_id_data->ai8Fr), p_id_data->ai8Fr);
8186  ni_log(NI_LOG_ERROR, "xcoder_num_elements: %d\n",
8187  p_id_data->xcoder_num_elements);
8188  LRETURN;
8189  }
8190  else
8191  {
8193  "Retry got valid a vendor ID 0x%X SSVID 0x%X. Netint ID 0x%X\n",
8194  p_id_data->ui16Vid, p_id_data->ui16Ssvid, NETINT_PCI_VENDOR_ID);
8195  }
8196  }
8197  else
8198  {
8199  LRETURN;
8200  }
8201  }
8202 
8203  memcpy(p_cap->serial_number, p_id_data->ai8Sn, sizeof(p_cap->serial_number));
8204  memcpy(p_cap->model_number, p_id_data->ai8Mn, sizeof(p_cap->model_number));
8205 
8206  memset(p_cap->fw_rev, 0, sizeof(p_cap->fw_rev));
8207  memcpy(p_cap->fw_rev, p_id_data->ai8Fr, sizeof(p_cap->fw_rev));
8208  ni_log(NI_LOG_DEBUG, "F/W rev: %.*s\n", (int)sizeof(p_cap->fw_rev),
8209  p_cap->fw_rev);
8210 
8211  if (p_id_data->xcoder_num_elements)
8212  {
8213  ni_log(NI_LOG_DEBUG, "xcoder_num_elements: %d xcoder_num_devices: %d\n",
8214  p_id_data->xcoder_num_elements,
8215  p_id_data->xcoder_num_devices);
8216 
8217  for (i = 0; i < NI_DEVICE_TYPE_XCODER_MAX; i++)
8218  {
8219  if (p_id_data->xcoder_cnt[i])
8220  {
8221  total_types++;
8222  total_modules += p_id_data->xcoder_cnt[i];
8223  ni_log(NI_LOG_DEBUG, "type #%d: xcoder_cnt[%d] = %d\n", total_types, i,
8224  p_id_data->xcoder_cnt[i]);
8225  }
8226  }
8227 
8228  if (p_id_data->xcoder_num_elements != total_types ||
8229  p_id_data->xcoder_num_devices != total_modules)
8230  {
8232  "Error: mismatch; xcoder_num_elements: %d (calculated: %d) "
8233  "xcoder_num_devices: %d (calculated: %d)\n",
8234  p_id_data->xcoder_num_elements, total_types,
8235  p_id_data->xcoder_num_devices, total_modules);
8236  LRETURN;
8237  }
8238 
8240  p_cap->hw_elements_cnt = p_id_data->xcoder_num_elements;
8241  p_cap->xcoder_devices_cnt = p_id_data->xcoder_num_devices;
8242  memcpy(p_cap->xcoder_cnt, p_id_data->xcoder_cnt,
8243  NI_DEVICE_TYPE_XCODER_MAX * sizeof(p_id_data->xcoder_cnt[0]));
8244 
8246  {
8247  if (!p_id_data->xcoder_cnt[i])
8248  continue;
8249 
8250  p_cap->xcoder_devices[i].hw_id = p_id_data->xcoder_devices[i].hw_id;
8254  p_cap->xcoder_devices[i].codec_format =
8255  p_id_data->xcoder_devices[i].hw_codec_format;
8256  p_cap->xcoder_devices[i].codec_type =
8257  p_id_data->xcoder_devices[i].hw_codec_type;
8260  if (i == NI_DEVICE_TYPE_ENCODER)
8261  {
8264  }
8265  else
8266  {
8269  }
8270 
8271  p_cap->xcoder_devices[i].video_profile =
8272  p_id_data->xcoder_devices[i].hw_video_profile;
8273  p_cap->xcoder_devices[i].video_level =
8274  p_id_data->xcoder_devices[i].hw_video_level;
8275  }
8276 
8277  goto CAP_POPULATED;
8278  }
8279 
8280  p_cap->device_is_xcoder = p_id_data->device_is_xcoder;
8281  ni_log(NI_LOG_DEBUG, "device_is_xcoder: value in id cmd: %u\n",
8282  p_cap->device_is_xcoder);
8283  if (0 == p_cap->device_is_xcoder)
8284  {
8285  ni_log(NI_LOG_ERROR, "Not an xcoder device !\n");
8286 
8287  if (device_in_ctxt)
8288  {
8290  "ERROR: Previously in context device is not a xcoder device "
8291  "now!\n");
8292  }
8293  LRETURN;
8294  }
8295 
8296  p_cap->hw_elements_cnt = p_id_data->xcoder_num_hw;
8297 
8298  total_modules = p_cap->xcoder_cnt[NI_DEVICE_TYPE_DECODER] =
8299  p_id_data->xcoder_num_h264_decoder_hw +
8300  p_id_data->xcoder_num_h265_decoder_hw;
8301 
8303  p_id_data->xcoder_num_h264_encoder_hw +
8304  p_id_data->xcoder_num_h265_encoder_hw;
8305 
8306  total_modules += p_cap->xcoder_cnt[NI_DEVICE_TYPE_ENCODER];
8307 
8308  p_cap->xcoder_devices_cnt = total_modules;
8309 
8310  if (total_modules >= 1)
8311  {
8312  p_cap->xcoder_devices[0].hw_id = p_id_data->hw0_id;
8316  p_cap->xcoder_devices[0].codec_format = p_id_data->hw0_codec_format;
8317  p_cap->xcoder_devices[0].codec_type = p_id_data->hw0_codec_type;
8322  p_cap->xcoder_devices[0].video_profile = p_id_data->hw0_video_profile;
8323  p_cap->xcoder_devices[0].video_level = p_id_data->hw0_video_level;
8324  }
8325  if (total_modules >= 2)
8326  {
8327  p_cap->xcoder_devices[1].hw_id = p_id_data->hw1_id;
8331  p_cap->xcoder_devices[1].codec_format = p_id_data->hw1_codec_format;
8332  p_cap->xcoder_devices[1].codec_type = p_id_data->hw1_codec_type;
8337  p_cap->xcoder_devices[1].video_profile = p_id_data->hw1_video_profile;
8338  p_cap->xcoder_devices[1].video_level = p_id_data->hw1_video_level;
8339  }
8340  if (total_modules >= 3)
8341  {
8342  p_cap->xcoder_devices[2].hw_id = p_id_data->hw2_id;
8346  p_cap->xcoder_devices[2].codec_format = p_id_data->hw2_codec_format;
8347  p_cap->xcoder_devices[2].codec_type = p_id_data->hw2_codec_type;
8352  p_cap->xcoder_devices[2].video_profile = p_id_data->hw2_video_profile;
8353  p_cap->xcoder_devices[2].video_level = p_id_data->hw2_video_level;
8354  }
8355  if (total_modules >= 4)
8356  {
8357  p_cap->xcoder_devices[3].hw_id = p_id_data->hw3_id;
8361  p_cap->xcoder_devices[3].codec_format = p_id_data->hw3_codec_format;
8362  p_cap->xcoder_devices[3].codec_type = p_id_data->hw3_codec_type;
8367  p_cap->xcoder_devices[3].video_profile = p_id_data->hw3_video_profile;
8368  p_cap->xcoder_devices[3].video_level = p_id_data->hw3_video_level;
8369  }
8370 
8371 CAP_POPULATED:
8372 
8373  for (i = 0; i < NI_MAX_DEVICES_PER_HW_INSTANCE; i++)
8374  {
8375  ni_log(NI_LOG_DEBUG, "HW%d hw_id: %d\n", i, p_cap->xcoder_devices[i].hw_id);
8376  ni_log(NI_LOG_DEBUG, "HW%d max_number_of_contexts: %d\n", i,
8378  ni_log(NI_LOG_DEBUG, "HW%d max_4k_fps: %d\n", i,
8379  p_cap->xcoder_devices[i].max_4k_fps);
8380  ni_log(NI_LOG_DEBUG, "HW%d codec_format: %d\n", i,
8381  p_cap->xcoder_devices[i].codec_format);
8382  ni_log(NI_LOG_DEBUG, "HW%d codec_type: %d\n", i,
8383  p_cap->xcoder_devices[i].codec_type);
8384  ni_log(NI_LOG_DEBUG, "HW%d max_video_width: %d\n", i,
8385  p_cap->xcoder_devices[i].max_video_width);
8386  ni_log(NI_LOG_DEBUG, "HW%d max_video_height: %d\n", i,
8387  p_cap->xcoder_devices[i].max_video_height);
8388  ni_log(NI_LOG_DEBUG, "HW%d min_video_width: %d\n", i,
8389  p_cap->xcoder_devices[i].min_video_width);
8390  ni_log(NI_LOG_DEBUG, "HW%d min_video_height: %d\n", i,
8391  p_cap->xcoder_devices[i].min_video_height);
8392  ni_log(NI_LOG_DEBUG, "HW%d video_profile: %d\n", i,
8393  p_cap->xcoder_devices[i].video_profile);
8394  ni_log(NI_LOG_DEBUG, "HW%d video_level: %d\n", i,
8395  p_cap->xcoder_devices[i].video_level);
8396  }
8397 
8398  memset(p_cap->fw_branch_name, 0, sizeof(p_cap->fw_branch_name));
8399  memcpy(p_cap->fw_branch_name, p_id_data->fw_branch_name,
8400  sizeof(p_cap->fw_branch_name) - 1);
8401  ni_log(NI_LOG_DEBUG, "F/W branch name: %s\n", p_cap->fw_branch_name);
8402  memset(p_cap->fw_commit_time, 0, sizeof(p_cap->fw_commit_time));
8403  memcpy(p_cap->fw_commit_time, p_id_data->fw_commit_time,
8404  sizeof(p_cap->fw_commit_time) - 1);
8405  ni_log(NI_LOG_DEBUG, "F/W commit time: %s\n", p_cap->fw_commit_time);
8406  memset(p_cap->fw_commit_hash, 0, sizeof(p_cap->fw_commit_hash));
8407  memcpy(p_cap->fw_commit_hash, p_id_data->fw_commit_hash,
8408  sizeof(p_cap->fw_commit_hash) - 1);
8409  ni_log(NI_LOG_DEBUG, "F/W commit hash: %s\n", p_cap->fw_commit_hash);
8410  memset(p_cap->fw_build_time, 0, sizeof(p_cap->fw_build_time));
8411  memcpy(p_cap->fw_build_time, p_id_data->fw_build_time,
8412  sizeof(p_cap->fw_build_time) - 1);
8413  ni_log(NI_LOG_DEBUG, "F/W build time: %s\n", p_cap->fw_build_time);
8414  memset(p_cap->fw_build_id, 0, sizeof(p_cap->fw_build_id));
8415  memcpy(p_cap->fw_build_id, p_id_data->fw_build_id,
8416  sizeof(p_cap->fw_build_id) - 1);
8417  ni_log(NI_LOG_DEBUG, "F/W build id: %s\n", p_cap->fw_build_id);
8418 
8419 END:
8420  return;
8421 }
8422 
8423 static uint32_t presetGopSize[] = {
8424  1,
8425  1,
8426  1,
8427  1,
8428  2,
8429  4,
8430  4,
8431  4,
8432  8 };
8433 
8434 /*!******************************************************************************
8435  * \brief insert the 32 bits of integer value at bit position pos
8436  *
8437  * \param int pos, int value
8438  *
8439  * \return void
8440  ******************************************************************************/
8441 void ni_fix_VUI(uint8_t *vui, int pos, int value)
8442 {
8443  int pos_byte = (pos/8);
8444  int pos_in_byte = pos%8;
8445  int remaining_bytes_in_current_byte = 8 - pos_in_byte;
8446 
8447  if (pos_in_byte == 0) // at beginning of the byte
8448  {
8449  vui[pos_byte] = (uint8_t)(value >> 24);
8450  vui[pos_byte+1] = (uint8_t)(value >> 16);
8451  vui[pos_byte+2] = (uint8_t)(value >> 8);
8452  vui[pos_byte+3] = (uint8_t)(value);
8453  }
8454  else
8455  {
8456  vui[pos_byte] = vui[pos_byte] + (uint8_t)(value >> (32-remaining_bytes_in_current_byte));
8457  vui[pos_byte+1] = (uint8_t)(value >> (32-remaining_bytes_in_current_byte-8));
8458  vui[pos_byte+2] = (uint8_t)(value >> (32-remaining_bytes_in_current_byte-16));
8459  vui[pos_byte+3] = (uint8_t)(value >> (32-remaining_bytes_in_current_byte-24));
8460  vui[pos_byte+4] = vui[pos_byte+4] + ((uint8_t)(value << remaining_bytes_in_current_byte));
8461  }
8462 
8463 }
8464 
8465 /*!******************************************************************************
8466 * \brief Setup all xcoder configurations with custom parameters (Rev. B)
8467 *
8468 * \param
8469 *
8470 * \return
8471 ******************************************************************************/
8473  ni_decoder_config_t *p_cfg,
8474  ni_xcoder_params_t *p_src,
8475  uint32_t max_pkt_size)
8476 {
8477  int i,j;
8478  ni_decoder_input_params_t* p_dec = NULL;
8479  if ((!p_ctx) || (!p_cfg) || (!p_src))
8480  {
8481  ni_log(NI_LOG_ERROR, "ERROR: %s() Null pointer parameters passed\n",
8482  __func__);
8483  return;
8484  }
8485  p_dec = &p_src->dec_input_params;
8486  int w = p_src->source_width;
8487  int h = p_src->source_height;
8488  bool shift_params = false;
8489 
8490  p_cfg->ui8HWFrame = p_dec->hwframes;
8491  p_cfg->ui8MCMode = p_dec->mcmode;
8494  p_cfg->ui8DisablePictureReordering = p_dec->decoder_low_delay > 0;
8495  p_ctx->force_low_delay = p_dec->force_low_delay;
8496  p_cfg->ui8EnablelowDelayCheck = p_dec->enable_low_delay_check == 1;
8497  p_cfg->ui32SourceWidth = w;
8498  p_cfg->ui32SourceHeight = h;
8499 
8500  if (max_pkt_size)
8501  {
8502  p_cfg->ui32MaxPktSize = max_pkt_size;
8503  } else {
8504  // p_cfg->ui32MaxPktSize = width x height x 3/2 x min compression ratio(QP=0);
8505  // set min compression ratio = 1/2, so MaxPktSize = w * h * 3/4
8506  p_cfg->ui32MaxPktSize = w * h * 3 / 4;
8507  }
8508  // packet buffer aligned to NI_MAX_PACKET_SZ(128k)
8509  p_cfg->ui32MaxPktSize =
8510  (((p_cfg->ui32MaxPktSize) / NI_MAX_PACKET_SZ) + 1) * NI_MAX_PACKET_SZ;
8511 
8512  p_cfg->fps_number =
8513  ((ni_xcoder_params_t *)p_ctx->p_session_config)->fps_number;
8514  p_cfg->fps_denominator =
8515  ((ni_xcoder_params_t *)p_ctx->p_session_config)->fps_denominator;
8516  ni_log2(p_ctx, NI_LOG_INFO, "%s height %d width %d fps_number %d fps_denominator %d\n",
8517  __func__, h, w, p_cfg->fps_number, p_cfg->fps_denominator);
8518 
8519  p_cfg->asOutputConfig[0].ui8Enabled = 1; // always enabled
8520  p_cfg->asOutputConfig[1].ui8Enabled = p_dec->enable_out1;
8521  p_cfg->asOutputConfig[2].ui8Enabled = p_dec->enable_out2;
8522  if (p_cfg->asOutputConfig[2].ui8Enabled && p_cfg->asOutputConfig[1].ui8Enabled == 0)
8523  {
8524  p_cfg->asOutputConfig[1].ui8Enabled = 1;
8525  p_cfg->asOutputConfig[2].ui8Enabled = 0;
8526  shift_params = true;
8527  ni_log2(p_ctx, NI_LOG_DEBUG, "Output 2 used before output 1, Shifting output2 settings to output1 and disabling output 2\n");
8528  }
8529 
8530  for (i = 0; i < NI_MAX_NUM_OF_DECODER_OUTPUTS; i++)
8531  {
8532  if (!shift_params || i == 0)
8533  {
8534  j = i;
8535  }
8536  else
8537  {
8538  j = (i == 1) ? 2 : 1; //swap settings
8539  }
8540  p_cfg->asOutputConfig[i].ui8Force8Bit = p_dec->force_8_bit[j];
8541  p_cfg->asOutputConfig[i].ui8SemiPlanarEnabled = p_dec->semi_planar[j];
8542  p_cfg->asOutputConfig[i].ui8CropMode = p_dec->crop_mode[j];
8544  (uint16_t)((p_dec->crop_whxy[j][0]) & 0xFFFE);
8546  (uint16_t)((p_dec->crop_whxy[j][1]) & 0xFFFE);
8548  (uint16_t)((p_dec->crop_whxy[j][2]) & 0xFFFE);
8550  (uint16_t)((p_dec->crop_whxy[j][3]) & 0xFFFE);
8551 
8552  //Offset resized if out of bounds
8554  {
8556  }
8558  {
8560  }
8561 
8562  if (p_dec->enable_ppu_scale_adapt)
8563  {
8565  }
8566  else if (p_dec->scale_long_short_edge[j] == 1)
8567  {
8568  p_cfg->asOutputConfig[i].ui8EnablePpuScaleAdapt = 3;
8569  }
8570  else if (p_dec->scale_long_short_edge[j] == 2)
8571  {
8572  p_cfg->asOutputConfig[i].ui8EnablePpuScaleAdapt = 4;
8573  }
8574  else
8575  {
8576  p_cfg->asOutputConfig[i].ui8EnablePpuScaleAdapt = 0;
8577  }
8579 
8580  if (p_dec->scale_round[j] == -1)
8581  {
8582  if (p_cfg->asOutputConfig[i].ui8EnablePpuScaleAdapt == 1 ||
8583  p_cfg->asOutputConfig[i].ui8EnablePpuScaleAdapt == 2)
8584  {
8585  p_cfg->asOutputConfig[i].ui8ScaleResCeil = p_dec->scale_resolution_ceil[j] - 1;
8586  }
8587  else
8588  {
8590  }
8591  }
8592  else if (p_dec->scale_round[j] == 0)
8593  {
8595  }
8596  else
8597  {
8598  p_cfg->asOutputConfig[i].ui8ScaleResCeil = p_dec->scale_resolution_ceil[j] - 1;
8599  }
8600 
8602  (uint16_t)((p_dec->scale_wh[j][0]+1) & 0xFFFE);
8604  (uint16_t)((p_dec->scale_wh[j][1]+1) & 0xFFFE);
8605 
8608  {
8609  p_cfg->asOutputConfig[i].ui8ScaleEnabled = 1;
8610  }
8611  else
8612  {
8613  p_cfg->asOutputConfig[i].ui8ScaleEnabled = 0;
8614  }
8615  }
8616 
8618  if (p_cfg->ui8MaxExtraHwFrameCnt != 255 &&
8619  ni_cmp_fw_api_ver((char *)&p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6rB") < 0)
8620  {
8621  ni_log2(p_ctx, NI_LOG_INFO, "Warning %s(): maxExtraHwFrameCnt is not support for FW < 6rB\n", __func__);
8622  }
8623  p_cfg->ui8EcPolicy = p_dec->ec_policy;
8624  p_cfg->ui8EnableAdvancedEc = p_dec->enable_advanced_ec;
8626  if (p_cfg->ui8EnableAdvancedEc == 2 &&
8627  ni_cmp_fw_api_ver((char *)&p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6rO") < 0)
8628  {
8629  ni_log2(p_ctx, NI_LOG_INFO, "Warning %s(): (enableAdvancedEc == 2) is not support for FW < 6rO\n", __func__);
8630  p_cfg->ui8EnableAdvancedEc = 1;
8631  ni_log2(p_ctx, NI_LOG_INFO, "Warning %s(): reset enableAdvancedEc to %d\n", __func__, p_cfg->ui8EnableAdvancedEc);
8632  }
8633  if (p_cfg->ui8EcPolicy == NI_EC_POLICY_LIMITED_ERROR &&
8634  ni_cmp_fw_api_ver((char *)&p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6ri") < 0)
8635  {
8636  ni_log2(p_ctx, NI_LOG_INFO, "Warning %s(): (EcPolicy == limited_error) not supported for FW < 6ri\n", __func__);
8638  ni_log2(p_ctx, NI_LOG_INFO, "Warning %s(): reset EcPolicy to %d\n", __func__, p_cfg->ui8EcPolicy);
8639  }
8641  ni_cmp_fw_api_ver((char *)&p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6ri") < 0)
8642  {
8643  ni_log2(p_ctx, NI_LOG_INFO, "Warning %s(): setting ecErrThreshold not supported for FW < 6ri\n", __func__);
8645  ni_log2(p_ctx, NI_LOG_INFO, "Warning %s(): reset ecErrThreshold to %d\n", __func__, NI_EC_ERR_THRESHOLD_DEFAULT);
8646  }
8648  ni_cmp_fw_api_ver((char *)&p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6s1") < 0)
8649  {
8650  ni_log2(p_ctx, NI_LOG_INFO, "Warning %s(): (EcPolicy == best_effort_out_dc) not supported for FW < 6s1\n", __func__);
8652  ni_log2(p_ctx, NI_LOG_INFO, "Warning %s(): reset EcPolicy to %d\n", __func__, p_cfg->ui8EcPolicy);
8653  }
8655  p_cfg->ui8SurviveStreamErr = p_dec->survive_stream_err;
8656  if (p_cfg->ui8SurviveStreamErr != 0 &&
8657  ni_cmp_fw_api_ver((char *)&p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6rl") < 0) {
8658  ni_log2(p_ctx, NI_LOG_INFO, "Warning %s(): surviveStreamErr is not supported for FW < 6rl\n", __func__);
8659  }
8660  if (p_dec->reduce_dpb_delay)
8661  {
8662  if (p_ctx->codec_format == NI_CODEC_FORMAT_H264 &&
8663  ni_cmp_fw_api_ver((char *)&p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6rs") >= 0)
8664  p_cfg->ui8ReduceDpbDelay = p_dec->reduce_dpb_delay;
8665  else
8666  {
8667  ni_log2(p_ctx, NI_LOG_INFO, "Warning %s():Not support to reduce dpb delay and reset.\n", __func__);
8668  p_dec->reduce_dpb_delay = 0;
8669  }
8670  }
8671 
8672  //print it all out
8673  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8HWFrame = %d\n", p_cfg->ui8HWFrame);
8674  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8MCMode = %d\n", p_cfg->ui8MCMode);
8675  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8UduSeiEnabled = %d\n", p_cfg->ui8UduSeiEnabled);
8676  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16MaxSeiDataSize = %d\n", p_cfg->ui16MaxSeiDataSize);
8677  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8DisablePictureReordering = %d\n", p_cfg->ui8DisablePictureReordering);
8678  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8Enabled0 = %d\n", p_cfg->asOutputConfig[0].ui8Enabled);
8679  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8Enabled1 = %d\n", p_cfg->asOutputConfig[1].ui8Enabled);
8680  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8Enabled2 = %d\n", p_cfg->asOutputConfig[2].ui8Enabled);
8681  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32MaxPktSize = %u\n", p_cfg->ui32MaxPktSize);
8682  for (i = 0; i < NI_MAX_NUM_OF_DECODER_OUTPUTS; i++)
8683  {
8684  ni_log2(p_ctx, NI_LOG_DEBUG, "[%d] ui8Force8Bit %d\n", i, p_cfg->asOutputConfig[i].ui8Force8Bit);
8685  ni_log2(p_ctx, NI_LOG_DEBUG, "[%d] ui8SemiPlanarEnabled %d\n", i, p_cfg->asOutputConfig[i].ui8SemiPlanarEnabled);
8686  ni_log2(p_ctx, NI_LOG_DEBUG, "[%d] ui8CropMode %d\n", i, p_cfg->asOutputConfig[i].ui8CropMode);
8687  ni_log2(p_ctx, NI_LOG_DEBUG, "[%d] sCroppingRectable.ui16XYWH %d,%d - %d x %d\n", i,
8692  ni_log2(p_ctx, NI_LOG_DEBUG, "[%d] sOutputPictureSize.ui16Width x height %d x %d\n", i,
8695  ni_log2(p_ctx, NI_LOG_DEBUG, "[%d] ui8ScaleEnabled %d\n", i, p_cfg->asOutputConfig[i].ui8ScaleEnabled);
8696  ni_log2(p_ctx, NI_LOG_DEBUG, "[%d] ui8EnablePpuScaleAdapt %u\n", i, p_cfg->asOutputConfig[i].ui8EnablePpuScaleAdapt);
8697  ni_log2(p_ctx, NI_LOG_DEBUG, "[%d] ui8EnablePpuScaleLimit %u\n", i, p_cfg->asOutputConfig[i].ui8EnablePpuScaleLimit);
8698  ni_log2(p_ctx, NI_LOG_DEBUG, "[%d] ui8ScaleResCeil %u\n", i, p_cfg->asOutputConfig[i].ui8ScaleResCeil);
8699  }
8700  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8MaxExtraHwFrameCnt %u\n", p_cfg->ui8MaxExtraHwFrameCnt);
8701  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8EcPolicy = %u\n", p_cfg->ui8EcPolicy);
8702  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8EnableAdvancedEc = %u\n", p_cfg->ui8EnableAdvancedEc);
8703  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8EnablelowDelayCheck = %u\n", p_cfg->ui8EnablelowDelayCheck);
8704  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8DisableAdaptiveBuffers = %u\n", p_cfg->ui8DisableAdaptiveBuffers);
8705  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32ErrRatioThreshold = %u\n", p_cfg->ui32ErrRatioThreshold);
8706  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8ReduceDpbDelay = %u\n", p_cfg->ui8ReduceDpbDelay);
8707 
8708 }
8709 
8710 
8711 /*!******************************************************************************
8712  * \brief Setup all xcoder configurations with custom parameters (Rev. B)
8713  *
8714  * \param
8715  *
8716  * \return
8717  ******************************************************************************/
8719  ni_encoder_config_t *p_cfg,
8720  ni_xcoder_params_t *p_src)
8721 {
8722  ni_t408_config_t* p_t408 = NULL;
8723  ni_encoder_cfg_params_t *p_enc = NULL;
8724  int i = 0;
8725 
8726  if ((!p_ctx) || (!p_cfg) || (!p_src))
8727  {
8728  ni_log(NI_LOG_ERROR, "ERROR: %s() Null pointer parameters passed\n",
8729  __func__);
8730  return;
8731  }
8732  p_t408 = &(p_cfg->niParamT408);
8733  p_enc = &p_src->cfg_enc_params;
8734 
8735  ni_set_default_template(p_ctx, p_cfg);
8736 
8737  p_cfg->i32picWidth = p_src->source_width;
8738  p_cfg->i32picHeight = p_src->source_height;
8739  p_t408->tier = p_enc->high_tier;
8741  p_t408->cu_size_mode = p_enc->cu_size_mode;
8742  p_t408->max_num_merge = p_enc->max_num_merge;
8743  p_cfg->ui8AiEnhanceMode = p_src->enable_ai_enhance;
8744  p_cfg->ui8enable2PassGopPatern = p_src->enable2PassGop;
8745  // enhance_level is in range [1,3] set level when paramters is valid, otherwise make it to 1.
8746  if(p_cfg->ui8AiEnhanceMode && p_src->ai_enhance_level > 0 && p_src->ai_enhance_level < 4){
8747  p_cfg->ui8AiEnhanceLevel = p_src->ai_enhance_level;
8748  }else{
8749  p_cfg->ui8AiEnhanceLevel = 1;
8750  }
8752  p_cfg->i8skipFrameEnable = p_enc->skip_frame_enable;
8754  p_cfg->u8skipFrameInterval = p_enc->skip_frame_interval;
8755  p_cfg->ui16iFrameSizeRatio = p_enc->iframe_size_ratio;
8756  p_cfg->ui8EnableAcqLimit = p_enc->enable_acq_limit;
8759  if (p_enc->crfFloat == (float)-1.0 && p_enc->enable_smooth_crf == 1)
8760  {
8761  //smooth crf only support when setting float crf
8762  p_enc->enable_smooth_crf = 0;
8763  }
8764  p_cfg->ui8enableSmoothCrf = p_enc->enable_smooth_crf;
8766  p_cfg->ui8adaptiveLamdaMode = p_enc->adaptiveLamdaMode;
8767  p_cfg->ui8adaptiveCrfMode = p_enc->adaptiveCrfMode;
8769 
8770  // enable_dynamic_8x8_merge, enable_dynamic_16x16_merge, enable_dynamic_32x32_merge,
8771  // trans_rate, enable_hvs_qp_scale:
8772  // are not present in Rev B p_config
8773 
8774 #if 0
8775  if ((p_enc->rc.enable_rate_control == 0) &&
8776  ((p_enc->rc.enable_mb_level_rc == 1) || (p_enc->rc.enable_cu_level_rate_control == 1)))
8777  {
8779  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");
8780  }
8781 #endif
8782 
8783  ni_log2(p_ctx, NI_LOG_TRACE, "### %s: b preset_index %d bitrate %d rcEnable %d %d EnableRdoQuant %d lookAheadDepth %d gop_preset_index %d rdoLevel %d vbv_buffer_size %d crf %d %f\n", __func__,
8784  p_enc->preset_index, p_src->bitrate, p_cfg->ui8rcEnable, p_enc->rc.enable_rate_control, p_enc->EnableRdoQuant, p_enc->lookAheadDepth, p_enc->gop_preset_index, p_enc->rdoLevel,
8785  p_enc->rc.vbv_buffer_size, p_enc->crf, p_enc->crfFloat);
8786 
8787  if (!p_enc->preset_enabled && p_enc->preset_index != NI_PRESETS_NONE) {
8788  p_enc->preset_enabled = 1;
8789  p_enc->reset_dts_offset = 1;
8790  p_enc->rc.enable_rate_control = 1;
8791  p_src->bitrate = (p_src->bitrate != AV_CODEC_DEFAULT_BITRATE) ? p_src->bitrate : PRESET_DEFAULT_BITRATE;
8792  p_enc->EnableRdoQuant = (STD_AV1 == p_cfg->ui8bitstreamFormat) ? 0 : (p_enc->EnableRdoQuant != -1 ? p_enc->EnableRdoQuant : 1);
8793 
8794  // Disable CRF if enabled
8795  if (p_enc->crf != -1 || p_enc->crfFloat != -1.0f) {
8796  p_enc->crf = -1;
8797  p_enc->crfFloat = -1.0f;
8798  ni_log2(p_ctx, NI_LOG_INFO, "Warning %s(): CRF disabled due to preset enabled\n", __func__);
8799  }
8800 
8801  // Ensure preset_index is valid to avoid out-of-bounds access
8802  if (p_enc->preset_index >= 0 && p_enc->preset_index < sizeof(preset_configs) / sizeof(preset_configs[0])) {
8803  const PresetConfig *cfg = &preset_configs[p_enc->preset_index];
8804 
8805  p_enc->rdoLevel = cfg->rdoLevel;
8806  p_enc->lookAheadDepth = cfg->lookAheadDepth;
8808  p_enc->rc.vbv_buffer_size = (p_enc->rc.vbv_buffer_size != -1) ? p_enc->rc.vbv_buffer_size : cfg->vbvBufferSize;
8810 
8811  // Only set tolCtbRcInter/Intra for veryfast, faster, fast presets
8812  if (p_enc->preset_index <= NI_VQ_FAST) {
8813  p_enc->tolCtbRcInter = (p_enc->tolCtbRcInter != 0.1f) ? p_enc->tolCtbRcInter : cfg->tolCtbRcInter;
8814  p_enc->tolCtbRcIntra = (p_enc->tolCtbRcIntra != 0.1f) ? p_enc->tolCtbRcIntra : cfg->tolCtbRcIntra;
8815  }
8816  }
8817 
8818  // Adjust rdoLevel for AVC format
8819  if (STD_AVC == p_cfg->ui8bitstreamFormat) {
8820  p_enc->rdoLevel = 1;
8821  }
8822  }
8823 
8824  if (p_enc->EnableRdoQuant == -1)
8825  {
8826  p_enc->EnableRdoQuant = 0;
8827  }
8828 
8830  {
8832  }
8833 
8834  if(p_ctx->last_bitrate != 0)
8835  {
8836  // Slow sequence change happened. Retain the last bitrate.
8837  ni_log2(p_ctx, NI_LOG_DEBUG, "### %s: Slow sequence happened retain last_bitrate %d. assigned bitrate %d\n",
8838  __FUNCTION__, p_ctx->last_bitrate, p_src->bitrate);
8839  p_src->bitrate = p_ctx->last_bitrate;
8840  }
8841 
8842  if (p_src->bitrate != 0)
8843  {
8844  p_cfg->i32bitRate = p_src->bitrate;
8845  }
8846 
8847  p_cfg->ui8rcEnable = p_enc->rc.enable_rate_control;
8848 
8849  ni_log2(p_ctx, NI_LOG_TRACE, "### %s: a bitrate %d rcEnable %d %d EnableRdoQuant %d lookAheadDepth %d gop_preset_index %d rdoLevel %d vbv_buffer_size %d enable_cu_level_rate_control %d\n", __func__,
8850  p_src->bitrate, p_enc->EnableRdoQuant, p_cfg->ui8rcEnable, p_enc->rc.enable_rate_control, p_enc->lookAheadDepth, p_enc->gop_preset_index, p_enc->rdoLevel, p_enc->rc.vbv_buffer_size,
8852 
8853  // Update the bitrate to be used after Slow sequence change
8854  p_ctx->last_bitrate = p_cfg->i32bitRate;
8855 
8856  p_t408->gop_preset_index = p_enc->gop_preset_index;
8858  p_t408->enable_hvs_qp = p_enc->rc.enable_hvs_qp;
8859  p_t408->hvs_qp_scale = p_enc->rc.hvs_qp_scale;
8860  p_t408->minQpI = p_enc->rc.min_qp;
8861  p_t408->minQpP = p_enc->rc.min_qp;
8862  p_t408->minQpB = p_enc->rc.min_qp;
8863  p_t408->maxQpI = p_enc->rc.max_qp;
8864  p_t408->maxQpP = p_enc->rc.max_qp;
8865  p_t408->maxQpB = p_enc->rc.max_qp;
8866 
8867  p_t408->max_delta_qp = p_enc->rc.max_delta_qp;
8868  if (p_enc->rc.vbv_buffer_size != -1)
8869  {
8870  p_cfg->i32vbvBufferSize = p_enc->rc.vbv_buffer_size;
8871  }
8872  else
8873  {
8874  if (p_enc->rc.enable_rate_control)
8875  p_cfg->i32vbvBufferSize = 3000; // enable CBR (default vbv buffer size 3000) even if user does not set vbvBufferSize
8876  else
8877  p_cfg->i32vbvBufferSize = 0; // if user sets CRF but not vbvBufferSize, do not eanble capped CRF
8878  }
8879  p_cfg->ui32vbvMaxRate = p_enc->rc.vbv_max_rate;
8880  p_cfg->i8intraQpDelta = p_enc->rc.intra_qp_delta;
8881  p_cfg->ui8fillerEnable = p_enc->rc.enable_filler;
8882  p_cfg->ui8picSkipEnable = p_enc->rc.enable_pic_skip;
8883  p_cfg->ui16maxFrameSize = p_enc->maxFrameSize / 2000;
8884  p_t408->intra_period = p_enc->intra_period;
8885  p_t408->roiEnable = p_enc->roi_enable;
8886  p_t408->useLongTerm = p_enc->long_term_ref_enable;
8887  if (QUADRA)
8888  {
8890  p_cfg->ui8setLongTermCount = p_enc->long_term_ref_count;
8891  }
8892  p_t408->conf_win_top = p_enc->conf_win_top;
8893  p_t408->conf_win_bottom = p_enc->conf_win_bottom;
8894  p_t408->conf_win_left = p_enc->conf_win_left;
8895  p_t408->conf_win_right = p_enc->conf_win_right;
8896  p_t408->avcIdrPeriod = p_enc->intra_period;
8897 
8898  if (QUADRA)
8899  {
8900  if(p_ctx->last_framerate.framerate_num != 0)
8901  {
8902  // Slow sequence change happened. Retain the last framerate.
8903  ni_log2(p_ctx, NI_LOG_DEBUG, "### %s: Slow sequence happened retain last_framerate num %d den %d. assigned num %d den %d\n",
8904  __FUNCTION__, p_ctx->last_framerate.framerate_num, p_ctx->last_framerate.framerate_denom,
8905  p_cfg->i32frameRateInfo, p_cfg->i32frameRateDenominator);
8906  p_src->fps_number = p_ctx->last_framerate.framerate_num;
8908 
8909  if (!p_src->enable_vfr) {
8910  p_enc->frame_rate = (int)(p_src->fps_number / p_src->fps_denominator);
8911  }
8912  }
8913 
8914  if (p_cfg->i32frameRateInfo != p_enc->frame_rate)
8915  {
8916  if (p_src->fps_denominator != 0 &&
8917  (p_src->fps_number % p_src->fps_denominator) != 0)
8918  {
8919  // Fractional FPS: use original fps_number/fps_denominator
8920  uint32_t numUnitsInTick = 1000;
8921  p_cfg->i32frameRateDenominator = numUnitsInTick + 1;
8922  p_cfg->i32frameRateInfo = (p_src->fps_number * numUnitsInTick) / p_src->fps_denominator;
8923  }
8924  else
8925  {
8926  // Integer FPS: use the frame_rate directly
8927  p_cfg->i32frameRateInfo = p_enc->frame_rate;
8928  p_cfg->i32frameRateDenominator = 1;
8929  }
8930  }
8931 
8932  // Update the framerate to be used after Slow sequence change
8935  }
8936  else
8937  {
8938  if (p_cfg->i32frameRateInfo != p_enc->frame_rate)
8939  {
8940  p_cfg->i32frameRateInfo = p_enc->frame_rate;
8941  p_t408->numUnitsInTick = 1000;
8942  if (p_src->fps_denominator != 0 &&
8943  (p_src->fps_number % p_src->fps_denominator) != 0)
8944  {
8945  p_t408->numUnitsInTick += 1;
8946  p_cfg->i32frameRateInfo += 1;
8947  }
8948  p_t408->timeScale = p_cfg->i32frameRateInfo * 1000;
8949  if (NI_CODEC_FORMAT_H264 == p_ctx->codec_format)
8950  {
8951  p_t408->timeScale *= 2;
8952  }
8953  }
8954  }
8955 
8956  p_t408->intra_qp = p_enc->rc.intra_qp;
8957 
8958  // "repeatHeaders" value 1 (all I frames) maps to forcedHeaderEnable
8959  // value 2; all other values are ignored
8960  if (p_t408->forcedHeaderEnable != p_enc->forced_header_enable &&
8962  {
8963  p_t408->forcedHeaderEnable = 2;
8964  p_cfg->ui8repeatHeaders = p_enc->forced_header_enable;
8965  }
8966 
8968 
8969  if (STD_AVC == p_cfg->ui8bitstreamFormat)
8970  {
8971  switch (p_t408->decoding_refresh_type)
8972  {
8973  case 0: // Non-IRAP I-p_frame
8974  {
8975  // intra_period set to user-configured (above), avcIdrPeriod set to 0
8976  p_t408->avcIdrPeriod = 0;
8977  break;
8978  }
8979  case 1: // CRA
8980  case 2: // IDR
8981  {
8982  // intra_period set to 0, avcIdrPeriod set to user-configured (above)
8983  p_t408->intra_period = 0;
8984  break;
8985  }
8986  default:
8987  {
8988  ni_log(
8989  NI_LOG_ERROR,
8990  "ERROR: %s() unknown value for p_t408->decoding_refresh_type: %d\n",
8991  __func__, p_t408->decoding_refresh_type);
8992  break;
8993  }
8994  }
8995  } else if (STD_HEVC == p_cfg->ui8bitstreamFormat ||
8996  STD_AV1 == p_cfg->ui8bitstreamFormat)
8997  {
8998  p_t408->avcIdrPeriod = 0;
8999  }
9000 
9001  // Rev. B: H.264 only parameters.
9002  p_t408->enable_transform_8x8 = p_enc->enable_transform_8x8;
9003  p_t408->entropy_coding_mode = p_enc->entropy_coding_mode;
9004 
9005  // Rev. B: shared between HEVC and H.264
9006  p_t408->slice_mode = p_enc->slice_mode;
9007  p_t408->slice_arg = p_enc->slice_arg;
9008  if (p_t408->intra_mb_refresh_mode != p_enc->intra_mb_refresh_mode)
9009  {
9011  if (1 != p_t408->intra_mb_refresh_mode)
9012  {
9013  p_t408->intra_mb_refresh_mode = 1;
9014  ni_log2(p_ctx, NI_LOG_DEBUG, "force intraRefreshMode to 1 because quadra only supports intra refresh by rows\n");
9015  }
9016  }
9017 
9018  if (p_t408->intra_mb_refresh_arg != p_enc->intra_mb_refresh_arg)
9019  {
9020  p_t408->intra_mb_refresh_arg = p_enc->intra_mb_refresh_arg;
9021  if (1 == p_t408->intra_mb_refresh_mode)
9022  {
9023  int mbHeight = (p_cfg->ui8bitstreamFormat == STD_AVC) ? 16 : 64;
9024  int mbRows = (p_cfg->i32picHeight + mbHeight - 1) / mbHeight;
9025  p_cfg->ui16gdrDuration = (mbRows + p_t408->intra_mb_refresh_arg - 1) / p_t408->intra_mb_refresh_arg;
9026  }
9027  }
9028 
9029  p_cfg->ui8intraResetRefresh = p_enc->intra_reset_refresh;
9030 
9031  // Rev. B: could be shared for HEVC and H.264
9032  p_t408->enable_mb_level_rc = p_enc->rc.enable_mb_level_rc;
9033 
9034  // profile setting: if user specified profile
9035  if (0 != p_enc->profile)
9036  {
9037  p_t408->profile = p_enc->profile;
9038  }
9039 
9040  p_t408->level = p_enc->level_idc;
9041 
9042  // main, extended or baseline profile of 8 bit (if input is 10 bit, Quadra auto converts to 8 bit) H.264 requires the following:
9043  // main: profile = 2 transform8x8Enable = 0
9044  // extended: profile = 3 entropyCodingMode = 0, transform8x8Enable = 0
9045  // baseline: profile = 1 entropyCodingMode = 0, transform8x8Enable = 0 and
9046  // gop with no B frames (gopPresetIdx=1, 2, 6, or 0
9047  // (custom with no B frames)
9048  if (STD_AVC == p_cfg->ui8bitstreamFormat)
9049  {
9050  if (2 == p_t408->profile)
9051  {
9052  p_t408->enable_transform_8x8 = 0;
9053  ni_log2(p_ctx, NI_LOG_DEBUG, "enable_transform_8x8 set to 0 for profile 2 (main)\n");
9054  }
9055  else if (3 == p_t408->profile || 1 == p_t408->profile)
9056  {
9057  p_t408->entropy_coding_mode = p_t408->enable_transform_8x8 = 0;
9058  ni_log2(p_ctx, NI_LOG_DEBUG, "entropy_coding_mode and enable_transform_8x8 set to 0 "
9059  "for profile 3 (extended) or 1 (baseline)\n");
9060  }
9061  }
9062 
9063  if (QUADRA)
9064  {
9065  p_enc->profile = p_t408->profile; // record profile for Bitstream Features reporting
9066  if (0 == p_t408->entropy_coding_mode && 1 == p_enc->EnableRdoQuant)
9067  {
9068  ni_log2(p_ctx, NI_LOG_DEBUG, "RDOQ does not support entropy_coding_mode 0 (CAVLC) "
9069  "force EnableRdoQuant 0 to accommodate HW limiation\n");
9070  p_enc->EnableRdoQuant = 0;
9071  }
9072  }
9073 
9074 #ifndef QUADRA
9075  if (!QUADRA)
9076  {
9077  if (GOP_PRESET_IDX_CUSTOM == p_t408->gop_preset_index)
9078  {
9080  for (i = 0; i < p_t408->custom_gop_params.custom_gop_size; i++)
9081  {
9084  p_t408->custom_gop_params.pic_param[i].pic_qp = p_enc->custom_gop_params.pic_param[i].pic_qp + p_t408->intra_qp;
9085  p_t408->custom_gop_params.pic_param[i].num_ref_pic_L0 = p_enc->custom_gop_params.pic_param[i].num_ref_pic_L0;
9086  p_t408->custom_gop_params.pic_param[i].ref_poc_L0 = p_enc->custom_gop_params.pic_param[i].ref_poc_L0;
9087  p_t408->custom_gop_params.pic_param[i].ref_poc_L1 = p_enc->custom_gop_params.pic_param[i].ref_poc_L1;
9089  }
9090  }
9091  }
9092  else // QUADRA
9093 #endif
9094  {
9095  if (p_enc->custom_gop_params.custom_gop_size &&
9098  {
9101  for (i = 0; i < p_t408->custom_gop_params.custom_gop_size; i++)
9102  {
9111  p_t408->custom_gop_params.pic_param[i].pic_type =
9115  for (int j = 0;
9116  j < p_enc->custom_gop_params.pic_param[i].num_ref_pics; j++)
9117  {
9118  p_t408->custom_gop_params.pic_param[i].rps[j].ref_pic =
9119  p_enc->custom_gop_params.pic_param[i].rps[j].ref_pic;
9122  }
9123  }
9124  }
9125  }
9126 
9127  p_ctx->key_frame_type = p_t408->decoding_refresh_type; //Store to use when force key p_frame
9128 
9129  // forceFrameType=1 requires intraPeriod=0 and avcIdrPeriod=0 and gopPresetIdx=8
9130  if (1 == p_src->force_frame_type)
9131  {
9132  p_t408->intra_period = 0;
9133  p_t408->avcIdrPeriod = 0;
9134  p_t408->gop_preset_index = 8;
9135  p_ctx->force_frame_type = 1;
9136  }
9137 
9138  p_cfg->hdrEnableVUI = p_src->hdrEnableVUI;
9139 
9140  if (p_cfg->i32hwframes != p_src->hwframes)
9141  {
9142  if (p_src->hwframes && p_ctx->auto_dl_handle == 0)
9143  {
9144  p_cfg->i32hwframes = p_src->hwframes;
9145  }
9146  else
9147  {
9148  p_cfg->i32hwframes = 0;
9149  }
9150  }
9151  p_cfg->ui16rootBufId = p_src->rootBufId;
9152 
9153  //set VUI info deprecated
9154  //p_cfg->ui32VuiDataSizeBits = p_src->ui32VuiDataSizeBits;
9155  //p_cfg->ui32VuiDataSizeBytes = p_src->ui32VuiDataSizeBytes;
9156  //memcpy(p_cfg->ui8VuiRbsp, p_src->ui8VuiRbsp, NI_MAX_VUI_SIZE);
9157  //if ((p_src->pos_num_units_in_tick > p_src->ui32VuiDataSizeBits) || (p_src->pos_time_scale > p_src->ui32VuiDataSizeBits))
9158  //{
9159  // ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() VUI filling error\n", __func__);
9160  // return;
9161  //}
9162  //else
9163  //{
9164  // ni_fix_VUI(p_cfg->ui8VuiRbsp, p_src->pos_num_units_in_tick, p_t408->numUnitsInTick);
9165  // ni_fix_VUI(p_cfg->ui8VuiRbsp, p_src->pos_time_scale, p_t408->timeScale);
9166  //}
9167 
9168  if (p_src->enable_vfr)
9169  {
9170  p_cfg->ui8fixedframerate = 0;
9171  } else
9172  {
9173  p_cfg->ui8fixedframerate = 1;
9174  }
9175 
9176  //new QUADRA param
9177  if (p_enc->EnableAUD != 0)
9178  {
9179  p_cfg->ui8EnableAUD = p_enc->EnableAUD;
9180  }
9181  if (p_enc->lookAheadDepth != 0)
9182  {
9183  p_cfg->ui8LookAheadDepth = p_enc->lookAheadDepth;
9184  }
9185  if (p_enc->rdoLevel != 1)
9186  {
9187  p_cfg->ui8rdoLevel = p_enc->rdoLevel;
9188  }
9189  if (p_enc->crf != -1)
9190  {
9191  p_cfg->i8crf = p_enc->crf;
9192  }
9193  if (p_enc->HDR10MaxLight != 0)
9194  {
9195  p_cfg->ui16HDR10MaxLight = p_enc->HDR10MaxLight;
9196  }
9197  if (p_enc->HDR10AveLight != 0)
9198  {
9199  p_cfg->ui16HDR10AveLight = p_enc->HDR10AveLight;
9200  }
9201  if (p_enc->HDR10CLLEnable != 0)
9202  {
9203  p_cfg->ui8HDR10CLLEnable = p_enc->HDR10CLLEnable;
9204  }
9205  if (p_enc->HDR10Enable != 0)
9206  {
9207  p_cfg->ui8hdr10_enable = p_enc->HDR10Enable;
9208  p_cfg->ui16hdr10_dx0 = p_enc->HDR10dx0;
9209  p_cfg->ui16hdr10_dy0 = p_enc->HDR10dy0;
9210  p_cfg->ui16hdr10_dx1 = p_enc->HDR10dx1;
9211  p_cfg->ui16hdr10_dy1 = p_enc->HDR10dy1;
9212  p_cfg->ui16hdr10_dx2 = p_enc->HDR10dx2;
9213  p_cfg->ui16hdr10_dy2 = p_enc->HDR10dy2;
9214  p_cfg->ui16hdr10_wx = p_enc->HDR10wx;
9215  p_cfg->ui16hdr10_wy = p_enc->HDR10wy;
9216  p_cfg->ui32hdr10_maxluma = p_enc->HDR10maxluma;
9217  p_cfg->ui32hdr10_minluma = p_enc->HDR10minluma;
9218  }
9219 
9220  if (p_enc->EnableRdoQuant != 0)
9221  {
9222  p_cfg->ui8EnableRdoQuant = p_enc->EnableRdoQuant;
9223  }
9224  if (p_enc->ctbRcMode != 0)
9225  {
9226  p_cfg->ui8ctbRcMode = p_enc->ctbRcMode;
9227  }
9228  if (p_enc->gopSize != 0)
9229  {
9230  p_cfg->ui8gopSize = p_enc->gopSize;
9231  }
9232  if (p_src->use_low_delay_poc_type != 0)
9233  {
9235  }
9236  if (p_enc->gopLowdelay != 0)
9237  {
9238  p_cfg->ui8gopLowdelay = p_enc->gopLowdelay;
9239  }
9240  if (p_enc->gdrDuration != 0)
9241  {
9242  p_cfg->ui16gdrDuration = p_enc->gdrDuration;
9243  }
9244  if (p_enc->colorDescPresent)
9245  {
9246  p_cfg->ui8colorDescPresent = 1;
9247  p_cfg->ui8colorPrimaries = p_enc->colorPrimaries;
9248  p_cfg->ui8colorTrc = p_enc->colorTrc;
9249  p_cfg->ui8colorSpace = p_enc->colorSpace;
9250  p_cfg->ui8videoFullRange = p_enc->videoFullRange;
9251  }
9252  if (p_enc->videoFullRange)
9253  {
9254  p_cfg->ui8videoFullRange = p_enc->videoFullRange;
9255  }
9256  if (p_enc->hrdEnable != 0)
9257  {
9258  p_cfg->ui8hrdEnable = p_enc->hrdEnable;
9259  }
9260 
9261  p_cfg->ui8planarFormat = p_src->cfg_enc_params.planar;
9262  p_cfg->ui16aspectRatioWidth = p_enc->aspectRatioWidth;
9263  p_cfg->ui16aspectRatioHeight = p_enc->aspectRatioHeight;
9264 
9265  if (p_enc->ltrRefInterval != 0)
9266  {
9267  p_cfg->ui32ltrRefInterval = p_enc->ltrRefInterval;
9268  p_cfg->i32ltrRefQpOffset = p_enc->ltrRefQpOffset;
9269  p_cfg->ui32ltrFirstGap = p_enc->ltrFirstGap;
9270  p_cfg->ui32ltrNextInterval = p_enc->ltrNextInterval;
9271  }
9272  if (p_enc->multicoreJointMode != 0)
9273  {
9274  p_cfg->ui8multicoreJointMode = p_enc->multicoreJointMode;
9275  }
9276  p_cfg->ui32QLevel = p_enc->qlevel;
9277 
9278  if (p_enc->chromaQpOffset != 0)
9279  {
9280  p_cfg->i8chromaQpOffset = p_enc->chromaQpOffset;
9281  }
9282 
9283  if (p_enc->tolCtbRcInter != (float)0.1)
9284  {
9285  p_cfg->i32tolCtbRcInter = (int32_t)(p_enc->tolCtbRcInter * 1000);
9286  }
9287 
9288  if (p_enc->tolCtbRcIntra != (float)0.1)
9289  {
9290  p_cfg->i32tolCtbRcIntra = (int32_t)(p_enc->tolCtbRcIntra * 1000);
9291  }
9292 
9293  if (p_enc->bitrateWindow != -255)
9294  {
9295  p_cfg->i16bitrateWindow = p_enc->bitrateWindow;
9296  }
9297 
9298  if (p_enc->inLoopDSRatio != 1)
9299  {
9300  p_cfg->ui8inLoopDSRatio = p_enc->inLoopDSRatio;
9301  }
9302 
9303  if (p_enc->blockRCSize != 0)
9304  {
9305  p_cfg->ui8blockRCSize = p_enc->blockRCSize;
9306  }
9307 
9308  if (p_enc->rcQpDeltaRange != 10)
9309  {
9310  p_cfg->ui8rcQpDeltaRange = p_enc->rcQpDeltaRange;
9311  }
9312 
9313  if (p_enc->ctbRowQpStep != 0)
9314  {
9315  p_cfg->i16ctbRowQpStep = p_enc->ctbRowQpStep;
9316  }
9317 
9318  if (p_enc->newRcEnable != -1)
9319  {
9320  p_cfg->ui8NewRCEnable = p_enc->newRcEnable;
9321  }
9322 
9323  // convert enable_mb_level_rc, enable_cu_level_rate_control, and enable_hvs_qp to ctbRcMode
9324  if (QUADRA)
9325  {
9326  // ctbRcMode has priority over enable_mb_level_rc, enable_cu_level_rate_control, and enable_hvs_qp
9327  if (!p_cfg->ui8ctbRcMode)
9328  {
9329  if (p_t408->enable_mb_level_rc || p_t408->enable_cu_level_rate_control)
9330  {
9331  if (p_t408->enable_hvs_qp)
9332  {
9333  p_cfg->ui8ctbRcMode = 3;
9334  // If hvsQP is enabled, disable strongIntraSmooth to apply one filtering at a time for better VQ.
9335  p_t408->strongIntraSmoothEnable = 0;
9337  "Turning off strongIntraSmoothing because hvsQPEnable=1 "
9338  "for better subjective VQ\n");
9339  } else
9340  {
9341  p_cfg->ui8ctbRcMode = 2;
9342  }
9343  }
9344  else if (p_t408->enable_hvs_qp)
9345  {
9346  p_cfg->ui8ctbRcMode = 1;
9347  // If hvsQP is enabled, disable strongIntraSmooth to apply one filtering at a time for better VQ.
9348  p_t408->strongIntraSmoothEnable = 0;
9350  "Turning off strongIntraSmoothing because hvsQPEnable=1 for "
9351  "better subjective VQ\n");
9352  }
9353  }
9354  }
9355 
9356  if (p_src->low_delay_mode != 0)
9357  {
9358  p_cfg->ui8LowDelay = !!(p_src->low_delay_mode);
9359  }
9360 
9361  if (p_enc->enable_ssim != 0)
9362  {
9363  p_cfg->ui8enableSSIM = p_enc->enable_ssim;
9364  }
9365 
9366  if (p_enc->avcc_hvcc != 0)
9367  {
9368  p_cfg->ui8avccHvcc = p_enc->avcc_hvcc;
9369  }
9370 
9371  if (p_enc->av1_error_resilient_mode != 0)
9372  {
9374  }
9375 
9376  if (p_enc->temporal_layers_enable != 0)
9377  {
9379  }
9380 
9381  if (p_enc->spatial_layers > 1)
9382  {
9383  p_cfg->ui8spatialLayersMinusOne = p_enc->spatial_layers - 1;
9384  if (ni_cmp_fw_api_ver((char *)&p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6rw") < 0) {
9385  ni_log2(p_ctx, NI_LOG_INFO, "Warning %s(): spatialLayers is not supported for FW < 6rw\n", __func__);
9386  }
9387  }
9388 
9389  if (p_enc->spatial_layers_ref_base_layer != 0)
9390  {
9392  if (ni_cmp_fw_api_ver((char *)&p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6s0") < 0) {
9393  ni_log2(p_ctx, NI_LOG_INFO, "Warning %s(): spatialLayers is not supported for FW < 6s0\n", __func__);
9394  }
9395  }
9396 
9397  if (p_ctx->pixel_format != NI_PIX_FMT_YUV420P)
9398  {
9399  p_cfg->ui8PixelFormat = p_ctx->pixel_format;
9400  }
9401 
9402  if (p_enc->get_psnr_mode != 3)
9403  {
9404  if (p_enc->get_psnr_mode == 2 && p_ctx->codec_format == NI_CODEC_FORMAT_H265)
9405  {
9406  // h.265 psnr_y is supported by HW for 8-bit & 10-bit, no need to reject setting or disable luma RFC
9409  {
9410  p_enc->get_psnr_mode = 3;
9411  ni_log(NI_LOG_INFO, "Warning h.265 psnr_y is only supported for YUV420P, YUV420P10LE, NV12, and P010LE\n");
9412  }
9413  }
9414  else if (p_enc->get_psnr_mode == 4)
9415  {
9416  p_cfg->ui8compressor = 0;
9418  {
9419  p_enc->get_psnr_mode = 3;
9420  p_cfg->ui8compressor = 3;
9421  ni_log(NI_LOG_INFO, "Warning reconstructed frames only supported for YUV420P, NV12\n");
9422  }
9423  if (p_enc->crop_width || p_enc->crop_height)
9424  {
9425  p_cfg->ui8compressor = 3;
9426  p_enc->get_psnr_mode = 3;
9427  ni_log(NI_LOG_INFO, "Warning reconstructed frames feature is not supported when cropWidth x cropHeight are set\n");
9428  }
9429  }
9430  else
9431  {
9432  p_cfg->ui8compressor = p_enc->get_psnr_mode;
9433  if (p_cfg->ui8PixelFormat != NI_PIX_FMT_YUV420P)
9434  {
9435  p_cfg->ui8compressor = 3;
9436  p_enc->get_psnr_mode = 3;
9437  ni_log(NI_LOG_INFO, "Warning get psnr feature only support YUV420P (except for h.265 psnr_y)\n");
9438  }
9439  if (p_enc->crop_width || p_enc->crop_height)
9440  {
9441  p_cfg->ui8compressor = 3;
9442  p_enc->get_psnr_mode = 3;
9443  ni_log(NI_LOG_INFO, "Warning get psnr feature is not supported when cropWidth x cropHeight are set\n");
9444  }
9445  }
9446  }
9447 
9448  if (p_src->zerocopy_mode == -1) // zero copy auto mode - disable zero copy for low resolution
9449  {
9450  bool is_rgba = (p_ctx->pixel_format == NI_PIX_FMT_RGBA ||
9451  p_ctx->pixel_format == NI_PIX_FMT_BGRA ||
9452  p_ctx->pixel_format == NI_PIX_FMT_ARGB ||
9453  p_ctx->pixel_format == NI_PIX_FMT_ABGR) ? true : false;
9454  if (is_rgba ||
9456  "6Q") >= 0) &&
9458  p_src->zerocopy_mode = 1;
9459  else
9460  p_src->zerocopy_mode = 0;
9461  }
9462 
9463  if (p_src->zerocopy_mode)
9464  {
9465  p_cfg->ui32lumaLinesize = p_src->luma_linesize;
9466  p_cfg->ui32chromaLinesize = p_src->chroma_linesize;
9467  }
9468  else
9469  {
9470  p_cfg->ui32lumaLinesize = p_src->luma_linesize = 0;
9471  p_cfg->ui32chromaLinesize = p_src->chroma_linesize = 0;
9472  }
9473  // for fast sequence change linesize check
9474  p_ctx->ori_luma_linesize = p_src->luma_linesize;
9475  p_ctx->ori_chroma_linesize = p_src->chroma_linesize;
9476 
9477  // calculate number for frames delay for minFramesDelay
9478  int lookAheadEnable = !!p_cfg->ui8LookAheadDepth;
9479 
9480  int gopSize = g_map_preset_to_gopsize[lookAheadEnable][p_t408->gop_preset_index + 1];
9481  int mulitcoreDelay = p_cfg->ui8multicoreJointMode ? 3 : 0;
9482 
9483  if (p_t408->gop_preset_index == 0) // Custom GOP
9484  gopSize = p_t408->custom_gop_params.custom_gop_size;
9485 
9486  if (lookAheadEnable)
9487  {
9488  int firstGopEnd = gopSize + 1 + mulitcoreDelay; // first I-frame gopSize is 1
9489  int lookaheadGopEnd = mulitcoreDelay ?
9490  p_cfg->ui8LookAheadDepth + mulitcoreDelay + (gopSize - ((p_cfg->ui8LookAheadDepth-1+mulitcoreDelay) % gopSize)) :
9491  p_cfg->ui8LookAheadDepth + (gopSize - ((p_cfg->ui8LookAheadDepth-1) % gopSize)); // lookAheadDepth-1 because lookahead queue includes first I-frame
9492  int initialDelayNum = (firstGopEnd > lookaheadGopEnd) ? firstGopEnd : lookaheadGopEnd;
9493  int maxDelayNum = p_cfg->ui8LookAheadDepth + 1 + gopSize / 2 + mulitcoreDelay;
9494  int maxLookaheadQueue = initialDelayNum + (gopSize - 1) + mulitcoreDelay; // assume worst case scenario - gop size changes from initial gop to gop size 1
9495 
9496  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
9497  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
9499  "6r2") >= 0)
9500  {
9501  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
9503  "6rX") >= 0)
9504  {
9505  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
9506  p_ctx->current_frame_delay = p_ctx->max_frame_delay;
9507  else
9508  p_ctx->current_frame_delay = p_ctx->initial_frame_delay;
9509  }
9510  else
9511  {
9512  p_ctx->current_frame_delay = p_ctx->max_frame_delay;
9513  }
9514  }
9515  else
9516  {
9517  p_ctx->last_gop_size = gopSize;
9518  p_ctx->current_frame_delay = p_ctx->max_frame_delay;
9519  }
9520  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: firstGopEnd %d lookaheadGopEnd %d initialDelayNum %d maxDelayNum %d maxLookaheadQueue %d\n",
9521  __FUNCTION__, firstGopEnd, lookaheadGopEnd, initialDelayNum, maxDelayNum, maxLookaheadQueue);
9522  }
9523  else
9524  {
9525  p_ctx->last_gop_size = gopSize;
9526  p_ctx->initial_frame_delay = p_ctx->max_frame_delay = p_ctx->current_frame_delay = gopSize + mulitcoreDelay;
9527  }
9528 
9529  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: preset %d lookAheadDepth %d gopSize %d mulitcoreDelay %d "
9530  "last_gop_size %d linitial_frame_delay %d current_frame_delay %d max_frame_delay %d\n",
9531  __FUNCTION__, p_t408->gop_preset_index, p_cfg->ui8LookAheadDepth, gopSize, mulitcoreDelay,
9532  p_ctx->last_gop_size, p_ctx->initial_frame_delay, p_ctx->current_frame_delay, p_ctx->max_frame_delay);
9533 
9534  if (p_enc->crop_width != 0 && p_enc->crop_height != 0)
9535  {
9536  p_cfg->ui32cropWidth = p_enc->crop_width;
9537  p_cfg->ui32cropHeight = p_enc->crop_height;
9538  p_cfg->ui32horOffset = p_enc->hor_offset;
9539  p_cfg->ui32verOffset = p_enc->ver_offset;
9540  }
9541 
9542  if (p_enc->crfMax != -1)
9543  {
9544  p_cfg->i8crfMax = (int8_t)(p_enc->crfMax) + 1; // for old libxcoder backward compatibility, use 1 to represent 0
9545  }
9546 
9547  if (p_enc->qcomp != (float)0.6)
9548  {
9549  p_cfg->i32qcomp = (int32_t)(p_enc->qcomp * 1000) + 1000; // for old libxcoder backward compatibility, use 1000 to represent 0
9550  }
9551 
9552  if (p_enc->noMbtree != 0)
9553  {
9554  p_cfg->ui8noMbtree = p_enc->noMbtree;
9555  }
9556 
9557  if (p_enc->noHWMultiPassSupport != 0)
9558  {
9560  }
9561 
9562  if (p_enc->cuTreeFactor != 5)
9563  {
9564  p_cfg->i8cuTreeFactor = p_enc->cuTreeFactor;
9565  }
9566 
9567  if (p_enc->ipRatio != (float)1.4)
9568  {
9569  p_cfg->i32ipRatio = (int32_t)(p_enc->ipRatio * 1000) + 1000; // for old libxcoder backward compatibility, use 1000 to represent 0
9570  }
9571 
9572  if (p_enc->pbRatio != (float)1.3)
9573  {
9574  p_cfg->i32pbRatio = (int32_t)(p_enc->pbRatio * 1000) + 1000; // for old libxcoder backward compatibility, use 1000 to represent 0
9575  }
9576 
9577  if (p_enc->cplxDecay != (float)0.5)
9578  {
9579  p_cfg->i32cplxDecay = (int32_t)(p_enc->cplxDecay * 1000) + 1000; // for old libxcoder backward compatibility, use 1000 to represent 0
9580  }
9581 
9582  if (p_enc->pps_init_qp != -1)
9583  {
9584  p_cfg->i8ppsInitQp = (int8_t)(p_enc->pps_init_qp) + 1; // for old libxcoder backward compatibility, use 1 to represent 0
9585  }
9586 
9587  if (p_enc->bitrateMode != -1)
9588  {
9589  p_cfg->ui8bitrateMode = p_enc->bitrateMode;
9590  }
9591 
9592  if (p_enc->pass1_qp != -1)
9593  {
9594  p_cfg->i8pass1Qp = (int8_t)(p_enc->pass1_qp) + 1; // for old libxcoder backward compatibility, use 1 to represent 0
9595  }
9596 
9597  if (p_enc->crfFloat != (float)-1.0)
9598  {
9599  // for old libxcoder backward compatibility
9600  p_cfg->i8crf = (int8_t)(p_enc->crfFloat);
9601  p_cfg->i8crfDecimal = (int8_t)((p_enc->crfFloat - (float)p_cfg->i8crf) * 100);
9602  }
9603 
9604  if (p_enc->hvsBaseMbComplexity != 15)
9605  {
9606  p_cfg->i8hvsBaseMbComplexity = (int8_t)p_enc->hvsBaseMbComplexity - 15; // for old libxcoder backward compatibility, use -15 to represent 0
9607  }
9608 
9609  if (p_enc->enableipRatio != 0 && p_cfg->i32vbvBufferSize != 0) //vbvBufferSize !=0 for CBR not for ABR
9610  {
9611  p_cfg->i8enableipRatio = p_enc->enableipRatio;
9612  }
9613 
9614  if (p_enc->crf_max_iframe_enable != 0)
9615  {
9617  }
9618 
9619  if (p_enc->vbv_min_rate != 0)
9620  {
9621  p_cfg->ui32vbvMinRate = p_enc->vbv_min_rate;
9622  }
9623 
9624  if (p_enc->disableBframeRdoq != 0)
9625  {
9626  p_cfg->ui8disableBframeRDOQ = p_enc->disableBframeRdoq;
9627  }
9628 
9629  if (p_enc->forceBframeQpfactor != (float)-1.0)
9630  {
9631  p_cfg->i32forceBframeQpFactor = (int32_t)(p_enc->forceBframeQpfactor * 1000) + 1000; // for old libxcoder backward compatibility, use 1000 to represent 0
9632  }
9633 
9634  if (p_enc->tune_bframe_visual != 0)
9635  {
9636  p_cfg->ui8tuneBframeVisual = p_enc->tune_bframe_visual;
9637  }
9638 
9641  }
9642 
9643  if (p_enc->motionConstrainedMode != 0)
9644  {
9646  }
9647 
9648  if(p_enc->encMallocStrategy != 0)
9649  {
9650  p_cfg->ui8mallocStrategy = p_enc->encMallocStrategy;
9651  }
9652 
9653  if (p_enc->enable_timecode != 0)
9654  {
9655  p_cfg->ui8enableTimecode = p_enc->enable_timecode;
9656  }
9657 
9658  if (p_enc->vbvBufferReencode != 0)
9659  {
9660  p_cfg->ui8vbvBufferReencode = p_enc->vbvBufferReencode;
9661  }
9662 
9663  if (p_enc->totalCuTreeDepth != 0)
9664  {
9665  p_cfg->ui8totalCuTreeDepth = p_enc->totalCuTreeDepth;
9666  }
9667 
9668  if (p_enc->adaptiveCuTree != 0)
9669  {
9670  p_cfg->ui8adaptiveCuTree = p_enc->adaptiveCuTree;
9671  }
9672 
9673  if (p_enc->preIntraHandling != 1)
9674  {
9675  p_cfg->ui8preIntraHandling = (uint8_t)p_enc->preIntraHandling - 1; // for old libxcoder backward compatibility, use -1 to represent 0
9676  }
9677 
9678  if (p_enc->baseLayerOnly != 0)
9679  {
9680  p_cfg->ui8baseLayerOnly = p_enc->baseLayerOnly;
9681  }
9682 
9683  if (p_enc->pastFrameMaxIntraRatio != 20)
9684  {
9685  p_cfg->ui8pastFrameMaxIntraRatio = (uint8_t)p_enc->pastFrameMaxIntraRatio - 20; // for old libxcoder backward compatibility, use -20 to represent 0
9686  }
9687 
9688  if (p_enc->linkFrameMaxIntraRatio != 40)
9689  {
9690  p_cfg->ui8linkFrameMaxIntraRatio = (uint8_t)p_enc->linkFrameMaxIntraRatio - 40; // for old libxcoder backward compatibility, use -40 to represent 0
9691  }
9692 
9693  for (i = 0; i < NI_MAX_SPATIAL_LAYERS; i++)
9694  {
9695  if (p_enc->spatialLayerBitrate[i] != 0)
9696  {
9697  p_cfg->i32spatialLayerBitrate[i] = p_enc->spatialLayerBitrate[i];
9698  }
9699  if (p_enc->av1OpLevel[i] != 0)
9700  {
9701  p_cfg->ui8av1OpLevel[i] = p_enc->av1OpLevel[i];
9702  }
9703  }
9704 
9705  if (p_enc->disableAv1TimingInfo != 0)
9706  {
9708  }
9709 
9710  ni_log2(p_ctx, NI_LOG_DEBUG, "lowDelay=%d\n", p_src->low_delay_mode);
9711  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8bitstreamFormat=%d\n", p_cfg->ui8bitstreamFormat);
9712  ni_log2(p_ctx, NI_LOG_DEBUG, "i32picWidth=%d\n", p_cfg->i32picWidth);
9713  ni_log2(p_ctx, NI_LOG_DEBUG, "i32picHeight=%d\n", p_cfg->i32picHeight);
9714  ni_log2(p_ctx, NI_LOG_DEBUG, "i32meBlkMode=%d\n", p_cfg->i32meBlkMode);
9715  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8sliceMode=%d\n", p_cfg->ui8sliceMode);
9716  ni_log2(p_ctx, NI_LOG_DEBUG, "i32frameRateInfo=%d\n", p_cfg->i32frameRateInfo);
9717  ni_log2(p_ctx, NI_LOG_DEBUG, "i32vbvBufferSize=%d\n", p_cfg->i32vbvBufferSize);
9718  ni_log2(p_ctx, NI_LOG_DEBUG, "i32userQpMax=%d\n", p_cfg->i32userQpMax);
9719  ni_log2(p_ctx, NI_LOG_DEBUG, "enableSSIM=%d\n", p_cfg->ui8enableSSIM);
9720  // AVC only
9721  ni_log2(p_ctx, NI_LOG_DEBUG, "i32maxIntraSize=%d\n", p_cfg->i32maxIntraSize);
9722  ni_log2(p_ctx, NI_LOG_DEBUG, "i32userMaxDeltaQp=%d\n", p_cfg->i32userMaxDeltaQp);
9723  ni_log2(p_ctx, NI_LOG_DEBUG, "i32userMinDeltaQp=%d\n", p_cfg->i32userMinDeltaQp);
9724  ni_log2(p_ctx, NI_LOG_DEBUG, "i32userQpMin=%d\n", p_cfg->i32userQpMin);
9725  ni_log2(p_ctx, NI_LOG_DEBUG, "i32bitRate=%d\n", p_cfg->i32bitRate);
9726  ni_log2(p_ctx, NI_LOG_DEBUG, "i32bitRateBL=%d\n", p_cfg->i32bitRateBL);
9727  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8rcEnable=%d\n", p_cfg->ui8rcEnable);
9728  ni_log2(p_ctx, NI_LOG_DEBUG, "i32srcBitDepth=%d\n", p_cfg->i32srcBitDepth);
9729  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8enablePTS=%d\n", p_cfg->ui8enablePTS);
9730  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8lowLatencyMode=%d\n", p_cfg->ui8lowLatencyMode);
9731  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32sourceEndian=%u\n", p_cfg->ui32sourceEndian);
9732  ni_log2(p_ctx, NI_LOG_DEBUG, "hdrEnableVUI=%u\n", p_cfg->hdrEnableVUI);
9733  ni_log2(p_ctx, NI_LOG_DEBUG, "i32hwframes=%i\n", p_cfg->i32hwframes);
9734 
9735  ni_log2(p_ctx, NI_LOG_DEBUG, "** ni_t408_config_t: \n");
9736  ni_log2(p_ctx, NI_LOG_DEBUG, "profile=%d\n", p_t408->profile);
9737  ni_log2(p_ctx, NI_LOG_DEBUG, "level=%d\n", p_t408->level);
9738  ni_log2(p_ctx, NI_LOG_DEBUG, "tier=%d\n", p_t408->tier);
9739 
9740  ni_log2(p_ctx, NI_LOG_DEBUG, "internalBitDepth=%d\n", p_t408->internalBitDepth);
9741  ni_log2(p_ctx, NI_LOG_DEBUG, "losslessEnable=%d\n", p_t408->losslessEnable);
9742  ni_log2(p_ctx, NI_LOG_DEBUG, "constIntraPredFlag=%d\n", p_t408->constIntraPredFlag);
9743 
9744  ni_log2(p_ctx, NI_LOG_DEBUG, "decoding_refresh_type=%d\n", p_t408->decoding_refresh_type);
9745  ni_log2(p_ctx, NI_LOG_DEBUG, "intra_qp=%d\n", p_t408->intra_qp);
9746  ni_log2(p_ctx, NI_LOG_DEBUG, "intra_period=%d\n", p_t408->intra_period);
9747  ni_log2(p_ctx, NI_LOG_DEBUG, "roi_enable=%d\n", p_t408->roiEnable);
9748 
9749  ni_log2(p_ctx, NI_LOG_DEBUG, "useLongTerm=%u\n", p_t408->useLongTerm);
9750  ni_log2(p_ctx, NI_LOG_DEBUG, "setLongTermInterval=%u\n", p_cfg->ui32setLongTermInterval);
9751  ni_log2(p_ctx, NI_LOG_DEBUG, "setLongTermCount=%u\n", p_cfg->ui8setLongTermCount);
9752 
9753  ni_log2(p_ctx, NI_LOG_DEBUG, "conf_win_top=%d\n", p_t408->conf_win_top);
9754  ni_log2(p_ctx, NI_LOG_DEBUG, "conf_win_bottom=%d\n", p_t408->conf_win_bottom);
9755  ni_log2(p_ctx, NI_LOG_DEBUG, "conf_win_left=%d\n", p_t408->conf_win_left);
9756  ni_log2(p_ctx, NI_LOG_DEBUG, "conf_win_right=%d\n", p_t408->conf_win_right);
9757 
9758  ni_log2(p_ctx, NI_LOG_DEBUG, "independSliceMode=%d\n", p_t408->independSliceMode);
9759  ni_log2(p_ctx, NI_LOG_DEBUG, "independSliceModeArg=%d\n", p_t408->independSliceModeArg);
9760 
9761  ni_log2(p_ctx, NI_LOG_DEBUG, "dependSliceMode=%d\n", p_t408->dependSliceMode);
9762  ni_log2(p_ctx, NI_LOG_DEBUG, "dependSliceModeArg=%d\n", p_t408->dependSliceModeArg);
9763 
9764  ni_log2(p_ctx, NI_LOG_DEBUG, "intraRefreshMode=%d\n", p_t408->intraRefreshMode);
9765 
9766  ni_log2(p_ctx, NI_LOG_DEBUG, "intraRefreshArg=%d\n", p_t408->intraRefreshArg);
9767 
9768  ni_log2(p_ctx, NI_LOG_DEBUG, "use_recommend_enc_params=%d\n", p_t408->use_recommend_enc_params);
9769  ni_log2(p_ctx, NI_LOG_DEBUG, "scalingListEnable=%d\n", p_t408->scalingListEnable);
9770 
9771  ni_log2(p_ctx, NI_LOG_DEBUG, "cu_size_mode=%d\n", p_t408->cu_size_mode);
9772  ni_log2(p_ctx, NI_LOG_DEBUG, "tmvpEnable=%d\n", p_t408->tmvpEnable);
9773  ni_log2(p_ctx, NI_LOG_DEBUG, "wppEnable=%d\n", p_t408->wppEnable);
9774  ni_log2(p_ctx, NI_LOG_DEBUG, "max_num_merge=%d\n", p_t408->max_num_merge);
9775  ni_log2(p_ctx, NI_LOG_DEBUG, "disableDeblk=%d\n", p_t408->disableDeblk);
9776  ni_log2(p_ctx, NI_LOG_DEBUG, "lfCrossSliceBoundaryEnable=%d\n", p_t408->lfCrossSliceBoundaryEnable);
9777  ni_log2(p_ctx, NI_LOG_DEBUG, "betaOffsetDiv2=%d\n", p_t408->betaOffsetDiv2);
9778  ni_log2(p_ctx, NI_LOG_DEBUG, "tcOffsetDiv2=%d\n", p_t408->tcOffsetDiv2);
9779  ni_log2(p_ctx, NI_LOG_DEBUG, "skipIntraTrans=%d\n", p_t408->skipIntraTrans);
9780  ni_log2(p_ctx, NI_LOG_DEBUG, "saoEnable=%d\n", p_t408->saoEnable);
9781  ni_log2(p_ctx, NI_LOG_DEBUG, "intraNxNEnable=%d\n", p_t408->intraNxNEnable);
9782  ni_log2(p_ctx, NI_LOG_DEBUG, "bitAllocMode=%d\n", p_t408->bitAllocMode);
9783 
9784  ni_log2(p_ctx, NI_LOG_DEBUG, "enable_cu_level_rate_control=%d\n", p_t408->enable_cu_level_rate_control);
9785 
9786  ni_log2(p_ctx, NI_LOG_DEBUG, "enable_hvs_qp=%d\n", p_t408->enable_hvs_qp);
9787 
9788  ni_log2(p_ctx, NI_LOG_DEBUG, "hvs_qp_scale=%d\n", p_t408->hvs_qp_scale);
9789 
9790  ni_log2(p_ctx, NI_LOG_DEBUG, "max_delta_qp=%d\n", p_t408->max_delta_qp);
9791 
9792  // CUSTOM_GOP
9793  ni_log2(p_ctx, NI_LOG_DEBUG, "gop_preset_index=%d\n", p_t408->gop_preset_index);
9794 #ifndef QUADRA
9795  if (!QUADRA)
9796  {
9797  if (p_t408->gop_preset_index == GOP_PRESET_IDX_CUSTOM)
9798  {
9799  ni_log2(p_ctx, NI_LOG_DEBUG, "custom_gop_params.custom_gop_size=%d\n", p_t408->custom_gop_params.custom_gop_size);
9800  for (i = 0; i < 8; i++)
9801  //for (i = 0; i < p_t408->custom_gop_params.custom_gop_size; i++)
9802  {
9803  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);
9804  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);
9805  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);
9806  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);
9807  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);
9808  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);
9809  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);
9810  }
9811  }
9812  }
9813  else // QUADRA
9814 #endif
9815  {
9816  if (p_t408->custom_gop_params.custom_gop_size)
9817  {
9818  int j;
9819  ni_log2(p_ctx, NI_LOG_DEBUG, "custom_gop_params.custom_gop_size=%d\n", p_t408->custom_gop_params.custom_gop_size);
9820  for (i = 0; i < NI_MAX_GOP_NUM; i++)
9821  {
9822  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);
9823  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);
9824  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);
9825  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);
9826  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);
9827  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);
9828  for (j = 0; j < NI_MAX_REF_PIC; j++)
9829  {
9830  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);
9831  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);
9832  }
9833  }
9834  }
9835  }
9836 
9837  ni_log2(p_ctx, NI_LOG_DEBUG, "roiEnable=%d\n", p_t408->roiEnable);
9838 
9839  ni_log2(p_ctx, NI_LOG_DEBUG, "numUnitsInTick=%u\n", p_t408->numUnitsInTick);
9840  ni_log2(p_ctx, NI_LOG_DEBUG, "timeScale=%u\n", p_t408->timeScale);
9841  ni_log2(p_ctx, NI_LOG_DEBUG, "numTicksPocDiffOne=%u\n", p_t408->numTicksPocDiffOne);
9842 
9843  ni_log2(p_ctx, NI_LOG_DEBUG, "chromaCbQpOffset=%d\n", p_t408->chromaCbQpOffset);
9844  ni_log2(p_ctx, NI_LOG_DEBUG, "chromaCrQpOffset=%d\n", p_t408->chromaCrQpOffset);
9845 
9846  ni_log2(p_ctx, NI_LOG_DEBUG, "initialRcQp=%d\n", p_t408->initialRcQp);
9847 
9848  ni_log2(p_ctx, NI_LOG_DEBUG, "nrYEnable=%u\n", p_t408->nrYEnable);
9849  ni_log2(p_ctx, NI_LOG_DEBUG, "nrCbEnable=%u\n", p_t408->nrCbEnable);
9850  ni_log2(p_ctx, NI_LOG_DEBUG, "nrCrEnable=%u\n", p_t408->nrCrEnable);
9851 
9852  // ENC_NR_WEIGHT
9853  ni_log2(p_ctx, NI_LOG_DEBUG, "nrIntraWeightY=%u\n", p_t408->nrIntraWeightY);
9854  ni_log2(p_ctx, NI_LOG_DEBUG, "nrIntraWeightCb=%u\n", p_t408->nrIntraWeightCb);
9855  ni_log2(p_ctx, NI_LOG_DEBUG, "nrIntraWeightCr=%u\n", p_t408->nrIntraWeightCr);
9856  ni_log2(p_ctx, NI_LOG_DEBUG, "nrInterWeightY=%u\n", p_t408->nrInterWeightY);
9857  ni_log2(p_ctx, NI_LOG_DEBUG, "nrInterWeightCb=%u\n", p_t408->nrInterWeightCb);
9858  ni_log2(p_ctx, NI_LOG_DEBUG, "nrInterWeightCr=%u\n", p_t408->nrInterWeightCr);
9859 
9860  ni_log2(p_ctx, NI_LOG_DEBUG, "nrNoiseEstEnable=%u\n", p_t408->nrNoiseEstEnable);
9861  ni_log2(p_ctx, NI_LOG_DEBUG, "nrNoiseSigmaY=%u\n", p_t408->nrNoiseSigmaY);
9862  ni_log2(p_ctx, NI_LOG_DEBUG, "nrNoiseSigmaCb=%u\n", p_t408->nrNoiseSigmaCb);
9863  ni_log2(p_ctx, NI_LOG_DEBUG, "nrNoiseSigmaCr=%u\n", p_t408->nrNoiseSigmaCr);
9864 
9865  // newly added for T408
9866  ni_log2(p_ctx, NI_LOG_DEBUG, "monochromeEnable=%u\n", p_t408->monochromeEnable);
9867  ni_log2(p_ctx, NI_LOG_DEBUG, "strongIntraSmoothEnable=%u\n",
9868  p_t408->strongIntraSmoothEnable);
9869 
9870  ni_log2(p_ctx, NI_LOG_DEBUG, "weightPredEnable=%u\n", p_t408->weightPredEnable);
9871  ni_log2(p_ctx, NI_LOG_DEBUG, "bgDetectEnable=%u\n", p_t408->bgDetectEnable);
9872  ni_log2(p_ctx, NI_LOG_DEBUG, "bgThrDiff=%u\n", p_t408->bgThrDiff);
9873  ni_log2(p_ctx, NI_LOG_DEBUG, "bgThrMeanDiff=%u\n", p_t408->bgThrMeanDiff);
9874  ni_log2(p_ctx, NI_LOG_DEBUG, "bgLambdaQp=%u\n", p_t408->bgLambdaQp);
9875  ni_log2(p_ctx, NI_LOG_DEBUG, "bgDeltaQp=%d\n", p_t408->bgDeltaQp);
9876 
9877  ni_log2(p_ctx, NI_LOG_DEBUG, "customLambdaEnable=%u\n", p_t408->customLambdaEnable);
9878  ni_log2(p_ctx, NI_LOG_DEBUG, "customMDEnable=%u\n", p_t408->customMDEnable);
9879  ni_log2(p_ctx, NI_LOG_DEBUG, "pu04DeltaRate=%d\n", p_t408->pu04DeltaRate);
9880  ni_log2(p_ctx, NI_LOG_DEBUG, "pu08DeltaRate=%d\n", p_t408->pu08DeltaRate);
9881  ni_log2(p_ctx, NI_LOG_DEBUG, "pu16DeltaRate=%d\n", p_t408->pu16DeltaRate);
9882  ni_log2(p_ctx, NI_LOG_DEBUG, "pu32DeltaRate=%d\n", p_t408->pu32DeltaRate);
9883  ni_log2(p_ctx, NI_LOG_DEBUG, "pu04IntraPlanarDeltaRate=%d\n", p_t408->pu04IntraPlanarDeltaRate);
9884  ni_log2(p_ctx, NI_LOG_DEBUG, "pu04IntraDcDeltaRate=%d\n", p_t408->pu04IntraDcDeltaRate);
9885  ni_log2(p_ctx, NI_LOG_DEBUG, "pu04IntraAngleDeltaRate=%d\n", p_t408->pu04IntraAngleDeltaRate);
9886  ni_log2(p_ctx, NI_LOG_DEBUG, "pu08IntraPlanarDeltaRate=%d\n", p_t408->pu08IntraPlanarDeltaRate);
9887  ni_log2(p_ctx, NI_LOG_DEBUG, "pu08IntraDcDeltaRate=%d\n", p_t408->pu08IntraDcDeltaRate);
9888  ni_log2(p_ctx, NI_LOG_DEBUG, "pu08IntraAngleDeltaRate=%d\n", p_t408->pu08IntraAngleDeltaRate);
9889  ni_log2(p_ctx, NI_LOG_DEBUG, "pu16IntraPlanarDeltaRate=%d\n", p_t408->pu16IntraPlanarDeltaRate);
9890  ni_log2(p_ctx, NI_LOG_DEBUG, "pu16IntraDcDeltaRate=%d\n", p_t408->pu16IntraDcDeltaRate);
9891  ni_log2(p_ctx, NI_LOG_DEBUG, "pu16IntraAngleDeltaRate=%d\n", p_t408->pu16IntraAngleDeltaRate);
9892  ni_log2(p_ctx, NI_LOG_DEBUG, "pu32IntraPlanarDeltaRate=%d\n", p_t408->pu32IntraPlanarDeltaRate);
9893  ni_log2(p_ctx, NI_LOG_DEBUG, "pu32IntraDcDeltaRate=%d\n", p_t408->pu32IntraDcDeltaRate);
9894  ni_log2(p_ctx, NI_LOG_DEBUG, "pu32IntraAngleDeltaRate=%d\n", p_t408->pu32IntraAngleDeltaRate);
9895  ni_log2(p_ctx, NI_LOG_DEBUG, "cu08IntraDeltaRate=%d\n", p_t408->cu08IntraDeltaRate);
9896  ni_log2(p_ctx, NI_LOG_DEBUG, "cu08InterDeltaRate=%d\n", p_t408->cu08InterDeltaRate);
9897  ni_log2(p_ctx, NI_LOG_DEBUG, "cu08MergeDeltaRate=%d\n", p_t408->cu08MergeDeltaRate);
9898  ni_log2(p_ctx, NI_LOG_DEBUG, "cu16IntraDeltaRate=%d\n", p_t408->cu16IntraDeltaRate);
9899  ni_log2(p_ctx, NI_LOG_DEBUG, "cu16InterDeltaRate=%d\n", p_t408->cu16InterDeltaRate);
9900  ni_log2(p_ctx, NI_LOG_DEBUG, "cu16MergeDeltaRate=%d\n", p_t408->cu16MergeDeltaRate);
9901  ni_log2(p_ctx, NI_LOG_DEBUG, "cu32IntraDeltaRate=%d\n", p_t408->cu32IntraDeltaRate);
9902  ni_log2(p_ctx, NI_LOG_DEBUG, "cu32InterDeltaRate=%d\n", p_t408->cu32InterDeltaRate);
9903  ni_log2(p_ctx, NI_LOG_DEBUG, "cu32MergeDeltaRate=%d\n", p_t408->cu32MergeDeltaRate);
9904  ni_log2(p_ctx, NI_LOG_DEBUG, "coefClearDisable=%d\n", p_t408->coefClearDisable);
9905  ni_log2(p_ctx, NI_LOG_DEBUG, "minQpI=%d\n", p_t408->minQpI);
9906  ni_log2(p_ctx, NI_LOG_DEBUG, "maxQpI=%d\n", p_t408->maxQpI);
9907  ni_log2(p_ctx, NI_LOG_DEBUG, "minQpP=%d\n", p_t408->minQpP);
9908  ni_log2(p_ctx, NI_LOG_DEBUG, "maxQpP=%d\n", p_t408->maxQpP);
9909  ni_log2(p_ctx, NI_LOG_DEBUG, "minQpB=%d\n", p_t408->minQpB);
9910  ni_log2(p_ctx, NI_LOG_DEBUG, "maxQpB=%d\n", p_t408->maxQpB);
9911 
9912  // for H.264 on T408
9913  ni_log2(p_ctx, NI_LOG_DEBUG, "avcIdrPeriod=%d\n", p_t408->avcIdrPeriod);
9914  ni_log2(p_ctx, NI_LOG_DEBUG, "rdoSkip=%d\n", p_t408->rdoSkip);
9915  ni_log2(p_ctx, NI_LOG_DEBUG, "lambdaScalingEnable=%d\n", p_t408->lambdaScalingEnable);
9916  ni_log2(p_ctx, NI_LOG_DEBUG, "enable_transform_8x8=%d\n", p_t408->enable_transform_8x8);
9917  ni_log2(p_ctx, NI_LOG_DEBUG, "slice_mode=%d\n", p_t408->slice_mode);
9918  ni_log2(p_ctx, NI_LOG_DEBUG, "slice_arg=%d\n", p_t408->slice_arg);
9919  ni_log2(p_ctx, NI_LOG_DEBUG, "intra_mb_refresh_mode=%d\n", p_t408->intra_mb_refresh_mode);
9920  ni_log2(p_ctx, NI_LOG_DEBUG, "intra_mb_refresh_arg=%d\n", p_t408->intra_mb_refresh_arg);
9921  ni_log2(p_ctx, NI_LOG_DEBUG, "enable_mb_level_rc=%d\n", p_t408->enable_mb_level_rc);
9922  ni_log2(p_ctx, NI_LOG_DEBUG, "entropy_coding_mode=%d\n", p_t408->entropy_coding_mode);
9923  ni_log2(p_ctx, NI_LOG_DEBUG, "forcedHeaderEnable=%u\n", p_t408->forcedHeaderEnable);
9924 
9925  //QUADRA
9926  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8EnableAUD=%d\n", p_cfg->ui8EnableAUD);
9927  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8LookAheadDepth=%d\n", p_cfg->ui8LookAheadDepth);
9928  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8rdoLevel=%d\n", p_cfg->ui8rdoLevel);
9929  ni_log2(p_ctx, NI_LOG_DEBUG, "i8crf=%d\n", p_cfg->i8crf);
9930  ni_log2(p_ctx, NI_LOG_DEBUG, "i8crfDecimal=%d\n", p_cfg->i8crfDecimal);
9931  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16HDR10MaxLight=%d\n", p_cfg->ui16HDR10MaxLight);
9932  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16HDR10AveLight=%d\n", p_cfg->ui16HDR10AveLight);
9933  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8HDR10CLLEnable=%d\n", p_cfg->ui8HDR10CLLEnable);
9934  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8EnableRdoQuant=%d\n", p_cfg->ui8EnableRdoQuant);
9935  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8ctbRcMode=%d\n", p_cfg->ui8ctbRcMode);
9936  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8gopSize=%d\n", p_cfg->ui8gopSize);
9937  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8useLowDelayPocType=%d\n", p_cfg->ui8useLowDelayPocType);
9938  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8gopLowdelay=%d\n", p_cfg->ui8gopLowdelay);
9939  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16gdrDuration=%d\n", p_cfg->ui16gdrDuration);
9940  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8hrdEnable=%d\n", p_cfg->ui8hrdEnable);
9941  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8colorDescPresent=%d\n", p_cfg->ui8colorDescPresent);
9942  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8colorPrimaries=%d\n", p_cfg->ui8colorPrimaries);
9943  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8colorTrc=%d\n", p_cfg->ui8colorTrc);
9944  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8colorSpace=%d\n", p_cfg->ui8colorSpace);
9945  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16aspectRatioWidth=%d\n", p_cfg->ui16aspectRatioWidth);
9946  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16aspectRatioHeight=%d\n", p_cfg->ui16aspectRatioHeight);
9947  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16rootBufId=%d\n", p_cfg->ui16rootBufId);
9948  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8planarFormat=%d\n", p_cfg->ui8planarFormat);
9949  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8PixelFormat=%d\n", p_cfg->ui8PixelFormat);
9950  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32ltrRefInterval=%u\n", p_cfg->ui32ltrRefInterval);
9951  ni_log2(p_ctx, NI_LOG_DEBUG, "i32ltrRefQpOffset=%d\n", p_cfg->i32ltrRefQpOffset);
9952  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32ltrFirstGap=%u\n", p_cfg->ui32ltrFirstGap);
9953  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32ltrNextInterval=%u\n", p_cfg->ui32ltrNextInterval);
9954  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8multicoreJointMode=%d\n", p_cfg->ui8multicoreJointMode);
9955  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8videoFullRange=%u\n", p_cfg->ui8videoFullRange);
9956  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32QLevel=%u\n", p_cfg->ui32QLevel);
9957  ni_log2(p_ctx, NI_LOG_DEBUG, "i8chromaQpOffset=%d\n", p_cfg->i8chromaQpOffset);
9958  ni_log2(p_ctx, NI_LOG_DEBUG, "i32tolCtbRcInter=0x%x\n", p_cfg->i32tolCtbRcInter);
9959  ni_log2(p_ctx, NI_LOG_DEBUG, "i32tolCtbRcIntra=0x%x\n", p_cfg->i32tolCtbRcIntra);
9960  ni_log2(p_ctx, NI_LOG_DEBUG, "i16bitrateWindow=%d\n", p_cfg->i16bitrateWindow);
9961  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8inLoopDSRatio=%u\n", p_cfg->ui8inLoopDSRatio);
9962  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8blockRCSize=%u\n", p_cfg->ui8blockRCSize);
9963  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8rcQpDeltaRange=%u\n", p_cfg->ui8rcQpDeltaRange);
9964  ni_log2(p_ctx, NI_LOG_DEBUG, "i16ctbRowQpStep=%u\n", p_cfg->i16ctbRowQpStep);
9965  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8NewRCEnable=%u\n", p_cfg->ui8NewRCEnable);
9966  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8LowDelay=%d\n", p_cfg->ui8LowDelay);
9967  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8hdr10_enable=%u\n", p_cfg->ui8hdr10_enable);
9968  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16hdr10_dx0=%u\n", p_cfg->ui16hdr10_dx0);
9969  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16hdr10_dy0=%u\n", p_cfg->ui16hdr10_dy0);
9970  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16hdr10_dx1=%u\n", p_cfg->ui16hdr10_dx1);
9971  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16hdr10_dy1=%u\n", p_cfg->ui16hdr10_dy1);
9972  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16hdr10_dx2=%u\n", p_cfg->ui16hdr10_dx2);
9973  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16hdr10_dy2=%u\n", p_cfg->ui16hdr10_dy2);
9974  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16hdr10_wx=%u\n", p_cfg->ui16hdr10_wx);
9975  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16hdr10_wy=%u\n", p_cfg->ui16hdr10_wy);
9976  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32hdr10_maxluma=%u\n", p_cfg->ui32hdr10_maxluma);
9977  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32hdr10_minluma=%u\n", p_cfg->ui32hdr10_minluma);
9978  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8avccHvcc=%u\n", p_cfg->ui8avccHvcc);
9979  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8av1ErrResilientMode=%u\n", p_cfg->ui8av1ErrResilientMode);
9980  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8intraResetRefresh=%d\n", p_cfg->ui8intraResetRefresh);
9981  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8temporalLayersEnable=%u\n", p_cfg->ui8temporalLayersEnable);
9982  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8enable2PassGopPattern=%u\n", p_cfg->ui8enable2PassGopPatern);
9983  ni_log2(p_ctx, NI_LOG_DEBUG, "zerocopy_mode=%d\n", p_src->zerocopy_mode);
9984  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32lumaLinesize=%u\n", p_cfg->ui32lumaLinesize);
9985  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32chromaLinesize=%u\n", p_cfg->ui32chromaLinesize);
9986  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32cropWidth=%u\n", p_cfg->ui32cropWidth);
9987  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32cropHeight=%u\n", p_cfg->ui32cropHeight);
9988  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32horOffset=%u\n", p_cfg->ui32horOffset);
9989  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32verOffset=%u\n", p_cfg->ui32verOffset);
9990  ni_log2(p_ctx, NI_LOG_DEBUG, "i8crfMax=%d\n", p_cfg->i8crfMax);
9991  ni_log2(p_ctx, NI_LOG_DEBUG, "i32qcomp=%d\n", p_cfg->i32qcomp);
9992  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8noMbtree=%u\n", p_cfg->ui8noMbtree);
9993  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8noHWMultiPassSupport=%u\n", p_cfg->ui8noHWMultiPassSupport);
9994  ni_log2(p_ctx, NI_LOG_DEBUG, "i8cuTreeFactor=%d\n", p_cfg->i8cuTreeFactor);
9995  ni_log2(p_ctx, NI_LOG_DEBUG, "i32ipRatio=%d\n", p_cfg->i32ipRatio);
9996  ni_log2(p_ctx, NI_LOG_DEBUG, "i32pbRatio=%d\n", p_cfg->i32pbRatio);
9997  ni_log2(p_ctx, NI_LOG_DEBUG, "i32cplxDecay=%d\n", p_cfg->i32cplxDecay);
9998  ni_log2(p_ctx, NI_LOG_DEBUG, "i8ppsInitQp=%d\n", p_cfg->i8ppsInitQp);
9999  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8bitrateMode=%u\n", p_cfg->ui8bitrateMode);
10000  ni_log2(p_ctx, NI_LOG_DEBUG, "i8pass1Qp=%d\n", p_cfg->i8pass1Qp);
10001  ni_log2(p_ctx, NI_LOG_DEBUG, "i8crfDecimal=%d\n", p_cfg->i8crfDecimal);
10002  ni_log2(p_ctx, NI_LOG_DEBUG, "i8hvsBaseMbComplexity=%u\n", p_cfg->i8hvsBaseMbComplexity);
10003  ni_log2(p_ctx, NI_LOG_DEBUG, "i8enableipRatio=%d\n",p_cfg->i8enableipRatio);
10004  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16iFrameSizeRatio=%u\n",p_cfg->ui16iFrameSizeRatio);
10005  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8crfMaxIframeEnable=%u\n", p_cfg->ui8crfMaxIframeEnable);
10006  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32vbvMinRate=%u\n", p_cfg->ui32vbvMinRate);
10007  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8disableBframeRDOQ=%u\n", p_cfg->ui8disableBframeRDOQ);
10008  ni_log2(p_ctx, NI_LOG_DEBUG, "i32forceBframeQpFactor=%d\n", p_cfg->i32forceBframeQpFactor);
10009  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8tuneBframeVisual=%u\n", p_cfg->ui8tuneBframeVisual);
10010  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8EnableAcqLimit=%u\n", p_cfg->ui8EnableAcqLimit);
10011  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8compressor=%u\n", p_cfg->ui8compressor);
10012  ni_log2(p_ctx, NI_LOG_DEBUG, "u8customizeRoiQpLevel=%u\n", p_cfg->u8customizeRoiQpLevel);
10013  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8motionConstrainedMode=%d\n", p_cfg->ui8motionConstrainedMode);
10014  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8mallocStrategy=%d\n", p_cfg->ui8mallocStrategy);
10015  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8spatialLayersMinusOne=%d\n", p_cfg->ui8spatialLayersMinusOne);
10016  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8enableTimecode=%d\n", p_cfg->ui8enableTimecode);
10017  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8spatialLayersRefBaseLayer=%d\n", p_cfg->ui8spatialLayersRefBaseLayer);
10018  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8vbvBufferReencode=%d\n", p_cfg->ui8vbvBufferReencode);
10019  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8totalCuTreeDepth=%u\n", p_cfg->ui8totalCuTreeDepth);
10020  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8adaptiveCuTree=%u\n", p_cfg->ui8adaptiveCuTree);
10021  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8preIntraHandling=%u\n", p_cfg->ui8preIntraHandling);
10022  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8baseLayerOnly=%u\n", p_cfg->ui8baseLayerOnly);
10023  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8pastFrameMaxIntraRatio=%u\n", p_cfg->ui8pastFrameMaxIntraRatio);
10024  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8linkFrameMaxIntraRatio=%u\n", p_cfg->ui8linkFrameMaxIntraRatio);
10025  ni_log2(p_ctx, NI_LOG_DEBUG, "i32spatialLayerBitrate=%d,%d,%d,%d\n", p_cfg->i32spatialLayerBitrate[0],
10026  p_cfg->i32spatialLayerBitrate[1], p_cfg->i32spatialLayerBitrate[2], p_cfg->i32spatialLayerBitrate[3]);
10027  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8disableAv1TimingInfo=%u\n", p_cfg->ui8disableAv1TimingInfo);
10028  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8av1OpLevel=%u,%u,%u,%u\n", p_cfg->ui8av1OpLevel[0],
10029  p_cfg->ui8av1OpLevel[1], p_cfg->ui8av1OpLevel[2], p_cfg->ui8av1OpLevel[3]);
10030  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8adaptiveLamdaMode=%u\n",p_cfg->ui8adaptiveLamdaMode);
10031  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8getCuInfo=%u\n", p_cfg->ui8getCuInfo);
10032  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8adaptiveCrfMode=%u\n",p_cfg->ui8adaptiveCrfMode);
10033 }
10034 
10035 /*!******************************************************************************
10036  * \brief Setup and initialize all xcoder configuration to default (Rev. B)
10037  *
10038  * \param
10039  *
10040  * \return
10041  ******************************************************************************/
10043 {
10044  int i = 0;
10045 
10046  if( (!p_ctx) || (!p_config) )
10047  {
10048  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Null pointer parameters passed\n",
10049  __func__);
10050  return;
10051  }
10052 
10053  memset(p_config, 0, sizeof(ni_encoder_config_t));
10054 
10055  // fill in common attributes values
10056  p_config->i32picWidth = 720;
10057  p_config->i32picHeight = 480;
10058  p_config->i32meBlkMode = 0; // (AVC ONLY) 0 means use all possible block partitions
10059  p_config->ui8sliceMode = 0; // 0 means 1 slice per picture
10060  p_config->i32frameRateInfo = 30;
10061  p_config->i32frameRateDenominator = 1;
10062  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
10063  p_config->i32userQpMax = 51; // this should also be h264-only parameter
10064 
10065  // AVC only
10066  if (NI_CODEC_FORMAT_H264 == p_ctx->codec_format)
10067  {
10068  p_config->i32maxIntraSize = 8000000; // how big an intra p_frame can get?
10069  p_config->i32userMaxDeltaQp = 51;
10070  p_config->i32userMinDeltaQp = 51;
10071  p_config->i32userQpMin = 8;
10072  }
10073 
10074  p_config->i32bitRate = 0;
10075  p_config->i32bitRateBL = 0;
10076  p_config->ui8rcEnable = 0;
10077  p_config->i32srcBitDepth = p_ctx->src_bit_depth;
10078  p_config->ui8enablePTS = 0;
10079  p_config->ui8lowLatencyMode = 0;
10080 
10081  // profiles for H.264: 1 = baseline, 2 = main, 3 = extended, 4 = high
10082  // 5 = high10 (default 8 bit: 4, 10 bit: 5)
10083  // profiles for HEVC: 1 = main, 2 = main10 (default 8 bit: 1, 10 bit: 2)
10084 
10085  // bitstream type: H.264 or HEVC
10086  if (NI_CODEC_FORMAT_H264 == p_ctx->codec_format)
10087  {
10088  p_config->ui8bitstreamFormat = STD_AVC;
10089 
10090  p_config->niParamT408.profile = 4;
10091  if (10 == p_ctx->src_bit_depth)
10092  {
10093  p_config->niParamT408.profile = 5;
10094  }
10095  } else if (NI_CODEC_FORMAT_JPEG == p_ctx->codec_format)
10096  {
10097  p_config->ui8bitstreamFormat = STD_JPEG;
10098  } else if (NI_CODEC_FORMAT_AV1 == p_ctx->codec_format)
10099  {
10100  p_config->ui8bitstreamFormat = STD_AV1;
10101 
10102  p_config->niParamT408.profile = 1;
10103  } else
10104  {
10106 
10107  p_config->ui8bitstreamFormat = STD_HEVC;
10108 
10109  p_config->niParamT408.profile = 1;
10110  if (10 == p_ctx->src_bit_depth)
10111  {
10112  p_config->niParamT408.profile = 2;
10113  }
10114  }
10115 
10116  p_config->hdrEnableVUI = 0;
10117  p_config->ui32sourceEndian = p_ctx->src_endian;
10118 
10119  p_config->niParamT408.level = 0;
10120  p_config->niParamT408.tier = 0; // 0 means main tier
10121 
10122  p_config->niParamT408.internalBitDepth = p_ctx->src_bit_depth;
10123  p_config->niParamT408.losslessEnable = 0;
10124  p_config->niParamT408.constIntraPredFlag = 0;
10125 
10126  if (QUADRA)
10128  else
10130 
10131  p_config->niParamT408.decoding_refresh_type = 1;
10132  p_config->niParamT408.intra_qp = 22;
10133  // avcIdrPeriod (H.264 on T408), NOT shared with intra_period
10134  p_config->niParamT408.intra_period = 120;
10135  p_config->niParamT408.avcIdrPeriod = 120;
10136 
10137  p_config->niParamT408.conf_win_top = 0;
10138  p_config->niParamT408.conf_win_bottom = 0;
10139  p_config->niParamT408.conf_win_left = 0;
10140  p_config->niParamT408.conf_win_right = 0;
10141 
10142  p_config->niParamT408.independSliceMode = 0;
10143  p_config->niParamT408.independSliceModeArg = 0;
10144  p_config->niParamT408.dependSliceMode = 0;
10145  p_config->niParamT408.dependSliceModeArg = 0;
10146  p_config->niParamT408.intraRefreshMode = 0;
10147  p_config->niParamT408.intraRefreshArg = 0;
10148 
10149  p_config->niParamT408.use_recommend_enc_params = 0; //1;
10150  p_config->niParamT408.scalingListEnable = 0;
10151 
10152  p_config->niParamT408.cu_size_mode = 7;
10153  p_config->niParamT408.tmvpEnable = 1;
10154  p_config->niParamT408.wppEnable = 0;
10155  p_config->niParamT408.max_num_merge = 2;
10156  p_config->niParamT408.disableDeblk = 0;
10158  p_config->niParamT408.betaOffsetDiv2 = 0;
10159  p_config->niParamT408.tcOffsetDiv2 = 0;
10160  p_config->niParamT408.skipIntraTrans = 1;
10161  p_config->niParamT408.saoEnable = 1;
10162  p_config->niParamT408.intraNxNEnable = 1;
10163 
10164  p_config->niParamT408.bitAllocMode = 0;
10165 
10166  for (i = 0; i < NI_MAX_GOP_NUM; i++)
10167  {
10168  p_config->niParamT408.fixedBitRatio[i] = 1;
10169  }
10170 
10171  if (QUADRA)
10173  else
10175 
10176  p_config->niParamT408.enable_hvs_qp = 0;
10177  p_config->niParamT408.hvs_qp_scale = 2;
10178 
10179  p_config->niParamT408.max_delta_qp = 10;
10180 
10181  // CUSTOM_GOP
10183 #ifndef QUADRA
10184  if (!QUADRA)
10185  {
10186  for (i = 0; i < p_config->niParamT408.custom_gop_params.custom_gop_size; i++)
10187  {
10190  p_config->niParamT408.custom_gop_params.pic_param[i].pic_qp = 0;
10191  p_config->niParamT408.custom_gop_params.pic_param[i].num_ref_pic_L0 = 0;
10192  p_config->niParamT408.custom_gop_params.pic_param[i].ref_poc_L0 = 0;
10193  p_config->niParamT408.custom_gop_params.pic_param[i].ref_poc_L1 = 0;
10195  }
10196  }
10197  else // QUADRA
10198 #endif
10199  {
10200  int j;
10201  for (i = 0; i < NI_MAX_GOP_NUM; i++)
10202  {
10206  (float)0.3; // QP Factor range is between 0.3 and 1, higher values mean lower quality and less bits
10210  for (j = 0; j < NI_MAX_REF_PIC; j++)
10211  {
10212  p_config->niParamT408.custom_gop_params.pic_param[i].rps[j].ref_pic = 0;
10214  }
10215  }
10216  }
10217 
10218  p_config->niParamT408.roiEnable = 0;
10219 
10220  p_config->niParamT408.numUnitsInTick = 1000;
10221  p_config->niParamT408.timeScale = p_config->i32frameRateInfo * 1000;
10222  if (NI_CODEC_FORMAT_H264 == p_ctx->codec_format)
10223  {
10224  p_config->niParamT408.timeScale *= 2;
10225  }
10226 
10227  p_config->niParamT408.numTicksPocDiffOne = 0;
10228 
10229  p_config->niParamT408.chromaCbQpOffset = 0;
10230  p_config->niParamT408.chromaCrQpOffset = 0;
10231 
10232  p_config->niParamT408.initialRcQp = 63; //-1;
10233 
10234  p_config->niParamT408.nrYEnable = 0;
10235  p_config->niParamT408.nrCbEnable = 0;
10236  p_config->niParamT408.nrCrEnable = 0;
10237 
10238  // ENC_NR_WEIGHT
10239  p_config->niParamT408.nrIntraWeightY = 7;
10240  p_config->niParamT408.nrIntraWeightCb = 7;
10241  p_config->niParamT408.nrIntraWeightCr = 7;
10242  p_config->niParamT408.nrInterWeightY = 4;
10243  p_config->niParamT408.nrInterWeightCb = 4;
10244  p_config->niParamT408.nrInterWeightCr = 4;
10245 
10246  p_config->niParamT408.nrNoiseEstEnable = 0;
10247  p_config->niParamT408.nrNoiseSigmaY = 0;
10248  p_config->niParamT408.nrNoiseSigmaCb = 0;
10249  p_config->niParamT408.nrNoiseSigmaCr = 0;
10250 
10251  p_config->niParamT408.useLongTerm = 0;
10252 
10253  // newly added for T408
10254  p_config->niParamT408.monochromeEnable = 0;
10255  p_config->niParamT408.strongIntraSmoothEnable = 1;
10256 
10257  p_config->niParamT408.weightPredEnable = 0;
10258  p_config->niParamT408.bgDetectEnable = 0;
10259  p_config->niParamT408.bgThrDiff = 8; // matching the C-model
10260  p_config->niParamT408.bgThrMeanDiff = 1; // matching the C-model
10261  p_config->niParamT408.bgLambdaQp = 32; // matching the C-model
10262  p_config->niParamT408.bgDeltaQp = 3; // matching the C-model
10263 
10264  p_config->niParamT408.customLambdaEnable = 0;
10265  p_config->niParamT408.customMDEnable = 0;
10266  p_config->niParamT408.pu04DeltaRate = 0;
10267  p_config->niParamT408.pu08DeltaRate = 0;
10268  p_config->niParamT408.pu16DeltaRate = 0;
10269  p_config->niParamT408.pu32DeltaRate = 0;
10270  p_config->niParamT408.pu04IntraPlanarDeltaRate = 0;
10271  p_config->niParamT408.pu04IntraDcDeltaRate = 0;
10272  p_config->niParamT408.pu04IntraAngleDeltaRate = 0;
10273  p_config->niParamT408.pu08IntraPlanarDeltaRate = 0;
10274  p_config->niParamT408.pu08IntraDcDeltaRate = 0;
10275  p_config->niParamT408.pu08IntraAngleDeltaRate = 0;
10276  p_config->niParamT408.pu16IntraPlanarDeltaRate = 0;
10277  p_config->niParamT408.pu16IntraDcDeltaRate = 0;
10278  p_config->niParamT408.pu16IntraAngleDeltaRate = 0;
10279  p_config->niParamT408.pu32IntraPlanarDeltaRate = 0;
10280  p_config->niParamT408.pu32IntraDcDeltaRate = 0;
10281  p_config->niParamT408.pu32IntraAngleDeltaRate = 0;
10282  p_config->niParamT408.cu08IntraDeltaRate = 0;
10283  p_config->niParamT408.cu08InterDeltaRate = 0;
10284  p_config->niParamT408.cu08MergeDeltaRate = 0;
10285  p_config->niParamT408.cu16IntraDeltaRate = 0;
10286  p_config->niParamT408.cu16InterDeltaRate = 0;
10287  p_config->niParamT408.cu16MergeDeltaRate = 0;
10288  p_config->niParamT408.cu32IntraDeltaRate = 0;
10289  p_config->niParamT408.cu32InterDeltaRate = 0;
10290  p_config->niParamT408.cu32MergeDeltaRate = 0;
10291  p_config->niParamT408.coefClearDisable = 0;
10292  p_config->niParamT408.minQpI = 8;
10293  p_config->niParamT408.maxQpI = 51;
10294  p_config->niParamT408.minQpP = 8;
10295  p_config->niParamT408.maxQpP = 51;
10296  p_config->niParamT408.minQpB = 8;
10297  p_config->niParamT408.maxQpB = 51;
10298 
10299  // for H.264 on T408
10300  p_config->niParamT408.rdoSkip = 0;
10301  p_config->niParamT408.lambdaScalingEnable = 0;
10302  p_config->niParamT408.enable_transform_8x8 = 1;
10303  p_config->niParamT408.slice_mode = 0;
10304  p_config->niParamT408.slice_arg = 0;
10305  p_config->niParamT408.intra_mb_refresh_mode = 0;
10306  p_config->niParamT408.intra_mb_refresh_arg = 0;
10307  if (QUADRA)
10308  p_config->niParamT408.enable_mb_level_rc = 0;
10309  else
10310  p_config->niParamT408.enable_mb_level_rc = 1;
10311  p_config->niParamT408.entropy_coding_mode = 1; // 1 means CABAC, make sure profile is main or above, can't have CABAC in baseline
10312  p_config->niParamT408.forcedHeaderEnable = 0; // first IDR frame
10313 
10314  //QUADRA
10315  p_config->ui8EnableAUD = 0;
10316  p_config->ui8LookAheadDepth = 0;
10317  p_config->ui8rdoLevel = (NI_CODEC_FORMAT_JPEG == p_ctx->codec_format) ? 0 : 1;
10318  p_config->i8crf = -1;
10319  p_config->ui16HDR10MaxLight = 0;
10320  p_config->ui16HDR10AveLight = 0;
10321  p_config->ui8HDR10CLLEnable = 0;
10322  p_config->ui8EnableRdoQuant = 0;
10323  p_config->ui8ctbRcMode = 0;
10324  p_config->ui8gopSize = 0;
10325  p_config->ui8useLowDelayPocType = 0;
10326  p_config->ui8gopLowdelay = 0;
10327  p_config->ui16gdrDuration = 0;
10328  p_config->ui8hrdEnable = 0;
10329  p_config->ui8colorDescPresent = 0;
10331  p_config->ui8colorPrimaries = 2;
10332  //AVCOL_TRC_UNSPECIFIED
10333  p_config->ui8colorTrc = 2;
10334  //AVCOL_SPC_UNSPECIFIED
10335  p_config->ui8colorSpace = 2;
10336  p_config->ui16aspectRatioWidth = 0;
10337  p_config->ui16aspectRatioHeight = 0;
10339  p_config->ui8PixelFormat = NI_PIX_FMT_YUV420P;
10340  p_config->ui32ltrRefInterval = 0;
10341  p_config->i32ltrRefQpOffset = 0;
10342  p_config->ui32ltrFirstGap= 0;
10343  p_config->ui32ltrNextInterval = 1;
10344  p_config->ui8multicoreJointMode = 0;
10345  p_config->ui8videoFullRange = 0;
10346  p_config->ui32setLongTermInterval = 0;
10347  p_config->ui8setLongTermCount = 2;
10348  p_config->ui32QLevel = -1;
10349  p_config->i8chromaQpOffset = 0;
10350  p_config->i32tolCtbRcInter = (int32_t)(0.1 * 1000);
10351  p_config->i32tolCtbRcIntra = (int32_t)(0.1 * 1000);
10352  p_config->i16bitrateWindow = -255;
10353  p_config->ui8inLoopDSRatio = 1;
10354  p_config->ui8blockRCSize = 0;
10355  p_config->ui8rcQpDeltaRange = 10;
10356  p_config->i16ctbRowQpStep = 0;
10357  p_config->ui8NewRCEnable = 255;
10358  p_config->ui8LowDelay = 0;
10359  p_config->ui8fixedframerate = 1;
10360  p_config->ui8enableSSIM = 0;
10361  p_config->ui8hdr10_enable = 0;
10362  p_config->ui16hdr10_dx0 = 0;
10363  p_config->ui16hdr10_dy0 = 0;
10364  p_config->ui16hdr10_dx1 = 0;
10365  p_config->ui16hdr10_dy1 = 0;
10366  p_config->ui16hdr10_dx2 = 0;
10367  p_config->ui16hdr10_dy2 = 0;
10368  p_config->ui16hdr10_wx = 0;
10369  p_config->ui16hdr10_wy = 0;
10370  p_config->ui32hdr10_maxluma = 0;
10371  p_config->ui32hdr10_minluma = 0;
10372  p_config->ui8avccHvcc = 0;
10373  p_config->ui8av1ErrResilientMode = 0;
10374  p_config->ui8intraResetRefresh = 0;
10375  p_config->ui8temporalLayersEnable = 0;
10376  p_config->ui8enable2PassGopPatern = 0;
10377  p_config->i8crfMax = (int8_t)(-1) + 1; // for old libxcoder backward compatibility, use 1 to represent 0
10378  p_config->i32qcomp = (int32_t)(0.6 * 1000) + 1000; // for old libxcoder backward compatibility, use 1000 to represent 0
10379  p_config->ui8noMbtree = 0;
10380  p_config->ui8noHWMultiPassSupport = 0;
10381  p_config->i8cuTreeFactor = 5;
10382  p_config->i32ipRatio = (int32_t)(1.4 * 1000) + 1000; // for old libxcoder backward compatibility, use 1000 to represent 0
10383  p_config->i32pbRatio = (int32_t)(1.3 * 1000) + 1000; // for old libxcoder backward compatibility, use 1000 to represent 0
10384  p_config->i32cplxDecay = (int32_t)(0.5 * 1000) + 1000; // for old libxcoder backward compatibility, use 1000 to represent 0
10385  p_config->ui32vbvMaxRate = 0;
10386  p_config->i8ppsInitQp = (int8_t)(-1) + 1; // for old libxcoder backward compatibility, use 1 to represent 0
10387  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)
10388  p_config->i8pass1Qp = (int8_t)(-1) + 1; // for old libxcoder backward compatibility, use 1 to represent 0
10389  p_config->i8crfDecimal = 0;
10390  p_config->i8hvsBaseMbComplexity = (int8_t)(15)-15; // for old libxcoder backward compatibility, use -15 to represent 0
10391  p_config->i8statisticOutputLevel = 0;
10392  p_config->i8enableipRatio = 0;
10393  p_config->ui16iFrameSizeRatio = 100;
10394  p_config->ui8compressor = 3;
10395  p_config->u8skipFrameInterval = 0;
10396  p_config->ui8crfMaxIframeEnable = 0;
10397  p_config->ui32vbvMinRate = 0;
10398  p_config->ui8disableBframeRDOQ = 0;
10399  p_config->i32forceBframeQpFactor = (int32_t)(-1.0 * 1000) + 1000; // for old libxcoder backward compatibility, use 1000 to represent 0
10400  p_config->ui8tuneBframeVisual = 0;
10401  p_config->ui8EnableAcqLimit = 0;
10403  p_config->ui32cropWidth = 0;
10404  p_config->ui32cropHeight = 0;
10405  p_config->ui32horOffset = 0;
10406  p_config->ui32verOffset = 0;
10407  p_config->ui8motionConstrainedMode = 0;
10408  p_config->ui8stillImageDetectLevel = 0;
10409  p_config->ui8sceneChangeDetectLevel = 0;
10410  p_config->ui8mallocStrategy = 0;
10411  p_config->ui8spatialLayersMinusOne = 0;
10412  p_config->ui8enableTimecode = 0;
10413  p_config->ui8spatialLayersRefBaseLayer = 0;
10414  p_config->ui8vbvBufferReencode = 0;
10415  p_config->ui8totalCuTreeDepth = 0;
10416  p_config->ui8adaptiveCuTree = 0;
10417  p_config->ui8preIntraHandling = (uint8_t)(1)-1; // for old libxcoder backward compatibility, use -1 to represent 0
10418  p_config->ui8baseLayerOnly = 0;
10419  p_config->ui8pastFrameMaxIntraRatio = (uint8_t)(20)-20; // for old libxcoder backward compatibility, use -20 to represent 0
10420  p_config->ui8linkFrameMaxIntraRatio = (uint8_t)(40)-40; // for old libxcoder backward compatibility, use -40 to represent 0
10421  for (i = 0; i < NI_MAX_SPATIAL_LAYERS; i++)
10422  {
10423  p_config->i32spatialLayerBitrate[i] = 0;
10424  p_config->ui8av1OpLevel[i] = 0;
10425  }
10426  p_config->ui8disableAv1TimingInfo = 0;
10427  p_config->ui8adaptiveLamdaMode = 0;
10428  p_config->ui8getCuInfo = 0;
10429  p_config->ui8adaptiveCrfMode = 0;
10430 }
10431 
10432 /*!******************************************************************************
10433 * \brief Perform validation on custom dec parameters (Rev. B)
10434 *
10435 * \param
10436 *
10437 * \return
10438 ******************************************************************************/
10440  ni_session_context_t *p_ctx,
10441  ni_decoder_config_t *p_cfg,
10442  char *p_param_err,
10443  uint32_t max_err_len)
10444 {
10445  ni_retcode_t param_ret = NI_RETCODE_SUCCESS;
10446  ni_retcode_t warning = NI_RETCODE_SUCCESS;
10447  int w = p_src->source_width;
10448  int h = p_src->source_height;
10449  char *p_param_warn = NULL;
10450  int i;
10451 
10452  if (!p_ctx || !p_cfg || !p_param_err)
10453  {
10454  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Null pointer parameters passed\n",
10455  __func__);
10456  param_ret = NI_RETCODE_INVALID_PARAM;
10457  LRETURN;
10458  }
10459 
10460  // Zero out the error buffer
10461  p_param_warn = malloc(sizeof(p_ctx->param_err_msg));
10462  if (!p_param_warn)
10463  {
10464  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() p_param_warn malloc failure\n",
10465  NI_ERRNO, __func__);
10466  param_ret = NI_RETCODE_ERROR_MEM_ALOC;
10467  LRETURN;
10468  }
10469 
10470  memset(p_param_err, 0, max_err_len);
10471  memset(p_param_warn, 0, max_err_len);
10472 
10473 
10474  //if (p_cfg->i32bitRate > NI_MAX_BITRATE)
10475  //{
10476  // ni_strncpy(p_param_err, max_err_len, "Invalid i32bitRate: too big", max_err_len - 1);
10477  // param_ret = NI_RETCODE_PARAM_ERROR_BRATE;
10478  // LRETURN;
10479  //}
10480 
10481  if (p_cfg->ui8HWFrame == 0 &&
10482  (p_cfg->asOutputConfig[1].ui8Enabled || p_cfg->asOutputConfig[2].ui8Enabled))
10483  {
10484  ni_strncpy(p_param_err, max_err_len, "Incompatible output format: hw frame must be used if out1 or out2 used", max_err_len - 1);
10485  param_ret = NI_RETCODE_INVALID_PARAM;
10486  LRETURN;
10487  }
10488 
10489  if (p_src->ddr_priority_mode >= 0)
10490  {
10492  "6e") < 0)
10493  {
10494  ni_strncpy(p_param_err, max_err_len, "ddr_priority_mode not supported on device with FW api version < 6.e",
10495  max_err_len - 1);
10497  LRETURN;
10498  }
10499  }
10500 
10502  {
10503  ni_strncpy(p_param_err, max_err_len, "LowDelay is not supported on jpeg/vp9 decoder",
10504  max_err_len - 1);
10506  LRETURN;
10507  }
10508 
10509  for (i = 0; i < NI_MAX_NUM_OF_DECODER_OUTPUTS; i++)
10510  {
10511  //checking cropping param
10513  {
10516  {
10517  ni_strncpy(p_param_err, max_err_len, "Invalid crop offset: extends past 48x48 minimum window", max_err_len - 1);
10518  param_ret = NI_RETCODE_PARAM_ERROR_OOR;
10519  LRETURN;
10520  }
10523  {
10524  ni_strncpy(p_param_err, max_err_len, "Invalid crop w or h: must be at least 48x48 minimum window", max_err_len - 1);
10526  LRETURN;
10527  }
10528  if (p_cfg->asOutputConfig[i].sCroppingRectable.ui16W > w ||
10529  p_cfg->asOutputConfig[i].sCroppingRectable.ui16H > h)
10530  {
10531  ni_strncpy(p_param_err, max_err_len, "Invalid crop w or h: must be smaller than input", max_err_len - 1);
10532  param_ret = NI_RETCODE_PARAM_ERROR_OOR;
10533  LRETURN;
10534  }
10537  {
10538  ni_strncpy(p_param_err, max_err_len, "Invalid crop rect: must fit in input", max_err_len - 1);
10539  param_ret = NI_RETCODE_PARAM_ERROR_OOR;
10540  LRETURN;
10541  }
10542  if (p_cfg->asOutputConfig[i].sCroppingRectable.ui16X & 1 ||
10543  p_cfg->asOutputConfig[i].sCroppingRectable.ui16Y & 1 ||
10544  p_cfg->asOutputConfig[i].sCroppingRectable.ui16W & 1 ||
10545  p_cfg->asOutputConfig[i].sCroppingRectable.ui16H & 1)
10546  {
10547  ni_strncpy(p_param_err, max_err_len, "Invalid crop value: even values only", max_err_len - 1);
10548  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
10549  LRETURN;
10550  }
10551  if (p_cfg->asOutputConfig[i].ui8Enabled == 0)
10552  {
10553  ni_strncpy(p_param_warn, max_err_len, "crop param used but output not enabled!", max_err_len - 1);
10554  warning = NI_RETCODE_PARAM_WARN;
10555  }
10556 
10557  }
10558 
10559  //checking scaling param
10560  if (p_cfg->asOutputConfig[i].ui8ScaleEnabled)
10561  {
10562  if (p_cfg->asOutputConfig[i].sOutputPictureSize.ui16Height == 0 ||
10564  {
10565  if (p_cfg->asOutputConfig[i].ui8EnablePpuScaleAdapt)
10566  {
10567  //may need to revisit if based on cropped input or base input for w/h limit
10568  ni_strncpy(p_param_err, max_err_len, "Invalid scale dimensions: zero", max_err_len - 1);
10570  LRETURN;
10571  }
10572  }
10573  if (p_cfg->asOutputConfig[i].sOutputPictureSize.ui16Height & 1 ||
10575  {
10576  ni_strncpy(p_param_err, max_err_len, "Invalid scale value: even values only", max_err_len - 1);
10577  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
10578  LRETURN;
10579  }
10580 
10581  if (p_cfg->asOutputConfig[i].ui8EnablePpuScaleLimit == 1)
10582  {
10583  if (p_cfg->asOutputConfig[i].sOutputPictureSize.ui16Width > w ||
10585  {
10586  ni_strncpy(p_param_err, max_err_len, "Invalid scale value: downscale only", max_err_len - 1);
10587  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
10588  LRETURN;
10589  }
10590  }
10591 
10593  {
10594  //reject if scale dimensions exceed crop dimensions
10597  {
10598  ni_strncpy(p_param_err, max_err_len, "Invalid scale dimensions: downscale only after cropping", max_err_len - 1);
10599  param_ret = NI_RETCODE_PARAM_ERROR_OOR;
10600  LRETURN;
10601  }
10602  }
10603  if (p_cfg->asOutputConfig[i].ui8Enabled == 0)
10604  {
10605  ni_strncpy(p_param_warn, max_err_len, "scale param used but output not enabled!", max_err_len - 1);
10606  warning = NI_RETCODE_PARAM_WARN;
10607  }
10608  }
10609  if (p_cfg->asOutputConfig[i].ui8Enabled == 0)
10610  {
10612  {
10613  ni_strncpy(p_param_warn, max_err_len, "force8bit or semiPlanar used but output not enabled!", max_err_len - 1);
10614  warning = NI_RETCODE_PARAM_WARN;
10615  }
10616  }
10617 
10618  //check tiled format compatibility
10620  {
10621  if (p_cfg->asOutputConfig[i].ui8Enabled == 0)
10622  {
10623  continue;
10624  }
10625  if (p_cfg->ui8HWFrame == 0)
10626  {
10627  ni_strncpy(p_param_err, max_err_len,
10628  "Invalid pairing: out=HW must be set with tiled format",
10629  max_err_len - 1);
10630  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
10631  LRETURN;
10632  }
10633  if (p_ctx->codec_format == NI_CODEC_FORMAT_VP9)
10634  {
10635  ni_strncpy(p_param_err, max_err_len, "Invalid pairing: VP9 not compatible with tiled format",
10636  max_err_len - 1);
10637  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
10638  LRETURN;
10639  }
10640  if (p_cfg->asOutputConfig[i].ui8ScaleEnabled)
10641  {
10642  //only no need to check crop compat if scale is set
10643  if (p_cfg->asOutputConfig[i].sOutputPictureSize.ui16Height % 4 ||
10645  {
10646  ni_strncpy(p_param_err, max_err_len,
10647  "Invalid scale height: mult of 4 only, >= 128",
10648  max_err_len - 1);
10649  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
10650  LRETURN;
10651  }
10652  if (p_cfg->asOutputConfig[i].sOutputPictureSize.ui16Width % 4 ||
10654  {
10655  //minimum supported dec is 128 but min enc is 144 so round up
10656  ni_strncpy(p_param_err, max_err_len,
10657  "Invalid scale width: mult of 128 only, >= 144",
10658  max_err_len - 1);
10659  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
10660  LRETURN;
10661  }
10662  }
10663  else if (p_cfg->asOutputConfig[i].ui8CropMode == NI_DEC_CROP_MODE_MANUAL)
10664  {
10665  if (p_cfg->asOutputConfig[i].sCroppingRectable.ui16H % 4 ||
10666  p_cfg->asOutputConfig[i].sCroppingRectable.ui16H < 128)
10667  {
10668  ni_strncpy(p_param_err, max_err_len,
10669  "Invalid crop height: mult of 4 only, >= 128",
10670  max_err_len - 1);
10671  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
10672  LRETURN;
10673  }
10674  if (p_cfg->asOutputConfig[i].sCroppingRectable.ui16W % 4 ||
10676  {
10677  //minimum supported dec is 128 but min enc is 144 so round up
10678  ni_strncpy(p_param_err, max_err_len,
10679  "Invalid crop width: mult of 128 only, >= 144",
10680  max_err_len - 1);
10681  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
10682  LRETURN;
10683  }
10684  }
10685  if (p_cfg->asOutputConfig[i].ui8Force8Bit)
10686  {
10687  ni_strncpy(p_param_err, max_err_len, "Force 8 bit: not supported with tiled format\n", max_err_len - 1);
10688  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
10689  LRETURN;
10690  }
10691  }
10692 
10693  }//end forloop
10694  if (warning == NI_RETCODE_PARAM_WARN && param_ret == NI_RETCODE_SUCCESS)
10695  {
10696  param_ret = NI_RETCODE_PARAM_WARN;
10697  ni_strncpy(p_param_err, max_err_len, p_param_warn, max_err_len - 1);
10698  }
10699 
10700 END:
10701 
10702  free(p_param_warn);
10703  return param_ret;
10704 }
10705 
10706 // Check encoder level parameters
10707 static ni_retcode_t ni_check_level(int level, int codec_id)
10708 {
10709  const int l_levels_264[] = {10, 11, 12, 13, 20, 21, 22, 30, 31, 32,
10710  40, 41, 42, 50, 51, 52, 60, 61, 62, 0};
10711  const int l_levels_265[] = {10, 20, 21, 30, 31, 40, 41,
10712  50, 51, 52, 60, 61, 62, 0};
10713  const int l_levels_av1[] = {20, 21, 30, 31, 40, 41, 50, 51, 52, 53, 60, 61, 62, 63, 0};
10714  const int *l_levels = l_levels_264;
10715 
10716  if (level == 0)
10717  {
10718  return NI_RETCODE_SUCCESS;
10719  }
10720 
10721  if (codec_id == NI_CODEC_FORMAT_H265)
10722  {
10723  l_levels = l_levels_265;
10724  } else if (codec_id == NI_CODEC_FORMAT_AV1)
10725  {
10726  l_levels = l_levels_av1;
10727  }
10728 
10729  while (*l_levels != 0)
10730  {
10731  if (*l_levels == level)
10732  {
10733  return NI_RETCODE_SUCCESS;
10734  }
10735  l_levels++;
10736  }
10737 
10738  return NI_RETCODE_FAILURE;
10739 }
10740 
10741 /*!******************************************************************************
10742  * \brief Perform validation on custom parameters (Rev. B)
10743  *
10744  * \param
10745  *
10746  * \return
10747  ******************************************************************************/
10749  ni_encoder_config_t *p_cfg,
10750  ni_xcoder_params_t *p_src,
10751  char *p_param_err,
10752  uint32_t max_err_len)
10753 {
10754  ni_retcode_t param_ret = NI_RETCODE_SUCCESS;
10755  ni_retcode_t warning = NI_RETCODE_SUCCESS;
10756  char* p_param_warn = malloc(sizeof(p_ctx->param_err_msg));
10757  ni_encoder_cfg_params_t *p_enc;
10758  int i;
10759 
10760  if( (!p_ctx) || (!p_cfg) || (!p_src) || (!p_param_err) )
10761  {
10762  ni_log(NI_LOG_ERROR, "ERROR: %s() Null pointer parameters passed\n",
10763  __func__);
10764  param_ret = NI_RETCODE_INVALID_PARAM;
10765  LRETURN;
10766  }
10767 
10768  //Zero out the error buffer
10769  p_enc = &p_src->cfg_enc_params;
10770  memset(p_param_err, 0, max_err_len);
10771  memset(p_param_warn, 0, max_err_len);
10772 
10773  if (0 == p_cfg->i32frameRateInfo)
10774  {
10775  ni_strncpy(p_param_err, max_err_len, "Invalid frame_rate of 0 value", max_err_len - 1);
10776  param_ret = NI_RETCODE_PARAM_ERROR_FRATE;
10777  LRETURN;
10778  }
10779 
10780  if (((p_cfg->i32frameRateInfo + p_cfg->i32frameRateDenominator - 1) /
10782  {
10783  ni_strncpy(p_param_err, max_err_len, "Invalid i32frameRateInfo: too big", max_err_len - 1);
10784  param_ret = NI_RETCODE_PARAM_ERROR_FRATE;
10785  LRETURN;
10786  }
10787 
10788  if (p_cfg->i32bitRate <= p_cfg->i32frameRateInfo)
10789  {
10790  ni_strncpy(p_param_err, max_err_len, "Invalid i32bitRate: smaller than or equal to frame rate", max_err_len - 1);
10791  param_ret = NI_RETCODE_PARAM_ERROR_BRATE;
10792  LRETURN;
10793  }
10794 
10795  if (p_cfg->i32bitRate > NI_MAX_BITRATE)
10796  {
10797  ni_strncpy(p_param_err, max_err_len, "Invalid i32bitRate: too big", max_err_len - 1);
10798  param_ret = NI_RETCODE_PARAM_ERROR_BRATE;
10799  LRETURN;
10800  }
10801 
10802  if (p_cfg->i32bitRate < NI_MIN_BITRATE )
10803  {
10804  ni_strncpy(p_param_err, max_err_len, "Invalid i32bitRate: too low", max_err_len - 1);
10805  param_ret = NI_RETCODE_PARAM_ERROR_BRATE;
10806  LRETURN;
10807  }
10808 
10810  {
10811  ni_strncpy(p_param_err, max_err_len, "Invalid Picture Width: too small", max_err_len - 1);
10813  LRETURN;
10814  }
10815 
10817  {
10818  ni_strncpy(p_param_err, max_err_len, "Invalid Picture Width: too big", max_err_len - 1);
10820  LRETURN;
10821  }
10822 
10824  {
10825  ni_strncpy(p_param_err, max_err_len, "Invalid Picture Height: too small", max_err_len - 1);
10827  LRETURN;
10828  }
10829 
10831  {
10832  ni_strncpy(p_param_err, max_err_len, "Invalid Picture Height: too big", max_err_len - 1);
10834  LRETURN;
10835  }
10836 
10837  if (p_cfg->ui32cropWidth || p_cfg->ui32cropHeight)
10838  {
10839  // check cropping width & height are both non-zero
10840  if (!p_cfg->ui32cropWidth || !p_cfg->ui32cropHeight)
10841  {
10842  snprintf(p_param_err, max_err_len, "Invalid Crop Width x Height (%u x %u): both need to be specified",
10843  p_cfg->ui32cropWidth, p_cfg->ui32cropHeight);
10844  param_ret = NI_RETCODE_INVALID_PARAM;
10845  LRETURN;
10846  }
10847  // check cropping width & height are even
10848  if ((p_cfg->ui32cropWidth % 2) || (p_cfg->ui32cropHeight % 2))
10849  {
10850  snprintf(p_param_err, max_err_len, "Invalid Crop Width x Height (%u x %uu): must be even",
10851  p_cfg->ui32cropWidth, p_cfg->ui32cropHeight);
10852  param_ret = NI_RETCODE_INVALID_PARAM;
10853  LRETURN;
10854  }
10855  // check cropping width & height meet resoultion constraint (including AV1 max resoultion)
10856  if (p_cfg->ui32cropWidth < NI_MIN_WIDTH)
10857  {
10858  snprintf(p_param_err, max_err_len, "Invalid Crop Width: less than %d",
10859  NI_MIN_WIDTH);
10861  LRETURN;
10862  }
10863  if (p_cfg->ui32cropHeight < NI_MIN_HEIGHT)
10864  {
10865  snprintf(p_param_err, max_err_len, "Invalid Crop Height: less than %d",
10866  NI_MIN_HEIGHT);
10868  LRETURN;
10869  }
10870  if (p_cfg->ui32cropWidth > NI_PARAM_MAX_WIDTH)
10871  {
10872  snprintf(p_param_err, max_err_len, "Invalid Crop Width: exceeds %d",
10875  LRETURN;
10876  }
10877  if (p_cfg->ui32cropHeight > NI_PARAM_MAX_HEIGHT)
10878  {
10879  snprintf(p_param_err, max_err_len, "Invalid Crop Height: exceeds %d",
10882  LRETURN;
10883  }
10884  if (p_cfg->ui32cropWidth * p_cfg->ui32cropHeight > NI_MAX_RESOLUTION_AREA)
10885  {
10886  snprintf(p_param_err, max_err_len, "Invalid Crop Width x Height: exceeds %d",
10889  LRETURN;
10890  }
10891 
10892  if (NI_CODEC_FORMAT_AV1 == p_ctx->codec_format)
10893  {
10895  {
10896  snprintf(p_param_err, max_err_len, "Invalid Crop Width: exceeds %d",
10899  LRETURN;
10900  }
10902  {
10903  snprintf(p_param_err, max_err_len, "Invalid Crop Height: exceeds %d",
10906  LRETURN;
10907  }
10908  if (p_cfg->ui32cropWidth * p_cfg->ui32cropHeight > NI_PARAM_AV1_MAX_AREA)
10909  {
10910  snprintf(p_param_err, max_err_len, "Invalid Crop Width x Height: exceeds %d",
10913  LRETURN;
10914  }
10915  }
10916 
10917  // check cropping horOffset & verOffset are even
10918  if ((p_cfg->ui32horOffset % 2) || (p_cfg->ui32verOffset % 2))
10919  {
10920  snprintf(p_param_err, max_err_len, "Invalid Crop horOffset %u and/or verOffset %u: must be even",
10921  p_cfg->ui32horOffset, p_cfg->ui32verOffset);
10922  param_ret = NI_RETCODE_INVALID_PARAM;
10923  LRETURN;
10924  }
10925 
10926  if (p_src->source_width < (int)(p_cfg->ui32horOffset + p_cfg->ui32cropWidth))
10927  {
10928  snprintf(p_param_err, max_err_len, "Invalid Crop Width: offset %u + crop width %u too big > %d",
10929  p_cfg->ui32horOffset, p_cfg->ui32cropWidth, p_src->source_width);
10931  LRETURN;
10932  }
10933  if (p_src->source_height < (int)(p_cfg->ui32verOffset + p_cfg->ui32cropHeight))
10934  {
10935  snprintf(p_param_err, max_err_len, "Invalid Crop Height: offset %u + crop height %u too big > %d",
10936  p_cfg->ui32verOffset, p_cfg->ui32cropHeight, p_src->source_height);
10938  LRETURN;
10939  }
10940  if (p_enc->roi_enable)
10941  {
10942  snprintf(p_param_err, max_err_len, "roiEnable must disabled when Crop Width > 0 || Crop Height > 0");
10943  param_ret = NI_RETCODE_INVALID_PARAM;
10944  LRETURN;
10945  }
10946  }
10947  else // check source width / height meet AV1 max resoultion constraint if cropping not specified
10948  {
10949  if (NI_CODEC_FORMAT_AV1 == p_ctx->codec_format)
10950  {
10951  if (p_src->source_width > NI_PARAM_AV1_MAX_WIDTH)
10952  {
10953  snprintf(p_param_err, max_err_len, "Invalid Picture Width: exceeds %d",
10956  LRETURN;
10957  }
10959  {
10960  snprintf(p_param_err, max_err_len, "Invalid Picture Height: exceeds %d",
10963  LRETURN;
10964  }
10965  if (p_src->source_width * p_src->source_height > NI_PARAM_AV1_MAX_AREA)
10966  {
10967  snprintf(p_param_err, max_err_len, "Invalid Picture Width x Height: exceeds %d",
10970  LRETURN;
10971  }
10972  }
10973  }
10974 
10975  if (p_cfg->ui8planarFormat >= NI_PIXEL_PLANAR_MAX)
10976  {
10977  ni_strncpy(p_param_err, max_err_len, "Invalid input planar format: out of range", max_err_len - 1);
10978  param_ret = NI_RETCODE_PARAM_ERROR_OOR;
10979  LRETURN;
10980  }
10982  {
10984  "68") < 0)
10985  {
10986  ni_strncpy(p_param_err, max_err_len, "Invalid input planar format for device with FW api version < 6.8",
10987  max_err_len - 1);
10989  LRETURN;
10990  }
10991  if (p_ctx->auto_dl_handle)
10992  {
10993  ni_strncpy(p_param_err, max_err_len, "Invalid Encoder Selected: Tiled format must be on same device",
10994  max_err_len - 1);
10995  param_ret = NI_RETCODE_ERROR_INVALID_HANDLE;
10996  LRETURN;
10997  }
10998  if (p_src->source_height % 4 != 0)
10999  {
11000  ni_strncpy(p_param_err, max_err_len,
11001  "Invalid Picture Height: tiled format only supports "
11002  "multiples of 4",
11003  max_err_len - 1);
11005  LRETURN;
11006  }
11007  if (p_src->source_width % 4 != 0)
11008  {
11009  ni_strncpy(p_param_err, max_err_len,
11010  "Invalid Picture Width: tiled format only supports "
11011  "multiples of 4",
11012  max_err_len - 1);
11014  LRETURN;
11015  }
11016  }
11017 
11018  if (p_cfg->ui8PixelFormat == NI_PIX_FMT_RGBA ||
11019  p_cfg->ui8PixelFormat == NI_PIX_FMT_BGRA ||
11020  p_cfg->ui8PixelFormat == NI_PIX_FMT_ABGR ||
11021  p_cfg->ui8PixelFormat == NI_PIX_FMT_ARGB)
11022  {
11023  if (p_src->zerocopy_mode == 0)
11024  {
11025  ni_strncpy(p_param_err, max_err_len, "zeroCopyMode must not be disabled for RGBA / BGRA / ABGR / ARGB pixel formats", max_err_len - 1);
11026  param_ret = NI_RETCODE_INVALID_PARAM;
11027  LRETURN;
11028  }
11029  if (p_cfg->ui8PixelFormat == NI_PIX_FMT_RGBA ||
11030  p_cfg->ui8PixelFormat == NI_PIX_FMT_BGRA)
11031  {
11033  "6Y") < 0)
11034  {
11035  ni_strncpy(p_param_err, max_err_len, "RGBA / BGRA pixel formats not supported on device with FW api version < 6.Y",
11036  max_err_len - 1);
11038  LRETURN;
11039  }
11040  }
11041  }
11042  else if (p_cfg->ui8PixelFormat != NI_PIX_FMT_YUV420P &&
11044  p_cfg->ui8PixelFormat != NI_PIX_FMT_NV12 &&
11045  p_cfg->ui8PixelFormat != NI_PIX_FMT_P010LE &&
11048  {
11049  snprintf(p_param_err, max_err_len, "Unsupported pixel format %d in encoder", p_cfg->ui8PixelFormat);
11050  param_ret = NI_RETCODE_INVALID_PARAM;
11051  LRETURN;
11052  }
11053 
11054  if (p_src->ddr_priority_mode >= 0)
11055  {
11057  "6e") < 0)
11058  {
11059  ni_strncpy(p_param_err, max_err_len, "ddr_priority_mode not supported on device with FW api version < 6.e",
11060  max_err_len - 1);
11062  LRETURN;
11063  }
11064  }
11065 
11066  if (NI_RETCODE_SUCCESS !=
11067  ni_check_level(p_src->cfg_enc_params.level_idc, p_ctx->codec_format))
11068  {
11069  ni_strncpy(p_param_err, max_err_len, "Invalid Encoder Level: out of range", max_err_len - 1);
11070  param_ret = NI_RETCODE_PARAM_ERROR_OOR;
11071  LRETURN;
11072  }
11073 
11074  if ((p_src->cfg_enc_params.intra_mb_refresh_mode < 0 ||
11075  p_src->cfg_enc_params.intra_mb_refresh_mode > 4) ||
11076  (NI_CODEC_FORMAT_H264 == p_ctx->codec_format &&
11078  {
11079  ni_strncpy(p_param_err, max_err_len, "Invalid intra_mb_refresh_mode: out of range",
11080  max_err_len - 1);
11081  param_ret = NI_RETCODE_PARAM_ERROR_OOR;
11082  LRETURN;
11083  }
11084 
11085  if (!QUADRA)
11086  {
11088  {
11090  {
11091  ni_strncpy(p_param_err, max_err_len, "Invalid custom GOP paramaters: custom_gop_size too small", max_err_len - 1);
11093  LRETURN;
11094  }
11097  {
11098  ni_strncpy(p_param_err, max_err_len, "Invalid custom GOP paramaters: custom_gop_size too big", max_err_len - 1);
11100  LRETURN;
11101  }
11102  }
11103  }
11104  else // QUADRA
11105  {
11107  {
11109  {
11110  ni_strncpy(p_param_err, max_err_len,
11111  "Invalid custom GOP paramaters: custom gop size must > 0",
11112  max_err_len - 1);
11114  LRETURN;
11115  }
11116  }
11118  {
11121  {
11122  ni_strncpy(p_param_err, max_err_len,
11123  "Invalid custom GOP paramaters: selected gopPresetIdx is "
11124  "not compatible with custom gop",
11125  max_err_len - 1);
11127  LRETURN;
11128  }
11130  {
11131  ni_strncpy(p_param_err, max_err_len, "Invalid custom GOP paramaters: custom_gop_size too small", max_err_len - 1);
11133  LRETURN;
11134  }
11137  {
11138  ni_strncpy(p_param_err, max_err_len, "Invalid custom GOP paramaters: custom_gop_size too big", max_err_len - 1);
11140  LRETURN;
11141  }
11142 
11143  for (i = 0; i < p_cfg->niParamT408.custom_gop_params.custom_gop_size; i++)
11144  {
11147  {
11148  ni_strncpy(p_param_err, max_err_len, "Invalid custom gop parameters: poc_offset larger"
11149  " than GOP size", max_err_len - 1);
11151  LRETURN;
11152  }
11153  }
11154  }
11155 
11156  if(p_cfg->ui8useLowDelayPocType != 0)
11157  {
11158  if (NI_CODEC_FORMAT_H264 != p_ctx->codec_format)
11159  {
11160  p_cfg->ui8useLowDelayPocType = 0;
11161  ni_strncpy(p_param_warn, max_err_len, "useLowDelayPocType is only supported for H.264. Change useLowDelayPocType to 0", max_err_len - 1);
11162  warning = NI_RETCODE_PARAM_WARN;
11163  }
11164  }
11165  if(p_cfg->niParamT408.entropy_coding_mode != 1)
11166  {
11167  if (NI_CODEC_FORMAT_H264 != p_ctx->codec_format)
11168  {
11169  ni_strncpy(p_param_err, max_err_len,
11170  "entropyCodingMode is only supported for H.264.",
11171  max_err_len - 1);
11173  LRETURN;
11174  }
11175  }
11176  if (p_cfg->ui8av1ErrResilientMode)
11177  {
11178  if (NI_CODEC_FORMAT_AV1 != p_ctx->codec_format)
11179  {
11180  ni_strncpy(p_param_err, max_err_len,
11181  "av1ErrorResilientMode is only supported for AV1.",
11182  max_err_len - 1);
11184  LRETURN;
11185  }
11186  }
11187  if (0 != p_src->enable_ai_enhance)
11188  {
11189  if (NI_CODEC_FORMAT_JPEG == p_ctx->codec_format)
11190  {
11191  snprintf(p_param_err, max_err_len, "%s is not supported for JPEG.",
11192  ((p_src->enable_ai_enhance == 2) ? "enableHVSPlus" : "enableAIEnhance"));
11194  LRETURN;
11195  }
11196  }
11197  if (-1 != p_cfg->ui32QLevel)
11198  {
11199  if (NI_CODEC_FORMAT_JPEG != p_ctx->codec_format)
11200  {
11201  ni_strncpy(p_param_err, max_err_len, "qLevel is only supported for JPEG.",
11202  max_err_len - 1);
11204  LRETURN;
11205  }
11206  }
11207 
11208  if(p_cfg->ui8enableSSIM != 0)
11209  {
11211  "62") < 0 || NI_CODEC_FORMAT_AV1 == p_ctx->codec_format)
11212  {
11213  p_cfg->ui8enableSSIM = 0;
11214  ni_strncpy(p_param_warn, max_err_len, "enableSSIM only supported on device with FW api version < 6.2 "
11215  "and the encoder is not av1_ni_quadra_enc. Reported ssim will be 0.", max_err_len - 1);
11216  warning = NI_RETCODE_PARAM_WARN;
11217  }
11218  }
11219 
11220  if (p_cfg->niParamT408.slice_mode || p_cfg->niParamT408.slice_arg)
11221  {
11223  {
11224  ni_strncpy(p_param_err, max_err_len, "sliceMode/sliceArg is only supported for H.264 or H.265.",
11225  max_err_len - 1);
11227  LRETURN;
11228  }
11229 
11230  if (p_cfg->niParamT408.slice_mode)
11231  {
11232  int ctu_mb_size = (NI_CODEC_FORMAT_H264 == p_ctx->codec_format) ? 16 : 64;
11233  int max_num_ctu_mb_row = (p_src->source_height + ctu_mb_size - 1) / ctu_mb_size;
11234  if (p_cfg->niParamT408.slice_arg < 1 || p_cfg->niParamT408.slice_arg > max_num_ctu_mb_row ||
11236  {
11237  snprintf(p_param_err, max_err_len, "Invalid number of rows per slice: should be between 1 and %d",
11238  max_num_ctu_mb_row < NI_MAX_SLICE_SIZE ? max_num_ctu_mb_row : NI_MAX_SLICE_SIZE);
11239  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
11240  LRETURN;
11241  }
11242  }
11243  }
11244  if (p_src->use_low_delay_poc_type)
11245  {
11246  if (NI_CODEC_FORMAT_H264 != p_ctx->codec_format)
11247  {
11248  ni_strncpy(p_param_err, max_err_len, "useLowDelayPocType is only supported for H.264.",
11249  max_err_len - 1);
11251  LRETURN;
11252  }
11253  }
11254 
11255  if(p_cfg->ui8enableCompensateQp != 0)
11256  {
11257  if (ni_cmp_fw_api_ver((char*) &p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6ru") < 0 ||
11258  NI_CODEC_FORMAT_AV1 == p_ctx->codec_format || p_cfg->ui16maxFrameSize == 0)
11259  {
11260  p_cfg->ui8enableCompensateQp = 0;
11261  ni_strncpy(p_param_warn, max_err_len, "ui8enableCompensateQp only supported when device with FW api version < 6rt "
11262  "and the encoder is not av1_ni_quadra_enc and ui16maxFrameSize > 0. Reported enableCompensateQp will be 0.", max_err_len - 1);
11263  warning = NI_RETCODE_PARAM_WARN;
11264  }
11265  }
11266 
11267  if (p_cfg->ui8enableTimecode != 0)
11268  {
11269  if (NI_CODEC_FORMAT_H264 != p_ctx->codec_format)
11270  {
11271  p_cfg->ui8enableTimecode = 0;
11272  if (NI_CODEC_FORMAT_H265 == p_ctx->codec_format)
11273  {
11274  ni_strncpy(p_param_warn, max_err_len, "enableTimecode does not need to be set for H.265. Use ni_enc_insert_timecode API"
11275  "from libxcoder directly to insert time code SEI", max_err_len - 1);
11276  }
11277  else
11278  {
11279  ni_strncpy(p_param_warn, max_err_len, "enableTimecode not supported for the codec used. Forcing value to 0.", max_err_len - 1);
11280  }
11281  warning = NI_RETCODE_PARAM_WARN;
11282  }
11283  }
11284  }
11285 
11286  if (QUADRA)
11287  {
11288  if (p_cfg->ui8LookAheadDepth != 0 || p_cfg->i8crf >= 0)
11289  {
11290  if ((1 == p_cfg->niParamT408.gop_preset_index && p_cfg->ui8LookAheadDepth != 0) ||
11291  3 == p_cfg->niParamT408.gop_preset_index ||
11292  7 == p_cfg->niParamT408.gop_preset_index ||
11293  10 == p_cfg->niParamT408.gop_preset_index ||
11294  15 == p_cfg->niParamT408.gop_preset_index)
11295  {
11296  ni_strncpy(p_param_err, max_err_len,
11297  "this gopPreset is not supported for lookahead and/or CRF",
11298  max_err_len - 1);
11300  LRETURN;
11301  }
11302 
11304  {
11305  bool bIsgopLowdelay = true;
11306  // lookahead does not support gopLowDelay (all B-frames, encode in order) gop patterns
11307  for (i = 0; i < p_cfg->niParamT408.custom_gop_params.custom_gop_size;
11308  i++)
11309  {
11310  // check if all frames are B-frames
11311  if (2 != p_cfg->niParamT408.custom_gop_params.pic_param[i].pic_type)
11312  {
11313  bIsgopLowdelay = false;
11314  break;
11315  }
11316  // check if all frames are encoded in display order
11317  if (p_cfg->niParamT408.custom_gop_params.pic_param[i].poc_offset != (i+1))
11318  {
11319  bIsgopLowdelay = false;
11320  break;
11321  }
11322  }
11323  if (bIsgopLowdelay)
11324  {
11325  ni_strncpy(p_param_err, max_err_len, "B-frames low delay custom gop is not supported for "
11326  "lookahead and/or CRF", max_err_len - 1);
11327  param_ret = NI_RETCODE_INVALID_PARAM;
11328  LRETURN;
11329  }
11330  }
11331 
11332  if (p_enc->bitrateMode != -1)
11333  {
11334  ni_strncpy(p_param_err, max_err_len,
11335  "bitrateMode is invalid when lookahead is enabled (or in CRF mode)",
11336  max_err_len - 1);
11338  LRETURN;
11339  }
11340  }
11341 
11342  if (p_cfg->ui8totalCuTreeDepth != 0)
11343  {
11344  if (p_cfg->ui8LookAheadDepth == 0 || p_cfg->ui8LookAheadDepth == 1)
11345  {
11346  ni_strncpy(p_param_err, max_err_len,
11347  "totalCuTreeDepth is invalid without lookahead",
11348  max_err_len - 1);
11350  LRETURN;
11351  }
11352 
11353  if (p_cfg->ui8totalCuTreeDepth <= p_cfg->ui8LookAheadDepth)
11354  {
11355  ni_strncpy(p_param_warn, max_err_len, "totalCuTreeDepth does not take effects when its value <= lookahead depth", max_err_len - 1);
11356  warning = NI_RETCODE_PARAM_WARN;
11357  p_cfg->ui8totalCuTreeDepth = 0;
11358  }
11359  else
11360  {
11361  if (p_cfg->ui8multicoreJointMode != 0)
11362  {
11363  ni_strncpy(p_param_err, max_err_len,
11364  "totalCuTreeDepth is not supported in multicoreJointMode",
11365  max_err_len - 1);
11367  LRETURN;
11368  }
11369  }
11370  }
11371 
11373  {
11374  // when gop_preset_index = -1 (default), gop pattern is decided by gopSize and gopLowdelay
11375  //p_cfg->ui8gopSize = 0;
11376  //p_cfg->ui8gopLowdelay = 0;
11377  }
11378  if (1 == p_cfg->niParamT408.gop_preset_index)
11379  {
11380  p_cfg->niParamT408.intra_period = 1;
11381  p_cfg->niParamT408.avcIdrPeriod = 1;
11382  p_cfg->ui8gopSize = 1;
11383  p_cfg->ui16gdrDuration = 0;
11384  }
11385  if (2 == p_cfg->niParamT408.gop_preset_index)
11386  {
11387  ni_strncpy(p_param_err, max_err_len,
11388  "gopPresetIdx 2 is obsolete, suggest to use gopPresetIdx 9 "
11389  "instead",
11390  max_err_len - 1);
11392  LRETURN;
11393  }
11394  if (3 == p_cfg->niParamT408.gop_preset_index)
11395  {
11396  p_cfg->ui8gopSize = 1;
11397  p_cfg->ui8gopLowdelay = 1;
11398  }
11399  if (4 == p_cfg->niParamT408.gop_preset_index)
11400  {
11401  p_cfg->ui8gopSize = 2;
11402  p_cfg->ui8gopLowdelay = 0;
11403  }
11404  if (5 == p_cfg->niParamT408.gop_preset_index)
11405  {
11406  p_cfg->ui8gopSize = 4;
11407  p_cfg->ui8gopLowdelay = 0;
11408  }
11409  if (6 == p_cfg->niParamT408.gop_preset_index)
11410  {
11411  ni_strncpy(p_param_err, max_err_len,
11412  "gopPresetIdx 6 is obsolete, suggest to use gopPresetIdx 7 "
11413  "instead",
11414  max_err_len - 1);
11416  LRETURN;
11417  }
11418  if (7 == p_cfg->niParamT408.gop_preset_index)
11419  {
11420  p_cfg->ui8gopSize = 4;
11421  p_cfg->ui8gopLowdelay = 1;
11422  }
11423  if (8 == p_cfg->niParamT408.gop_preset_index)
11424  {
11425  p_cfg->ui8gopSize = 8;
11426  p_cfg->ui8gopLowdelay = 0;
11427  }
11428  if (9 == p_cfg->niParamT408.gop_preset_index)
11429  {
11430  p_cfg->ui8gopSize = 1;
11431  p_cfg->ui8gopLowdelay = 0;
11432  }
11433  if (10 == p_cfg->niParamT408.gop_preset_index)
11434  {
11435  p_cfg->ui8gopSize = 4;
11436  p_cfg->ui8gopLowdelay = 0;
11437  }
11438  if (15 == p_cfg->niParamT408.gop_preset_index)
11439  {
11440  p_cfg->ui8gopSize = 16;
11441  p_cfg->ui8gopLowdelay = 0;
11442  }
11443 
11444  if (p_cfg->ui8LookAheadDepth != 0
11445  || p_cfg->ui8bitrateMode == 1
11446  || p_cfg->ui32vbvMaxRate
11447  || p_cfg->ui32vbvMinRate
11448  || p_cfg->i8enableipRatio)
11449  {
11450  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");
11451  p_cfg->ui8NewRCEnable = 0;
11452  }
11453  else if (p_cfg->ui8NewRCEnable == 255)
11454  {
11455  if ((p_cfg->ui8gopSize > 1 || p_cfg->ui8gopSize == 0) && p_cfg->ui8gopLowdelay == 0)
11456  {
11457  ni_log2(p_ctx, NI_LOG_DEBUG, "Set newRcEnable to 0 in non low delay gop preset\n");
11458  p_cfg->ui8NewRCEnable = 0;
11459  }
11460  else if (p_cfg->i32spatialLayerBitrate[0] > 0)
11461  {
11462  ni_log2(p_ctx, NI_LOG_DEBUG, "Set newRcEnable to 0 in spatial layer separated rate control mode\n");
11463  p_cfg->ui8NewRCEnable = 0;
11464  }
11465  else if (p_cfg->niParamT408.intra_period == 1 || p_cfg->niParamT408.avcIdrPeriod == 1)
11466  {
11467  ni_log2(p_ctx, NI_LOG_DEBUG, "Set newRcEnable to 0 in intra only mode\n");
11468  p_cfg->ui8NewRCEnable = 0;
11469  }
11470  }
11471 
11472  if ((p_cfg->ui8stillImageDetectLevel != 0 || p_cfg->ui8sceneChangeDetectLevel != 0) &&
11473  (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))
11474  {
11475  ni_strncpy(p_param_warn, max_err_len, "StillImageDetect or SceneChangeDetect only support gopPresetIdx=9 with FW > 6rm\n", max_err_len - 1);
11476  warning = NI_RETCODE_PARAM_WARN;
11477  p_cfg->ui8stillImageDetectLevel = 0;
11478  p_cfg->ui8sceneChangeDetectLevel = 0;
11479  }
11480 
11481  if (p_cfg->ui8spatialLayersMinusOne > 0 || p_cfg->i32spatialLayerBitrate[0] > 0)
11482  {
11484  {
11485  ni_strncpy(p_param_err, max_err_len,
11486  "currently do not support gop preset in multi spatial layers encode or spatial layer separated rate control mode",
11487  max_err_len - 1);
11489  LRETURN;
11490  }
11491  p_cfg->ui8gopSize = p_cfg->ui8spatialLayersMinusOne + 1;
11492  p_cfg->ui8gopLowdelay = 0;
11493  }
11494 
11495  if (p_cfg->ui8spatialLayersRefBaseLayer != 0)
11496  {
11497  if (p_cfg->ui8spatialLayersMinusOne == 0)
11498  {
11499  ni_strncpy(p_param_err, max_err_len,
11500  "higher spatial layers referencing base layer is only supported in multi spatial layers encode",
11501  max_err_len - 1);
11503  LRETURN;
11504  }
11505  }
11506 
11507  for (i = 0; i < NI_MAX_SPATIAL_LAYERS; i++)
11508  {
11509  if (p_cfg->i32spatialLayerBitrate[i] == 0)
11510  {
11511  break;
11512  }
11513  }
11514  if (i > 0)
11515  {
11516  if (p_cfg->ui8spatialLayersMinusOne + 1 != i)
11517  {
11518  ni_strncpy(p_param_err, max_err_len, "number of values specified in spatialLayerBitrate must match the total number of spatial layers", max_err_len - 1);
11519  param_ret = NI_RETCODE_INVALID_PARAM;
11520  LRETURN;
11521  }
11522  if (p_cfg->ui8multicoreJointMode != 0)
11523  {
11524  if (ni_cmp_fw_api_ver(
11526  "6sD") < 0)
11527  {
11528  ni_strncpy(p_param_err, max_err_len, "spatialLayerBitrate is not supported in multicoreJointMode for FW api version < 6sD", max_err_len - 1);
11530  LRETURN;
11531  }
11532  }
11533  }
11534 
11535  for (i = 0; i < NI_MAX_SPATIAL_LAYERS; i++)
11536  {
11537  if (p_cfg->ui8av1OpLevel[i] == 0)
11538  {
11539  break;
11540  }
11541  else if (NI_RETCODE_SUCCESS !=
11542  ni_check_level((int)p_cfg->ui8av1OpLevel[i], p_ctx->codec_format))
11543  {
11544  ni_strncpy(p_param_err, max_err_len, "Invalid av1OpLevel: out of range", max_err_len - 1);
11545  param_ret = NI_RETCODE_PARAM_ERROR_OOR;
11546  LRETURN;
11547  }
11548  }
11549  if (i > 0)
11550  {
11551  if (p_cfg->niParamT408.level != 0)
11552  {
11553  ni_strncpy(p_param_err, max_err_len, "av1OpLevel and level cannot be both specified", max_err_len - 1);
11554  param_ret = NI_RETCODE_INVALID_PARAM;
11555  LRETURN;
11556  }
11557 
11558  if (p_cfg->ui8spatialLayersMinusOne + 1 != i)
11559  {
11560  ni_strncpy(p_param_err, max_err_len, "number of values specified in av1OpLevel must match the total number of spatial layers", max_err_len - 1);
11561  param_ret = NI_RETCODE_INVALID_PARAM;
11562  LRETURN;
11563  }
11564  }
11565 
11566  if (p_cfg->ui8crfMaxIframeEnable > 0)
11567  {
11568  if (p_cfg->i8crf < 0 || p_cfg->i32vbvBufferSize == 0 || p_cfg->ui8LookAheadDepth == 0)
11569  {
11570  ni_strncpy(p_param_err, max_err_len,
11571  "crfMaxIframeEnable is only supported in Capped CRF mode with lookahead enabled",
11572  max_err_len - 1);
11573  param_ret = NI_RETCODE_INVALID_PARAM;
11574  LRETURN;
11575  }
11576  }
11577  }
11578 
11579  if (NI_CODEC_FORMAT_H264 == p_ctx->codec_format)
11580  {
11581  if (!QUADRA)
11582  {
11583  if (10 == p_ctx->src_bit_depth)
11584  {
11585  if (p_cfg->niParamT408.profile != 5)
11586  {
11587  ni_strncpy(p_param_err, max_err_len, "Invalid profile: must be 5 (high10)",
11588  max_err_len - 1);
11589  param_ret = NI_RETCODE_INVALID_PARAM;
11590  LRETURN;
11591  }
11592  }
11593  else
11594  {
11595  if (p_cfg->niParamT408.profile < 1 || p_cfg->niParamT408.profile > 5)
11596  {
11597  ni_strncpy(p_param_err, max_err_len, "Invalid profile: must be 1 (baseline), 2 (main),"
11598  " 3 (extended), 4 (high), or 5 (high10)", max_err_len - 1);
11599  param_ret = NI_RETCODE_INVALID_PARAM;
11600  LRETURN;
11601  }
11602  }
11603  if (1 == p_cfg->niParamT408.profile &&
11604  ! (0 == p_cfg->niParamT408.gop_preset_index ||
11605  1 == p_cfg->niParamT408.gop_preset_index ||
11606  2 == p_cfg->niParamT408.gop_preset_index ||
11607  6 == p_cfg->niParamT408.gop_preset_index))
11608  {
11609  ni_strncpy(p_param_err, max_err_len, "Invalid gopPresetIdx for H.264 baseline profile:"
11610  " must be 1, 2, 6 or 0 (custom with no B frames)", max_err_len - 1);
11611  param_ret = NI_RETCODE_INVALID_PARAM;
11612  LRETURN;
11613  }
11614  if (1 == p_cfg->niParamT408.profile &&
11616  {
11617  for (i = 0; i < p_cfg->niParamT408.custom_gop_params.custom_gop_size;
11618  i++)
11619  {
11620  if (2 == p_cfg->niParamT408.custom_gop_params.pic_param[i].pic_type)
11621  {
11622  ni_strncpy(p_param_err, max_err_len, "H.264 baseline profile: custom GOP can not "
11623  "have B frames", max_err_len - 1);
11624  param_ret = NI_RETCODE_INVALID_PARAM;
11625  LRETURN;
11626  }
11627  }
11628  }
11629  }
11630  else // QUADRA
11631  {
11632  if (p_cfg->niParamT408.profile < 1 || p_cfg->niParamT408.profile > 5 || p_cfg->niParamT408.profile == 3)
11633  {
11634  ni_strncpy(p_param_err, max_err_len, "Invalid profile: must be 1 (baseline), 2 (main),"
11635  " 4 (high), or 5 (high10)", max_err_len - 1);
11636  param_ret = NI_RETCODE_INVALID_PARAM;
11637  LRETURN;
11638  }
11639  if (10 == p_ctx->src_bit_depth && p_cfg->niParamT408.profile != 5)
11640  {
11641  ni_strncpy(p_param_warn, max_err_len, "AVC Baseline/Main/High Profile do not support 10-bit, auto convert to 8-bit", max_err_len - 1);
11642  warning = NI_RETCODE_PARAM_WARN;
11643  }
11644  if (1 == p_cfg->niParamT408.profile)
11645  {
11647  {
11648  for (i = 0; i < p_cfg->niParamT408.custom_gop_params.custom_gop_size; i++)
11649  {
11650  if (2 == p_cfg->niParamT408.custom_gop_params.pic_param[i].pic_type)
11651  {
11652  ni_strncpy(p_param_err, max_err_len, "H.264 baseline profile: custom GOP can not "
11653  "have B frames", max_err_len - 1);
11654  param_ret = NI_RETCODE_INVALID_PARAM;
11655  LRETURN;
11656  }
11657  }
11658  } else if (((p_cfg->ui8gopSize != 1 || p_cfg->ui8gopLowdelay)) &&
11659  (p_cfg->niParamT408.intra_period != 1) &&
11661  {
11663  p_cfg->ui8gopSize != 0 ||
11664  p_cfg
11665  ->ui8gopLowdelay) // if gopSize is 0 (default / adapative gop), autoset to 1
11666  {
11667  ni_strncpy(p_param_err, max_err_len,
11668  "Must use gopPresetIdx 1,9,10 (no "
11669  "B frames) for profile 1",
11670  max_err_len - 1);
11671  param_ret = NI_RETCODE_INVALID_PARAM;
11672  LRETURN;
11673  }
11674  p_cfg->ui8gopSize = 1; //autoset to 1
11675  }
11676  }
11677  if (p_cfg->niParamT408.tier)
11678  {
11679  ni_strncpy(p_param_err, max_err_len, "Tier is not supported for H.264", max_err_len - 1);
11680  param_ret = NI_RETCODE_INVALID_PARAM;
11681  LRETURN;
11682  }
11683  if (p_cfg->ui8spatialLayersMinusOne > 0)
11684  {
11685  ni_strncpy(p_param_err, max_err_len,
11686  "spatialLayers is not supported for h.264 encode",
11687  max_err_len - 1);
11689  LRETURN;
11690  }
11691  if (p_cfg->ui8disableAv1TimingInfo != 0)
11692  {
11693  ni_strncpy(p_param_err, max_err_len, "disableAv1TimingInfo is not supported for h.264",
11694  max_err_len - 1);
11696  LRETURN;
11697  }
11698  if (p_cfg->ui8av1OpLevel[0] != 0)
11699  {
11700  ni_strncpy(p_param_err, max_err_len, "av1OpLevel is not supported for h.264",
11701  max_err_len - 1);
11703  LRETURN;
11704  }
11705  }
11706  }
11707  else if (NI_CODEC_FORMAT_H265 == p_ctx->codec_format)
11708  {
11709  if (!QUADRA)
11710  {
11711  if (10 == p_ctx->src_bit_depth)
11712  {
11713  if (p_cfg->niParamT408.profile != 2)
11714  {
11715  ni_strncpy(p_param_err, max_err_len, "Invalid profile: must be 2 (main10)",
11716  max_err_len - 1);
11717  param_ret = NI_RETCODE_INVALID_PARAM;
11718  LRETURN;
11719  }
11720  }
11721  else
11722  {
11723  if (p_cfg->niParamT408.profile < 1 || p_cfg->niParamT408.profile > 2)
11724  {
11725  ni_strncpy(p_param_err, max_err_len, "Invalid profile: must be 1 (main) or 2 (main10)",
11726  max_err_len - 1);
11727  param_ret = NI_RETCODE_INVALID_PARAM;
11728  LRETURN;
11729  }
11730  }
11731  }
11732  else // QUADRA
11733  {
11734  if (p_cfg->niParamT408.profile < 1 || p_cfg->niParamT408.profile > 2)
11735  {
11736  ni_strncpy(p_param_err, max_err_len, "Invalid profile: must be 1 (main) or 2 (main10)",
11737  max_err_len - 1);
11738  param_ret = NI_RETCODE_INVALID_PARAM;
11739  LRETURN;
11740  }
11741  if (10 == p_ctx->src_bit_depth && p_cfg->niParamT408.profile != 2)
11742  {
11743  ni_strncpy(p_param_warn, max_err_len, "HEVC Main Profile does not support 10-bit, auto convert to 8-bit", max_err_len - 1);
11744  warning = NI_RETCODE_PARAM_WARN;
11745  }
11746  if (p_cfg->ui8spatialLayersMinusOne > 0)
11747  {
11748  ni_strncpy(p_param_err, max_err_len,
11749  "spatialLayers is not supported for h.265 encode",
11750  max_err_len - 1);
11752  LRETURN;
11753  }
11754  if (p_cfg->ui8disableAv1TimingInfo != 0)
11755  {
11756  ni_strncpy(p_param_err, max_err_len, "disableAv1TimingInfo is not supported for h.265",
11757  max_err_len - 1);
11759  LRETURN;
11760  }
11761  if (p_cfg->ui8av1OpLevel[0] != 0)
11762  {
11763  ni_strncpy(p_param_err, max_err_len, "av1OpLevel is not supported for h.265",
11764  max_err_len - 1);
11766  LRETURN;
11767  }
11768  }
11769  } else if (NI_CODEC_FORMAT_AV1 == p_ctx->codec_format)
11770  {
11772  {
11773  ni_strncpy(p_param_err, max_err_len, "TuneBframeVisual MEDIUM is not supported for AV1", max_err_len - 1);
11774  param_ret = NI_RETCODE_INVALID_PARAM;
11775  LRETURN;
11776  }
11777  if (p_cfg->niParamT408.profile != 1)
11778  {
11779  ni_strncpy(p_param_err, max_err_len, "Invalid profile: must be 1 (main)",
11780  max_err_len - 1);
11781  param_ret = NI_RETCODE_INVALID_PARAM;
11782  LRETURN;
11783  }
11784  if (p_cfg->niParamT408.level) // 0 means auto level
11785  {
11786  if (p_cfg->niParamT408.level < 20)
11787  {
11788  p_cfg->niParamT408.level = 20;
11789  ni_strncpy(p_param_warn, max_err_len,
11790  "AV1 level < 2.0 is not supported, change to level 2.0",
11791  max_err_len - 1);
11792  warning = NI_RETCODE_PARAM_WARN;
11793  } else if (p_cfg->niParamT408.level > 63)
11794  {
11795  p_cfg->niParamT408.level = 63;
11796  ni_strncpy(p_param_warn, max_err_len,
11797  "AV1 level > 6.3 is not supported, change to level 6.3",
11798  max_err_len - 1);
11799  warning = NI_RETCODE_PARAM_WARN;
11800  }
11801  }
11802 
11803  if (p_cfg->niParamT408.conf_win_top != 0)
11804  {
11806  0;
11807  ni_strncpy(p_param_warn, max_err_len, "confWinTop is not supported in AV1",
11808  max_err_len - 1);
11809  warning = NI_RETCODE_PARAM_WARN;
11810  }
11811  if (p_cfg->niParamT408.conf_win_bottom != 0)
11812  {
11813  p_cfg->niParamT408.conf_win_bottom =
11814  p_src->cfg_enc_params.conf_win_bottom = 0;
11815  ni_strncpy(p_param_warn, max_err_len, "confWinBottom is not supported in AV1",
11816  max_err_len - 1);
11817  warning = NI_RETCODE_PARAM_WARN;
11818  }
11819  if (p_cfg->niParamT408.conf_win_left != 0)
11820  {
11821  p_cfg->niParamT408.conf_win_left =
11822  p_src->cfg_enc_params.conf_win_left = 0;
11823  ni_strncpy(p_param_warn, max_err_len, "confWinLeft is not supported in AV1",
11824  max_err_len - 1);
11825  warning = NI_RETCODE_PARAM_WARN;
11826  }
11827  if (p_cfg->niParamT408.conf_win_right != 0)
11828  {
11829  p_cfg->niParamT408.conf_win_right =
11830  p_src->cfg_enc_params.conf_win_right = 0;
11831  ni_strncpy(p_param_warn, max_err_len, "confWinRight is not supported in AV1",
11832  max_err_len - 1);
11833  warning = NI_RETCODE_PARAM_WARN;
11834  }
11835  if (p_cfg->ui8hdr10_enable)
11836  {
11837  ni_strncpy(p_param_err, max_err_len,
11838  "masterDisplay not supported for AV1",
11839  max_err_len - 1);
11840  warning = NI_RETCODE_PARAM_WARN;
11841  }
11842  if (p_cfg->ui8hrdEnable)
11843  {
11844  ni_strncpy(p_param_err, max_err_len, "hrdEnable is not supported on av1 encoder",
11845  max_err_len - 1);
11847  LRETURN;
11848  }
11849  if (p_cfg->ui8EnableAUD)
11850  {
11851  ni_strncpy(p_param_err, max_err_len, "enableAUD is not supported on av1 encoder",
11852  max_err_len - 1);
11854  LRETURN;
11855  }
11856  if (p_cfg->ui8HDR10CLLEnable)
11857  {
11858  ni_strncpy(p_param_err, max_err_len, "maxCLL is not supported on av1 encoder",
11859  max_err_len - 1);
11861  LRETURN;
11862  }
11863  if (p_cfg->ui8repeatHeaders)
11864  {
11865  ni_strncpy(p_param_err, max_err_len, "repeatHeaders is not supported on av1 encoder",
11866  max_err_len - 1);
11867  warning = NI_RETCODE_PARAM_WARN;
11868  }
11869  if (p_cfg->ui8enableSSIM)
11870  {
11871  ni_strncpy(p_param_err, max_err_len, "enableSSIM is not supported on av1 encoder",
11872  max_err_len - 1);
11874  LRETURN;
11875  }
11876  if (p_cfg->ui8EnableRdoQuant)
11877  {
11878  ni_strncpy(p_param_err, max_err_len, "EnableRdoQuant is not supported on av1 encoder",
11879  max_err_len - 1);
11881  LRETURN;
11882  }
11883  if (p_cfg->ui8fillerEnable)
11884  {
11885  ni_strncpy(p_param_err, max_err_len, "fillerEnable is not supported on av1 encoder",
11886  max_err_len - 1);
11888  LRETURN;
11889  }
11890  if (p_cfg->i8ppsInitQp)
11891  {
11892  ni_strncpy(p_param_err, max_err_len, "ppsInitQp is not supported for av1 encoder",
11893  max_err_len - 1);
11895  LRETURN;
11896  }
11897  if (p_cfg->ui8vbvBufferReencode && p_cfg->ui8multicoreJointMode)
11898  {
11899  ni_strncpy(p_param_err, max_err_len, "vbvBufferReencode is not supported for av1 multicoreJointMode", max_err_len - 1);
11901  LRETURN;
11902  }
11903  } else if (NI_CODEC_FORMAT_JPEG == p_ctx->codec_format)
11904  {
11905  if (p_cfg->ui32cropWidth || p_cfg->ui32cropHeight || p_cfg->ui32horOffset || p_cfg->ui32verOffset)
11906  {
11907  ni_strncpy(p_param_err, max_err_len, "crop Parameters not supported for JPEG", max_err_len - 1);
11909  LRETURN;
11910  }
11911  if (p_cfg->niParamT408.tier)
11912  {
11913  ni_strncpy(p_param_err, max_err_len, "Tier is not supported for JPEG", max_err_len - 1);
11915  LRETURN;
11916  }
11917  if (p_cfg->ui8tuneBframeVisual)
11918  {
11919  ni_strncpy(p_param_err, max_err_len, "TuneBframeVisual is not supported for JPEG", max_err_len - 1);
11921  LRETURN;
11922  }
11923  if (p_cfg->ui8hdr10_enable)
11924  {
11925  ni_strncpy(p_param_err, max_err_len,
11926  "masterDisplay not supported for jpeg",
11927  max_err_len - 1);
11928  warning = NI_RETCODE_PARAM_WARN;
11929  }
11930  if (p_cfg->niParamT408.conf_win_top != 0)
11931  {
11932  ni_strncpy(p_param_err, max_err_len, "confWinTop is not supported in jpeg",
11933  max_err_len - 1);
11935  LRETURN;
11936  }
11937  if (p_cfg->niParamT408.conf_win_bottom != 0)
11938  {
11939  ni_strncpy(p_param_err, max_err_len, "confWinBottom is not supported in jpeg",
11940  max_err_len - 1);
11942  LRETURN;
11943  }
11944  if (p_cfg->niParamT408.conf_win_left != 0)
11945  {
11946  ni_strncpy(p_param_err, max_err_len, "confWinLeft is not supported in jpeg",
11947  max_err_len - 1);
11949  LRETURN;
11950  }
11951  if (p_cfg->niParamT408.conf_win_right != 0)
11952  {
11953  ni_strncpy(p_param_err, max_err_len, "confWinRight is not supported in jpeg",
11954  max_err_len - 1);
11956  LRETURN;
11957  }
11958  if (p_cfg->ui8hrdEnable)
11959  {
11960  ni_strncpy(p_param_err, max_err_len, "hrdEnable is not supported on jpeg encoder",
11961  max_err_len - 1);
11963  LRETURN;
11964  }
11965  if (p_cfg->ui8EnableAUD)
11966  {
11967  ni_strncpy(p_param_err, max_err_len, "enableAUD is not supported on jpeg encoder",
11968  max_err_len - 1);
11970  LRETURN;
11971  }
11972  if (p_cfg->ui8repeatHeaders)
11973  {
11974  ni_strncpy(p_param_err, max_err_len, "repeatHeaders is not supported on jpeg encoder",
11975  max_err_len - 1);
11977  LRETURN;
11978  }
11980  {
11981  ni_strncpy(p_param_err, max_err_len, "prefTRC is not supported on jpeg encoder",
11982  max_err_len - 1);
11984  LRETURN;
11985  }
11986  if (p_cfg->ui8HDR10CLLEnable)
11987  {
11988  ni_strncpy(p_param_err, max_err_len, "maxCLL is not supported on jpeg encoder",
11989  max_err_len - 1);
11991  LRETURN;
11992  }
11993  if (p_cfg->ui8colorPrimaries != 2)
11994  {
11995  ni_strncpy(p_param_err, max_err_len, "colorPri is not supported on jpeg encoder",
11996  max_err_len - 1);
11998  LRETURN;
11999  }
12000  if (p_cfg->ui8colorTrc != 2)
12001  {
12002  ni_strncpy(p_param_err, max_err_len, "colorTrc is not supported on jpeg encoder",
12003  max_err_len - 1);
12005  LRETURN;
12006  }
12007  if (p_cfg->ui8colorSpace != 2)
12008  {
12009  ni_strncpy(p_param_err, max_err_len, "colorSpc is not supported on jpeg encoder",
12010  max_err_len - 1);
12012  LRETURN;
12013  }
12014  if (p_src->sar_num)
12015  {
12016  ni_strncpy(p_param_err, max_err_len, "sarNum is not supported on jpeg encoder",
12017  max_err_len - 1);
12019  LRETURN;
12020  }
12021  if (p_src->sar_denom != 1)
12022  {
12023  ni_strncpy(p_param_err, max_err_len, "sarDenom is not supported on jpeg encoder",
12024  max_err_len - 1);
12026  LRETURN;
12027  }
12028  if (p_src->video_full_range_flag != -1)
12029  {
12030  ni_strncpy(p_param_err, max_err_len,
12031  "videoFullRangeFlag is not supported on jpeg encoder",
12032  max_err_len - 1);
12034  LRETURN;
12035  }
12036  if (p_cfg->ui8temporalLayersEnable)
12037  {
12038  ni_strncpy(p_param_err, max_err_len,
12039  "temporalLayersEnable is not supported on jpeg encoder",
12040  max_err_len - 1);
12042  LRETURN;
12043  }
12044  if (p_cfg->ui8spatialLayersMinusOne > 0)
12045  {
12046  ni_strncpy(p_param_err, max_err_len,
12047  "spatialLayers is not supported for jpeg encode",
12048  max_err_len - 1);
12050  LRETURN;
12051  }
12052  if (p_cfg->ui8LowDelay)
12053  {
12054  ni_strncpy(p_param_err, max_err_len, "LowDelay is not supported on jpeg encoder",
12055  max_err_len - 1);
12057  LRETURN;
12058  }
12059  if (p_cfg->ui8rdoLevel)
12060  {
12061  ni_strncpy(p_param_err, max_err_len, "rdoLevel is not supported on jpeg encoder",
12062  max_err_len - 1);
12064  LRETURN;
12065  }
12066  if (p_cfg->ui8EnableRdoQuant)
12067  {
12068  ni_strncpy(p_param_err, max_err_len, "EnableRdoQuant is not supported on jpeg encoder",
12069  max_err_len - 1);
12071  LRETURN;
12072  }
12073  if (p_cfg->ui8enable2PassGopPatern)
12074  {
12075  ni_strncpy(p_param_err, max_err_len,
12076  "enable2PassGop is not supported on jpeg encoder",
12077  max_err_len - 1);
12079  LRETURN;
12080  }
12081  if (p_cfg->ui8LookAheadDepth)
12082  {
12083  ni_strncpy(p_param_err, max_err_len,
12084  "lookAheadDepth is not supported on jpeg encoder",
12085  max_err_len - 1);
12087  LRETURN;
12088  }
12090  {
12091  ni_strncpy(p_param_err, max_err_len,
12092  "gopPresetIdx is not supported on jpeg encoder",
12093  max_err_len - 1);
12095  LRETURN;
12096  }
12097  if (p_cfg->niParamT408.roiEnable)
12098  {
12099  ni_strncpy(p_param_err, max_err_len, "roiEnable is not supported on jpeg encoder",
12100  max_err_len - 1);
12102  LRETURN;
12103  }
12104  if(p_src->roi_demo_mode)
12105  {
12106  ni_strncpy(p_param_err, max_err_len,
12107  "RoiDemoMode is not supported on jpeg encoder",
12108  max_err_len - 1);
12110  LRETURN;
12111  }
12112  if (p_src->cacheRoi)
12113  {
12114  ni_strncpy(p_param_err, max_err_len, "cacheRoi is not supported on jpeg encoder",
12115  max_err_len - 1);
12117  LRETURN;
12118  }
12120  {
12121  ni_strncpy(p_param_err, max_err_len,
12122  "ReconfDemoMode is not supported on jpeg encoder",
12123  max_err_len - 1);
12125  LRETURN;
12126  }
12127  if (p_cfg->niParamT408.intraRefreshMode)
12128  {
12129  ni_strncpy(p_param_err, max_err_len,
12130  "intraRefreshMode is not supported on jpeg encoder",
12131  max_err_len - 1);
12133  LRETURN;
12134  }
12135  if (p_cfg->niParamT408.intraRefreshArg)
12136  {
12137  ni_strncpy(p_param_err, max_err_len,
12138  "intraRefreshArg is not supported on jpeg encoder",
12139  max_err_len - 1);
12141  LRETURN;
12142  }
12143  if (p_cfg->niParamT408.intra_period != 120)
12144  {
12145  ni_strncpy(p_param_err, max_err_len,
12146  "intraPeriod is not supported on jpeg encoder",
12147  max_err_len - 1);
12149  LRETURN;
12150  }
12151  if (p_cfg->ui8intraResetRefresh)
12152  {
12153  ni_strncpy(
12154  p_param_err, max_err_len,
12155  "IntraRefreshResetOnForceIDR is not supported on jpeg encoder",
12156  max_err_len - 1);
12158  LRETURN;
12159  }
12160  if (p_cfg->niParamT408.useLongTerm)
12161  {
12162  ni_strncpy(p_param_err, max_err_len,
12163  "longTermReferenceEnable is not supported on jpeg encoder",
12164  max_err_len - 1);
12166  LRETURN;
12167  }
12168  if (p_cfg->ui32setLongTermInterval)
12169  {
12170  ni_strncpy(p_param_err, max_err_len,
12171  "longTermReferenceInterval is not supported on jpeg encoder",
12172  max_err_len - 1);
12174  LRETURN;
12175  }
12176  if (p_cfg->ui8setLongTermCount != 2)
12177  {
12178  ni_strncpy(p_param_err, max_err_len,
12179  "longTermReferenceCount is not supported on jpeg encoder",
12180  max_err_len - 1);
12182  LRETURN;
12183  }
12184  if (p_cfg->ui8multicoreJointMode)
12185  {
12186  ni_strncpy(p_param_err, max_err_len,
12187  "multicoreJointMode is not supported on jpeg encoder",
12188  max_err_len - 1);
12190  LRETURN;
12191  }
12192  if (p_cfg->ui8enableSSIM)
12193  {
12194  ni_strncpy(p_param_err, max_err_len, "enableSSIM is not supported on jpeg encoder",
12195  max_err_len - 1);
12197  LRETURN;
12198  }
12199  if (p_enc->rc.vbv_buffer_size != -1)
12200  {
12201  ni_strncpy(p_param_err, max_err_len,
12202  "vbvBufferSize is not supported on jpeg encoder",
12203  max_err_len - 1);
12205  LRETURN;
12206  }
12207  if (p_cfg->ui8fillerEnable)
12208  {
12209  ni_strncpy(p_param_err, max_err_len,
12210  "fillerEnable is not supported on jpeg encoder",
12211  max_err_len - 1);
12213  LRETURN;
12214  }
12215  if (p_cfg->ui8picSkipEnable)
12216  {
12217  ni_strncpy(p_param_err, max_err_len, "picSkip is not supported on jpeg encoder",
12218  max_err_len - 1);
12220  LRETURN;
12221  }
12222  if (p_cfg->ui16maxFrameSize)
12223  {
12224  ni_strncpy(p_param_err, max_err_len, "maxFrameSize is not supported on jpeg encoder",
12225  max_err_len - 1);
12227  LRETURN;
12228  }
12230  {
12231  ni_strncpy(p_param_err, max_err_len,
12232  "cuLevelRCEnable is not supported on jpeg encoder",
12233  max_err_len - 1);
12235  LRETURN;
12236  }
12237  if (p_cfg->niParamT408.enable_hvs_qp)
12238  {
12239  ni_strncpy(p_param_err, max_err_len, "hvsQPEnable is not supported on jpeg encoder",
12240  max_err_len - 1);
12242  LRETURN;
12243  }
12244  if (p_cfg->niParamT408.profile)
12245  {
12246  ni_strncpy(p_param_err, max_err_len, "profile is not supported on jpeg encoder",
12247  max_err_len - 1);
12249  LRETURN;
12250  }
12252  {
12253  ni_strncpy(p_param_err, max_err_len, "intraRefreshMode or intraRefreshArg is not supported on jpeg encoder",
12254  max_err_len - 1);
12256  LRETURN;
12257  }
12258  if (p_cfg->i8crf != -1)
12259  {
12260  ni_strncpy(p_param_err, max_err_len, "crf is not supported on jpeg encoder",
12261  max_err_len - 1);
12263  LRETURN;
12264  }
12265  if (p_cfg->i32tolCtbRcInter != (int32_t)(0.1 * 1000))
12266  {
12267  ni_strncpy(p_param_err, max_err_len, "tolCtbRcInter is not supported on jpeg encoder",
12268  max_err_len - 1);
12270  LRETURN;
12271  }
12272  if (p_cfg->i32tolCtbRcIntra != (int32_t)(0.1 * 1000))
12273  {
12274  ni_strncpy(p_param_err, max_err_len, "tolCtbRcIntra is not supported on jpeg encoder",
12275  max_err_len - 1);
12277  LRETURN;
12278  }
12279  if (p_cfg->ui8rcQpDeltaRange != 10)
12280  {
12281  ni_strncpy(p_param_err, max_err_len,
12282  "rcQpDeltaRange is not supported on jpeg encoder",
12283  max_err_len - 1);
12285  LRETURN;
12286  }
12287  if (p_cfg->i16bitrateWindow != -255)
12288  {
12289  ni_strncpy(p_param_err, max_err_len, "bitrateWindow is not supported on jpeg encoder",
12290  max_err_len - 1);
12292  LRETURN;
12293  }
12294  if (p_cfg->i16ctbRowQpStep)
12295  {
12296  ni_strncpy(p_param_err, max_err_len, "ctbRowQpStep is not supported on jpeg encoder",
12297  max_err_len - 1);
12299  LRETURN;
12300  }
12301  if (p_cfg->ui8AiEnhanceMode)
12302  {
12303  ni_strncpy(p_param_err, max_err_len,
12304  "enableAIEnhance is not supported on jpeg encoder",
12305  max_err_len - 1);
12307  LRETURN;
12308  }
12309  if (p_cfg->i8ppsInitQp)
12310  {
12311  ni_strncpy(p_param_err, max_err_len, "ppsInitQp is not supported for jpeg encoder",
12312  max_err_len - 1);
12314  LRETURN;
12315  }
12316  if (p_cfg->i8pass1Qp)
12317  {
12318  ni_strncpy(p_param_err, max_err_len, "pass1Qp is not supported for jpeg encoder",
12319  max_err_len - 1);
12321  LRETURN;
12322  }
12323  if (p_enc->bitrateMode != -1)
12324  {
12325  ni_strncpy(p_param_err, max_err_len, "bitrateMode is not supported for jpeg encoder",
12326  max_err_len - 1);
12328  LRETURN;
12329  }
12330  if (p_cfg->ui8vbvBufferReencode)
12331  {
12332  ni_strncpy(p_param_err, max_err_len,
12333  "vbvBufferReencode is not supported on jpeg encoder",
12334  max_err_len - 1);
12336  LRETURN;
12337  }
12338  if (p_enc->get_psnr_mode != 3)
12339  {
12340  ni_strncpy(p_param_err, max_err_len,
12341  "getPsnrMode or getReconstructedMode is not supported on jpeg encoder",
12342  max_err_len - 1);
12344  LRETURN;
12345  }
12346  if (p_cfg->ui8spatialLayersMinusOne > 0)
12347  {
12348  ni_strncpy(p_param_err, max_err_len,
12349  "spatialLayers is not supported for Jpeg encode",
12350  max_err_len - 1);
12352  LRETURN;
12353  }
12354  if (p_cfg->ui8disableAv1TimingInfo != 0)
12355  {
12356  ni_strncpy(p_param_err, max_err_len, "disableAv1TimingInfo is not supported for Jpeg",
12357  max_err_len - 1);
12359  LRETURN;
12360  }
12361  if (p_cfg->ui8av1OpLevel[0] != 0)
12362  {
12363  ni_strncpy(p_param_err, max_err_len, "av1OpLevel is not supported for Jpeg",
12364  max_err_len - 1);
12366  LRETURN;
12367  }
12368  }
12369 
12370  if (p_src->force_frame_type != 0 && p_src->force_frame_type != 1)
12371  {
12372  ni_strncpy(p_param_err, max_err_len, "Invalid forceFrameType: out of range",
12373  max_err_len - 1);
12374  param_ret = NI_RETCODE_INVALID_PARAM;
12375  LRETURN;
12376  }
12377 
12378  if (p_cfg->niParamT408.forcedHeaderEnable > 2)
12379  {
12380  ni_strncpy(p_param_err, max_err_len, "Invalid forcedHeaderEnable: out of range",
12381  max_err_len - 1);
12382  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
12383  LRETURN;
12384  }
12385 
12386  if (p_cfg->niParamT408.decoding_refresh_type < 0 ||
12388  {
12389  ni_strncpy(p_param_err, max_err_len, "Invalid decoding_refresh_type: out of range", max_err_len - 1);
12391  LRETURN;
12392  }
12393 
12394  if (!QUADRA)
12395  {
12396  if (p_cfg->niParamT408.gop_preset_index < 0 ||
12397  p_cfg->niParamT408.gop_preset_index > 8)
12398  {
12399  ni_strcpy(p_param_err, max_err_len, "Invalid gop_preset_index: out of range");
12401  LRETURN;
12402  }
12403 
12404  if (p_src->low_delay_mode && 1 != p_cfg->niParamT408.gop_preset_index &&
12405  2 != p_cfg->niParamT408.gop_preset_index &&
12406  3 != p_cfg->niParamT408.gop_preset_index &&
12407  6 != p_cfg->niParamT408.gop_preset_index &&
12408  7 != p_cfg->niParamT408.gop_preset_index &&
12409  !(0 == p_cfg->niParamT408.gop_preset_index &&
12411  {
12412  ni_strcpy(p_param_err, max_err_len, "GOP size must be 1 when lowDelay is enabled");
12414  LRETURN;
12415  }
12416  }
12417  else // QUADRA
12418  {
12419  if (p_cfg->ui8gopSize > 16)
12420  {
12421  ni_strncpy(p_param_err, max_err_len, "Invalid gopSize out of range", max_err_len - 1);
12423  LRETURN;
12424  }
12425 
12426  if (p_cfg->ui8gopLowdelay &&
12427  p_cfg->ui8gopSize > 4)
12428  {
12429  ni_strncpy(p_param_err, max_err_len, "GOP size must be <= 4 for low delay GOP", max_err_len - 1);
12431  LRETURN;
12432  }
12433 
12434  if (p_cfg->ui8LookAheadDepth)
12435  {
12436  if (p_cfg->ui8LookAheadDepth < 4 || p_cfg->ui8LookAheadDepth > 40)
12437  {
12438  ni_strncpy(p_param_err, max_err_len, "Invalid LookAheadDepth: out of range. <[4-40]>", max_err_len - 1);
12440  LRETURN;
12441  }
12442  if (p_cfg->ui8gopLowdelay)
12443  {
12444  ni_strncpy(p_param_err, max_err_len, "2-pass encode does not support low delay GOP", max_err_len - 1);
12446  LRETURN;
12447  }
12449  {
12450  ni_strncpy(p_param_err, max_err_len, "2-pass encode does not support tile4x4 format",
12451  max_err_len - 1);
12453  LRETURN;
12454  }
12455  if (p_cfg->ui8spatialLayersMinusOne > 0)
12456  {
12457  ni_strncpy(p_param_err, max_err_len, "currently do not support lookahead encode with multi spatial layers", max_err_len - 1);
12459  LRETURN;
12460  }
12461  }
12462 
12463  if (p_src->low_delay_mode || p_cfg->ui8picSkipEnable)
12464  {
12466  {
12467  for (i = 0; i < p_cfg->niParamT408.custom_gop_params.custom_gop_size; i++)
12468  {
12469  if (p_cfg->niParamT408.custom_gop_params.pic_param[i].poc_offset != (i+1))
12470  {
12471  if (p_src->low_delay_mode)
12472  ni_strncpy(p_param_err, max_err_len, "Custom GOP must not include backward prediction when lowDelay is enabled", max_err_len - 1);
12473  else
12474  ni_strncpy(p_param_err, max_err_len, "Custom GOP must not include backward prediction when picSkip is enabled", max_err_len - 1);
12475  param_ret = NI_RETCODE_INVALID_PARAM;
12476  LRETURN;
12477  }
12478  }
12479  } else if (1 != p_cfg->ui8gopSize && !p_cfg->ui8gopLowdelay &&
12480  p_cfg->niParamT408.intra_period != 1 &&
12482  {
12483  if (p_src->low_delay_mode)
12484  {
12485  if (p_cfg->ui8spatialLayersMinusOne == 0)
12486  {
12487  ni_strncpy(p_param_err, max_err_len, "Must use low delay GOP (gopPresetIdx 1,3,7,9,10) when lowDelay is enabled", max_err_len - 1);
12489  LRETURN;
12490  }
12491  }
12492  else
12493  {
12494  ni_strncpy(p_param_err, max_err_len, "Must use low delay GOP (gopPresetIdx 1,3,7,9,10) when picSkip is enabled", max_err_len - 1);
12496  LRETURN;
12497  }
12498  } else if ((p_cfg->ui8LookAheadDepth != 0) && (!p_cfg->ui8useLowDelayPocType))
12499  {
12500  if (p_src->low_delay_mode)
12501  ni_strncpy(p_param_err, max_err_len, "lookAheadDepth must be 0 when lowDelay is enabled", max_err_len - 1);
12502  else
12503  ni_strncpy(p_param_err, max_err_len, "lookAheadDepth must be 0 when picSkip is enabled", max_err_len - 1);
12504  param_ret = NI_RETCODE_INVALID_PARAM;
12505  LRETURN;
12506  }
12507 
12508  if (p_cfg->ui8multicoreJointMode)
12509  {
12510  if (p_src->low_delay_mode)
12511  ni_strncpy(p_param_err, max_err_len,
12512  "Cannot use multicoreJointMode when lowDelay is enabled",
12513  max_err_len - 1);
12514  else
12515  ni_strncpy(p_param_err, max_err_len,
12516  "Cannot use multicoreJointMode when picSkip is enabled",
12517  max_err_len - 1);
12518  param_ret = NI_RETCODE_INVALID_PARAM;
12519  LRETURN;
12520  }
12521 
12522  if (p_src->minFramesDelay)
12523  {
12524  if (p_src->low_delay_mode)
12525  ni_strncpy(p_param_err, max_err_len,
12526  "Cannot enable minFramesDelay when lowDelay is enabled",
12527  max_err_len - 1);
12528  else
12529  ni_strncpy(p_param_err, max_err_len,
12530  "Cannot enable minFramesDelay when picSkip is enabled",
12531  max_err_len - 1);
12532  param_ret = NI_RETCODE_INVALID_PARAM;
12533  LRETURN;
12534  }
12535  }
12536 
12537  if (p_cfg->ui8spatialLayersMinusOne > 0)
12538  {
12539  int intra_period_in_frames = p_cfg->niParamT408.intra_period * (p_cfg->ui8spatialLayersMinusOne+1);
12540  p_cfg->niParamT408.intra_period = p_cfg->niParamT408.avcIdrPeriod = intra_period_in_frames;
12541  }
12542 
12543  if (p_cfg->ui8useLowDelayPocType)
12544  {
12546  {
12547  ni_strncpy(p_param_err, max_err_len, "Custom GOP size must be 1 when useLowDelayPocType is enabled", max_err_len - 1);
12548  param_ret = NI_RETCODE_INVALID_PARAM;
12549  LRETURN;
12550  } else if (1 != p_cfg->ui8gopSize && !p_cfg->ui8gopLowdelay &&
12551  p_cfg->niParamT408.intra_period != 1)
12552  {
12553  ni_strncpy(p_param_err, max_err_len, "Must use GOP with all frames as reference frames (gopPresetIdx 1,3,7,9) when useLowDelayPocType is enabled", max_err_len - 1);
12555  LRETURN;
12556  }
12557  }
12558 
12559  if (p_src->low_delay_mode)
12560  {
12561  // minimum acceptable value of maxFrameSize is bitrate / framerate in bytes
12562  uint32_t min_maxFrameSize = p_cfg->i32bitRate / p_cfg->i32frameRateInfo * p_cfg->i32frameRateDenominator / 8;
12563 
12564  if (p_cfg->ui16maxFrameSize == 0)
12565  {
12566  if (p_enc->maxFrameSizeRatio > 0)
12567  {
12568  if (min_maxFrameSize * p_enc->maxFrameSizeRatio > NI_MAX_FRAME_SIZE)
12569  {
12570  p_cfg->ui16maxFrameSize = NI_MAX_FRAME_SIZE / 2000;
12571  } else
12572  {
12573  p_cfg->ui16maxFrameSize = min_maxFrameSize * p_enc->maxFrameSizeRatio / 2000;
12574  }
12575  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: Set maxFrameSize to %d times the "
12576  "minimum frame size %d bytes in low delay mode\n", __func__,
12577  p_enc->maxFrameSizeRatio, p_cfg->ui16maxFrameSize * 2000);
12578  } else
12579  {
12580  p_cfg->ui16maxFrameSize = ((p_src->source_width * p_src->source_height * 3 / 4) * p_ctx->bit_depth_factor) / 2000;
12581  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: maxFrameSize is not set in low delay "
12582  "mode. Set it to half of the maximum frame size %d bytes\n",
12583  __func__, p_cfg->ui16maxFrameSize*2000);
12584  }
12585  }
12586 
12587  if (p_cfg->ui16maxFrameSize < min_maxFrameSize / 2000)
12588  {
12589  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: maxFrameSize %u is too small. Changed to minimum value (bitrate/framerate in byte): %u\n",
12590  __func__, p_cfg->ui16maxFrameSize*2000, min_maxFrameSize);
12591  p_cfg->ui16maxFrameSize = min_maxFrameSize / 2000;
12592  }
12593  }
12594  else
12595  {
12596  if (p_cfg->ui16maxFrameSize != 0 || p_enc->maxFrameSizeRatio > 0)
12597  {
12598  ni_strncpy(p_param_err, max_err_len, "maxFrameSize can only be used when lowDelay is enabled", max_err_len - 1);
12599  param_ret = NI_RETCODE_INVALID_PARAM;
12600  LRETURN;
12601  }
12602  }
12603  }
12604 
12605  if (QUADRA)
12606  {
12607  if (p_cfg->ui16gdrDuration)
12608  {
12610  {
12611  for (i = 0; i < p_cfg->niParamT408.custom_gop_params.custom_gop_size; i++)
12612  {
12613  if (2 == p_cfg->niParamT408.custom_gop_params.pic_param[i].pic_type)
12614  {
12615  ni_strncpy(p_param_err, max_err_len, "Custom GOP can not have B frames for intra refresh", max_err_len - 1);
12616  param_ret = NI_RETCODE_INVALID_PARAM;
12617  LRETURN;
12618  }
12619  }
12620  }
12621  else if (p_cfg->ui8gopSize != 1 || p_cfg->ui8gopLowdelay)
12622  {
12624  {
12625  ni_strncpy(p_param_err, max_err_len,
12626  "Must use gopPresetIdx 9 (consecutive P frame) for intra refresh",
12627  max_err_len - 1);
12628  param_ret = NI_RETCODE_INVALID_PARAM;
12629  LRETURN;
12630  }
12631  ni_strncpy(p_param_warn, max_err_len, "GOP size forced to 1 and low delay GOP force disabled (no B frames) for intra refresh", max_err_len - 1);
12632  warning = NI_RETCODE_PARAM_WARN;
12633  p_cfg->ui8gopSize = 1;
12634  p_cfg->ui8gopLowdelay = 0;
12635  }
12636  if (p_cfg->ui16gdrDuration == 1)
12637  {
12638  ni_strncpy(p_param_err, max_err_len,
12639  "intra refresh cycle (height / intraRefreshArg MB or CTU) must > 1",
12640  max_err_len - 1);
12641  param_ret = NI_RETCODE_INVALID_PARAM;
12642  LRETURN;
12643  }
12644  if (p_cfg->ui8LookAheadDepth != 0)
12645  {
12646  ni_strncpy(p_param_err, max_err_len, "lookaheadDepth must be 0 for intra refresh", max_err_len - 1);
12647  param_ret = NI_RETCODE_INVALID_PARAM;
12648  LRETURN;
12649  }
12650  if (STD_HEVC == p_cfg->ui8bitstreamFormat ||
12651  STD_AV1 == p_cfg->ui8bitstreamFormat)
12652  {
12653  if (p_cfg->niParamT408.intra_period < p_cfg->ui16gdrDuration)
12654  {
12655  ni_strncpy(p_param_warn, max_err_len, "intraPeriod forced to match intra refersh cycle (intraPeriod must >= intra refersh cycle)", max_err_len - 1);
12656  warning = NI_RETCODE_PARAM_WARN;
12657  p_cfg->niParamT408.intra_period = p_cfg->ui16gdrDuration;
12658  }
12659  }
12660  else if (STD_AVC == p_cfg->ui8bitstreamFormat)
12661  {
12662  if (p_cfg->niParamT408.avcIdrPeriod < p_cfg->ui16gdrDuration)
12663  {
12664  ni_strncpy(p_param_warn, max_err_len, "intraPeriod forced to match intra refersh cycle (intraPeriod must >= intra refersh cycle)", max_err_len - 1);
12665  warning = NI_RETCODE_PARAM_WARN;
12666  p_cfg->niParamT408.avcIdrPeriod = p_cfg->ui16gdrDuration;
12667  }
12668  }
12669  }
12670 
12671  if (p_cfg->ui8multicoreJointMode)
12672  {
12673  if (p_cfg->ui8hrdEnable)
12674  {
12675  ni_strncpy(p_param_warn, max_err_len, "HRD conformance is not guaranteed in multicoreJointMode", max_err_len - 1);
12676  warning = NI_RETCODE_PARAM_WARN;
12677  }
12678  }
12679 
12680  if (p_cfg->ui8temporalLayersEnable)
12681  {
12682  if (p_cfg->ui8hrdEnable)
12683  {
12684  snprintf(p_param_err, max_err_len, "HRD conformance with multiple temporal layers is currently not supported");
12685  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
12686  LRETURN;
12687  }
12688  }
12689 
12690  if ((p_cfg->ui8hrdEnable) || (p_cfg->ui8fillerEnable))
12691  {
12692  // enable rate control
12693  if (p_cfg->ui8rcEnable == 0)
12694  {
12695  p_cfg->ui8rcEnable = p_src->cfg_enc_params.rc.enable_rate_control = 1;
12696  }
12697 
12698  // enable hrd if it is off
12699  if (p_cfg->i32vbvBufferSize == 0)
12700  {
12701  p_cfg->i32vbvBufferSize = 3000;
12702  }
12703  }
12704 
12705  // maxrate must >= bitrate
12706  if (p_cfg->ui32vbvMaxRate != 0)
12707  {
12708  if ((int)p_cfg->ui32vbvMaxRate < p_cfg->i32bitRate)
12709  {
12710  snprintf(p_param_err, max_err_len, "vbvMaxRate %u cannot be smaller than bitrate %d",
12711  p_cfg->ui32vbvMaxRate, p_cfg->i32bitRate);
12712  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
12713  LRETURN;
12714  }
12715  }
12716 
12717  // minrate must <= bitrate
12718  if (p_cfg->ui32vbvMinRate != 0)
12719  {
12720  if ((int)p_cfg->ui32vbvMinRate > p_cfg->i32bitRate)
12721  {
12722  snprintf(p_param_err, max_err_len, "vbvMinRate %u cannot be larger than bitrate %d",
12723  p_cfg->ui32vbvMinRate, p_cfg->i32bitRate);
12724  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
12725  LRETURN;
12726  }
12727  }
12728 
12729  // The supported range of vbvBufferSize are 0, or 1/framerate to 3000. If the vbvBufferSize is not 0.
12730  // The minimum vbvBufferSize in msec is 1/framerate. The actual minimum in bits is bitrate/framerate.
12731  if (p_cfg->i32vbvBufferSize != 0)
12732  {
12733  // check buffer size compatible to bitrate
12734  int32_t minVbvBufferSize = p_cfg->i32frameRateDenominator * 1000 / p_cfg->i32frameRateInfo;
12735  if (p_cfg->i32vbvBufferSize < minVbvBufferSize)
12736  {
12737  snprintf(p_param_err, max_err_len, "vbvBufferSize must be greater than the average frame size. Minimum is %d msec for framerate %d fps",
12738  minVbvBufferSize, (p_cfg->i32frameRateInfo / p_cfg->i32frameRateDenominator));
12739  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
12740  LRETURN;
12741  }
12742 
12743  // check buffer size compatible to maxrate
12744  if (p_cfg->ui32vbvMaxRate != 0)
12745  {
12746  int32_t maxRateMinVbvBufferSize = ((int64_t)p_cfg->i32frameRateDenominator * 1000 / p_cfg->i32frameRateInfo) * p_cfg->ui32vbvMaxRate / p_cfg->i32bitRate;
12747  if (p_cfg->i32vbvBufferSize < maxRateMinVbvBufferSize)
12748  {
12749  snprintf(p_param_warn, max_err_len, "vbvBufferSize cannot be smaller than one frame size based on vbvMaxRate, force vbvBufferSize to %d msec for bitrate %d vbvMaxRate %u and framerate %d fps",
12750  maxRateMinVbvBufferSize, p_cfg->i32bitRate, p_cfg->ui32vbvMaxRate, (p_cfg->i32frameRateInfo / p_cfg->i32frameRateDenominator));
12751  warning = NI_RETCODE_PARAM_WARN;
12752  p_cfg->i32vbvBufferSize = maxRateMinVbvBufferSize;
12753  }
12754  }
12755  // check buffer size compatible to minrate (capped CRF may set minrate without maxrate)
12756  else if (p_cfg->ui32vbvMinRate != 0)
12757  {
12758  int32_t minRateMinVbvBufferSize = ((int64_t)p_cfg->i32frameRateDenominator * 1000 / p_cfg->i32frameRateInfo) * p_cfg->ui32vbvMinRate / p_cfg->i32bitRate;
12759  if (p_cfg->i32vbvBufferSize < minRateMinVbvBufferSize)
12760  {
12761  snprintf(p_param_warn, max_err_len, "vbvBufferSize cannot be smaller than one frame size based on vbvMinRate, force vbvBufferSize to %d msec for bitrate %d vbvMinRate %u and framerate %d fps",
12762  minRateMinVbvBufferSize, p_cfg->i32bitRate, p_cfg->ui32vbvMinRate, (p_cfg->i32frameRateInfo / p_cfg->i32frameRateDenominator));
12763  warning = NI_RETCODE_PARAM_WARN;
12764  p_cfg->i32vbvBufferSize = minRateMinVbvBufferSize;
12765  }
12766  }
12767  }
12768  else
12769  {
12770  // check buffer size compatible to maxrate and/or minrate
12771  if ( p_cfg->ui32vbvMaxRate != 0 || p_cfg->ui32vbvMinRate != 0 )
12772  {
12773  snprintf(p_param_warn, max_err_len, "vbvMaxRate %u vbvMinRate %u does not take effect when vbvBufferSize is 0, force vbvMaxRate vbvMinRate to 0",
12774  p_cfg->ui32vbvMaxRate, p_cfg->ui32vbvMinRate);
12775  warning = NI_RETCODE_PARAM_WARN;
12776  p_cfg->ui32vbvMaxRate = 0;
12777  p_cfg->ui32vbvMinRate = 0;
12778  }
12779  }
12780 
12781  if (p_cfg->ui32ltrRefInterval || p_cfg->niParamT408.useLongTerm)
12782  {
12783  if (p_cfg->ui32ltrRefInterval && p_cfg->niParamT408.useLongTerm)
12784  {
12785  ni_strncpy(p_param_err, max_err_len,
12786  "Can't enable ltrRefInterval and longTermReferenceEnable "
12787  "at same time",
12788  max_err_len - 1);
12789  param_ret = NI_RETCODE_INVALID_PARAM;
12790  LRETURN;
12791  }
12792 
12794  {
12796  {
12797  ni_strncpy(p_param_err, max_err_len, "Custom GOP size can not be > 1 for long term reference", max_err_len - 1);
12798  param_ret = NI_RETCODE_INVALID_PARAM;
12799  LRETURN;
12800  }
12801  } else if ((p_cfg->ui8gopSize != 1) && (p_cfg->ui8gopLowdelay == 0) &&
12802  (p_cfg->niParamT408.intra_period != 1))
12803  {
12805  {
12806  ni_strncpy(p_param_err, max_err_len,
12807  "Must use low delay GOP (gopPresetIdx 1,3,7,9) for long term reference",
12808  max_err_len - 1);
12809  param_ret = NI_RETCODE_INVALID_PARAM;
12810  LRETURN;
12811  }
12812  ni_strncpy(p_param_warn, max_err_len, "GOP size forced to 1 for long term reference", max_err_len - 1);
12813  warning = NI_RETCODE_PARAM_WARN;
12814  p_cfg->ui8gopSize = 1;
12815  }
12816 
12817  if (p_cfg->ui8LookAheadDepth != 0)
12818  {
12819  ni_strncpy(p_param_err, max_err_len, "lookaheadDepth must be 0 for long term reference", max_err_len - 1);
12820  param_ret = NI_RETCODE_INVALID_PARAM;
12821  LRETURN;
12822  }
12823  if (p_cfg->i8crf >= 0)
12824  {
12825  ni_strncpy(p_param_err, max_err_len, "crf must < 0 for long term reference", max_err_len - 1);
12826  param_ret = NI_RETCODE_INVALID_PARAM;
12827  LRETURN;
12828  }
12829  }
12830 
12831  if (p_cfg->ui32setLongTermInterval && (p_cfg->niParamT408.useLongTerm == 0))
12832  {
12833  ni_strncpy(
12834  p_param_err, max_err_len,
12835  "Must set longTermReferenceEnable for longTermReferenceInterval",
12836  max_err_len - 1);
12837  param_ret = NI_RETCODE_INVALID_PARAM;
12838  LRETURN;
12839  }
12840 
12841  if (p_cfg->ui8av1ErrResilientMode)
12842  {
12843  if (STD_AV1 != p_cfg->ui8bitstreamFormat)
12844  {
12845  ni_strncpy(p_param_warn, max_err_len, "AV1 err resilient mode forced to 0 when using other codecs", max_err_len - 1);
12846  warning = NI_RETCODE_PARAM_WARN;
12847  p_cfg->ui8av1ErrResilientMode = 0;
12848  }
12849  }
12850 
12851  if (p_cfg->ui8motionConstrainedMode)
12852  {
12853  if (STD_HEVC != p_cfg->ui8bitstreamFormat)
12854  {
12855  ni_strncpy(p_param_warn, max_err_len, "Motion Constrained mode force disabled for codecs other than HEVC", max_err_len - 1);
12856  warning = NI_RETCODE_PARAM_WARN;
12857  p_cfg->ui8motionConstrainedMode = 0;
12858  }
12859  else
12860  {
12861  if (p_cfg->ui8rdoLevel != 1)
12862  {
12863  ni_strncpy(
12864  p_param_err, max_err_len,
12865  "rdoLevel must be 1 for Motion Constrained mode 1 or 2",
12866  max_err_len - 1);
12867  param_ret = NI_RETCODE_INVALID_PARAM;
12868  LRETURN;
12869  }
12870 
12872  {
12873  if (p_cfg->ui8multicoreJointMode)
12874  {
12875  ni_strncpy(
12876  p_param_err, max_err_len,
12877  "multicoreJointMode must be 0 for Motion Constrained mode 2",
12878  max_err_len - 1);
12879  param_ret = NI_RETCODE_INVALID_PARAM;
12880  LRETURN;
12881  }
12882  if (p_src->source_width % 64 || p_src->source_height % 64)
12883  {
12884  if (!p_cfg->ui32cropWidth || !p_cfg->ui32cropHeight ||
12885  p_cfg->ui32cropWidth % 64 || p_cfg->ui32cropHeight % 64)
12886  {
12887  ni_strncpy(
12888  p_param_err, max_err_len,
12889  "input resolution (or cropping window) must be 64x64 aligned for Motion Constrained mode 2",
12890  max_err_len - 1);
12891  param_ret = NI_RETCODE_INVALID_PARAM;
12892  LRETURN;
12893  }
12894  }
12895  }
12896  }
12897  }
12898 
12899  if (p_cfg->ui8avccHvcc)
12900  {
12901  if ((STD_AVC != p_cfg->ui8bitstreamFormat) && (STD_HEVC != p_cfg->ui8bitstreamFormat))
12902  {
12903  ni_strncpy(p_param_warn, max_err_len, "AVCC HVCC forced to 0 for codecs other than AVC HEVC", max_err_len - 1);
12904  warning = NI_RETCODE_PARAM_WARN;
12905  p_cfg->ui8avccHvcc = 0;
12906  }
12907  }
12908  }
12909 
12910  if (p_cfg->niParamT408.cu_size_mode < 0 ||
12911  p_cfg->niParamT408.cu_size_mode > 7)
12912  {
12913  ni_strncpy(p_param_err, max_err_len, "Invalid cu_size_mode: out of range", max_err_len - 1);
12915  LRETURN;
12916  }
12917 
12918 
12919 
12920  if (p_cfg->niParamT408.use_recommend_enc_params < 0 ||
12922  {
12923  ni_strncpy(p_param_err, max_err_len, "Invalid use_recommend_enc_params: out of range", max_err_len - 1);
12925  LRETURN;
12926  }
12927 
12928  switch (p_cfg->niParamT408.use_recommend_enc_params)
12929  {
12930  case 0:
12931  case 2:
12932  case 3:
12933  {
12934  if (p_cfg->niParamT408.use_recommend_enc_params != 3)
12935  {
12936  // in FAST mode (recommendEncParam==3), max_num_merge value will be
12937  // decided in FW
12938  if (p_cfg->niParamT408.max_num_merge < 0 ||
12939  p_cfg->niParamT408.max_num_merge > 3)
12940  {
12941  ni_strncpy(p_param_err, max_err_len, "Invalid max_num_merge: out of range", max_err_len - 1);
12943  LRETURN;
12944  }
12945  }
12946  break;
12947  }
12948 
12949  default: break;
12950  }
12951 
12952  if ( p_cfg->niParamT408.intra_qp < -1 ||
12953  p_cfg->niParamT408.intra_qp > 51 )
12954  {
12955  ni_strncpy(p_param_err, max_err_len, "Invalid intra_qp: out of range", max_err_len - 1);
12956  param_ret = NI_RETCODE_PARAM_ERROR_INTRA_QP;
12957  LRETURN;
12958  }
12959 
12960  if (QUADRA)
12961  {
12962  if (p_cfg->i8crf >= 0 && p_cfg->i8crf <= 51)
12963  {
12964  if (p_cfg->ui8LookAheadDepth < 4 || p_cfg->ui8LookAheadDepth > 40)
12965  {
12967  "6X") < 0)
12968  {
12969  ni_strncpy(p_param_err, max_err_len, "CRF requres LookAheadDepth <[4-40]>", max_err_len - 1);
12971  LRETURN;
12972  }
12973  else
12974  {
12975  p_cfg->ui8LookAheadDepth = 1;
12976  p_cfg->ui8noMbtree = 1;
12977  ni_strncpy(p_param_warn, max_err_len, "enable lookahead of current frame", max_err_len - 1);
12978  warning = NI_RETCODE_PARAM_WARN;
12979  }
12980  }
12981 
12982  if (p_cfg->ui8rcEnable == 1)
12983  {
12984  ni_strncpy(p_param_err, max_err_len, "CRF requires RcEnable 0", max_err_len - 1);
12985  param_ret = NI_RETCODE_PARAM_ERROR_RCENABLE;
12986  LRETURN;
12987  }
12988  #if 0
12989  if (p_cfg->ui8ctbRcMode > 0)
12990  {
12991  ni_strncpy(p_param_warn, sizeof(p_param_warn), "Lookahead with cuLevelRCEnable or hvsQPEnable may degrade quality", max_err_len - 1);
12992  warning = NI_RETCODE_PARAM_WARN;
12993  //LRETURN;
12994  }
12995  #endif
12996  }
12997 
12999  {
13000  if (p_cfg->ui8LookAheadDepth == 0)
13001  {
13002  ni_strncpy(p_param_err, max_err_len, "tuneBframeVisual level 1 (medium) requires lookahead or crf encode", max_err_len - 1);
13003  param_ret = NI_RETCODE_PARAM_ERROR_RCENABLE;
13004  LRETURN;
13005  }
13006  }
13007  }
13008 
13009  if ( p_cfg->niParamT408.enable_mb_level_rc != 1 &&
13010  p_cfg->niParamT408.enable_mb_level_rc != 0 )
13011  {
13012  ni_strncpy(p_param_err, max_err_len, "Invalid enable_mb_level_rc: out of range", max_err_len - 1);
13013  param_ret = NI_RETCODE_PARAM_ERROR_RCENABLE;
13014  LRETURN;
13015  }
13016 
13017  {
13018  if ( p_cfg->niParamT408.minQpI < 0 ||
13019  p_cfg->niParamT408.minQpI > 51 )
13020  {
13021  ni_strncpy(p_param_err, max_err_len, "Invalid min_qp: out of range", max_err_len - 1);
13022  param_ret = NI_RETCODE_PARAM_ERROR_MN_QP;
13023  LRETURN;
13024  }
13025 
13026  if ( p_cfg->niParamT408.maxQpI < 0 ||
13027  p_cfg->niParamT408.maxQpI > 51 )
13028  {
13029  ni_strncpy(p_param_err, max_err_len, "Invalid max_qp: out of range", max_err_len - 1);
13030  param_ret = NI_RETCODE_PARAM_ERROR_MX_QP;
13031  LRETURN;
13032  }
13033 
13034  if ( p_cfg->niParamT408.enable_cu_level_rate_control != 1 &&
13036  {
13037  ni_strncpy(p_param_err, max_err_len, "Invalid enable_cu_level_rate_control: out of range", max_err_len - 1);
13039  LRETURN;
13040  }
13041 
13042  //if (p_cfg->niParamT408.enable_cu_level_rate_control == 1)
13043  {
13044  if ( p_cfg->niParamT408.enable_hvs_qp != 1 &&
13045  p_cfg->niParamT408.enable_hvs_qp != 0 )
13046  {
13047  ni_strncpy(p_param_err, max_err_len, "Invalid enable_hvs_qp: out of range", max_err_len - 1);
13049  LRETURN;
13050  }
13051 
13052  if (p_cfg->niParamT408.enable_hvs_qp)
13053  {
13054  if ( p_cfg->niParamT408.max_delta_qp < 0 ||
13055  p_cfg->niParamT408.max_delta_qp > 51 )
13056  {
13057  ni_strncpy(p_param_err, max_err_len, "Invalid max_delta_qp: out of range", max_err_len - 1);
13059  LRETURN;
13060  }
13061  }
13062  }
13063  // hrd is off when i32vbvBufferSize is 0
13064  if ((p_cfg->i32vbvBufferSize < 10 && p_cfg->i32vbvBufferSize != 0) || p_cfg->i32vbvBufferSize > 3000)
13065  {
13066  ni_strncpy(p_param_err, max_err_len, "Invalid i32vbvBufferSize: out of range", max_err_len - 1);
13068  LRETURN;
13069  }
13070  }
13071 
13072  // check valid for common param
13073  param_ret = ni_check_common_params(&p_cfg->niParamT408, p_src, p_param_err, max_err_len - 1);
13074  if (param_ret != NI_RETCODE_SUCCESS)
13075  {
13076  LRETURN;
13077  }
13078 
13079  // check valid for RC param
13080  param_ret = ni_check_ratecontrol_params(p_cfg, p_param_err, max_err_len - 1);
13081  if (param_ret != NI_RETCODE_SUCCESS)
13082  {
13083  LRETURN;
13084  }
13085 
13086  if (warning == NI_RETCODE_PARAM_WARN && param_ret == NI_RETCODE_SUCCESS)
13087  {
13088  param_ret = NI_RETCODE_PARAM_WARN;
13089  ni_strncpy(p_param_err, max_err_len, p_param_warn, max_err_len - 1);
13090  }
13091 
13092 END:
13093  free(p_param_warn);
13094  return param_ret;
13095 }
13096 
13098  ni_xcoder_params_t *p_src,
13099  char *p_param_err, uint32_t max_err_len)
13100 {
13102  int32_t low_delay = 0;
13103  int32_t intra_period_gop_step_size;
13104  int32_t i, j;
13105 
13106  if (!p_param || !p_src || !p_param_err)
13107  {
13108  ni_log(NI_LOG_ERROR, "ERROR: %s() Null pointer parameters passed\n",
13109  __func__);
13111  LRETURN;
13112  }
13113 
13114  //Zero out the error buffer
13115  memset(p_param_err, 0, max_err_len - 1);
13116 
13117  // check low-delay gop structure
13118  if (!QUADRA)
13119  {
13120  if (0 == p_param->gop_preset_index) // common gop
13121  {
13122  if (p_param->custom_gop_params.custom_gop_size > 1)
13123  {
13124  int minVal = p_param->custom_gop_params.pic_param[0].poc_offset;
13125  low_delay = 1;
13126  for (i = 1; i < p_param->custom_gop_params.custom_gop_size; i++)
13127  {
13128  if (minVal > p_param->custom_gop_params.pic_param[i].poc_offset)
13129  {
13130  low_delay = 0;
13131  break;
13132  } else
13133  {
13134  minVal = p_param->custom_gop_params.pic_param[i].poc_offset;
13135  }
13136  }
13137  }
13138  }
13139  else if (p_param->gop_preset_index == 2 ||
13140  p_param->gop_preset_index == 3 ||
13141  p_param->gop_preset_index == 6 ||
13142  p_param->gop_preset_index == 7) // low-delay case (IPPP, IBBB)
13143  {
13144  low_delay = 1;
13145  }
13146 
13147  if (low_delay)
13148  {
13149  intra_period_gop_step_size = 1;
13150  }
13151  else
13152  {
13153  if (p_param->gop_preset_index == GOP_PRESET_IDX_CUSTOM)
13154  {
13155  intra_period_gop_step_size = p_param->custom_gop_params.custom_gop_size;
13156  }
13157  else
13158  {
13159  intra_period_gop_step_size = presetGopSize[p_param->gop_preset_index];
13160  }
13161  }
13162 
13163  if (((p_param->intra_period != 0) && ((p_param->intra_period < intra_period_gop_step_size+1) == 1)) ||
13164  ((p_param->avcIdrPeriod != 0) && ((p_param->avcIdrPeriod < intra_period_gop_step_size+1) == 1)))
13165  {
13166  ni_strncpy(p_param_err, max_err_len, "Invalid intra_period and gop_preset_index: gop structure is larger than intra period", max_err_len - 1);
13168  LRETURN;
13169  }
13170 
13171  if (((!low_delay) && (p_param->intra_period != 0) && ((p_param->intra_period % intra_period_gop_step_size) != 0)) ||
13172  ((!low_delay) && (p_param->avcIdrPeriod != 0) && ((p_param->avcIdrPeriod % intra_period_gop_step_size) != 0)))
13173  {
13174  ni_strncpy(p_param_err, max_err_len, "Invalid intra_period and gop_preset_index: intra period is not a multiple of gop structure size", max_err_len - 1);
13176  LRETURN;
13177  }
13178 
13179  if (p_param->gop_preset_index == GOP_PRESET_IDX_CUSTOM)
13180  {
13181  int temp_poc[NI_MAX_GOP_NUM];
13182  int min_poc = p_param->custom_gop_params.pic_param[0].poc_offset;
13183  for (i = 0; i < p_param->custom_gop_params.custom_gop_size; i++)
13184  {
13186  {
13187  ni_strncpy(p_param_err, max_err_len, "Invalid custom gop parameters: temporal_id larger than 7", max_err_len - 1);
13189  LRETURN;
13190  }
13191 
13192  if (p_param->custom_gop_params.pic_param[i].temporal_id < 0)
13193  {
13194  ni_strncpy(p_param_err, max_err_len, "Invalid custom gop parameters: temporal_id is zero or negative", max_err_len - 1);
13196  LRETURN;
13197  }
13198  temp_poc[i] = p_param->custom_gop_params.pic_param[i].poc_offset;
13199  if (min_poc > temp_poc[i])
13200  {
13201  min_poc = temp_poc[i];
13202  }
13203  }
13204  int count_pos = 0;
13205  for (i = 0; i < p_param->custom_gop_params.custom_gop_size; i++)
13206  {
13207  for (j = 0; j < p_param->custom_gop_params.custom_gop_size; j++)
13208  {
13209  if (temp_poc[j] == min_poc)
13210  {
13211  count_pos++;
13212  min_poc++;
13213  }
13214  }
13215  }
13216  if (count_pos != p_param->custom_gop_params.custom_gop_size)
13217  {
13218  ni_strncpy(p_param_err, max_err_len, "Invalid custom gop parameters: poc_offset is invalid", max_err_len - 1);
13220  LRETURN;
13221  }
13222  }
13223  }
13224  else // QUADRA
13225  {
13226  if (p_param->custom_gop_params.custom_gop_size)
13227  {
13228  int temp_poc[NI_MAX_GOP_NUM];
13229  int min_poc = p_param->custom_gop_params.pic_param[0].poc_offset;
13230  for (i = 0; i < p_param->custom_gop_params.custom_gop_size; i++)
13231  {
13233  {
13234  ni_strncpy(p_param_err, max_err_len, "Invalid custom gop parameters: temporal_id larger than 7", max_err_len - 1);
13236  LRETURN;
13237  }
13238 
13239  if (p_param->custom_gop_params.pic_param[i].temporal_id < 0)
13240  {
13241  ni_strncpy(p_param_err, max_err_len, "Invalid custom gop parameters: temporal_id is negative", max_err_len - 1);
13243  LRETURN;
13244  }
13245 
13246  for (j = 0; j < p_param->custom_gop_params.pic_param[i].num_ref_pics; j++)
13247  {
13248  if (p_param->custom_gop_params.pic_param[i].rps[j].ref_pic == 0)
13249  {
13250  ni_strncpy(p_param_err, max_err_len, "Invalid custom gop parameters: ref pic delta cannot be 0", max_err_len - 1);
13252  LRETURN;
13253  }
13254  }
13255 
13256  for (j = 0; j < NI_MAX_REF_PIC; j++)
13257  {
13258  if (p_param->custom_gop_params.pic_param[i].rps[j].ref_pic != 0 &&
13259  p_param->custom_gop_params.pic_param[i].rps[j].ref_pic_used == -1)
13260  {
13261  ni_log(NI_LOG_ERROR,"g%drefPic%d specified without g%drefPic%dUsed specified!\n", i, j, i, j);
13263  LRETURN;
13264  }
13265  }
13266 
13267  temp_poc[i] = p_param->custom_gop_params.pic_param[i].poc_offset;
13268  if (min_poc > temp_poc[i])
13269  {
13270  min_poc = temp_poc[i];
13271  }
13272  }
13273  int count_pos = 0;
13274  for (i = 0; i < p_param->custom_gop_params.custom_gop_size; i++)
13275  {
13276  for (j = 0; j < p_param->custom_gop_params.custom_gop_size; j++)
13277  {
13278  if (temp_poc[j] == min_poc)
13279  {
13280  count_pos++;
13281  min_poc++;
13282  }
13283  }
13284  }
13285  if (count_pos != p_param->custom_gop_params.custom_gop_size)
13286  {
13287  ni_strncpy(p_param_err, max_err_len, "Invalid custom gop parameters: poc_offset is invalid", max_err_len - 1);
13289  LRETURN;
13290  }
13291  }
13292  }
13293 
13294  if (0 == p_param->use_recommend_enc_params)
13295  {
13296  // RDO
13297  {
13298  int align_32_width_flag = p_src->source_width % 32;
13299  int align_16_width_flag = p_src->source_width % 16;
13300  int align_8_width_flag = p_src->source_width % 8;
13301  int align_32_height_flag = p_src->source_height % 32;
13302  int align_16_height_flag = p_src->source_height % 16;
13303  int align_8_height_flag = p_src->source_height % 8;
13304 
13305  if (((p_param->cu_size_mode & 0x1) == 0) && ((align_8_width_flag != 0) || (align_8_height_flag != 0)))
13306  {
13307  ni_strncpy(p_param_err, max_err_len, "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 - 1);
13309  LRETURN;
13310  }
13311  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)))
13312  {
13313  ni_strncpy(p_param_err, max_err_len, "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 - 1);
13315  LRETURN;
13316  }
13317  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)))
13318  {
13319  ni_strncpy(p_param_err, max_err_len, "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 - 1);
13321  LRETURN;
13322  }
13323  }
13324  }
13325 
13326  if ((p_param->conf_win_top < 0) || (p_param->conf_win_top > 8192))
13327  {
13328  ni_strncpy(p_param_err, max_err_len, "Invalid conf_win_top: out of range", max_err_len - 1);
13330  LRETURN;
13331  }
13332  if (p_param->conf_win_top % 2)
13333  {
13334  ni_strncpy(p_param_err, max_err_len, "Invalid conf_win_top: not multiple of 2", max_err_len - 1);
13336  LRETURN;
13337  }
13338 
13339  if ((p_param->conf_win_bottom < 0) || (p_param->conf_win_bottom > 8192))
13340  {
13341  ni_strncpy(p_param_err, max_err_len, "Invalid conf_win_bottom: out of range", max_err_len - 1);
13343  LRETURN;
13344  }
13345  if (p_param->conf_win_bottom % 2)
13346  {
13347  ni_strncpy(p_param_err, max_err_len, "Invalid conf_win_bottom: not multiple of 2", max_err_len - 1);
13349  LRETURN;
13350  }
13351 
13352  if ((p_param->conf_win_left < 0) || (p_param->conf_win_left > 8192))
13353  {
13354  ni_strncpy(p_param_err, max_err_len, "Invalid conf_win_left: out of range", max_err_len - 1);
13356  LRETURN;
13357  }
13358  if (p_param->conf_win_left % 2)
13359  {
13360  ni_strncpy(p_param_err, max_err_len, "Invalid conf_win_left: not multiple of 2", max_err_len - 1);
13362  LRETURN;
13363  }
13364 
13365  if (p_param->conf_win_right < 0 || p_param->conf_win_right > 8192)
13366  {
13367  ni_strncpy(p_param_err, max_err_len, "Invalid conf_win_right: out of range", max_err_len - 1);
13369  LRETURN;
13370  }
13371  if (p_param->conf_win_right % 2)
13372  {
13373  ni_strncpy(p_param_err, max_err_len, "Invalid conf_win_right: not multiple of 2", max_err_len - 1);
13375  }
13376 
13377 END:
13378 
13379  return ret;
13380 }
13381 
13382 ni_retcode_t ni_check_ratecontrol_params(ni_encoder_config_t* p_cfg, char* p_param_err, uint32_t max_err_len)
13383 {
13385  ni_t408_config_t* p_param = NULL;
13386 
13387  if( (!p_cfg) || (!p_param_err) )
13388  {
13389  ni_log(NI_LOG_ERROR, "ERROR: %s() Null pointer parameters passed\n",
13390  __func__);
13392  LRETURN;
13393  }
13394  p_param = &p_cfg->niParamT408;
13395 
13396  //Zero out the error buffer
13397  memset(p_param_err, 0, max_err_len - 1);
13398 
13399  if (p_param->roiEnable != 0 && p_param->roiEnable != 1)
13400  {
13401  ni_strncpy(p_param_err, max_err_len, "Invalid roiEnable: out of range", max_err_len - 1);
13403  LRETURN;
13404  }
13405 
13406  if (p_param->roiEnable && p_param->enable_hvs_qp)
13407  {
13408  ni_strncpy(p_param_err, max_err_len, "hvsQPEnable and roiEnable: not mutually exclusive", max_err_len - 1);
13410  LRETURN;
13411  }
13412 
13413  if (p_cfg->ui8rcEnable == 1)
13414  {
13415  if (p_param->minQpP > p_param->maxQpP || p_param->minQpB > p_param->maxQpB)
13416  {
13417  ni_strncpy(p_param_err, max_err_len, "Invalid min_qp(P/B) and max_qp(P/B): min_qp cannot be larger than max_qp", max_err_len - 1);
13419  LRETURN;
13420  }
13421  }
13422 
13423 END:
13424 
13425  return ret;
13426 }
13427 
13428 
13429 /*!******************************************************************************
13430  * \brief Print xcoder user configurations
13431  *
13432  * \param
13433  *
13434  * \return
13435  *******************************************************************************/
13436 void ni_params_print(ni_xcoder_params_t *const p_encoder_params)
13437 {
13438  if (!p_encoder_params)
13439  {
13440  return;
13441  }
13442 
13443  ni_encoder_cfg_params_t *p_enc = &p_encoder_params->cfg_enc_params;
13444 
13445  ni_log(NI_LOG_DEBUG, "XCoder Params:\n");
13446 
13447  ni_log(NI_LOG_DEBUG, "preset=%d\n", p_encoder_params->preset);
13448  ni_log(NI_LOG_DEBUG, "fps_number / fps_denominator=%u / %u\n",
13449  p_encoder_params->fps_number,
13450  p_encoder_params->fps_denominator);
13451 
13452  ni_log(NI_LOG_DEBUG, "source_width x source_height=%dx%d\n", p_encoder_params->source_width, p_encoder_params->source_height);
13453  ni_log(NI_LOG_DEBUG, "bitrate=%d\n", p_encoder_params->bitrate);
13454 
13455  ni_log(NI_LOG_DEBUG, "profile=%d\n", p_enc->profile);
13456  ni_log(NI_LOG_DEBUG, "level_idc=%d\n", p_enc->level_idc);
13457  ni_log(NI_LOG_DEBUG, "high_tier=%d\n", p_enc->high_tier);
13458 
13459  ni_log(NI_LOG_DEBUG, "frame_rate=%d\n", p_enc->frame_rate);
13460 
13461  ni_log(NI_LOG_DEBUG, "use_recommend_enc_params=%d\n", p_enc->use_recommend_enc_params);
13462  ni_log(NI_LOG_DEBUG, "cu_size_mode=%d\n", p_enc->cu_size_mode);
13463  ni_log(NI_LOG_DEBUG, "max_num_merge=%d\n", p_enc->max_num_merge);
13464  ni_log(NI_LOG_DEBUG, "enable_dynamic_8x8_merge=%d\n", p_enc->enable_dynamic_8x8_merge);
13465  ni_log(NI_LOG_DEBUG, "enable_dynamic_16x16_merge=%d\n", p_enc->enable_dynamic_16x16_merge);
13466  ni_log(NI_LOG_DEBUG, "enable_dynamic_32x32_merge=%d\n", p_enc->enable_dynamic_32x32_merge);
13467  // trans_rate not available in Rev B
13468  ni_log(NI_LOG_DEBUG, "enable_rate_control=%d\n", p_enc->rc.enable_rate_control);
13469  ni_log(NI_LOG_DEBUG, "enable_cu_level_rate_control=%d\n", p_enc->rc.enable_cu_level_rate_control);
13470  ni_log(NI_LOG_DEBUG, "enable_hvs_qp=%d\n", p_enc->rc.enable_hvs_qp);
13471  ni_log(NI_LOG_DEBUG, "enable_hvs_qp_scale=%d\n", p_enc->rc.enable_hvs_qp_scale);
13472  ni_log(NI_LOG_DEBUG, "hvs_qp_scale=%d\n", p_enc->rc.hvs_qp_scale);
13473  ni_log(NI_LOG_DEBUG, "min_qp=%d\n", p_enc->rc.min_qp);
13474  ni_log(NI_LOG_DEBUG, "max_qp=%d\n", p_enc->rc.max_qp);
13475  ni_log(NI_LOG_DEBUG, "max_delta_qp=%d\n", p_enc->rc.max_delta_qp);
13476  ni_log(NI_LOG_DEBUG, "vbv_buffer_size=%d\n", p_enc->rc.vbv_buffer_size);
13477  ni_log(NI_LOG_DEBUG, "enable_filler=%d\n", p_enc->rc.enable_filler);
13478  ni_log(NI_LOG_DEBUG, "enable_pic_skip=%d\n", p_enc->rc.enable_pic_skip);
13479 
13480  ni_log(NI_LOG_DEBUG, "forcedHeaderEnable=%d\n", p_enc->forced_header_enable);
13481  ni_log(NI_LOG_DEBUG, "roi_enable=%d\n", p_enc->roi_enable);
13482  ni_log(NI_LOG_DEBUG, "long_term_ref_enable=%d\n", p_enc->long_term_ref_enable);
13483  ni_log(NI_LOG_DEBUG, "long_term_ref_interval=%d\n", p_enc->long_term_ref_interval);
13484  ni_log(NI_LOG_DEBUG, "long_term_ref_count=%d\n", p_enc->long_term_ref_count);
13485  ni_log(NI_LOG_DEBUG, "conf_win_top=%d\n", p_enc->conf_win_top);
13486  ni_log(NI_LOG_DEBUG, "conf_win_bottom=%d\n", p_enc->conf_win_bottom);
13487  ni_log(NI_LOG_DEBUG, "conf_win_left=%d\n", p_enc->conf_win_left);
13488  ni_log(NI_LOG_DEBUG, "conf_win_right=%d\n", p_enc->conf_win_right);
13489 
13490  ni_log(NI_LOG_DEBUG, "intra_qp=%d\n", p_enc->rc.intra_qp);
13491  ni_log(NI_LOG_DEBUG, "enable_mb_level_rc=%d\n", p_enc->rc.enable_mb_level_rc);
13492 
13493  ni_log(NI_LOG_DEBUG, "intra_period=%d\n", p_enc->intra_period);
13494  ni_log(NI_LOG_DEBUG, "decoding_refresh_type=%d\n", p_enc->decoding_refresh_type);
13495 
13496  // Rev. B: H.264 only or HEVC-shared parameters, in ni_t408_config_t
13497  ni_log(NI_LOG_DEBUG, "enable_transform_8x8=%d\n", p_enc->enable_transform_8x8);
13498  ni_log(NI_LOG_DEBUG, "slice_mode=%d\n", p_enc->slice_mode);
13499  ni_log(NI_LOG_DEBUG, "slice_arg=%d\n", p_enc->slice_arg);
13500  ni_log(NI_LOG_DEBUG, "entropy_coding_mode=%d\n", p_enc->entropy_coding_mode);
13501  ni_log(NI_LOG_DEBUG, "intra_mb_refresh_mode=%d\n", p_enc->intra_mb_refresh_mode);
13502  ni_log(NI_LOG_DEBUG, "intra_mb_refresh_arg=%d\n", p_enc->intra_mb_refresh_arg);
13503  ni_log(NI_LOG_DEBUG, "intra_reset_refresh=%d\n", p_enc->intra_reset_refresh);
13504 
13505  ni_log(NI_LOG_DEBUG, "gop_preset_index=%d\n", p_enc->gop_preset_index);
13506 #ifndef QUADRA
13507  if (!QUADRA)
13508  {
13510  {
13511  int i;
13512  ni_log(NI_LOG_DEBUG, "custom_gop_params.custom_gop_size=%d\n", p_enc->custom_gop_params.custom_gop_size);
13513  for (i = 0; i < p_enc->custom_gop_params.custom_gop_size; i++)
13514  {
13515  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);
13516  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);
13517  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);
13518  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);
13519  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);
13520  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);
13521  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);
13522  }
13523  }
13524  }
13525  else // QUADRA
13526 #endif
13527  {
13529  {
13530  int i, j;
13531  ni_log(NI_LOG_DEBUG, "custom_gop_params.custom_gop_size=%d\n", p_enc->custom_gop_params.custom_gop_size);
13532  for (i = 0; i < NI_MAX_GOP_NUM; i++)
13533  {
13534  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);
13535  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);
13536  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);
13537  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);
13538  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);
13539  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);
13540  for (j = 0; j < NI_MAX_REF_PIC; j++)
13541  {
13542  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);
13543  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);
13544  }
13545  }
13546  }
13547  }
13548 
13549  return;
13550 }
13551 
13552 /*!******************************************************************************
13553  * \brief decoder keep alive thread function triggers every 1 second
13554  *
13555  * \param void thread args
13556  *
13557  * \return void
13558  *******************************************************************************/
13559 void *ni_session_keep_alive_thread(void *arguments)
13560 {
13562  ni_thread_arg_struct_t *args = (ni_thread_arg_struct_t *)arguments;
13563  ni_session_stats_t inst_info = {0};
13564  ni_session_context_t ctx = {0};
13565  uint64_t endtime = ni_gettime_ns();
13566  uint64_t current_time;
13567  ni_pthread_mutex_t *p_mutex;
13568  //interval(nanoseconds) is equals to ctx.keep_alive_timeout/3(330,000,000ns approximately equal to 1/3 second).
13569  uint64_t interval = args->keep_alive_timeout * 330000000LL;
13570 #ifndef _ANDROID
13571 #ifdef __linux__
13572  struct sched_param sched_param;
13573 
13574  // Linux has a wide variety of signals, Windows has a few.
13575  // A large number of signals will interrupt the thread, which will cause heartbeat command interval more than 1 second.
13576  // So just mask the unuseful signals in Linux
13577  sigset_t signal;
13578  sigfillset(&signal);
13579  ni_pthread_sigmask(SIG_BLOCK, &signal, NULL);
13580 
13581  /* set up schedule priority
13582  * first try to run with RR mode.
13583  * if fails, try to set nice value.
13584  * if fails either, ignore it and run with default priority.
13585  * Note: Scheduling requires root permission. App is probably exectued
13586  * without root so the priority for this thread might just end up
13587  * being default.
13588  */
13589  if (((sched_param.sched_priority = sched_get_priority_max(SCHED_RR)) ==
13590  -1) ||
13591  sched_setscheduler(syscall(SYS_gettid), SCHED_RR, &sched_param) < 0)
13592  {
13593  char errmsg[NI_ERRNO_LEN] = {0};
13594  ni_strerror(errmsg, NI_ERRNO_LEN, NI_ERRNO);
13595  ni_log(NI_LOG_DEBUG, "%s cannot set scheduler: %s\n", __func__,
13596  errmsg);
13597  if (setpriority(PRIO_PROCESS, 0, -20) != 0)
13598  {
13599  ni_strerror(errmsg, NI_ERRNO_LEN, NI_ERRNO);
13600  ni_log(NI_LOG_DEBUG, "%s cannot set nice value: %s\n", __func__,
13601  errmsg);
13602  }
13603  }
13604 
13605 #elif defined(_WIN32)
13606  /* set up schedule priority.
13607  * try to set the current thread to time critical level which is the highest prioriy
13608  * level.
13609  */
13610  if (SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL) ==
13611  0)
13612  {
13613  ni_log(NI_LOG_DEBUG, "%s cannot set priority: %d.\n", __func__,
13614  GetLastError());
13615  }
13616 #endif
13617 #endif
13618 #ifndef _WIN32
13619  // Set thread name, name is KAT + hw_id + session_id
13620  // hw_id need at most 2 bytes, session_id at most 4 bytes.
13621  char name[16] = {0};
13622  snprintf(name, sizeof(name), "%s%.2x%.4x", "KAT", args->hw_id, args->session_id);
13623 #if __linux__
13624  prctl(PR_SET_NAME, name);
13625 #elif __APPLE__
13626  pthread_setname_np(name);
13627 #endif
13628 #endif
13629  // Initializes the session context variables that keep alive command and query status command need.
13631  ctx.last_access_time = endtime;
13632  ctx.hw_id = args->hw_id;
13633  ctx.session_id = args->session_id;
13635  ctx.device_type = args->device_type;
13636  ctx.blk_io_handle = args->device_handle;
13637  ctx.event_handle = args->thread_event_handle;
13638  ctx.p_all_zero_buf = args->p_buffer;
13640  volatile uint64_t * plast_access_time = args->plast_access_time;
13641  if((ctx.last_access_time - *plast_access_time) >=
13642  (uint64_t)(ctx.keep_alive_timeout * 1000000000LL))
13643  {
13645  "%s creation timeout. session_id=0x%X requested timeout: %" PRIu64
13646  "ns, ping time delta: %" PRIu64 "ns\n ",
13647  __func__, ctx.session_id,
13648  (uint64_t)ctx.keep_alive_timeout * 1000000000LL,
13649  ctx.last_access_time - *plast_access_time);
13650  }
13651  ni_log(NI_LOG_DEBUG, "%s ctx.keep_alive_timeout: %us.\n", __func__,
13652  ctx.keep_alive_timeout);
13653  p_mutex = args->p_mutex;
13654 
13655  for (;;)
13656  {
13657  ni_pthread_mutex_lock(p_mutex);
13658 
13659  retval =
13661  ctx.event_handle, ctx.p_all_zero_buf);
13662 
13663  retval = ni_query_session_stats(&ctx,
13664  ctx.device_type,
13665  &inst_info,
13666  retval,
13668 
13669  if (NI_RETCODE_SUCCESS == retval)
13670  {
13673  ctx.device_type,
13674  ctx.hw_id,
13675  &(ctx.session_id));
13676  }
13677 
13678  ni_pthread_mutex_unlock(p_mutex);
13679 
13680  if(retval)
13681  {
13682  uint32_t error_status = inst_info.ui32LastTransactionCompletionStatus;
13683  if(error_status == NI_RETCODE_SUCCESS)
13684  {
13685  /* QDFWSH-971: Error is sometimes captured by keep_alive_thread
13686  but LastTransactionCompletionStatus may be overwrited and cause
13687  incorrect log. In this case, check LastErrorStatus.*/
13688  ni_log(NI_LOG_ERROR, "session_no 0x%x inst_err_no may be overwrited!\n",
13689  ctx.session_id);
13692  ctx.device_type,
13693  ctx.hw_id,
13694  &(ctx.session_id));
13695  error_status = inst_info.ui32LastErrorStatus;
13696  }
13698  "Persistent failures detected, %s() line-%d: session_no 0x%x sess_err_no %u "
13699  "inst_err_no %u\n",
13700  __func__, __LINE__, ctx.session_id, inst_info.ui16ErrorCount, error_status);
13701  LRETURN;
13702  }
13704  /*If the interval between two heartbeats is greater then expected(interval) or
13705  acceptable(timeout) then the thread might have been blocked.*/
13706  if ((current_time - ctx.last_access_time) >= (2 * interval) || //*2 is for safety
13707  (current_time - ctx.last_access_time) >=
13708  (uint64_t)(args->keep_alive_timeout * 1000000000LL))
13709  {
13710  ni_log(
13711  NI_LOG_INFO,
13712  "%s was possibly blocked. session_id=0x%X requested timeout: %" PRIu64
13713  "ns, ping time delta: %" PRIu64 "ns\n ",
13714  __func__, ctx.session_id,
13715  (uint64_t)ctx.keep_alive_timeout * 1000000000LL,
13717  }
13718  *plast_access_time = ctx.last_access_time = current_time;
13719  if (ctx.session_id == NI_INVALID_SESSION_ID)
13720  {
13722  }
13723 
13724  // 1. If received failure, set the close_thread flag to TRUE, and exit,
13725  // then main thread will check this flag and return failure directly;
13726  // 2. skip checking VPU recovery.
13727  // If keep_alive thread detect the VPU RECOVERY before main thread,
13728  // the close_thread flag may damage the vpu recovery handling process.
13729  if ((NI_RETCODE_SUCCESS != retval) &&
13730  (NI_RETCODE_NVME_SC_VPU_RECOVERY != retval))
13731  {
13732  LRETURN;
13733  }
13734  endtime += interval;
13735  while (ni_gettime_ns() < endtime)
13736  {
13737  if (args->close_thread)
13738  {
13739  LRETURN;
13740  }
13741  ni_usleep(10000); // 10ms per loop
13742  }
13743  }
13744 
13745 END:
13746 
13747  if (NI_RETCODE_SUCCESS != retval)
13748  {
13749  ni_log(NI_LOG_ERROR, "%s abnormal closed:%d\n", __func__, retval);
13750  // changing the value to be True here means the thread has been closed.
13751  args->close_thread = true;
13752  }
13753 
13755 
13756  ni_log(NI_LOG_DEBUG, "%s(): exit\n", __func__);
13757 
13758  return NULL;
13759 }
13760 
13761 /*!******************************************************************************
13762 * \brief Open a xcoder upload instance
13763 *
13764 * \param p_ctx - pointer to caller allocated uploader session context
13765 *
13766 * \return
13767 * On success
13768 * NI_RETCODE_SUCCESS
13769 *
13770 * On failure
13771 * NI_RETCODE_INVALID_PARAM
13772 * NI_RETCODE_ERROR_MEM_ALOC
13773 * NI_RETCODE_ERROR_INVALID_SESSION
13774 * NI_RETCODE_FAILURE
13775 *******************************************************************************/
13777 {
13779  void * p_buffer = NULL;
13780  uint32_t ui32LBA = 0;
13781  uint32_t modelled_load;
13782 
13783  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
13784 
13785  if (!p_ctx)
13786  {
13787  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): passed parameters are null!, return\n",
13788  __func__);
13789  retval = NI_RETCODE_INVALID_PARAM;
13790  LRETURN;
13791  }
13792 
13793  //Create the session if the create session flag is set
13794  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
13795  {
13797  p_ctx->pts_table = NULL;
13798  p_ctx->dts_queue = NULL;
13799  p_ctx->p_leftover = NULL;
13800  p_ctx->buffer_pool = NULL;
13801  p_ctx->prev_size = 0;
13802  p_ctx->sent_size = 0;
13803  p_ctx->status = 0;
13804  p_ctx->key_frame_type = 0;
13805  p_ctx->ready_to_close = 0;
13806  p_ctx->rc_error_count = 0;
13807  p_ctx->frame_num = 0;
13808  p_ctx->pkt_num = 0;
13809  p_ctx->pkt_index = 0;
13810 
13811  //malloc zero data buffer
13812  if (ni_posix_memalign(&p_ctx->p_all_zero_buf, sysconf(_SC_PAGESIZE),
13814  {
13815  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() alloc all zero buffer failed\n",
13816  NI_ERRNO, __func__);
13817  retval = NI_RETCODE_ERROR_MEM_ALOC;
13818  LRETURN;
13819  }
13820  memset(p_ctx->p_all_zero_buf, 0, NI_DATA_BUFFER_LEN);
13821 
13822  //malloc data buffer
13823  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), NI_DATA_BUFFER_LEN))
13824  {
13825  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() alloc data buffer failed\n",
13826  NI_ERRNO, __func__);
13827  retval = NI_RETCODE_ERROR_MEM_ALOC;
13828  LRETURN;
13829  }
13830  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
13831 
13832  //Set session ID to be invalid. In case we cannot open session, the session id wold remain invalid.
13833  //In case we can open sesison, the session id would become valid.
13834  ((ni_session_stats_t *)p_buffer)->ui16SessionId =
13835  (uint16_t)NI_INVALID_SESSION_ID;
13836 
13837  // First uint32_t is either an invaild session ID or a valid session ID, depending on if session could be opened
13838  ui32LBA = OPEN_SESSION_CODEC(NI_DEVICE_TYPE_ENCODER, ni_htonl(p_ctx->codec_format), 1/*1 for uploadMode*/);
13839  retval = ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
13840  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
13841  if (retval != NI_RETCODE_SUCCESS)
13842  {
13843  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR ni_nvme_send_read_cmd\n");
13844  LRETURN;
13845  }
13846  //Open will return a session status structure with a valid session id if it worked.
13847  //Otherwise the invalid session id set before the open command will stay
13848  p_ctx->session_id = ni_ntohs(((ni_session_stats_t *)p_buffer)->ui16SessionId);
13849  p_ctx->session_timestamp = ni_htonl(((ni_session_stats_t *)p_buffer)->ui32Session_timestamp_high);
13850  p_ctx->session_timestamp = (p_ctx->session_timestamp << 32) |
13851  ni_htonl(((ni_session_stats_t *)p_buffer)->ui32Session_timestamp_low);
13852  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
13853  {
13854  ni_log2(p_ctx, NI_LOG_ERROR,
13855  "ERROR %s(): p_ctx->device_handle=%" PRIx64
13856  ", p_ctx->hw_id=%d, p_ctx->session_id=%d\n",
13857  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
13858  p_ctx->session_id);
13859  ni_encoder_session_close(p_ctx, 0);
13861  LRETURN;
13862  }
13863  ni_log2(p_ctx, NI_LOG_DEBUG,
13864  "Uploader open session ID:0x%x,timestamp:%" PRIu64 "\n",
13865  p_ctx->session_id, p_ctx->session_timestamp);
13866 
13867  //Send keep alive timeout Info
13868  uint64_t keep_alive_timeout =
13869  p_ctx->keep_alive_timeout * 1000000; //send us to FW
13870  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
13871  memcpy(p_buffer, &keep_alive_timeout, sizeof(keep_alive_timeout));
13872  ni_log2(p_ctx, NI_LOG_DEBUG, "%s keep_alive_timeout %" PRIx64 "\n", __func__,
13873  keep_alive_timeout);
13875  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
13876  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
13877  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config, p_ctx->device_type,
13878  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
13879  CHECK_VPU_RECOVERY(retval);
13880 
13881  if (NI_RETCODE_SUCCESS != retval)
13882  {
13883  ni_log2(p_ctx, NI_LOG_ERROR,
13884  "ERROR %s(): nvme write keep_alive_timeout command "
13885  "failed, blk_io_handle: %" PRIx64 ", hw_id, %d\n",
13886  __func__, (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id);
13888  LRETURN;
13889  }
13890 
13891  // only update firmware with pixel rate if firmware >= 6rf
13892  if (ni_cmp_fw_api_ver(
13893  (char *) &p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6rf") >= 0)
13894  {
13895  /* Add the modelled load of the hwuploader */
13896  if (p_ctx->framerate.framerate_denom == 0)
13897  {
13898  // unknown or variable frame rate. Assume 25 fps
13899  modelled_load = (p_ctx->active_video_width * p_ctx->active_video_height) * 25;
13900  }
13901  else
13902  {
13903  modelled_load = (p_ctx->active_video_width * p_ctx->active_video_height *
13905  }
13906 
13907  // modelled_load can overflow a 4-byte value so we will downscale it to
13908  // kilopixels per sec by shifting the modelled load down by 10 bits.
13909  modelled_load >>= 10;
13910 
13912 
13913  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
13914  memcpy(p_buffer, &modelled_load, 4);
13915 
13916  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
13917  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
13918 
13919  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config, p_ctx->device_type,
13920  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
13921  CHECK_VPU_RECOVERY(retval);
13922 
13923  if (NI_RETCODE_SUCCESS != retval)
13924  {
13926  LRETURN;
13927  }
13928  }
13929 
13930  ni_log2(p_ctx, NI_LOG_DEBUG, "Open session completed\n");
13931  }
13932 
13933  // init for frame pts calculation
13934  p_ctx->is_first_frame = 1;
13935  p_ctx->last_pts = 0;
13936  p_ctx->last_dts = 0;
13937 
13938  ni_timestamp_init(p_ctx, &p_ctx->pts_table, "dec_pts");
13939  ni_timestamp_init(p_ctx, &p_ctx->dts_queue, "dec_dts");
13940 
13941  //p_ctx->active_video_width = 0;
13942  //p_ctx->active_video_height = 0;
13943 
13944  ni_log2(p_ctx, NI_LOG_DEBUG,
13945  "%s(): p_ctx->device_handle=%" PRIx64 ", p_ctx->hw_id=%d, "
13946  "p_ctx->session_id=%d\n",
13947  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
13948  p_ctx->session_id);
13949 
13950  p_ctx->hw_action = NI_CODEC_HW_NONE;
13951 #if !defined(_WIN32) && !defined(__APPLE__) && !defined(__OPEN_HARMONY__) && !defined(_ANDROID)
13952  // If this is a P2P upload session, open the Netint kernel driver
13953  if (p_ctx->isP2P)
13954  {
13955  retval = p2p_fill_pcie_address(p_ctx);
13956  if(retval != NI_RETCODE_SUCCESS)
13957  {
13958  LRETURN;
13959  }
13960  }
13961 #endif
13962 
13963 END:
13964 
13965  ni_aligned_free(p_buffer);
13966  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
13967  return retval;
13968 }
13969 
13970 /*!******************************************************************************
13971 * \brief Copy a xcoder decoder worker thread info
13972 *
13973 * \param
13974 *
13975 * \return
13976 *******************************************************************************/
13978 {
13979  if (!src_p_ctx || !dst_p_ctx)
13980  {
13981  ni_log(NI_LOG_ERROR, "ERROR %s(): passed parameters are null!, return\n", __func__);
13982  return NI_RETCODE_INVALID_PARAM;
13983  }
13984 
13985  dst_p_ctx->pext_mutex = src_p_ctx->pext_mutex; //for hwdl
13986  dst_p_ctx->max_nvme_io_size = src_p_ctx->max_nvme_io_size;
13987  dst_p_ctx->device_handle = src_p_ctx->device_handle;
13988  dst_p_ctx->blk_io_handle = src_p_ctx->blk_io_handle;
13989  dst_p_ctx->hw_id = src_p_ctx->hw_id;
13990  dst_p_ctx->session_timestamp = src_p_ctx->session_timestamp;
13991  memcpy(dst_p_ctx->fw_rev, src_p_ctx->fw_rev, sizeof(src_p_ctx->fw_rev));
13992  if (src_p_ctx->isP2P)
13993  {
13994  dst_p_ctx->isP2P = src_p_ctx->isP2P;
13995  dst_p_ctx->ddr_config = src_p_ctx->ddr_config;
13996  dst_p_ctx->domain = src_p_ctx->domain;
13997  dst_p_ctx->bus = src_p_ctx->bus;
13998  dst_p_ctx->dev = src_p_ctx->dev;
13999  dst_p_ctx->fn = src_p_ctx->fn;
14000  dst_p_ctx->netint_fd = src_p_ctx->netint_fd;
14001  }
14002 
14003  return NI_RETCODE_SUCCESS;
14004 }
14005 
14006 
14007 /*!******************************************************************************
14008  * \brief Query and acquire buffer from xcoder scaler instance
14009  *
14010  * \param[in] p_ctx pointer to scaler session context
14011  *
14012  * \return NI_RETCODE_INVALID_PARAM
14013  * NI_RETCODE_ERROR_INVALID_SESSION
14014  * NI_RETCODE_ERROR_MEM_ALOC
14015  * NI_RETCODE_ERROR_NVME_CMD_FAILED
14016  * NI_RETCODE_FAILURE
14017  *******************************************************************************/
14019 {
14021  ni_instance_buf_info_t sInstanceBuf = {0};
14022  int query_retry = 0;
14023 
14024  if (p_ctx->session_id == NI_INVALID_SESSION_ID)
14025  {
14026  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
14027  __func__);
14029  }
14030 
14031  if (p_ctx->buffered_frame_index > 0)
14032  {
14033  ni_log2(p_ctx, NI_LOG_ERROR,
14034  "Session=0x%x: %s buffered_frame_index=%d return %d\n",
14035  p_ctx->session_id,
14036  __func__,
14037  p_ctx->buffered_frame_index,
14038  retval);
14039  return retval;
14040  }
14041 
14042  for (;;)
14043  {
14044  query_sleep(p_ctx);
14045  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: query by ni_query_instance_buf_info INST_BUF_INFO_RW_UPLOAD\n", __func__);
14047  NI_DEVICE_TYPE_SCALER, &sInstanceBuf);
14048 
14049  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
14050  p_ctx->device_type, p_ctx->hw_id,
14051  &(p_ctx->session_id), OPT_3);
14052 
14053  /* A frame index of zero is invalid, the memory acquisition failed */
14054  if (NI_RETCODE_SUCCESS != retval || (sInstanceBuf.hw_inst_ind.frame_index == 0 && query_retry >=500))
14055  {
14056  if (query_retry >=500)
14057  {
14058  ni_log2(p_ctx, NI_LOG_DEBUG,
14059  "scaler write exceeded max query retries. rc=%d try=%d\n",
14060  retval, query_retry);
14061  }
14062  retval = NI_RETCODE_ERROR_MEM_ALOC;
14063  LRETURN;
14064  }
14065  if (sInstanceBuf.hw_inst_ind.frame_index == 0 && query_retry < 500)
14066  {
14067  query_retry++;
14068  ni_pthread_mutex_unlock(&p_ctx->mutex);
14069  ni_usleep(100);
14070  ni_pthread_mutex_lock(&p_ctx->mutex);
14071  }
14072  else
14073  {
14074  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: FID %d\n", __func__,
14075  sInstanceBuf.hw_inst_ind.frame_index);
14076  p_ctx->buffered_frame_index = sInstanceBuf.hw_inst_ind.frame_index;
14077  break;
14078  }
14079  }
14080 
14081  ni_log2(p_ctx, NI_LOG_DEBUG,
14082  "Session=0x%x: %s got FrameIndex=%u buffered_frame_index=%d\n",
14083  p_ctx->session_id,
14084  __func__,
14085  sInstanceBuf.hw_inst_ind.frame_index,p_ctx->buffered_frame_index);
14086 
14087 END:
14088 
14089  if (NI_RETCODE_SUCCESS != retval)
14090  {
14091  ni_log2(p_ctx, NI_LOG_DEBUG,
14092  "%s Warning scalar read hwdesc fail rc %d or ind !\n",
14093  __func__, retval);
14094  }
14095  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
14096 
14097  return retval;
14098 }
14099 
14100 
14101 /*!******************************************************************************
14102 * \brief Query and acquire buffer from xcoder upload instance
14103 *
14104 * \param p_ctx pointer to uploader session context
14105 *
14106 * \return NI_RETCODE_SUCCESS
14107 * NI_RETCODE_ERROR_INVALID_SESSION
14108 * NI_RETCODE_ERROR_MEM_ALOC
14109 *******************************************************************************/
14111 {
14113  //uint32_t metadata_size = NI_APP_ENC_FRAME_META_DATA_SIZE;
14114  uint32_t frame_size_bytes = 0;
14115  uint32_t retry_count = 0;
14116  ni_instance_buf_info_t buf_info = { 0 };
14117 
14118  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
14119 
14120  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
14121  {
14122  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
14123  __func__);
14125  LRETURN;
14126  }
14127 
14128 //#ifdef MEASURE_LATENCY
14129 #if 0
14130  if ((p_frame->pts != NI_NOPTS_VALUE) && (p_ctx->frame_time_q != NULL))
14131  {
14132  uint64_t abs_time_ns = ni_gettime_ns();
14134  abs_time_ns, p_frame->pts);
14135  }
14136 #endif
14137 
14138  //p_ctx->status = 0;
14139 
14140  if (p_ctx->buffered_frame_index > 0)
14141  return retval;
14142 
14143  for (;;)
14144  {
14145  query_sleep(p_ctx);
14146 
14148  NI_DEVICE_TYPE_ENCODER, &buf_info);
14149  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
14150  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
14151  if (NI_RETCODE_SUCCESS != retval ||
14152  (buf_info.hw_inst_ind.buffer_avail == 0 && retry_count >= 500))
14153  {
14154  if (retry_count >= 500)
14155  {
14156  ni_log2(p_ctx, NI_LOG_DEBUG,
14157  "hwupload write exceeded max query retries. rc=%d try=%d"
14158  "\n",
14159  retval, retry_count);
14160  }
14161  retval = NI_RETCODE_ERROR_MEM_ALOC;
14162  //p_ctx->status = NI_RETCODE_NVME_SC_WRITE_BUFFER_FULL;
14163  LRETURN;
14164  }
14165  if (buf_info.hw_inst_ind.buffer_avail == 0 && retry_count < 500)
14166  {
14167  retry_count++;
14168  ni_pthread_mutex_unlock(&p_ctx->mutex);
14169  ni_usleep(100);
14170  ni_pthread_mutex_lock(&p_ctx->mutex);
14171  }
14172  else
14173  {
14174  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: avail %d, FID %d\n", __func__,
14175  buf_info.hw_inst_ind.buffer_avail,
14176  buf_info.hw_inst_ind.frame_index);
14177 
14178  p_ctx->buffered_frame_index = buf_info.hw_inst_ind.frame_index;
14179  break;
14180  }
14181  }
14182  ni_log2(p_ctx, NI_LOG_DEBUG, "Info hwupload write query success, available buf "
14183  "size %u >= frame size %u , retry %u\n",
14184  buf_info.buf_avail_size, frame_size_bytes, retry_count);
14185 
14186 //#ifdef MEASURE_LATENCY
14187 #if 0
14188  if ((p_frame->pts != NI_NOPTS_VALUE) && (p_ctx->frame_time_q != NULL))
14189  {
14190  uint64_t abs_time_ns = ni_gettime_ns();
14192  ni_log(NI_LOG_INFO, "PTS:%" PRId64 ",DELTA:%" PRId64 ",uLAT:%" PRIu64 ";\n",
14193  p_frame->pts, abs_time_ns - q->last_benchmark_time,
14194  ni_lat_meas_q_check_latency(q, abs_time_ns, p_frame->pts));
14195  q->last_benchmark_time = abs_time_ns;
14196  }
14197 #endif
14198 
14199  //retval = size;
14200 
14201 END:
14202 
14203  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
14204  return retval;
14205 }
14206 
14207 
14208 /*!******************************************************************************
14209 * \brief Send a YUV p_frame to upload session
14210 *
14211 * \param
14212 *
14213 * \return
14214 *******************************************************************************/
14216  niFrameSurface1_t *hwdesc)
14217 {
14218  int retval = 0;
14219  uint32_t size = 0;
14220  //uint32_t metadata_size = NI_APP_ENC_FRAME_META_DATA_SIZE;
14221  uint32_t i = 0;
14222  uint32_t sent_size = 0;
14223  uint32_t frame_size_bytes = 0;
14224  uint32_t retry_count = 0;
14225  ni_instance_buf_info_t buf_info = { 0 };
14226 
14227  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
14228  //niFrameSurface1_t* p_data3 = (niFrameSurface1_t*)((uint8_t*)p_frame->p_data[3]);
14230  //ni_log2(p_ctx, NI_LOG_DEBUG, "%s:mar16 HW=%d ui16FrameIdx=%d i8InstID=%d device_handle=%d\n",
14231  // ishwframe, p_data3->ui16FrameIdx, p_data3->i8InstID, p_data3->device_handle);
14232 
14233  if (!p_ctx || !p_frame)
14234  {
14235  ni_log(NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
14236  __func__);
14237  retval = NI_RETCODE_INVALID_PARAM;
14238  LRETURN;
14239  }
14240 
14241  uint8_t separate_metadata = p_frame->separate_metadata;
14242  uint8_t separate_start = (p_frame->separate_start && p_frame->total_start_len) ? 1 : 0;
14243 
14244  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
14245  {
14246  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
14247  __func__);
14249  LRETURN;
14250  }
14251 
14252 #ifdef MEASURE_LATENCY
14253  if ((p_frame->pts != NI_NOPTS_VALUE) && (p_ctx->frame_time_q != NULL))
14254  {
14255  uint64_t abs_time_ns = ni_gettime_ns();
14257  abs_time_ns, p_frame->pts);
14258  }
14259 #endif
14260 
14261  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;
14262  ni_log2(p_ctx, NI_LOG_DEBUG, "frame size bytes =%u %d is metadata!\n", frame_size_bytes,
14263  0);
14264  p_ctx->status = 0;
14265 
14266  if (p_frame->end_of_stream)
14267  {
14268  retval = NI_RETCODE_SUCCESS;
14269  LRETURN;
14270  }
14271 
14272  if (p_ctx->buffered_frame_index > 0)
14273  {
14274  buf_info.hw_inst_ind.frame_index = p_ctx->buffered_frame_index;
14275  p_ctx->buffered_frame_index = 0;
14276  }
14277  else
14278  {
14279  for (;;)
14280  {
14281  query_sleep(p_ctx);
14282 
14284  NI_DEVICE_TYPE_ENCODER, &buf_info);
14285  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
14286  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
14287  if (NI_RETCODE_SUCCESS != retval ||
14288  (buf_info.hw_inst_ind.buffer_avail == 0 && retry_count >= 500))
14289  {
14290  if (retry_count >= 500)
14291  {
14292  ni_log2(p_ctx, NI_LOG_DEBUG,
14293  "hwupload write exceeded max query retries. rc=%d try=%d"
14294  "\n",
14295  retval, retry_count);
14296  }
14297  retval = NI_RETCODE_ERROR_MEM_ALOC;
14299  LRETURN;
14300  }
14301  if (buf_info.hw_inst_ind.buffer_avail == 0 && retry_count < 500)
14302  {
14303  retry_count++;
14304  ni_pthread_mutex_unlock(&p_ctx->mutex);
14305  ni_usleep(100);
14306  ni_pthread_mutex_lock(&p_ctx->mutex);
14307  } else //available
14308  {
14309  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: avail %d, FID %d\n", __func__,
14310  buf_info.hw_inst_ind.buffer_avail,
14311  buf_info.hw_inst_ind.frame_index);
14312  break;
14313  }
14314  }
14315  ni_log2(p_ctx, NI_LOG_DEBUG, "Info hwupload write query success, available buf "
14316  "size %u >= frame size %u , retry %u\n",
14317  buf_info.buf_avail_size, frame_size_bytes, retry_count);
14318  }
14319 
14320  {
14321 #ifdef XCODER_TIMESTAMP_DTS_ENABLED
14322  retval = ni_timestamp_register(p_ctx->buffer_pool, p_ctx->dts_queue,
14323  p_frame->dts, 0);
14324  if (NI_RETCODE_SUCCESS != retval)
14325  {
14326  ni_log2(p_ctx, NI_LOG_ERROR,
14327  "ERROR %s(): ni_timestamp_register() for dts "
14328  "returned: %d\n",
14329  __func__, retval);
14330  }
14331 #endif
14332 
14333  //Apply write configuration here
14334  retval = ni_config_session_rw(p_ctx, SESSION_WRITE_CONFIG, 1,
14335  NI_CODEC_HW_UPLOAD, 0);
14336  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
14337  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
14338  CHECK_VPU_RECOVERY(retval);
14339 
14340  if (separate_metadata)
14341  {
14343  "6S") < 0)
14344  {
14345  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): uploader separated metadata not supported on device with FW api version < 6.S\n",
14346  __func__);
14348  LRETURN;
14349  }
14350 
14351  if (!p_frame->p_metadata_buffer)
14352  {
14353  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): p_metadata_buffer is NULL, allocation failed?\n",
14354  __func__);
14355  retval = NI_RETCODE_ERROR_MEM_ALOC;
14356  LRETURN;
14357  }
14358 
14359  // fill in metadata
14360  ni_metadata_enc_frame_t *p_meta;
14361  p_meta = (ni_metadata_enc_frame_t *)p_frame->p_metadata_buffer;
14362 
14363  if (separate_start)
14364  {
14365  for (i = 0; i < NI_MAX_NUM_SW_FRAME_DATA_POINTERS; i++)
14366  p_meta->start_len[i] = p_frame->start_len[i];
14367  }
14368  else
14369  {
14370  memset(p_meta->start_len, 0, sizeof(p_meta->start_len));
14371  }
14373 
14374  ni_log(
14375  NI_LOG_DEBUG,
14376  "%s(): %d.%u p_ctx->frame_num=%" PRIu64 ", "
14377  "p_frame->video_width=%u, p_frame->video_height=%u, "
14378  "start_len [%u,%u,%u] inconsecutive_transfer %u\n",
14379  __func__, p_ctx->hw_id, p_ctx->session_id, p_ctx->frame_num,
14380  p_frame->video_width, p_frame->video_height,
14381  p_meta->start_len[0], p_meta->start_len[1], p_meta->start_len[2],
14382  p_meta->inconsecutive_transfer);
14383 
14384  uint32_t ui32LBA_metadata =
14386  ni_log2(p_ctx, NI_LOG_DEBUG,
14387  "%s: p_metadata_buffer = %p, metadata_buffer_size "
14388  "= %u, p_ctx->frame_num = %" PRIu64 ", LBA = 0x%x\n",
14389  __func__, p_frame->p_metadata_buffer,
14390  p_frame->metadata_buffer_size, p_ctx->frame_num,
14391  ui32LBA_metadata);
14392 
14393  sent_size =
14395 
14396  retval = ni_nvme_send_write_cmd(
14397  p_ctx->blk_io_handle, p_ctx->event_handle,
14398  p_frame->p_metadata_buffer, sent_size,
14399  ui32LBA_metadata);
14400  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_write, p_ctx->device_type,
14401  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
14402  CHECK_VPU_RECOVERY(retval);
14403  if (retval < 0)
14404  {
14405  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n",
14406  __func__);
14408  LRETURN;
14409  }
14410  }
14411 
14412  if (separate_start)
14413  {
14414  if (!p_frame->p_start_buffer)
14415  {
14416  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): p_start_buffer is NULL, allocation failed?\n",
14417  __func__);
14418  retval = NI_RETCODE_ERROR_MEM_ALOC;
14419  LRETURN;
14420  }
14421 
14422  uint32_t ui32LBA =
14424  ni_log2(p_ctx, NI_LOG_DEBUG,
14425  "%s: p_start_buffer = %p, p_frame->start_buffer_size "
14426  "= %u, p_ctx->frame_num = %" PRIu64 ", LBA = 0x%x\n",
14427  __func__, p_frame->p_start_buffer, p_frame->start_buffer_size, p_ctx->frame_num,
14428  ui32LBA);
14429 
14430  sent_size =
14432 
14433  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
14434  p_frame->p_start_buffer, sent_size, ui32LBA);
14435  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_write, p_ctx->device_type,
14436  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
14437  CHECK_VPU_RECOVERY(retval);
14438  if (retval < 0)
14439  {
14440  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
14442  LRETURN;
14443  }
14444  }
14445 
14446  if (p_frame->inconsecutive_transfer)
14447  {
14448  uint32_t ui32LBA =
14450 
14451  for (i = 0; i < NI_MAX_NUM_SW_FRAME_DATA_POINTERS; i++)
14452  {
14453  ni_log2(p_ctx, NI_LOG_DEBUG,
14454  "%s: p_data = %p, p_frame->buffer_size "
14455  "= %u, p_ctx->frame_num = %" PRIu64 ", LBA = 0x%x\n",
14456  __func__, p_frame->p_data, p_frame->buffer_size, p_ctx->frame_num,
14457  ui32LBA);
14458 
14459  if (p_frame->data_len[i])
14460  {
14461  sent_size = p_frame->data_len[i];
14462  if (separate_start)
14463  sent_size -= p_frame->start_len[i];
14464 
14465  sent_size =
14467 
14468  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
14469  p_frame->p_data[i]+p_frame->start_len[i], sent_size, ui32LBA);
14470  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_write, p_ctx->device_type,
14471  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
14472  CHECK_VPU_RECOVERY(retval);
14473  if (retval < 0)
14474  {
14475  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
14477  LRETURN;
14478  }
14479  }
14480  }
14481  }
14482  else
14483  {
14484  uint32_t ui32LBA =
14486  ni_log2(p_ctx, NI_LOG_DEBUG,
14487  "%s: p_data = %p, p_frame->buffer_size = %u, "
14488  "p_ctx->frame_num = %" PRIu64 ", LBA = 0x%x\n",
14489  __func__, p_frame->p_data, p_frame->buffer_size, p_ctx->frame_num,
14490  ui32LBA);
14491 
14492  sent_size = frame_size_bytes;
14493  if (separate_start)
14494  sent_size -= p_frame->total_start_len;
14495 
14496  sent_size =
14498 
14499  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
14500  p_frame->p_buffer+p_frame->total_start_len, sent_size, ui32LBA);
14501  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_write, p_ctx->device_type,
14502  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
14503  CHECK_VPU_RECOVERY(retval);
14504  if (retval < 0)
14505  {
14506  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
14508  LRETURN;
14509  }
14510  }
14511 
14512  hwdesc->ui16FrameIdx = buf_info.hw_inst_ind.frame_index;
14513  hwdesc->ui16session_ID = p_ctx->session_id;
14514  hwdesc->device_handle =
14515  (int32_t)((int64_t)p_ctx->blk_io_handle & 0xFFFFFFFF);
14516  hwdesc->bit_depth = p_ctx->bit_depth_factor;
14517  hwdesc->src_cpu = (uint8_t)NI_DEVICE_TYPE_ENCODER;
14518  hwdesc->output_idx = hwdesc->ui32nodeAddress = 0;
14519 
14520  p_ctx->frame_num++;
14521  size = frame_size_bytes;
14522 
14523 #ifdef XCODER_DUMP_DATA
14524  char dump_file[256];
14525  snprintf(dump_file, sizeof(dump_file), "%ld-%u-hwup-fme/fme-%04ld.yuv",
14526  (long)getpid(), p_ctx->session_id, (long)p_ctx->frame_num);
14527 
14528  FILE *f = NULL;
14529  ni_fopen(&f, dump_file, "wb");
14530  fwrite(p_frame->p_buffer,
14531  p_frame->data_len[0] + p_frame->data_len[1] + p_frame->data_len[2],
14532  1, f);
14533  fflush(f);
14534  fclose(f);
14535 #endif
14536  }
14537 
14538 #ifdef MEASURE_LATENCY
14539  if ((p_frame->pts != NI_NOPTS_VALUE) && (p_ctx->frame_time_q != NULL))
14540  {
14541  uint64_t abs_time_ns = ni_gettime_ns();
14543  ni_log(NI_LOG_INFO, "PTS:%" PRId64 ",DELTA:%" PRId64 ",uLAT:%" PRIu64 ";\n",
14544  p_frame->pts, abs_time_ns - q->last_benchmark_time,
14545  ni_lat_meas_q_check_latency(q, abs_time_ns, p_frame->pts));
14546  q->last_benchmark_time = abs_time_ns;
14547  }
14548 #endif
14549 
14550  retval = size;
14551 
14552 END:
14553 
14554  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
14555  return retval;
14556 }
14557 
14558 /*!******************************************************************************
14559 * \brief Retrieve a HW descriptor of uploaded frame
14560 *
14561 * \param p_ctx pointer to uploader session context
14562 * hwdesc pointer to hw descriptor
14563 *
14564 * \return
14565 * On success
14566 * NI_RETCODE_SUCCESS
14567 * On failure
14568 * NI_RETCODE_INVALID_PARAM
14569 * NI_RETCODE_ERROR_INVALID_SESSION
14570 * NI_RETCODE_FAILURE
14571 *******************************************************************************/
14573  niFrameSurface1_t *hwdesc)
14574 {
14575  int retval = 0;
14576  ni_instance_buf_info_t hwdesc_info = { 0 };
14577  int query_retry = 0;
14578 
14579  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
14580 
14581  if (!p_ctx || !hwdesc)
14582  {
14583  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
14584  __func__);
14585  retval = NI_RETCODE_INVALID_PARAM;
14586  LRETURN;
14587  }
14588 
14589  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
14590  {
14591  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
14592  __func__);
14594  LRETURN;
14595  }
14596 
14597  for (;;)
14598  {
14599  query_sleep(p_ctx);
14600 
14601  query_retry++;
14602 #ifndef _WIN32
14604  NI_DEVICE_TYPE_ENCODER, &hwdesc_info);
14605 #else
14607  NI_DEVICE_TYPE_ENCODER, &hwdesc_info);
14608 #endif
14609  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
14610  p_ctx->device_type, p_ctx->hw_id,
14611  &(p_ctx->session_id), OPT_1);
14612 
14613  if (NI_RETCODE_SUCCESS != retval)
14614  {
14615  ni_log2(p_ctx, NI_LOG_DEBUG, "Warning upload read hwdesc fail rc %d or ind "
14616  "!\n", retval);
14617 
14618  if (query_retry >= 1000)
14619  {
14620  retval = NI_RETCODE_FAILURE;
14621  LRETURN;
14622  }
14623  ni_usleep(100);
14624  }
14625  else
14626  {
14627  ni_log2(p_ctx, NI_LOG_DEBUG, "Info hwupload read hwdesc success, "
14628  "frame_ind=%d !\n", hwdesc_info.hw_inst_ind.frame_index);
14629 
14630  hwdesc->ui16FrameIdx = hwdesc_info.hw_inst_ind.frame_index;
14631  hwdesc->ui16session_ID = p_ctx->session_id;
14632  hwdesc->device_handle =
14633  (int32_t)((int64_t)p_ctx->blk_io_handle & 0xFFFFFFFF);
14634  hwdesc->bit_depth = p_ctx->bit_depth_factor;
14635  hwdesc->src_cpu = (uint8_t)NI_DEVICE_TYPE_ENCODER;
14636  hwdesc->output_idx = 0;
14637  LRETURN;
14638  }
14639  }
14640 
14641 END:
14642  return retval;
14643 }
14644 
14645 /*!*****************************************************************************
14646 * \brief clear a particular xcoder instance buffer/data
14647 *
14648 * \param ni_session_context_t p_ctx - xcoder Context
14649 * \param ni_instance_buf_info_rw_type_t rw_type
14650 * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
14651 * \param ni_instance_buf_info_t *out - Struct preallocated from the caller
14652 * where the resulting data will be placed
14653 *
14654 * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION,
14655 * NI_RETCODE_ERROR_MEM_ALOC or NI_RETCODE_ERROR_NVME_CMD_FAILED on
14656 * failure
14657 ******************************************************************************/
14659 {
14660  void* p_buffer = NULL;
14662  uint32_t ui32LBA = 0;
14663 
14664  ni_log(NI_LOG_TRACE, "%s(): enter - device_handle %d\n", __func__,
14665  surface->device_handle);
14666 
14667  if ((uint16_t)NI_INVALID_SESSION_ID == surface->ui16session_ID)
14668  {
14669  ni_log(NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
14670  __func__);
14672  LRETURN;
14673  }
14674 
14675  //malloc data buffer
14676  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), NI_DATA_BUFFER_LEN))
14677  {
14678  ni_log(NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
14679  NI_ERRNO, __func__);
14680  retval = NI_RETCODE_ERROR_MEM_ALOC;
14681  LRETURN;
14682  }
14683  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
14684  ni_log(NI_LOG_DEBUG, "%s(): FID = %d\n", __func__, surface->ui16FrameIdx);
14685  ui32LBA = CLEAR_INSTANCE_BUF_W(((uint16_t)surface->ui16FrameIdx));
14686  retval = ni_nvme_send_write_cmd((ni_device_handle_t)(int64_t)surface->device_handle,
14687  NI_INVALID_DEVICE_HANDLE, p_buffer,
14688  NI_DATA_BUFFER_LEN, ui32LBA);
14689  //Cannot check sessio stats here since this isn't a session command.
14690  if (retval < 0)
14691  {
14692  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__);
14694  LRETURN;
14695  }
14696 
14697 END:
14698 
14699  ni_aligned_free(p_buffer);
14700  ni_log(NI_LOG_TRACE, "%s(): exit\n", __func__);
14701  return retval;
14702 }
14703 
14704 /*!******************************************************************************
14705 * \brief Retrieve a hw desc p_frame from decoder
14706 * \param
14707 *
14708 * \return
14709 *******************************************************************************/
14711 {
14712  //Needs serious editing to support hwdesc read again, this is currently vanilla read
14713  //queue_info decoder_read_workerqueue;
14714  ni_instance_mgr_stream_info_t data = {0};
14715  int rx_size = 0;
14716  uint64_t frame_offset = 0;
14717  uint32_t frame_dropped = 0;
14718  uint8_t *p_data_buffer = NULL;
14719  uint32_t i = 0;
14720  int retval = NI_RETCODE_SUCCESS;
14721  int metadata_hdr_size = NI_FW_META_DATA_SZ -
14723  int sei_size = 0;
14724  uint32_t total_bytes_to_read = 0;
14725  uint32_t total_yuv_met_size = 0;
14726  uint32_t read_size_bytes = 0;
14727  ni_instance_buf_info_t buf_info = {0};
14728  int query_retry = 0;
14729  uint32_t ui32LBA = 0;
14730  unsigned int bytes_read_so_far = 0;
14731  int query_type = INST_BUF_INFO_RW_READ;
14732  int low_delay_notify = 0;
14733  ni_session_statistic_t sessionStatistic = {0};
14734  uint32_t frames_dropped = 0;
14735  ni_xcoder_params_t *p_param;
14736  uint8_t get_first_metadata = 0;
14737  uint8_t sequence_change = 0;
14738 
14739  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
14740 
14741  if (!p_ctx || !p_frame)
14742  {
14743  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
14744  __func__);
14745  return NI_RETCODE_INVALID_PARAM;
14746  }
14747 
14748  ni_pthread_mutex_lock(&p_ctx->mutex);
14749 
14750 start:
14751  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
14752  {
14753  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
14754  __func__);
14756  LRETURN;
14757  }
14758 
14759  p_param = (ni_xcoder_params_t *)p_ctx->p_session_config;
14760  p_data_buffer = (uint8_t *)p_frame->p_buffer;
14761 
14762  // p_frame->p_data[] can be NULL before actual resolution is returned by
14763  // decoder and buffer pool is allocated, so no checking here.
14764 
14765  total_bytes_to_read = p_frame->data_len[3] + metadata_hdr_size;
14766  total_yuv_met_size = p_frame->data_len[0] + p_frame->data_len[1] + p_frame->data_len[2] + metadata_hdr_size;
14767  ni_log2(p_ctx, NI_LOG_DEBUG,
14768  "Total bytes to read %u total_yuv_met_size %u, low_delay %u\n",
14769  total_bytes_to_read, total_yuv_met_size, p_ctx->decoder_low_delay);
14770  if (p_ctx->decoder_low_delay > 0 && !p_ctx->ready_to_close)
14771  {
14772  ni_log2(p_ctx, NI_LOG_DEBUG, "frame_num = %" PRIu64 ", pkt_num = %" PRIu64 "\n",
14773  p_ctx->frame_num, p_ctx->pkt_num);
14774  frames_dropped = p_ctx->session_statistic.ui32FramesDropped;
14775  if (p_ctx->force_low_delay && (p_ctx->force_low_delay_cnt < frames_dropped)) {
14776  p_ctx->force_low_delay_cnt = frames_dropped;
14777  }
14778  if (p_ctx->frame_num + p_ctx->force_low_delay_cnt
14779  >= p_ctx->pkt_num)
14780  {
14781  //nothing to query, leave
14782  retval = NI_RETCODE_SUCCESS;
14783  LRETURN;
14784  }
14785  query_type = INST_BUF_INFO_RW_READ_BUSY;
14786  }
14787  for (;;)
14788  {
14789  query_sleep(p_ctx);
14790 
14791  query_retry++;
14792 
14793  if (ni_cmp_fw_api_ver(
14795  "6r3") >= 0)
14796  {
14798  &sessionStatistic);
14799  CHECK_ERR_RC(p_ctx, retval, &sessionStatistic,
14801  p_ctx->hw_id, &(p_ctx->session_id), OPT_2);
14802  CHECK_VPU_RECOVERY(retval);
14803 
14804  buf_info.buf_avail_size = sessionStatistic.ui32RdBufAvailSize;
14805  } else
14806  {
14807  retval = ni_query_instance_buf_info(p_ctx, query_type,
14808  NI_DEVICE_TYPE_DECODER, &buf_info);
14809  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
14810  p_ctx->device_type, p_ctx->hw_id,
14811  &(p_ctx->session_id), OPT_1);
14812  CHECK_VPU_RECOVERY(retval);
14813  }
14814 
14815  ni_log2(p_ctx, NI_LOG_TRACE, "Dec read desc query buf_info.size = %u\n",
14816  buf_info.buf_avail_size);
14817 
14818  if (NI_RETCODE_SUCCESS != retval)
14819  {
14820  ni_log2(p_ctx, NI_LOG_TRACE, "Dec read desc failed. Retry %d\n", query_retry);
14821 
14822  if (query_retry >= 1000)
14823  {
14824  ni_log2(p_ctx, NI_LOG_DEBUG, "Warning: dec read desc failed %d retries. rc=%d"
14825  "\n", query_retry, retval);
14826  p_ctx->max_retry_fail_count[1]++;
14827  low_delay_notify = 1;
14829  LRETURN;
14830  }
14831  ni_pthread_mutex_unlock(&p_ctx->mutex);
14833  ni_pthread_mutex_lock(&p_ctx->mutex);
14834  } else if (buf_info.buf_avail_size == DP_IPC_PASSTHRU)
14835  {
14836  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): Bad available buffer size %u\n", __FUNCTION__, buf_info.buf_avail_size);
14837  retval = NI_RETCODE_FAILURE;
14838  LRETURN;
14839  } else if (buf_info.buf_avail_size == metadata_hdr_size)
14840  {
14841  ni_log2(p_ctx, NI_LOG_DEBUG,
14842  "Dec read desc only hdr metadata is available. Seq change may "
14843  "have occured.\n");
14844  total_bytes_to_read = metadata_hdr_size;
14845  sequence_change = 1;
14846  break;
14847  } else if (buf_info.buf_avail_size < total_yuv_met_size)
14848  {
14849  ni_log2(p_ctx, NI_LOG_TRACE, "Dec read desc buf_size < frame_size. Retry %d\n", query_retry);
14850 
14851  // query to see if it is eos now, if we have sent it
14852  if (p_ctx->ready_to_close)
14853  {
14854  ni_log2(p_ctx, NI_LOG_TRACE,
14855  "Dec read desc query, ready_to_close %u, query eos\n",
14856  p_ctx->ready_to_close);
14857  retval = ni_query_stream_info(p_ctx, NI_DEVICE_TYPE_DECODER, &data);
14858  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
14859  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
14860  CHECK_VPU_RECOVERY(retval);
14861 
14862  if (data.is_flushed ||
14863  query_retry >=
14865  {
14866  if (query_retry >=
14868  {
14869  ni_log2(p_ctx, NI_LOG_ERROR,
14870  "WARNING: Dec read desc query eos reached but exceeded max "
14871  "retries. is_flushed=%u try=%d.\n",
14872  data.is_flushed, query_retry);
14873  } else
14874  {
14875  ni_log2(p_ctx, NI_LOG_DEBUG,
14876  "Dec read desc query eos reached. is_flushed=%u try=%d"
14877  "\n",
14878  data.is_flushed, query_retry);
14879  }
14880  p_frame->end_of_stream = 1;
14881  low_delay_notify = 1;
14882  retval = NI_RETCODE_SUCCESS;
14883  LRETURN;
14884  }
14885  else
14886  {
14887  ni_log2(p_ctx, NI_LOG_TRACE,
14888  "Dec read desc available buf size == %d, query try %d, "
14889  "retrying...\n",
14890  buf_info.buf_avail_size, query_retry);
14891  ni_pthread_mutex_unlock(&p_ctx->mutex);
14893  ni_pthread_mutex_lock(&p_ctx->mutex);
14894  continue;
14895  }
14896  }
14897 
14899  (p_ctx->decoder_low_delay > 0 &&
14900  ((p_ctx->frame_num + p_ctx->force_low_delay_cnt)
14901  < p_ctx->pkt_num))) &&
14902  query_retry < 1000 / 2)
14903  {
14904  if (p_ctx->decoder_low_delay && p_ctx->force_low_delay) {
14905  if (p_ctx->session_statistic.ui32FramesDropped > frames_dropped) {
14906  // last pkt sent to decoder marked as dropped, no output,
14907  // so just stop query and return
14908  p_ctx->force_low_delay_cnt++;
14909  low_delay_signal(p_ctx);
14910  retval = NI_RETCODE_SUCCESS;
14911  LRETURN;
14912  }
14913  } else if (NI_RETCODE_NVME_SC_WRITE_BUFFER_FULL == p_ctx->status &&
14914  sessionStatistic.ui32WrBufAvailSize > p_ctx->required_buf_size)
14915  {
14916  ni_log2(p_ctx, NI_LOG_TRACE, "Info dec write buffer is enough, available buf "
14917  "size %u >= required size %u !\n",
14918  sessionStatistic.ui32WrBufAvailSize, p_ctx->required_buf_size);
14919  p_ctx->status = 0;
14920  p_ctx->required_buf_size = 0;
14921  retval = NI_RETCODE_SUCCESS;
14922  LRETURN;
14923  }
14924  ni_pthread_mutex_unlock(&p_ctx->mutex);
14926  ni_pthread_mutex_lock(&p_ctx->mutex);
14927 
14928  continue;
14929  } else
14930  {
14931  if (p_ctx->decoder_low_delay > 0)
14932  {
14933  if (p_ctx->force_low_delay) {
14934  p_ctx->force_low_delay_cnt++;
14935  low_delay_signal(p_ctx);
14936  } else {
14937  ni_log2(p_ctx, NI_LOG_ERROR,"Warning: low delay mode with non sequential "
14938  "input (B frames)? Just cancel the low delay mode then\n");
14939  // Here it should be the last signal to release the send thread
14940  // holding the low delay mutex.
14941  low_delay_signal(p_ctx);
14942  p_ctx->decoder_low_delay = 0;
14943  }
14944  }
14945 
14946  if ((p_param->dec_input_params.min_packets_delay && p_ctx->pkt_delay_cnt))
14947  {
14948  if(p_ctx->pkt_num >= (p_ctx->frame_num + p_ctx->pkt_delay_cnt +
14950  {
14951  if(query_retry <= 2000)
14952  {
14953  ni_pthread_mutex_unlock(&p_ctx->mutex);
14954  ni_usleep(25);
14955  ni_pthread_mutex_lock(&p_ctx->mutex);
14956  continue;
14957  } else {
14958  p_ctx->pkt_delay_cnt++;
14959  ni_log2(p_ctx, NI_LOG_ERROR,
14960  "Warning: decoder pkt_num %u frame_num %u "
14961  "timeout, increasing pkt_delay_cnt to %u\n",
14962  p_ctx->pkt_num, p_ctx->frame_num,
14963  p_ctx->pkt_delay_cnt);
14964  }
14965  }
14966  }
14967  ni_log2(p_ctx, NI_LOG_DEBUG, "Warning: dec read desc failed %d retries. rc=%d"
14968  "\n", query_retry, retval);
14969  }
14970  retval = NI_RETCODE_SUCCESS;
14971  LRETURN;
14972  }
14973  else
14974  {
14975  // We have to ensure there are adequate number of DTS for picture
14976  // reorder delay otherwise wait for more packets to be sent to decoder.
14977  ni_timestamp_table_t *p_dts_queue = p_ctx->dts_queue;
14978  if ((int)p_dts_queue->list.count < p_ctx->pic_reorder_delay + 1 &&
14979  !p_ctx->ready_to_close &&
14981  {
14982  retval = NI_RETCODE_SUCCESS;
14983  ni_log2(p_ctx, NI_LOG_DEBUG,
14984  "At least %d packets should be sent before reading the "
14985  "first frame!\n",
14986  p_ctx->pic_reorder_delay + 1);
14987  LRETURN;
14988  }
14989  p_ctx->max_retry_fail_count[1] = 0;
14990 
14991  // get actual YUV transfer size if this is the stream's very first read
14992  if (0 == p_ctx->active_video_width || 0 == p_ctx->active_video_height)
14993  {
14994  retval = ni_query_stream_info(p_ctx, NI_DEVICE_TYPE_DECODER, &data);
14995  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
14996  p_ctx->device_type, p_ctx->hw_id,
14997  &(p_ctx->session_id), OPT_1);
14998  CHECK_VPU_RECOVERY(retval);
14999 
15000  ni_log2(p_ctx, NI_LOG_DEBUG, "Info dec YUV query, pic size %ux%u xfer frame size "
15001  "%ux%u frame-rate %u is_flushed %u\n",
15002  data.picture_width, data.picture_height,
15004  data.frame_rate, data.is_flushed);
15007  p_ctx->actual_video_width = data.picture_width;
15008  p_ctx->pixel_format = data.pix_format;
15010  //p_ctx->bit_depth_factor = data.transfer_frame_stride / data.picture_width;
15011  p_ctx->is_first_frame = 1;
15012  p_ctx->pixel_format_changed = 0;
15013 
15014  ni_log2(p_ctx, NI_LOG_DEBUG, "Info dec YUV, adjust frame size from %ux%u to "
15015  "%ux%u\n", p_frame->video_width, p_frame->video_height,
15016  p_ctx->active_video_width, p_ctx->active_video_height);
15017 
15018  retval = ni_frame_buffer_alloc(
15019  p_frame, p_ctx->actual_video_width, p_ctx->active_video_height,
15020  p_ctx->codec_format == NI_CODEC_FORMAT_H264, 1,
15021  p_ctx->bit_depth_factor,
15022  3, // Alloc space for write to data[3] and metadata
15023  1);
15024 
15025  if (NI_RETCODE_SUCCESS != retval)
15026  {
15027  LRETURN;
15028  }
15029  total_bytes_to_read = p_frame->data_len[3] + metadata_hdr_size;
15030  p_data_buffer = (uint8_t*)p_frame->p_buffer;
15031  // make sure we don't read more than available
15032  ni_log2(p_ctx, NI_LOG_DEBUG, "Info dec buf size: %u YUV frame + meta-hdr size: %u "
15033  "available: %u\n", p_frame->buffer_size,
15034  total_bytes_to_read, buf_info.buf_avail_size);
15035  }
15036  break;
15037  }
15038  }// end while1 query retry
15039 
15040  ni_log2(p_ctx, NI_LOG_DEBUG, "total_bytes_to_read %u max_nvme_io_size %u ylen %u cr len "
15041  "%u cb len %u hdr %d\n",
15042  total_bytes_to_read, p_ctx->max_nvme_io_size,
15043  p_frame->data_len[0], p_frame->data_len[1],
15044  p_frame->data_len[2], metadata_hdr_size);
15045 
15046  ni_log2(p_ctx, NI_LOG_DEBUG, "p_frame->data_len[3] = %u\n", p_frame->data_len[3]);
15047  if (buf_info.buf_avail_size < total_bytes_to_read)
15048  {
15049  ni_pthread_mutex_unlock(&p_ctx->mutex);
15050 
15051  ni_log2(p_ctx, NI_LOG_ERROR,
15052  "ERROR %s() avaliable size(%u) less than "
15053  "needed (%u)\n",
15054  __func__, buf_info.buf_avail_size, total_bytes_to_read);
15055  abort();
15056  } else if (total_bytes_to_read == metadata_hdr_size && !p_ctx->frame_num)
15057  {
15058  if (ni_cmp_fw_api_ver(
15060  "6rE") >= 0)
15061  {
15062  // allocate p_data_buffer to read the first metadata
15063  void *p_metadata_buffer = NULL;
15064  int buffer_size = ((metadata_hdr_size + (NI_MEM_PAGE_ALIGNMENT - 1)) /
15066  if (ni_posix_memalign(&p_metadata_buffer, sysconf(_SC_PAGESIZE), buffer_size))
15067  {
15068  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate metadata buffer.\n",
15069  NI_ERRNO, __func__);
15070  retval = NI_RETCODE_ERROR_MEM_ALOC;
15071  LRETURN;
15072  }
15073  p_data_buffer = (uint8_t *)p_metadata_buffer;
15074  get_first_metadata = 1;
15075  sequence_change = 0;
15076  }
15077  }
15078 
15079  //Apply read configuration here
15080  retval = ni_config_session_rw(p_ctx, SESSION_READ_CONFIG, 1,
15081  NI_CODEC_HW_ENABLE, 0);
15082  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config, p_ctx->device_type,
15083  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
15084  CHECK_VPU_RECOVERY(retval);
15085 
15086  read_size_bytes = total_bytes_to_read;
15088  if (read_size_bytes % NI_MEM_PAGE_ALIGNMENT)
15089  {
15090  read_size_bytes = ( (read_size_bytes / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT) + NI_MEM_PAGE_ALIGNMENT;
15091  }
15092 
15093  retval = ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
15094  p_data_buffer, read_size_bytes, ui32LBA);
15095  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_read, p_ctx->device_type,
15096  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
15097  CHECK_VPU_RECOVERY(retval);
15098  if (retval < 0)
15099  {
15100  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
15102  LRETURN;
15103  } else if (get_first_metadata) {
15104  // got first metadata alone
15105  ni_metadata_dec_frame_t *p_meta =
15106  (ni_metadata_dec_frame_t *)((uint8_t *)p_data_buffer);
15107  ni_log2(p_ctx, NI_LOG_DEBUG, "Got first pkt_delay_cnt %u\n",
15108  p_meta->metadata_common.pkt_delay_cnt);
15109  if (p_ctx->pkt_delay_cnt < p_meta->metadata_common.pkt_delay_cnt)
15110  p_ctx->pkt_delay_cnt = p_meta->metadata_common.pkt_delay_cnt;
15111  get_first_metadata = 0;
15112  ni_aligned_free(p_data_buffer);
15113  goto start;
15114  } else
15115  {
15116  // command issued successfully, now exit
15117  ni_metadata_dec_frame_t *p_meta;
15118  p_meta =
15119  (ni_metadata_dec_frame_t *)((uint8_t *)p_frame->p_buffer +
15120  p_frame->data_len[0] +
15121  p_frame->data_len[1] +
15122  p_frame->data_len[2] +
15123  p_frame->data_len[3]);
15124 
15125  if (buf_info.buf_avail_size != metadata_hdr_size)
15126  {
15127  low_delay_notify = 1;
15128  // shift metadata to end of triple output
15129 #ifdef _WIN32
15130  p_data_buffer = (uint8_t *)p_frame->p_buffer +
15132  memcpy(p_meta, p_data_buffer, metadata_hdr_size);
15133 #else
15134  memcpy(p_meta,
15135  p_frame->p_buffer +
15137  metadata_hdr_size);
15138 #endif
15139  sei_size = p_meta->sei_size;
15140  niFrameSurface1_t *p_data3 =
15141  (niFrameSurface1_t *)((uint8_t *)p_frame->p_buffer +
15142  p_frame->data_len[0] +
15143  p_frame->data_len[1] +
15144  p_frame->data_len[2]);
15145 
15146  niFrameSurface1_t *p_data3_1 =
15147  (niFrameSurface1_t *)((uint8_t *)p_frame->p_buffer +
15148  sizeof(niFrameSurface1_t));
15149  niFrameSurface1_t *p_data3_2 =
15150  (niFrameSurface1_t *)((uint8_t *)p_frame->p_buffer +
15151  2 * sizeof(niFrameSurface1_t));
15152  // Libxcoder knows the handle so overwrite here
15153  p_data3->device_handle =
15154  (int32_t)((int64_t)p_ctx->blk_io_handle & 0xFFFFFFFF);
15155  p_data3_1->device_handle =
15156  (int32_t)((int64_t)p_ctx->blk_io_handle & 0xFFFFFFFF);
15157  p_data3_2->device_handle =
15158  (int32_t)((int64_t)p_ctx->blk_io_handle & 0xFFFFFFFF);
15159  p_data3->ui16session_ID = p_data3_1->ui16session_ID =
15160  p_data3_2->ui16session_ID = (uint16_t)p_ctx->session_id;
15161  p_data3->src_cpu = p_data3_1->src_cpu = p_data3_2->src_cpu =
15162  (uint8_t)NI_DEVICE_TYPE_DECODER;
15163 
15164  p_data3->output_idx = 0;
15165  p_data3_1->output_idx = 1;
15166  p_data3_2->output_idx = 2;
15167 
15168  ni_log2(p_ctx, NI_LOG_DEBUG,
15169  "p_data3_1:sei_size=%d device_handle=%d == hw_id=%d ses_id=%d\n",
15170  sei_size, p_data3_1->device_handle, p_ctx->hw_id,
15171  p_data3_1->ui16session_ID);
15172  ni_log2(p_ctx, NI_LOG_DEBUG,
15173  "p_data3_1: ui16FrameIdx=%d NodeAddre=0x%x planar=%d bd=%d\n",
15174  p_data3_1->ui16FrameIdx, p_data3_1->ui32nodeAddress,
15175  p_data3_1->encoding_type, p_data3_1->bit_depth);
15176  ni_log2(p_ctx,
15177  NI_LOG_DEBUG,
15178  "p_data3_2:sei_size=%d device_handle=%d == hw_id=%d ses_id=%d\n",
15179  sei_size, p_data3_2->device_handle, p_ctx->hw_id,
15180  p_data3_2->ui16session_ID);
15181  ni_log2(p_ctx, NI_LOG_DEBUG,
15182  "p_data3_2: ui16FrameIdx=%d NodeAddre=0x%x planar=%d bd=%d\n",
15183  p_data3_2->ui16FrameIdx, p_data3_2->ui32nodeAddress,
15184  p_data3_2->encoding_type, p_data3_2->bit_depth);
15185 
15186  ni_log2(p_ctx, NI_LOG_DEBUG,
15187  "%s:sei_size=%d device_handle=%d == hw_id=%d "
15188  "ses_id=%d\n",
15189  __func__, sei_size, p_data3->device_handle, p_ctx->hw_id,
15190  p_data3->ui16session_ID);
15191  ni_log2(p_ctx, NI_LOG_DEBUG,
15192  "%s: session=0x%x ui16FrameIdx=%u NodeAddress=0x%x, "
15193  "planar=%d bd=%d\n",
15194  __func__, p_ctx->session_id, p_data3->ui16FrameIdx, p_data3->ui32nodeAddress,
15195  p_data3->encoding_type, p_data3->bit_depth);
15196  } else if (ni_cmp_fw_api_ver(
15198  "6rE") >= 0)
15199  {
15200  p_meta =
15201  (ni_metadata_dec_frame_t *)((uint8_t *)p_frame->p_buffer);
15202  ni_log2(p_ctx, NI_LOG_DEBUG, "Got pkt_delay_cnt %u\n",
15203  p_meta->metadata_common.pkt_delay_cnt);
15204  if (p_ctx->pkt_delay_cnt < p_meta->metadata_common.pkt_delay_cnt)
15205  p_ctx->pkt_delay_cnt = p_meta->metadata_common.pkt_delay_cnt;
15206  }
15207 
15208  total_bytes_to_read = total_bytes_to_read + sei_size;
15209  ni_log2(p_ctx, NI_LOG_DEBUG,
15210  "%s decoder read desc success, retval %d "
15211  "total_bytes_to_read include sei %u sei_size %d\n",
15212  __func__, retval, total_bytes_to_read, sei_size);
15213 
15214  if (total_bytes_to_read > NI_MEM_PAGE_ALIGNMENT)
15215  {
15216  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Oversized metadata!\n", __func__);
15217  retval = NI_RETCODE_ERROR_MEM_ALOC;
15218  LRETURN;
15219  }
15220  }
15221 
15222  //bytes_read_so_far = total_bytes_to_read;
15223  // Note: session status is NOT reset but tracked between send
15224  // and recv to catch and recover from a loop condition
15225  //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
15226  bytes_read_so_far = total_bytes_to_read;
15227  //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
15229  "6sP") >= 0)
15230  {
15231  rx_size = ni_create_frame(p_frame, bytes_read_so_far, &frame_offset, &frame_dropped, true);
15232  }
15233  else
15234  {
15235  rx_size = ni_create_frame(p_frame, bytes_read_so_far, &frame_offset, 0, true);
15236  }
15237  p_ctx->frame_pkt_offset = frame_offset;
15238  if (p_ctx->decoder_low_delay > 0 && buf_info.buf_avail_size == metadata_hdr_size &&
15239  p_ctx->enable_low_delay_check)
15240  {
15241  ni_log2(p_ctx, NI_LOG_TRACE, "Low delay mode amd check header if has b frame\n");
15242 
15243  ni_metadata_dec_frame_t *p_meta =
15244  (ni_metadata_dec_frame_t *)((uint8_t *)p_frame->p_buffer +
15245  p_frame->data_len[0] +
15246  p_frame->data_len[1] +
15247  p_frame->data_len[2]);
15248  if (p_meta->metadata_common.has_b_frame == 1)
15249  {
15250  ni_log2(p_ctx, NI_LOG_ERROR,"Warning: session 0x%x decoder lowDelay mode "
15251  "is cancelled due to has_b_frames, frame_num %u\n",
15252  p_ctx->session_id, p_ctx->frame_num);
15253  p_ctx->decoder_low_delay = 0;
15254  }
15255  }
15256 
15257  if (rx_size > 0)
15258  {
15259  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): s-state %d first_frame %d\n", __func__,
15260  p_ctx->session_run_state, p_ctx->is_first_frame);
15261 
15262  int64_t tmp_dts, prev_dts = INT64_MIN, ts_diff = 0;
15263  int nb_diff = 0;
15264 
15266  "6sP") >= 0)
15267  {
15268  if(p_ctx->last_frame_dropped + frame_dropped != p_ctx->session_statistic.ui32FramesDropped)
15269  {
15270  ni_log2(p_ctx, NI_LOG_DEBUG,
15271  "### %s(): Warning: ui32FramesDropped %u should be %u + %u\n",
15272  __func__, p_ctx->session_statistic.ui32FramesDropped,
15273  p_ctx->last_frame_dropped, frame_dropped);
15274 
15275  p_ctx->session_statistic.ui32FramesDropped = p_ctx->last_frame_dropped + frame_dropped;
15276  }
15277 
15279  }
15280 
15281  if (p_ctx->is_first_frame)
15282  {
15283  ni_log2(p_ctx, NI_LOG_DEBUG,
15284  "%s(): First frame : session_id 0x%x, pic_reorder_delay: %d "
15285  "total frames input:%u buffered: %u completed: %u output: %u "
15286  "dropped: %u (%u %u) error: %u\n",
15287  __func__, p_ctx->session_id, p_ctx->pic_reorder_delay,
15293  p_ctx->last_frame_dropped, frame_dropped,
15295 
15296  if (p_ctx->session_statistic.ui32FramesDropped > 0)
15297  {
15298  for (i = 0; i < p_ctx->session_statistic.ui32FramesDropped; i++)
15299  {
15301  p_ctx->dts_queue, 0, &tmp_dts,
15303  p_ctx->buffer_pool) != NI_RETCODE_SUCCESS)
15304  {
15305  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): FramesDropped pop "
15306  "decoder dts queue %d %ld failed !\n",
15307  __func__, i, tmp_dts);
15308  break;
15309  } else
15310  {
15311  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): FramesDropped pop "
15312  "decoder dts queue %d %ld success !\n",
15313  __func__, i, tmp_dts);
15314  if (prev_dts != INT64_MIN) {
15315  ts_diff += llabs(tmp_dts - prev_dts);
15316  nb_diff++;
15317  }
15318  prev_dts = tmp_dts;
15319  }
15320  }
15321  if (nb_diff > 1)
15322  {
15323  ts_diff = ts_diff / nb_diff;
15324  }
15326  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): FramesDropped pop dts "
15327  "average diff: %ld\n", __func__, ts_diff);
15328  }
15329  }
15330  else
15331  {
15333  {
15334  for(i = p_ctx->decoder_last_drop_frame_num; i < p_ctx->session_statistic.ui32FramesDropped; i++)
15335  {
15337  p_ctx->dts_queue, 0, &tmp_dts,
15339  p_ctx->buffer_pool) != NI_RETCODE_SUCCESS)
15340  {
15341  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): FramesDropped pop "
15342  "decoder dts queue %d %ld failed !\n",
15343  __func__, i, tmp_dts);
15344  break;
15345  }
15346  }
15348  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): FramesDropped pop dts %d\n", __func__, p_ctx->decoder_last_drop_frame_num);
15349  }
15350  }
15351 
15353  p_ctx->dts_queue, 0, (int64_t *)&p_frame->dts,
15355  p_ctx->buffer_pool) != NI_RETCODE_SUCCESS)
15356  {
15357  if (p_ctx->last_dts != NI_NOPTS_VALUE && !p_ctx->ready_to_close)
15358  {
15359  p_ctx->pic_reorder_delay++;
15360  p_frame->dts = p_ctx->last_dts + p_ctx->last_dts_interval;
15361  ni_log2(p_ctx, NI_LOG_DEBUG, "Padding DTS: %" PRId64 "\n", p_frame->dts);
15362  } else
15363  {
15364  p_frame->dts = NI_NOPTS_VALUE;
15365  }
15366  }
15367 
15368  if (p_ctx->is_first_frame)
15369  {
15370  for (i = 0; (int)i < p_ctx->pic_reorder_delay; i++)
15371  {
15372  if (p_ctx->last_pts == NI_NOPTS_VALUE &&
15373  p_ctx->last_dts == NI_NOPTS_VALUE)
15374  {
15375  // If the p_frame->pts is unknown in the very beginning we assume
15376  // p_frame->pts == 0 as well as DTS less than PTS by 1000 * 1/timebase
15377  if (p_frame->pts >= p_frame->dts &&
15378  p_frame->pts - p_frame->dts < 1000)
15379  {
15380  break;
15381  }
15382  }
15383 
15385  p_ctx->dts_queue, 0, (int64_t *)&p_frame->dts,
15387  p_ctx->frame_num % 500 == 0,
15388  p_ctx->buffer_pool) != NI_RETCODE_SUCCESS)
15389  {
15390  p_frame->dts = NI_NOPTS_VALUE;
15391  } else
15392  {
15393  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): pic_reorder_delay pop "
15394  "decoder dts queue %d %ld success !\n",
15395  __func__, i, p_frame->dts);
15396  }
15397  }
15398  // Reset for DTS padding counting
15399  p_ctx->pic_reorder_delay = 0;
15400  }
15401  if (p_ctx->codec_format == NI_CODEC_FORMAT_JPEG)//fw won't save frameoffset when decoding jpeg.
15402  {
15403  if (p_ctx->is_first_frame)
15404  {
15405  p_ctx->is_first_frame = 0;
15406  }
15407  p_frame->pts = p_ctx->pts_offsets[p_ctx->frame_num % NI_FIFO_SZ];
15408  p_frame->flags = p_ctx->flags_array[p_ctx->frame_num % NI_FIFO_SZ];
15409  p_frame->pkt_pos = p_ctx->pkt_pos[p_ctx->frame_num % NI_FIFO_SZ];
15410  ni_log2(p_ctx, NI_LOG_DEBUG, "p_frame->pts = %u, frame_num = %d, p_frame->dts = %u\n",
15411  p_frame->pts, p_ctx->frame_num, p_frame->dts);
15412  }
15413  else
15414  {
15415  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: frame_offset %" PRIu64 "\n", __func__,
15416  frame_offset);
15417 
15418  // search for the pkt_offsets of received frame according to frame_offset.
15419  // 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)
15420  // i = -1 if not found
15421  int j = 0;
15422  j = rotated_array_binary_search(p_ctx->pkt_offsets_index_min,
15423  p_ctx->pkt_offsets_index, NI_FIFO_SZ,
15424  frame_offset);
15425  if (j >= 0)
15426  {
15427  p_frame->pts = p_ctx->pts_offsets[j];
15428  p_frame->flags = p_ctx->flags_array[j];
15429  p_frame->pkt_pos = p_ctx->pkt_pos[j];
15430  ni_log2(p_ctx, NI_LOG_DEBUG,
15431  "%s: (found pts) dts %" PRId64 " pts "
15432  "%" PRId64 " frame_offset %" PRIu64 " j %d "
15433  "pkt_offsets_index_min %" PRIu64 " "
15434  "pkt_offsets_index %" PRIu64 " pkt_pos %" PRIu64 "\n",
15435  __func__, p_frame->dts, p_frame->pts, frame_offset, j,
15436  p_ctx->pkt_offsets_index_min[j],
15437  p_ctx->pkt_offsets_index[j],
15438  p_ctx->pkt_pos[j]);
15439 
15440  if (p_ctx->is_first_frame)
15441  {
15442  // if the first frame is I frame and there are dropped frames,
15443  // find the dts closest to its pts using the average of dts diff
15444  if (p_frame->dts != NI_NOPTS_VALUE &&
15445  p_frame->pts != NI_NOPTS_VALUE &&
15446  PIC_TYPE_I == p_frame->ni_pict_type &&
15447  p_ctx->session_statistic.ui32FramesDropped > 0 &&
15448  ts_diff > 0)
15449  {
15450  while (p_frame->dts < p_frame->pts &&
15451  llabs(p_frame->pts - p_frame->dts) > ts_diff)
15452  {
15453  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: First I frame pts %ld "
15454  "dts %ld diff. %ld > ts_diff %ld\n",
15455  __func__, p_frame->pts, p_frame->dts,
15456  llabs(p_frame->pts - p_frame->dts), ts_diff);
15457 
15459  p_ctx->dts_queue, 0, (int64_t *)&p_frame->dts,
15461  {
15462  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): First I frame "
15463  "pop decoder dts queue error.\n", __func__);
15464  break;
15465  }
15466  }
15467  }
15468  }
15469 
15470  p_frame->p_custom_sei_set = p_ctx->pkt_custom_sei_set[j];
15471  p_ctx->pkt_custom_sei_set[j] = NULL;
15472  } else
15473  {
15474  //backup solution pts
15475  if (p_param->dec_input_params.skip_pts_guess && p_ctx->last_pts != NI_NOPTS_VALUE)
15476  {
15477  // if skip guess_correct_pts, use pts interval to get the correct pts
15478  p_frame->pts = p_ctx->last_pts + (p_ctx->last_pts_interval > 0 ? p_ctx->last_pts_interval : 1);
15479  }
15480  else
15481  {
15482  p_frame->pts = p_ctx->last_pts + (p_frame->dts - p_ctx->last_dts);
15483  }
15484  p_frame->pkt_pos = p_ctx->last_pkt_pos + (frame_offset - p_ctx->last_frame_offset);
15485  ni_log2(p_ctx, NI_LOG_ERROR,
15486  "ERROR: Frame pts %" PRId64 " not found for offset "
15487  "%" PRIu64 "\n", p_frame->pts, frame_offset);
15488  ni_log2(p_ctx, NI_LOG_DEBUG,
15489  "%s: (not found use default) dts %" PRId64 " pts %" PRId64
15490  "\n",
15491  __func__, p_frame->dts, p_frame->pts);
15492  }
15493 
15494  if (p_ctx->is_first_frame)
15495  {
15496  p_ctx->is_first_frame = 0;
15497  }
15498  }
15499 
15500  p_frame->orignal_pts = p_frame->pts;
15501  p_ctx->last_pkt_pos = p_frame->pkt_pos;
15502  p_ctx->last_frame_offset = frame_offset;
15503  if (!p_param->dec_input_params.skip_pts_guess)
15504  p_frame->pts = guess_correct_pts(p_ctx, p_frame->pts, p_frame->dts);
15505  if (p_frame->pts != NI_NOPTS_VALUE && p_ctx->last_pts != NI_NOPTS_VALUE)
15506  p_ctx->last_pts_interval = p_frame->pts - p_ctx->last_pts;
15507  p_ctx->last_pts = p_frame->pts;
15508  if (p_frame->dts != NI_NOPTS_VALUE && p_ctx->last_dts != NI_NOPTS_VALUE)
15509  p_ctx->last_dts_interval = p_frame->dts - p_ctx->last_dts;
15510  p_ctx->last_dts = p_frame->dts;
15511  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: (best_effort_timestamp) pts %" PRId64 "\n",
15512  __func__, p_frame->pts);
15513  p_ctx->frame_num++;
15514 
15516  if (p_frame->error_ratio > 0)
15517  {
15518  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: frame number = %d, error_ratio = %u\n",
15519  __func__, p_ctx->frame_num, p_frame->error_ratio);
15520  }
15521 
15522  if (p_ctx->ppu_reconfig_pkt_pos != 0 &&
15524  {
15525  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: ppu reconfig done. ppu_reconfig_pkt_pos = %u, frame_num = %u droped = %u\n",
15526  __func__, p_ctx->ppu_reconfig_pkt_pos, p_ctx->frame_num, p_ctx->session_statistic.ui32FramesDropped);
15527  p_ctx->ppu_reconfig_pkt_pos = 0;
15528  }
15529 #ifdef MEASURE_LATENCY
15530 #ifndef XCODER_311
15531  ni_log2(p_ctx, NI_LOG_INFO, "DEC pkt_num %d, fme_num %d, latecy is %d\n",
15532  p_ctx->pkt_num, p_ctx->frame_num, p_ctx->pkt_num - p_ctx->frame_num);
15533 #endif
15534 #endif
15535 
15536  }
15537 
15538  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): received data: [0x%08x]\n", __func__, rx_size);
15539  ni_log2(p_ctx, NI_LOG_DEBUG,
15540  "%s(): p_frame->start_of_stream=%u, "
15541  "p_frame->end_of_stream=%u, p_frame->video_width=%u, "
15542  "p_frame->video_height=%u\n",
15543  __func__, p_frame->start_of_stream, p_frame->end_of_stream,
15544  p_frame->video_width, p_frame->video_height);
15545  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): p_frame->data_len[0/1/2]=%u/%u/%u\n", __func__,
15546  p_frame->data_len[0], p_frame->data_len[1], p_frame->data_len[2]);
15547 
15548  if (p_ctx->frame_num % 500 == 0)
15549  {
15550  ni_log2(p_ctx, NI_LOG_DEBUG,
15551  "Decoder pts queue size = %d dts queue size = %d\n\n",
15552  p_ctx->pts_table->list.count, p_ctx->dts_queue->list.count);
15553  // scan and clean up
15555  p_ctx->buffer_pool);
15556  }
15557 
15558 #ifdef MEASURE_LATENCY
15559  if ((p_frame->dts != NI_NOPTS_VALUE) && (p_ctx->frame_time_q != NULL))
15560  {
15561  uint64_t abs_time_ns = ni_gettime_ns();
15563  ni_log2(p_ctx, NI_LOG_INFO, "DTS:%" PRId64 ",DELTA:%" PRId64 ",dLAT:%" PRIu64 ";\n",
15564  p_frame->dts, abs_time_ns - q->last_benchmark_time,
15565  ni_lat_meas_q_check_latency(q, abs_time_ns, p_frame->dts));
15566  q->last_benchmark_time = abs_time_ns;
15567  }
15568 #endif
15569 
15570 END:
15571 
15572  ni_pthread_mutex_unlock(&p_ctx->mutex);
15573 
15574  if (get_first_metadata && p_data_buffer)
15575  ni_aligned_free(p_data_buffer);
15576  if (sequence_change && p_ctx->frame_num)
15577  {
15578  if (p_ctx->actual_video_width == p_frame->video_width &&
15579  p_ctx->active_video_height == p_frame->video_height)
15580  {
15581  p_ctx->pixel_format_changed = 1;
15582  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): format changed\n", __func__);
15583  }
15584  }
15585  if (NI_RETCODE_SUCCESS != retval)
15586  {
15587  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): bad exit, retval = %d\n", __func__, retval);
15588  if (retval == NI_RETCODE_ERROR_VPU_RECOVERY)
15589  {
15590  low_delay_signal(p_ctx);
15591  }
15592 
15593  return retval;
15594  } else
15595  {
15596  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit, rx_size = %d\n", __func__, rx_size);
15597  if (low_delay_notify)
15598  {
15599  low_delay_signal(p_ctx);
15600  }
15601 
15602  return rx_size;
15603  }
15604 }
15605 
15606 /*!******************************************************************************
15607 * \brief Retrieve a YUV p_frame from decoder
15608 *
15609 * \param
15610 *
15611 * \return
15612 *******************************************************************************/
15614 {
15615  int retval = NI_RETCODE_SUCCESS;
15616  int rx_size = 0;
15617  uint64_t frame_offset = 0;
15618  uint8_t *p_data_buffer;
15619  int metadata_hdr_size = NI_FW_META_DATA_SZ -
15621  uint32_t total_bytes_to_read = 0;
15622  uint32_t read_size_bytes = 0;
15623  uint32_t ui32LBA = 0;
15624 
15625  //ni_log2(p_ctx, NI_LOG_DEBUG, "hwcontext.c:ni_hwdl_frame() hwdesc %d %d %d\n",
15626  // hwdesc->ui16FrameIdx,
15627  // hwdesc->i8InstID,
15628  // hwdesc->ui16session_ID);
15629 
15630  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
15631 
15632  if ((!p_ctx) || (!p_frame))
15633  {
15634  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
15635  __func__);
15636  return NI_RETCODE_INVALID_PARAM;
15637  }
15638 
15639  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
15640  {
15641  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
15642  __func__);
15644  LRETURN;
15645  }
15646 
15647  p_data_buffer = (uint8_t *)p_frame->p_buffer;
15648 
15649  if (!p_frame->p_data[0] || !p_data_buffer)
15650  {
15651  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): No receive buffer allocated.\n",
15652  __func__);
15653  retval = NI_RETCODE_INVALID_PARAM;
15654  LRETURN;
15655  }
15656 
15657  if (0 == p_frame->data_len[0])
15658  {
15659  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): p_frame->data_len[0] = 0!.\n",
15660  __func__);
15661  retval = NI_RETCODE_INVALID_PARAM;
15662  LRETURN;
15663  }
15664 
15666  {
15667  p_frame->data_len[2] = 0;
15668  } else if (hwdesc->encoding_type == NI_PIXEL_PLANAR_FORMAT_TILED4X4)
15669  {
15670  ni_log2(p_ctx, NI_LOG_ERROR,
15671  "ERROR %s(): NI_PIXEL_PLANAR_FORMAT_TILED4X4 not supported in download.\n",
15672  __func__);
15673  retval = NI_RETCODE_INVALID_PARAM;
15674  LRETURN;
15675  }
15676  total_bytes_to_read = p_frame->data_len[0] + p_frame->data_len[1] +
15677  p_frame->data_len[2];// +metadata_hdr_size;
15678  unsigned int bytes_read_so_far = 0;
15679  uint32_t output_chunk_offset = hwdesc->ui32nodeAddress / FRAME_CHUNK_INDEX_SIZE; //for reading output1 or output2
15680  uint32_t output_minor_offset = hwdesc->ui32nodeAddress - output_chunk_offset * FRAME_CHUNK_INDEX_SIZE;
15681  ni_log2(p_ctx, NI_LOG_DEBUG, "Total bytes to download %u, start offset = %u, chunkOffset "
15682  "%u, minorOffset %u\n",
15683  total_bytes_to_read, hwdesc->ui32nodeAddress,
15684  output_chunk_offset, output_minor_offset);
15685 
15686  ni_log2(p_ctx, NI_LOG_DEBUG, "total_bytes_to_read %u max_nvme_io_size %u ylen %u cr len "
15687  "%u cb len %u hdr %d\n",
15688  total_bytes_to_read, p_ctx->max_nvme_io_size,
15689  p_frame->data_len[0], p_frame->data_len[1],
15690  p_frame->data_len[2], metadata_hdr_size);
15691 
15692  //Apply read configuration here
15693  retval =
15695  (output_minor_offset << NI_CODEC_HW_PAYLOAD_OFFSET) |
15697  hwdesc->ui16FrameIdx);
15698  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config, hwdesc->src_cpu,
15699  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
15700  CHECK_VPU_RECOVERY(retval);
15701  if (retval < 0)
15702  {
15703  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
15705  LRETURN;
15706  } else
15707  {
15708  ni_log2(p_ctx, NI_LOG_DEBUG,
15709  "Config HW download read desc success, retval %d total_bytes_to_read %u\n",
15710  retval, total_bytes_to_read);
15711  }
15712 
15713  read_size_bytes = total_bytes_to_read;
15714  ui32LBA = READ_INSTANCE_R(p_ctx->session_id, hwdesc->src_cpu);
15715  ui32LBA += output_chunk_offset;
15716  if (read_size_bytes % NI_MEM_PAGE_ALIGNMENT)
15717  {
15718  read_size_bytes = ( (read_size_bytes / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT) + NI_MEM_PAGE_ALIGNMENT;
15719  }
15720 
15721  retval = ni_nvme_send_read_cmd(
15722  (ni_device_handle_t)(int64_t)hwdesc->device_handle,
15723  NI_INVALID_DEVICE_HANDLE, p_data_buffer, read_size_bytes, ui32LBA);
15724  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_read, hwdesc->src_cpu,
15725  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
15726  CHECK_VPU_RECOVERY(retval);
15727  if (retval < 0)
15728  {
15729  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
15731  LRETURN;
15732  } else
15733  {
15734  ni_log2(p_ctx, NI_LOG_DEBUG,
15735  "HW download read desc success, retval %d total_bytes_to_read %u\n",
15736  retval, total_bytes_to_read);
15737  }
15738 
15739  //Unset applied read configuration here
15740  retval = ni_config_session_rw(p_ctx, SESSION_READ_CONFIG, 0, 0, 0);
15741  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config, hwdesc->src_cpu,
15742  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
15743  CHECK_VPU_RECOVERY(retval);
15744 
15745  if (retval < 0)
15746  {
15747  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
15749  LRETURN;
15750  } else
15751  {
15752  ni_log2(p_ctx, NI_LOG_DEBUG,
15753  "Unconfig HW download read desc success, retval %d total_bytes_to_read %u\n",
15754  retval, total_bytes_to_read);
15755  }
15756 
15757  bytes_read_so_far = total_bytes_to_read;
15758  // Note: session status is NOT reset but tracked between send
15759  // and recv to catch and recover from a loop condition
15760 
15761  if (p_ctx->is_auto_dl)
15762  rx_size = (int)bytes_read_so_far;
15763  else
15764  {
15765  rx_size =
15766  ni_create_frame(p_frame, bytes_read_so_far, &frame_offset, 0, false);
15767  p_ctx->frame_pkt_offset = frame_offset;
15768  }
15769 
15770  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): received data: [0x%08x]\n",
15771  __func__, rx_size);
15772  ni_log2(p_ctx, NI_LOG_DEBUG,
15773  "%s(): p_frame->start_of_stream=%u, "
15774  "p_frame->end_of_stream=%u, p_frame->video_width=%u, "
15775  "p_frame->video_height=%u\n",
15776  __func__, p_frame->start_of_stream, p_frame->end_of_stream,
15777  p_frame->video_width, p_frame->video_height);
15778  ni_log2(p_ctx, NI_LOG_DEBUG,
15779  "%s(): p_ctx->frame_num %" PRIu64 ", "
15780  "p_frame->data_len[0/1/2]=%u/%u/%u\n",
15781  __func__, p_ctx->frame_num, p_frame->data_len[0], p_frame->data_len[1],
15782  p_frame->data_len[2]);
15783 
15784  //if (decq_count % 500 == 0)
15785  //{
15786  // ni_log2(p_ctx, NI_LOG_DEBUG, "Decoder pts queue size = %d dts queue size = %d\n\n",
15787  // p_ctx->pts_table->list.count, p_ctx->dts_queue)->list.count);
15788  // // scan and clean up
15789  // ni_timestamp_scan_cleanup(p_ctx->pts_table, p_ctx->dts_queue, p_ctx->buffer_pool);
15790  //}
15791 
15792 END:
15793 
15794  if (NI_RETCODE_SUCCESS != retval)
15795  {
15796  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): bad exit, retval = %d\n", __func__, retval);
15797  return retval;
15798  } else
15799  {
15800  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit, rx_size = %d\n", __func__, rx_size);
15801  return rx_size;
15802  }
15803 }
15804 
15805 int ni_hwdownload_by_frame_idx(niFrameSurface1_t* hwdesc, ni_frame_t* p_frame, int is_auto_dl)
15806 {
15807  int retval = NI_RETCODE_SUCCESS;
15808  int rx_size = 0;
15809  uint64_t frame_offset = 0;
15810  uint8_t *p_data_buffer;
15811  int metadata_hdr_size = NI_FW_META_DATA_SZ -
15813  uint32_t total_bytes_to_read = 0;
15814  uint32_t read_size_bytes = 0;
15815  uint32_t ui32LBA = 0;
15816  ni_global_session_stats_t *global_state;
15817  const char *error_flag = "NetintQuadraErr";
15818 
15819  ni_log(NI_LOG_TRACE, "%s(): enter\n", __func__);
15820 
15821  p_data_buffer = (uint8_t *)p_frame->p_buffer;
15822 
15823  if (!p_frame->p_data[0] || !p_data_buffer || 0 == p_frame->data_len[0])
15824  {
15825  ni_log(NI_LOG_ERROR, "ERROR %s(): passed parameters are null or p_frame->data_len is 0.\n",
15826  __func__);
15827  retval = NI_RETCODE_INVALID_PARAM;
15828  LRETURN;
15829  }
15830 
15832  {
15833  p_frame->data_len[2] = 0;
15834  } else if (hwdesc->encoding_type >= NI_PIXEL_PLANAR_FORMAT_TILED4X4)
15835  {
15837  "ERROR %s(): Unsupported format %d for hw download.\n",
15838  __func__, hwdesc->encoding_type);
15839  retval = NI_RETCODE_INVALID_PARAM;
15840  LRETURN;
15841  }
15842  total_bytes_to_read = p_frame->data_len[0] + p_frame->data_len[1] +
15843  p_frame->data_len[2];// +metadata_hdr_size;
15844  unsigned int bytes_read_so_far = 0;
15845  uint32_t output_chunk_offset = hwdesc->ui32nodeAddress / FRAME_CHUNK_INDEX_SIZE; //for reading output1 or output2
15846  uint32_t output_minor_offset = hwdesc->ui32nodeAddress - output_chunk_offset * FRAME_CHUNK_INDEX_SIZE;
15847 
15848  ni_log(NI_LOG_DEBUG, "total_bytes_to_read %u ylen %u cr len "
15849  "%u cb len %u hdr %d\n",
15850  total_bytes_to_read,
15851  p_frame->data_len[0], p_frame->data_len[1],
15852  p_frame->data_len[2], metadata_hdr_size);
15853 
15854  // preset content of buffer, if not refresh means download fail
15855  memset(p_data_buffer, 0, NI_DATA_BUFFER_LEN);
15856  global_state = (ni_global_session_stats_t *)p_data_buffer;
15857  memcpy(global_state->error_flag, error_flag, strlen(error_flag)+1);
15858  global_state->check_flag[0] = global_state->check_flag[2] = 0x0;
15859  global_state->check_flag[1] = global_state->check_flag[3] = 0xFF;
15860 
15861  read_size_bytes = total_bytes_to_read;
15862  ui32LBA = DOWNLOAD_FRAMEIDX_R(hwdesc->ui16FrameIdx);
15863  ui32LBA += output_chunk_offset;
15864  if (read_size_bytes % NI_MEM_PAGE_ALIGNMENT)
15865  {
15866  read_size_bytes = ( (read_size_bytes / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT) + NI_MEM_PAGE_ALIGNMENT;
15867  }
15868  ni_log(NI_LOG_DEBUG, "Total bytes to download %u, start offset = %u, chunkOffset "
15869  "%u, minorOffset %u hwdesc->ui16FrameIdx %d ui32LBA 0x%x\n",
15870  total_bytes_to_read, hwdesc->ui32nodeAddress,
15871  output_chunk_offset, output_minor_offset, hwdesc->ui16FrameIdx, ui32LBA);
15872 
15873  retval = ni_nvme_send_read_cmd(
15874  (ni_device_handle_t)(int64_t)hwdesc->device_handle,
15875  NI_INVALID_DEVICE_HANDLE, p_data_buffer, read_size_bytes, ui32LBA);
15876 
15877  if (retval < 0)
15878  {
15879  ni_log(NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
15881  LRETURN;
15882  }
15883  global_state = (ni_global_session_stats_t *)p_data_buffer;
15884  if (!strcmp(global_state->error_flag, error_flag) &&
15885  global_state->check_flag[0] == 0x0 && global_state->check_flag[1] == 0xFF &&
15886  global_state->check_flag[2] == 0x0 && global_state->check_flag[3] == 0xFF) {
15887  ni_log(NI_LOG_ERROR, "ERROR %s(): nvme download failed, invalid frameidx %u or "
15888  "size %u + offset %u out of range\n",
15889  __func__, hwdesc->ui16FrameIdx, read_size_bytes, output_chunk_offset);
15890  retval = NI_RETCODE_INVALID_PARAM;
15891  LRETURN;
15892  }
15893 
15894  bytes_read_so_far = total_bytes_to_read;
15895  // Note: session status is NOT reset but tracked between send
15896  // and recv to catch and recover from a loop condition
15897 
15898  if (is_auto_dl)
15899  rx_size = (int)bytes_read_so_far;
15900  else
15901  {
15902  rx_size =
15903  ni_create_frame(p_frame, bytes_read_so_far, &frame_offset, 0, false);
15904  }
15905 
15907  "HW download read desc success, retval %d total_bytes_to_read %u\n",
15908  retval, total_bytes_to_read);
15909 
15910 END:
15911 
15912  if (NI_RETCODE_SUCCESS != retval)
15913  {
15914  ni_log(NI_LOG_ERROR, "%s(): bad exit, retval = %d\n", __func__, retval);
15915  return retval;
15916  } else
15917  {
15918  ni_log(NI_LOG_TRACE, "%s(): exit, rx_size = %d\n", __func__, rx_size);
15919  return rx_size;
15920  }
15921 }
15922 
15924  ni_frameclone_desc_t *p_frameclone_desc)
15925 {
15926  int retval = NI_RETCODE_SUCCESS;
15927  uint8_t *p_data = NULL;
15928  uint32_t dataLen;
15929  uint32_t ui32LBA = 0;
15930 
15931  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
15932 
15933  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
15934  {
15935  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
15936  __func__);
15938  LRETURN;
15939  }
15940 
15941  dataLen = (sizeof(ni_frameclone_desc_t) + NI_MEM_PAGE_ALIGNMENT - 1) &
15942  ~(NI_MEM_PAGE_ALIGNMENT - 1);
15943  if (ni_posix_memalign((void **)&p_data, sysconf(_SC_PAGESIZE), dataLen))
15944  {
15945  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
15946  NI_ERRNO, __func__);
15947  retval = NI_RETCODE_ERROR_MEM_ALOC;
15948  LRETURN;
15949  }
15950  memset(p_data, 0, dataLen);
15951  memcpy(p_data, p_frameclone_desc, sizeof(ni_frameclone_desc_t));
15952  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): ui16DstIdx %u, ui16SrcIdx %u, size %u, offset %u\n", __func__,
15953  p_frameclone_desc->ui16DstIdx, p_frameclone_desc->ui16SrcIdx,
15954  p_frameclone_desc->ui32Size, p_frameclone_desc->ui32Offset);
15955 
15956  ui32LBA = CONFIG_SESSION_FRAME_COPY_W(p_ctx->session_id);
15957  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, NI_INVALID_EVENT_HANDLE,
15958  p_data, dataLen, ui32LBA);
15959  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
15960  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id),
15961  OPT_1);
15962  CHECK_VPU_RECOVERY(retval);
15963  if (NI_RETCODE_SUCCESS != retval)
15964  {
15965  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed!\n",
15966  __func__);
15968  LRETURN;
15969  }
15970 
15971 END:
15972  ni_aligned_free(p_data);
15973  if (NI_RETCODE_SUCCESS != retval)
15974  {
15975  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): bad exit, retval = %d\n", __func__, retval);
15976  }
15977  return retval;
15978 }
15979 
15980 /*!******************************************************************************
15981 * \brief Close an xcoder upload instance
15982 *
15983 * \param p_ctx pointer to uploader session context
15984 *
15985 * \return NI_RETCODE_SUCCESS
15986 *******************************************************************************/
15988 {
15989 #ifndef _WIN32
15990  if (p_ctx->isP2P)
15991  {
15992  if (p_ctx->netint_fd)
15993  {
15994  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: close driver fd %d\n", __func__,
15995  p_ctx->netint_fd);
15996  close(p_ctx->netint_fd);
15997  }
15998  }
15999 #endif
16000  return NI_RETCODE_SUCCESS;
16001 }
16002 
16003 /*!******************************************************************************
16004 * \brief Send a p_config command to configure uploading parameters.
16005 *
16006 * \param ni_session_context_t p_ctx - xcoder Context
16007 * \param[in] pool_size pool size to create
16008 * \param[in] pool 0 = normal pool, 1 = P2P pool
16009 *
16010 * \return - NI_RETCODE_SUCCESS on success,
16011 * NI_RETCODE_ERROR_INVALID_SESSION
16012 * NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
16013 *******************************************************************************/
16015  uint32_t pool_size,
16016  uint32_t pool)
16017 {
16018  void* p_uploader_config = NULL;
16019  ni_uploader_config_t* p_cfg = NULL;
16020  uint32_t buffer_size = sizeof(ni_encoder_config_t);
16022  uint32_t ui32LBA = 0;
16023  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
16024 
16025  if (!p_ctx)
16026  {
16027  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
16028  __func__);
16029  retval = NI_RETCODE_INVALID_PARAM;
16030  LRETURN;
16031  }
16032 
16033  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
16034  {
16035  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
16036  __func__);
16038  LRETURN;
16039  }
16040 
16041  buffer_size = ((buffer_size + (NI_MEM_PAGE_ALIGNMENT - 1)) / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT;
16042  if (ni_posix_memalign(&p_uploader_config, sysconf(_SC_PAGESIZE),
16043  buffer_size))
16044  {
16045  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
16046  NI_ERRNO, __func__);
16047  retval = NI_RETCODE_ERROR_MEM_ALOC;
16048  LRETURN;
16049  }
16050  memset(p_uploader_config, 0, buffer_size);//
16051 
16052  p_cfg = (ni_uploader_config_t*)p_uploader_config;
16053  p_cfg->ui16picWidth = p_ctx->active_video_width;
16054  p_cfg->ui16picHeight = p_ctx->active_video_height;
16055  p_cfg->ui8poolSize = pool_size;
16056  p_cfg->ui8PixelFormat = p_ctx->pixel_format;
16057  p_cfg->ui8Pool = pool;
16058  p_cfg->ui32lumaLinesize = 0; // cannot support config linesize yet because ni_frames_init is called when filter graph is created (no frame linesize info)
16059  p_cfg->ui32chromaLinesize = 0;
16060  bool isrgb = ((NI_PIX_FMT_RGBA == p_cfg->ui8PixelFormat) || (NI_PIX_FMT_BGRA == p_cfg->ui8PixelFormat) ||
16061  (NI_PIX_FMT_ARGB == p_cfg->ui8PixelFormat) || (NI_PIX_FMT_ABGR == p_cfg->ui8PixelFormat) ||
16062  (NI_PIX_FMT_BGR0 == p_cfg->ui8PixelFormat));
16066  {
16067  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);
16068  retval = NI_RETCODE_INVALID_PARAM;
16069  LRETURN;
16070  }
16071 
16072  ni_log2(p_ctx, NI_LOG_DEBUG, "ni_config_instance_set_uploader_params():%d x %d x Format %d with %d framepool\n",
16073  p_cfg->ui16picWidth, p_cfg->ui16picHeight, p_cfg->ui8PixelFormat, p_cfg->ui8poolSize);
16074 
16075  //configure the session here
16076  ui32LBA =
16078 
16079  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
16080  p_uploader_config, buffer_size, ui32LBA);
16081  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
16082  p_ctx->device_type, p_ctx->hw_id,
16083  &(p_ctx->session_id), OPT_1);
16084  if (NI_RETCODE_SUCCESS != retval)
16085  {
16086  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);
16087  //Close the session since we can't configure it
16088  retval = ni_encoder_session_close(p_ctx, 0);
16089  if (NI_RETCODE_SUCCESS != retval)
16090  {
16091  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);
16092  }
16093 
16095  }
16096  else
16097  {
16099  }
16100 
16101 END:
16102 
16103  ni_aligned_free(p_uploader_config);
16104  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
16105 
16106  return retval;
16107 }
16108 
16109 /*!******************************************************************************
16110 * \brief Send a p_config command to configure decoding parameters.
16111 *
16112 * \param ni_session_context_t p_ctx - xcoder Context
16113 * \param uint32_t max_pkt_size - overwrite maximum packet size if nonzero
16114 *
16115 * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION, NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
16116 *******************************************************************************/
16118 {
16119  void* p_decoder_config = NULL;
16120  uint32_t buffer_size = sizeof(ni_decoder_config_t);
16122  uint32_t ui32LBA = 0;
16123  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
16124 
16125  if (!p_ctx)
16126  {
16127  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
16128  __func__);
16129  retval = NI_RETCODE_INVALID_PARAM;
16130  LRETURN;
16131  }
16132 
16133  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
16134  {
16135  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
16136  __func__);
16138  LRETURN;
16139  }
16140 
16141  if (ni_cmp_fw_api_ver((char*)&p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6rT") < 0)
16142  {
16143  ni_log2(p_ctx, NI_LOG_INFO, "%s() FW rev %s < 6rT-- load balancing might be affected\n", __func__,
16145  }
16146 
16147  buffer_size = ((buffer_size + (NI_MEM_PAGE_ALIGNMENT - 1)) / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT;
16148  if (ni_posix_memalign(&p_decoder_config, sysconf(_SC_PAGESIZE), buffer_size))
16149  {
16150  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate p_decoder_config buffer\n",
16151  NI_ERRNO, __func__);
16152  retval = NI_RETCODE_ERROR_MEM_ALOC;
16153  LRETURN;
16154  }
16155  memset(p_decoder_config, 0, buffer_size);
16156 
16157  ni_set_custom_dec_template(p_ctx, p_decoder_config, p_ctx->p_session_config, max_pkt_size);
16158  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));
16159  if (NI_RETCODE_PARAM_WARN == retval)
16160  {
16161  ni_log2(p_ctx, NI_LOG_INFO, "WARNING: %s . %s\n", __func__, p_ctx->param_err_msg);
16162  fflush(stdout);
16163  }
16164  else if (NI_RETCODE_SUCCESS != retval)
16165  {
16166  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s failed. %s\n", __func__,
16167  p_ctx->param_err_msg);
16168  fflush(stdout);
16169  retval = NI_RETCODE_INVALID_PARAM;
16170  LRETURN;
16171  }
16172 
16173  // configure the session here
16175 
16176  //Flip the bytes!!
16177  //p_cfg = (ni_decoder_config_t*)p_decoder_config;
16178  //p_cfg->i32picWidth = ni_htonl(p_cfg->i32picWidth);
16179  //p_cfg->i32picHeight = ni_htonl(p_cfg->i32picHeight);
16180  //p_cfg->i32meBlkMode = ni_htonl(p_cfg->i32meBlkMode);
16181  //p_cfg->i32frameRateInfo = ni_htonl(p_cfg->i32frameRateInfo);
16182  //p_cfg->i32vbvBufferSize = ni_htonl(p_cfg->i32vbvBufferSize);
16183  //p_cfg->i32userQpMax = ni_htonl(p_cfg->i32userQpMax);
16184  //p_cfg->i32maxIntraSize = ni_htonl(p_cfg->i32maxIntraSize);
16185  //p_cfg->i32userMaxDeltaQp = ni_htonl(p_cfg->i32userMaxDeltaQp);
16186  //p_cfg->i32userMinDeltaQp = ni_htonl(p_cfg->i32userMinDeltaQp);
16187  //p_cfg->i32userQpMin = ni_htonl(p_cfg->i32userQpMin);
16188  //p_cfg->i32bitRate = ni_htonl(p_cfg->i32bitRate);
16189  //p_cfg->i32bitRateBL = ni_htonl(p_cfg->i32bitRateBL);
16190  //p_cfg->i32srcBitDepth = ni_htonl(p_cfg->i32srcBitDepth);
16191  //p_cfg->hdrEnableVUI = ni_htonl(p_cfg->hdrEnableVUI);
16192  //p_cfg->ui32VuiDataSizeBits = ni_htonl(p_cfg->ui32VuiDataSizeBits);
16193  //p_cfg->ui32VuiDataSizeBytes = ni_htonl(p_cfg->ui32VuiDataSizeBytes);
16194  //p_cfg->i32hwframes = ni_htonl(p_cfg->i32hwframes);
16195  // flip the 16 bytes of the reserved field using 32 bits pointers
16196  //for (i = 0; i < (16 >> 2); i++)
16197  //{
16198  // ((uint32_t*)p_cfg->ui8Reserved)[i] = ni_htonl(((uint32_t*)p_cfg->ui8Reserved)[i]);
16199  //}
16201  //for (i = 0; i < (NI_MAX_VUI_SIZE >> 2); i++) // apply on 32 bits
16202  //{
16203  // ((uint32_t*)p_cfg->ui8VuiRbsp)[i] = ni_htonl(((uint32_t*)p_cfg->ui8VuiRbsp)[i]);
16204  //}
16205 
16206  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: ui32LBA = 0x%x\n", __func__, ui32LBA);
16207  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
16208  p_decoder_config, buffer_size, ui32LBA);
16209  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
16210  p_ctx->device_type, p_ctx->hw_id,
16211  &(p_ctx->session_id), OPT_1);
16212  if (NI_RETCODE_SUCCESS != retval)
16213  {
16214  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);
16215  //Close the session since we can't configure it
16216  retval = ni_decoder_session_close(p_ctx, 0);
16217  if (NI_RETCODE_SUCCESS != retval)
16218  {
16219  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);
16220  }
16221 
16223  }
16224 
16225 END:
16226 
16227  ni_aligned_free(p_decoder_config);
16228  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
16229  return retval;
16230 }
16231 
16232 /*!******************************************************************************
16233 * \brief Send a p_config command to configure decoding parameters.
16234 *
16235 * \param ni_session_context_t p_ctx - xcoder Context
16236 * \param uint32_t max_pkt_size - overwrite maximum packet size if nonzero
16237 *
16238 * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION, NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
16239 *******************************************************************************/
16241  void *p_dec_ppu_config, int buffer_size)
16242 {
16243  void* p_ppu_config = NULL;
16244  // uint32_t buffer_size = sizeof(ni_decoder_output_picture_size);
16245  uint32_t tmp_buffer_size = buffer_size;
16247  uint32_t ui32LBA = 0;
16248 
16249  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
16250 
16251  if (!p_ctx || !p_dec_ppu_config)
16252  {
16253  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
16254  __func__);
16255  retval = NI_RETCODE_INVALID_PARAM;
16256  LRETURN;
16257  }
16258 
16259  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
16260  {
16261  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
16262  __func__);
16264  LRETURN;
16265  }
16266 
16267  ni_pthread_mutex_lock(&p_ctx->mutex);
16268 
16269  if (ni_cmp_fw_api_ver((char*)&p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6rT") < 0)
16270  {
16271  ni_log2(p_ctx, NI_LOG_INFO, "%s() FW rev %s < 6rT-- load balancing might be affected\n", __func__,
16273  }
16274 
16275  tmp_buffer_size = ((tmp_buffer_size + (NI_MEM_PAGE_ALIGNMENT - 1)) / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT;
16276  if (ni_posix_memalign(&p_ppu_config, sysconf(_SC_PAGESIZE), tmp_buffer_size))
16277  {
16278  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate memory\n",
16279  NI_ERRNO, __func__);
16280  retval = NI_RETCODE_ERROR_MEM_ALOC;
16281  LRETURN;
16282  }
16283  memset(p_ppu_config, 0, tmp_buffer_size);
16284 
16285  memcpy(p_ppu_config, p_dec_ppu_config, buffer_size);
16286  // configure the session here
16288 
16289  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
16290  p_ppu_config, tmp_buffer_size, ui32LBA);
16291  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
16292  p_ctx->device_type, p_ctx->hw_id,
16293  &(p_ctx->session_id), OPT_1);
16294  if (NI_RETCODE_SUCCESS != retval)
16295  {
16296  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);
16297  ni_pthread_mutex_unlock(&p_ctx->mutex);
16298  //Close the session since we can't configure it
16299  retval = ni_decoder_session_close(p_ctx, 0);
16300  if (NI_RETCODE_SUCCESS != retval)
16301  {
16302  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: ni_decoder_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);
16303  }
16304  ni_pthread_mutex_lock(&p_ctx->mutex);
16306  }
16307 
16308 END:
16309 
16310  ni_pthread_mutex_unlock(&p_ctx->mutex);
16311  ni_aligned_free(p_ppu_config);
16312  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
16313  return retval;
16314 }
16315 
16316 /*!******************************************************************************
16317  * \brief read a hardware descriptor from a scaler session
16318  *
16319  * \param[in] p_ctx pointer to session context
16320  * \param[out] p_frame pointer to frame to write hw descriptor
16321  *
16322  * \return NI_RETCODE_INVALID_PARAM
16323  * NI_RETCODE_ERROR_INVALID_SESSION
16324  * NI_RETCODE_ERROR_MEM_ALOC
16325  * NI_RETCODE_ERROR_NVME_CMD_FAILED
16326  * NI_RETCODE_FAILURE
16327  *******************************************************************************/
16329  ni_session_context_t *p_ctx,
16330  ni_frame_t *p_frame)
16331 {
16332  ni_retcode_t retval;
16333  ni_instance_buf_info_t sInstanceBuf = {0};
16334  niFrameSurface1_t *pFrameSurface;
16335  int query_retry = 0;
16336 
16337  if (!p_ctx || !p_frame || !p_frame->p_data[3])
16338  {
16339  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
16340  __func__);
16341  return NI_RETCODE_INVALID_PARAM;
16342  }
16343 
16344  if (p_ctx->session_id == NI_INVALID_SESSION_ID)
16345  {
16346  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
16347  __func__);
16349  }
16350 
16351  if (p_ctx->buffered_frame_index)
16352  {
16353  pFrameSurface = (niFrameSurface1_t *) p_frame->p_data[3];
16354  pFrameSurface->ui16FrameIdx = p_ctx->buffered_frame_index;
16355  pFrameSurface->ui16session_ID = p_ctx->session_id;
16356  pFrameSurface->device_handle =
16357  (int32_t)((int64_t)p_ctx->blk_io_handle & 0xFFFFFFFF);
16358  pFrameSurface->src_cpu = (uint8_t) NI_DEVICE_TYPE_SCALER;
16359  pFrameSurface->output_idx = 0;
16360 
16361  p_ctx->buffered_frame_index = 0;
16362  return NI_RETCODE_SUCCESS;
16363  }
16364 
16365  ni_pthread_mutex_lock(&p_ctx->mutex);
16366 
16367  for (;;)
16368  {
16369  query_retry++;
16370 
16371  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: query by ni_query_instance_buf_info INST_BUF_INFO_RW_UPLOAD\n",
16372  __func__);
16374  NI_DEVICE_TYPE_SCALER, &sInstanceBuf);
16375 
16376  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
16377  p_ctx->device_type, p_ctx->hw_id,
16378  &(p_ctx->session_id), OPT_3);
16379 
16381  {
16382  if (query_retry >= 1000)
16383  {
16384  ni_log2(p_ctx, NI_LOG_DEBUG, "Warning hwdesc read fail rc %d\n", retval);
16385  LRETURN;
16386  }
16387  }
16388  else if (retval != NI_RETCODE_SUCCESS)
16389  {
16390  LRETURN;
16391  }
16392  else
16393  {
16394  pFrameSurface = (niFrameSurface1_t *) p_frame->p_data[3];
16395  pFrameSurface->ui16FrameIdx = sInstanceBuf.hw_inst_ind.frame_index;
16396  pFrameSurface->ui16session_ID = p_ctx->session_id;
16397  pFrameSurface->device_handle =
16398  (int32_t)((int64_t)p_ctx->blk_io_handle & 0xFFFFFFFF);
16399  pFrameSurface->src_cpu = (uint8_t) NI_DEVICE_TYPE_SCALER;
16400  pFrameSurface->output_idx = 0;
16401 
16402  /* A frame index of zero is invalid, the memory acquisition failed */
16403  if (pFrameSurface->ui16FrameIdx == 0)
16404  {
16405  if (query_retry >= 1000)
16406  {
16407  ni_log2(p_ctx, NI_LOG_ERROR, "Error: 2D could not acquire frame\n");
16408  retval = NI_RETCODE_FAILURE;
16409  LRETURN;
16410  }
16411  ni_usleep(100);
16412  continue;
16413  }
16414 
16415  ni_log2(p_ctx, NI_LOG_DEBUG,
16416  "Session=0x%x: %s got FrameIndex=%u\n",
16417  p_ctx->session_id,
16418  __func__,
16419  pFrameSurface->ui16FrameIdx);
16420 
16421  LRETURN;
16422  }
16423  }
16424 
16425 END:
16426 
16427  ni_pthread_mutex_unlock(&p_ctx->mutex);
16428 
16429  if (NI_RETCODE_SUCCESS != retval)
16430  {
16431  ni_log2(p_ctx, NI_LOG_DEBUG,
16432  "%s Warning scalar read hwdesc fail rc %d or ind !\n",
16433  __func__, retval);
16434  retval = NI_RETCODE_FAILURE;
16435  }
16436 
16437  return retval;
16438 }
16439 
16440 /*!******************************************************************************
16441 * \brief Grab bitdepth factor from NI_PIX_FMT
16442 *
16443 * \param[in] pix_fmt ni_pix_fmt_t
16444 *
16445 * \return 1 or 2 for success, -1 for error
16446 *******************************************************************************/
16448 {
16449  switch (pix_fmt)
16450  {
16451  case NI_PIX_FMT_YUV420P:
16452  case NI_PIX_FMT_NV12:
16453  case NI_PIX_FMT_8_TILED4X4:
16454  return 1;
16456  case NI_PIX_FMT_P010LE:
16458  return 2;
16459  default:
16460  {
16461  ni_log(NI_LOG_ERROR, "ERROR: %s() non applicable format %d\n", __func__,
16462  pix_fmt);
16463  break;
16464  }
16465  }
16466  return -1;
16467 }
16468 
16469 /*!******************************************************************************
16470 * \brief Grab planar info from NI_PIX_FMT
16471 *
16472 * \param[in] pix_fmt ni_pix_fmt_t
16473 *
16474 * \return 0 or 1 for success, -1 for error
16475 *******************************************************************************/
16477 {
16478  switch (pix_fmt)
16479  {
16480  case NI_PIX_FMT_YUV420P:
16482  case NI_PIX_FMT_ABGR:
16483  case NI_PIX_FMT_ARGB:
16484  case NI_PIX_FMT_RGBA:
16485  case NI_PIX_FMT_BGRA:
16486  return 1;
16487  break;
16488  case NI_PIX_FMT_NV12:
16489  case NI_PIX_FMT_P010LE:
16490  return 0;
16491  break;
16492  default:
16493  {
16494  ni_log(NI_LOG_ERROR, "ERROR: %s() non applicable format %d\n", __func__,
16495  pix_fmt);
16496  break;
16497  }
16498  }
16499  return -1;
16500 }
16501 
16502 #ifndef _WIN32
16503 /*!*****************************************************************************
16504  * \brief Get an address offset from a hw descriptor
16505  *
16506  * \param[in] p_ctx ni_session_context_t to be referenced
16507  * \param[in] hwdesc Pointer to caller allocated niFrameSurface1_t
16508  * \param[out] p_offset Value of offset
16509  *
16510  * \return On success NI_RETCODE_SUCCESS
16511  * On failure NI_RETCODE_INVALID_PARAM
16512  ******************************************************************************/
16514  uint32_t *p_offset)
16515 {
16516  if (!hwdesc)
16517  {
16518  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed null parameter\n", __func__);
16519  return NI_RETCODE_INVALID_PARAM;
16520  }
16521 
16522  if (hwdesc->ui16FrameIdx <= NI_GET_MIN_HWDESC_P2P_BUF_ID(p_ctx->ddr_config) ||
16524  {
16525  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() pass invalid data. "
16526  "FrameIdx %d OOR (%d,%d]. DDR config %d \n", __func__, hwdesc->ui16FrameIdx,
16528  p_ctx->ddr_config);
16529  return NI_RETCODE_INVALID_PARAM;
16530  }
16531 
16532  *p_offset = (hwdesc->ui16FrameIdx - NI_GET_MIN_HWDESC_P2P_BUF_ID(p_ctx->ddr_config)) *
16534 
16535  return NI_RETCODE_SUCCESS;
16536 }
16537 #endif
16538 
16539 /* AI functions */
16541  void *nb_data, uint32_t nb_size)
16542 {
16543  void *p_ai_config = NULL;
16544  void *p_nb_data = NULL;
16545  uint32_t buffer_size;
16546  // uint8_t *p_data;
16547  // uint32_t transferred, this_size;
16549  uint32_t ui32LBA = 0;
16550  uint32_t config_size;
16551  void *p_buffer = NULL;
16552  uint32_t dataLen;
16553  ni_instance_buf_info_t buf_info = {0};
16554  int retry_count = 0;
16555 
16556  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
16557 
16558  if (!p_ctx)
16559  {
16560  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
16561  __func__);
16562  return NI_RETCODE_INVALID_PARAM;
16563  }
16564 
16565  if (!nb_data || nb_size == 0) {
16566  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() invalid nb_data %p nb_size %u\n",
16567  __func__, nb_data, nb_size);
16568  return NI_RETCODE_INVALID_PARAM;
16569  }
16570 
16571  ni_pthread_mutex_lock(&p_ctx->mutex);
16572 
16573  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
16574  {
16575  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
16576  __func__);
16578  LRETURN;
16579  }
16580 
16581  config_size = (sizeof(ni_ai_config_t) + (NI_MEM_PAGE_ALIGNMENT - 1)) &
16582  ~(NI_MEM_PAGE_ALIGNMENT - 1);
16583  if (ni_posix_memalign(&p_ai_config, sysconf(_SC_PAGESIZE), config_size))
16584  {
16585  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: Cannot allocate ai config buffer.\n");
16586  retval = NI_RETCODE_ERROR_MEM_ALOC;
16587  LRETURN;
16588  }
16589 
16590  ((ni_ai_config_t *)p_ai_config)->ui32NetworkBinarySize = nb_size;
16591  ni_calculate_sha256(nb_data, nb_size,
16592  ((ni_ai_config_t *)p_ai_config)->ui8Sha256);
16593 
16594  buffer_size =
16595  (nb_size + (NI_MEM_PAGE_ALIGNMENT - 1)) & ~(NI_MEM_PAGE_ALIGNMENT - 1);
16596  if (ni_posix_memalign(&p_nb_data, sysconf(_SC_PAGESIZE), buffer_size))
16597  {
16598  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: Cannot allocate encConf buffer.\n");
16599  retval = NI_RETCODE_ERROR_MEM_ALOC;
16600  LRETURN;
16601  }
16602 
16603  memcpy(p_nb_data, nb_data, nb_size);
16604 
16605  /* configure network binary size to be written */
16607  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): LBA 0x%x, nb_size %u\n", __func__, ui32LBA,
16608  ((ni_ai_config_t *)p_ai_config)->ui32NetworkBinarySize);
16609 
16610  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
16611  p_ai_config, config_size, ui32LBA);
16612  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
16613  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
16614  if (NI_RETCODE_SUCCESS != retval)
16615  {
16616  ni_log2(p_ctx, NI_LOG_ERROR,
16617  "ERROR: ni_nvme_send_admin_cmd failed: blk_io_handle: %" PRIx64
16618  ", hw_id, %u, xcoder_inst_id: %d\n",
16619  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
16620  //Close the session since we can't configure it
16621  retval = ni_ai_session_close(p_ctx, 0);
16622  if (NI_RETCODE_SUCCESS != retval)
16623  {
16624  ni_log2(p_ctx, NI_LOG_ERROR,
16625  "ERROR: ni_ai_session_close failed: blk_io_handle: %" PRIx64
16626  ", hw_id, %u, xcoder_inst_id: %d\n",
16627  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id,
16628  p_ctx->session_id);
16629  }
16630 
16632  LRETURN;
16633  }
16634 
16635  dataLen = (sizeof(ni_network_layer_info_t) + (NI_MEM_PAGE_ALIGNMENT - 1)) &
16636  ~(NI_MEM_PAGE_ALIGNMENT - 1);
16637  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), dataLen))
16638  {
16639  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: Cannot allocate buffer.\n");
16640  retval = NI_RETCODE_ERROR_MEM_ALOC;
16641  LRETURN;
16642  }
16643 
16644  for (;;)
16645  {
16646  /* test if the model is already exist. if not, then continue to write binary data */
16647  memset(p_buffer, 0, dataLen);
16649  "6J") >= 0)
16650  {
16651  ui32LBA = QUERY_INSTANCE_NL_SIZE_V2_R(p_ctx->session_id,
16653  } else
16654  {
16655  ui32LBA =
16657  }
16658  retval =
16660  p_buffer, dataLen, ui32LBA);
16661  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
16662  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
16663  if (retval != NI_RETCODE_SUCCESS)
16664  {
16665  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
16667  LRETURN;
16668  }
16669  if (((ni_instance_buf_info_t *)p_buffer)->buf_avail_size > 0)
16670  {
16671  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): network binary registered\n", __func__);
16672  LRETURN;
16673  }
16674 
16675  /* if this model is not ready, try to require nbuffer to write */
16677  NI_DEVICE_TYPE_AI, &buf_info);
16678  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
16679  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
16680  if (retval != NI_RETCODE_SUCCESS)
16681  {
16682  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): Query Write buffer Failed\n", __func__);
16683  LRETURN;
16684  }
16685  if (buf_info.buf_avail_size < nb_size)
16686  {
16687  /*
16688  * Cannot aquire the write access to nbuffer because someone else on the go,
16689  * Retry to see if model is ready.
16690  */
16691  ni_log2(p_ctx, NI_LOG_TRACE,
16692  "AI write query failed or buf_size %u < "
16693  "frame_size %u.\n", buf_info.buf_avail_size, nb_size);
16694  } else
16695  {
16696  /* Grant write access to nbuffer */
16697  ni_log2(p_ctx, NI_LOG_DEBUG,
16698  "Info ai write query success, available buf "
16699  "size %u >= frame size %u !\n",
16700  buf_info.buf_avail_size, nb_size);
16701  break;
16702  }
16703  retry_count++;
16704  /* Wait for at least 30s */
16706  {
16707  ni_pthread_mutex_unlock(&p_ctx->mutex);
16708  ni_usleep(100);
16709  ni_pthread_mutex_lock(&p_ctx->mutex);
16710  } else
16711  {
16712  ni_log2(p_ctx, NI_LOG_DEBUG, "AI network binary configuration polling timeout\n");
16714  LRETURN;
16715  }
16716  }
16717 
16718 #if 1
16719  /* write network binary data */
16720  ui32LBA = WRITE_INSTANCE_W(p_ctx->session_id, NI_DEVICE_TYPE_AI);
16721  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): write nb LBA 0x%x\n", __func__, ui32LBA);
16722  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
16723  p_nb_data, buffer_size, ui32LBA);
16724  ni_log2(p_ctx, NI_LOG_DEBUG, "write complete retval %d\n", retval);
16725  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_write, p_ctx->device_type,
16726  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
16727  if (NI_RETCODE_SUCCESS != retval)
16728  {
16729  ni_log2(p_ctx, NI_LOG_ERROR,
16730  "ERROR: ni_nvme_send_admin_cmd failed: blk_io_handle: %" PRIx64
16731  ", hw_id, %u, xcoder_inst_id: %d\n",
16732  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
16733  //Close the session since we can't configure it
16734  retval = ni_ai_session_close(p_ctx, 0);
16735  if (NI_RETCODE_SUCCESS != retval)
16736  {
16737  ni_log2(p_ctx, NI_LOG_ERROR,
16738  "ERROR: ni_ai_session_close failed: blk_io_handle: %" PRIx64
16739  ", hw_id, %u, xcoder_inst_id: %d\n",
16740  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id,
16741  p_ctx->session_id);
16742  }
16743 
16745  LRETURN;
16746  }
16747 
16748 #else
16749  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): write nb buffer_size %u\n", __func__,
16750  buffer_size);
16751  for (transferred = 0; transferred < buffer_size; transferred += this_size)
16752  {
16753  this_size = p_ctx->max_nvme_io_size < (buffer_size - transferred) ?
16754  p_ctx->max_nvme_io_size :
16755  (buffer_size - transferred);
16756 
16757  if (this_size & (4096 - 1))
16758  {
16759  this_size = (this_size + (4096 - 1)) & ~(4096 - 1);
16760  }
16761  /* write network binary data */
16762  ui32LBA = WRITE_INSTANCE_W(p_ctx->session_id, NI_DEVICE_TYPE_AI) +
16763  (transferred >> 12);
16764  ni_log2(p_ctx, NI_LOG_DEBUG,
16765  "%s(): write nb LBA 0x%x, this_size %u, page_offset %u\n",
16766  __func__, ui32LBA, this_size, (transferred >> 12));
16767  p_data = (uint8_t *)p_nb_data + transferred;
16768  retval = ni_nvme_send_write_cmd(
16769  p_ctx->blk_io_handle, p_ctx->event_handle,
16770  (uint8_t *)p_nb_data + transferred, this_size, ui32LBA);
16771  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): write retval %d\n", __func__, retval);
16772  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_write, p_ctx->device_type,
16773  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
16774  if (NI_RETCODE_SUCCESS != retval)
16775  {
16776  ni_log(
16777  NI_LOG_ERROR,
16778  "ERROR: ni_nvme_send_admin_cmd failed: blk_io_handle: %" PRIx64
16779  ", hw_id, %u, xcoder_inst_id: %d\n",
16780  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
16781  //Close the session since we can't configure it
16782  retval = ni_ai_session_close(p_ctx, 0);
16783  if (NI_RETCODE_SUCCESS != retval)
16784  {
16785  ni_log(
16786  NI_LOG_ERROR,
16787  "ERROR: ni_ai_session_close failed: blk_io_handle: %" PRIx64
16788  ", hw_id, %u, xcoder_inst_id: %d\n",
16789  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id,
16790  p_ctx->session_id);
16791  }
16792 
16794  LRETURN;
16795  }
16796  }
16797 
16798 #endif
16799 
16800 END:
16801  ni_pthread_mutex_unlock(&p_ctx->mutex);
16802 
16803  ni_aligned_free(p_ai_config);
16804  ni_aligned_free(p_nb_data);
16805  ni_aligned_free(p_buffer);
16806 
16807  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
16808 
16809  return retval;
16810 }
16811 
16813 {
16814  void *p_stream_info = NULL;
16815  void *p_ai_config = NULL;
16816  void *p_nb_data = NULL;
16818  uint32_t ui32LBA = 0;
16819  uint32_t config_size;
16820 
16821  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
16822 
16823  if (!p_ctx)
16824  {
16825  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
16826  __func__);
16827  retval = NI_RETCODE_INVALID_PARAM;
16828  LRETURN;
16829  }
16830 
16831  ni_pthread_mutex_lock(&p_ctx->mutex);
16832 
16833  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
16834  {
16835  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
16836  __func__);
16838  LRETURN;
16839  }
16840 
16841  config_size = (sizeof(ni_instance_mgr_stream_info_t) + (NI_MEM_PAGE_ALIGNMENT - 1)) &
16842  ~(NI_MEM_PAGE_ALIGNMENT - 1);
16843 
16844  if (ni_posix_memalign(&p_stream_info, sysconf(_SC_PAGESIZE), config_size))
16845  {
16846  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: Cannot allocate stream info buffer.\n");
16847  retval = NI_RETCODE_ERROR_MEM_ALOC;
16848  LRETURN;
16849  }
16850 
16851  ((ni_instance_mgr_stream_info_t *)p_stream_info)->picture_width = p_ctx->active_video_width;
16852  ((ni_instance_mgr_stream_info_t *)p_stream_info)->picture_height = p_ctx->active_video_height;
16853  ((ni_instance_mgr_stream_info_t *)p_stream_info)->ai_model_type = AI_MODEL_TYPE_HVSPLUS_FILTER;
16854  ((ni_instance_mgr_stream_info_t *)p_stream_info)->ai_enhance_level = p_ctx->hvsplus_level;
16855  ((ni_instance_mgr_stream_info_t *)p_stream_info)->pix_format = p_ctx->pixel_format;
16856 
16857  ni_log2(p_ctx, NI_LOG_DEBUG,
16858  "### %s: width %d %d height %d model type %d level %d pix_format %d\n", __func__,
16859  p_ctx->active_video_width, p_ctx->ori_width, p_ctx->active_video_height,
16860  ((ni_instance_mgr_stream_info_t *)p_stream_info)->ai_model_type,
16861  ((ni_instance_mgr_stream_info_t *)p_stream_info)->ai_enhance_level,
16862  ((ni_instance_mgr_stream_info_t *)p_stream_info)->pix_format);
16863 
16864  /* configure AI manager to load the suitable network binary */
16866  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));
16867 
16868  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
16869  p_stream_info, config_size, ui32LBA);
16870  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
16871  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
16872  if (NI_RETCODE_SUCCESS != retval)
16873  {
16874  ni_log2(p_ctx, NI_LOG_ERROR,
16875  "ERROR: ni_nvme_send_admin_cmd failed: blk_io_handle: %" PRIx64
16876  ", hw_id, %u, xcoder_inst_id: %d\n",
16877  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
16878  //Close the session since we can't configure it
16879  retval = ni_ai_session_close(p_ctx, 0);
16880  if (NI_RETCODE_SUCCESS != retval)
16881  {
16882  ni_log2(p_ctx, NI_LOG_ERROR,
16883  "ERROR: ni_ai_session_close failed: blk_io_handle: %" PRIx64
16884  ", hw_id, %u, xcoder_inst_id: %d\n",
16885  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id,
16886  p_ctx->session_id);
16887  }
16888 
16890  LRETURN;
16891  }
16892 
16893 END:
16894  ni_pthread_mutex_unlock(&p_ctx->mutex);
16895 
16896  ni_aligned_free(p_ai_config);
16897  ni_aligned_free(p_nb_data);
16898  ni_aligned_free(p_stream_info);
16899 
16900  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
16901 
16902  return retval;
16903 }
16904 
16906 {
16908  void *p_buffer = NULL;
16909  uint32_t ui32LBA = 0;
16910  int retry_count = 0;
16911  uint32_t dataLen = (sizeof(ni_network_layer_info_t) + (NI_MEM_PAGE_ALIGNMENT - 1)) &
16912  ~(NI_MEM_PAGE_ALIGNMENT - 1);
16913  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), dataLen))
16914  {
16915  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: Cannot allocate buffer.\n");
16916  retval = NI_RETCODE_ERROR_MEM_ALOC;
16917  LRETURN;
16918  }
16919  for(;;)
16920  {
16921  memset(p_buffer, 0, dataLen);
16922  ui32LBA =
16924  retval =
16926  p_buffer, dataLen, ui32LBA);
16927  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
16928  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
16929  if (retval != NI_RETCODE_SUCCESS)
16930  {
16931  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
16932  }
16933  if (((ni_instance_buf_info_t *)p_buffer)->buf_avail_size > 0)
16934  {
16935  ni_log2(p_ctx, NI_LOG_INFO, "%s(): network binary registered\n", __func__);
16936  break;
16937  }
16938 
16939  retry_count++;
16940  /* Wait for at least 30s */
16942  {
16943  ni_usleep(100);
16944  } else
16945  {
16946  ni_log2(p_ctx, NI_LOG_DEBUG, "AI network binary configuration polling timeout\n");
16948  LRETURN;
16949  }
16950  }
16951 END:
16952 
16953  ni_aligned_free(p_buffer);
16954  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
16955 
16956  return retval;
16957 
16958 }
16959 
16961  ni_frame_t *p_frame)
16962 {
16964  uint32_t ui32LBA = 0;
16965  ni_instance_buf_info_t buf_info = {0};
16966  uint32_t frame_size_bytes;
16967  uint32_t sent_size = 0;
16968  int32_t query_retry = 0;
16969  uint32_t dataLen;
16970  ni_network_buffer_info_t *buffer_info = NULL;
16971 
16972  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
16973 
16974  if (!p_ctx || !p_frame)
16975  {
16976  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s(): passed parameters is null\n",
16977  __func__);
16978  retval = NI_RETCODE_INVALID_PARAM;
16979  return retval;
16980  }
16981 
16982  if (p_frame->data_len[0] == 0)
16983  {
16984  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() invalid data length\n",
16985  __func__);
16986  retval = NI_RETCODE_INVALID_PARAM;
16987  return retval;
16988  }
16989 
16990  ni_pthread_mutex_lock(&p_ctx->mutex);
16991 
16992 
16993  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
16994  {
16995  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
16996  __func__);
16998  LRETURN;
16999  }
17000 
17001  frame_size_bytes = p_frame->data_len[0];
17002  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): frame_size_bytes %u\n", __func__,
17003  frame_size_bytes);
17004 
17005  for (;;)
17006  {
17007  if (p_ctx->session_statistic.ui32WrBufAvailSize >= frame_size_bytes)
17008  {
17009  buf_info.buf_avail_size =
17011  ni_log2(p_ctx, NI_LOG_DEBUG,
17012  "Info ai write query success, available buf "
17013  "size %u >= frame size %u !\n",
17014  buf_info.buf_avail_size, frame_size_bytes);
17015  break;
17016  }
17017 
17019  "6K") >= 0)
17020  {
17022  &p_ctx->session_statistic);
17023  CHECK_ERR_RC(p_ctx, retval, &p_ctx->session_statistic,
17025  p_ctx->hw_id, &(p_ctx->session_id), OPT_2);
17026  buf_info.buf_avail_size =
17028  } else
17029  {
17031  NI_DEVICE_TYPE_AI, &buf_info);
17032  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
17033  p_ctx->device_type, p_ctx->hw_id,
17034  &(p_ctx->session_id), OPT_1);
17035  }
17036  if (NI_RETCODE_SUCCESS != retval ||
17037  buf_info.buf_avail_size < frame_size_bytes)
17038  {
17039  ni_log2(p_ctx, NI_LOG_TRACE,
17040  "AI write query failed or buf_size < frame_size. Retry %d\n",
17041  query_retry);
17042  // extend to 5000 retries for 8K encode on FPGA
17043  if (query_retry >= NI_MAX_ENCODER_QUERY_RETRIES)
17044  {
17045  ni_log2(p_ctx, NI_LOG_DEBUG,
17046  "AI write query exceeded max retries: %d\n",
17049  retval = NI_RETCODE_SUCCESS;
17050  LRETURN;
17051  }
17052  ni_pthread_mutex_unlock(&p_ctx->mutex);
17054  ni_pthread_mutex_lock(&p_ctx->mutex);
17055  query_retry++;
17056  }
17057  }
17058 
17060  < 0 || p_frame->iovec_num <= 1) {
17061  void *p_data;
17062  if (p_frame->iovec_num == 1) {
17063  if (!p_frame->iovec) {
17064  ni_log2(p_ctx, NI_LOG_ERROR, "Ai session write: invaid iovec\n");
17065  retval = NI_RETCODE_INVALID_PARAM;
17066  LRETURN;
17067  }
17068  if (!p_frame->iovec[0].ptr || p_frame->iovec[0].size == 0 ||
17069  ((int64_t)p_frame->iovec[0].ptr & (NI_MEM_PAGE_ALIGNMENT - 1))) {
17070  ni_log2(p_ctx, NI_LOG_ERROR, "Ai session write: invalid iovec ptr 0x%lx, size %u\n",
17071  (int64_t)p_frame->iovec[0].ptr, p_frame->iovec[0].size);
17072  retval = NI_RETCODE_INVALID_PARAM;
17073  LRETURN;
17074  }
17075  p_data = p_frame->iovec[0].ptr;
17076  } else {
17077  p_data = p_frame->p_buffer; //equals to p_frame->p_data[0]
17078  }
17079  ui32LBA = WRITE_INSTANCE_W(p_ctx->session_id, NI_DEVICE_TYPE_AI);
17080  ni_log2(p_ctx, NI_LOG_DEBUG, "Ai session write: p_data = %p, p_frame->buffer_size = %u, "
17081  "p_ctx->frame_num = %" PRIu64 ", LBA = 0x%x\n",
17082  p_data, p_frame->buffer_size, p_ctx->frame_num,
17083  ui32LBA);
17084 
17085  sent_size = frame_size_bytes;
17086  if (sent_size & (NI_MEM_PAGE_ALIGNMENT - 1))
17087  {
17088  sent_size = (sent_size + NI_MEM_PAGE_ALIGNMENT - 1) &
17089  ~(NI_MEM_PAGE_ALIGNMENT - 1);
17090  }
17091 
17092  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
17093  p_data, sent_size, ui32LBA);
17095  "6K") >= 0)
17096  {
17097  if (retval != NI_RETCODE_SUCCESS)
17098  {
17099  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
17101  LRETURN;
17102  }
17104  &p_ctx->session_statistic);
17105  CHECK_ERR_RC(p_ctx, retval, &p_ctx->session_statistic,
17107  p_ctx->hw_id, &(p_ctx->session_id), OPT_2);
17108  } else
17109  {
17110  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_write, p_ctx->device_type,
17111  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
17112  if (retval < 0)
17113  {
17114  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
17116  LRETURN;
17117  }
17118  }
17119  } else { //for iovec
17120  //validate iovec
17121  uint32_t i;
17122  uint32_t rel_offset = 0;
17123  int32_t iovec_index = 0;
17124  int32_t iovec_left;
17125 
17126  if (!p_frame->iovec) {
17127  ni_log2(p_ctx, NI_LOG_ERROR, "Ai session write: invaid iovec\n");
17128  retval = NI_RETCODE_INVALID_PARAM;
17129  LRETURN;
17130  }
17131 
17132  for (i = 0; i < p_frame->iovec_num; i++) {
17133  ni_iovec_t *this_iovec = &p_frame->iovec[i];
17134 
17135  ni_log2(p_ctx, NI_LOG_DEBUG, "iovec %d, ptr %p, size %u\n", i,
17136  this_iovec->ptr, this_iovec->size);
17137  if ((this_iovec->ptr == NULL) || (this_iovec->size == 0) ||
17138  ((int64_t)this_iovec->ptr & (NI_MEM_PAGE_ALIGNMENT - 1))) {
17139  ni_log2(p_ctx, NI_LOG_ERROR, "Ai session write: invalid iovec ptr 0x%lx, size %u\n",
17140  (int64_t)this_iovec->ptr, this_iovec->size);
17141  retval = NI_RETCODE_INVALID_PARAM;
17142  LRETURN;
17143  }
17144  }
17145 
17146  //1, write iovec details
17147  dataLen = (sizeof(ni_network_buffer_info_t) + NI_MEM_PAGE_ALIGNMENT - 1) &
17148  ~(NI_MEM_PAGE_ALIGNMENT - 1);
17149  if (ni_posix_memalign((void **)&buffer_info, sysconf(_SC_PAGESIZE), dataLen))
17150  {
17151  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
17152  NI_ERRNO, __func__);
17154  }
17155  memset(buffer_info, 0x00, dataLen);
17156 
17157  //2, write iovec
17158  iovec_left = p_frame->iovec_num;
17159  do {
17160  int iovec_batch = iovec_left < NI_MAX_SEGMENT_NUM ? iovec_left : NI_MAX_SEGMENT_NUM;
17161  uint32_t rel_lba = 0;
17162 
17163  buffer_info->ui16Option = NI_AI_FLAG_IOVEC;
17164  buffer_info->ui32IovecNum = iovec_batch;
17165  buffer_info->ui32RelOffset = rel_offset;
17166  for (i = 0; (int)i < iovec_batch; i++) {
17167  ni_iovec_t *iovec = &p_frame->iovec[iovec_index + i];
17168  buffer_info->segment[i].ui32RelLba = rel_lba;
17169  buffer_info->segment[i].ui32Size = iovec->size;
17170  rel_lba += (((iovec->size + NI_MEM_PAGE_ALIGNMENT - 1) &
17171  ~(NI_MEM_PAGE_ALIGNMENT - 1)) >> LBA_BIT_OFFSET) + 1;
17172  rel_offset += iovec->size;
17173  }
17174  ni_log2(p_ctx, NI_LOG_DEBUG, "Dev alloc frame: frame_index %u\n",
17175  buffer_info->ui16FrameIdx[0]);
17176 
17177  ui32LBA =
17179 
17180  retval =
17182  buffer_info, dataLen, ui32LBA);
17183  //CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
17184  // p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id),
17185  // OPT_1);
17186  if (NI_RETCODE_SUCCESS != retval)
17187  {
17188  ni_log2(p_ctx,
17189  NI_LOG_ERROR,
17190  "ERROR: ni_nvme_send_admin_cmd failed: blk_io_handle: %" PRIx64
17191  ", hw_id, %u, xcoder_inst_id: %d\n",
17192  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
17193  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed!\n",
17194  __func__);
17196  LRETURN;
17197  }
17198 
17199 #ifdef __linux__
17200  for (i = 0; (int)i < iovec_batch; i++) {
17201  ni_iovec_t *iovec = &p_frame->iovec[iovec_index + i];
17202  ni_segment_t *segment = &buffer_info->segment[i];
17203  ui32LBA = WRITE_INSTANCE_W(p_ctx->session_id, NI_DEVICE_TYPE_AI) + segment->ui32RelLba;
17204  ni_log2(p_ctx, NI_LOG_DEBUG, "Ai session write: data = %p, size = %u, "
17205  "p_ctx->frame_num = %" PRIu64 ", LBA = 0x%x\n",
17206  iovec->ptr, iovec->size, p_ctx->frame_num, ui32LBA);
17207  ni_nvme_setup_aio_iocb(p_ctx->blk_io_handle, p_ctx->iocbs[i], iovec->ptr,
17208  (iovec->size + NI_MEM_PAGE_ALIGNMENT - 1) & ~(NI_MEM_PAGE_ALIGNMENT - 1),
17209  ui32LBA, 1);
17210  }
17211 
17212  retval = ni_nvme_batch_cmd_aio(p_ctx->aio_context, p_ctx->iocbs, p_ctx->io_event, iovec_batch);
17213  if (retval != NI_RETCODE_SUCCESS) {
17214  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): failed to do batch io\n", __func__);
17215  LRETURN;
17216  }
17217 #else
17218  for (i = 0; (int)i < iovec_batch; i++) {
17219  ni_iovec_t *iovec = &p_frame->iovec[iovec_index + i];
17220  ui32LBA = WRITE_INSTANCE_W(p_ctx->session_id, NI_DEVICE_TYPE_AI) + rel_lba;
17221  ni_log2(p_ctx, NI_LOG_DEBUG, "Ai session write: data = %p, size = %u, "
17222  "p_ctx->frame_num = %" PRIu64 ", LBA = 0x%x\n",
17223  iovec->ptr, iovec->size, p_ctx->frame_num,
17224  ui32LBA);
17225 
17226  sent_size = (iovec->size + NI_MEM_PAGE_ALIGNMENT - 1) &
17227  ~(NI_MEM_PAGE_ALIGNMENT - 1);
17228  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
17229  iovec->ptr, sent_size, ui32LBA);
17230  if (retval != NI_RETCODE_SUCCESS) {
17231  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
17232  LRETURN;
17233  }
17234  rel_lba = (((iovec->size + NI_MEM_PAGE_ALIGNMENT - 1) &
17235  ~(NI_MEM_PAGE_ALIGNMENT - 1)) >> LBA_BIT_OFFSET) + 1;
17236  }
17237 #endif
17238  iovec_index += iovec_batch;
17239  iovec_left -= iovec_batch;
17240  } while (iovec_left);
17241 
17243  "6K") >= 0)
17244  {
17245  if (retval != NI_RETCODE_SUCCESS)
17246  {
17247  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
17249  LRETURN;
17250  }
17252  &p_ctx->session_statistic);
17253  CHECK_ERR_RC(p_ctx, retval, &p_ctx->session_statistic,
17255  p_ctx->hw_id, &(p_ctx->session_id), OPT_2);
17256  } else
17257  {
17258  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_write, p_ctx->device_type,
17259  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
17260  if (retval < 0)
17261  {
17262  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
17264  LRETURN;
17265  }
17266  }
17267  }
17268 
17269  p_ctx->frame_num++;
17270 
17271  retval = frame_size_bytes;
17272 
17273 END:
17274 
17275  ni_pthread_mutex_unlock(&p_ctx->mutex);
17276 
17277  free(buffer_info);
17278 
17279  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
17280 
17281  return retval;
17282 }
17283 
17285  ni_packet_t *p_packet)
17286 {
17287  uint32_t actual_read_size = 0;
17288  int retval = NI_RETCODE_SUCCESS;
17289  uint32_t ui32LBA = 0;
17290  ni_instance_buf_info_t buf_info = {0};
17291 
17292  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
17293 
17294  if (!p_ctx || !p_packet || !p_packet->p_data)
17295  {
17296  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
17297  __func__);
17298  retval = NI_RETCODE_INVALID_PARAM;
17299  return retval;
17300  }
17301 
17302  ni_pthread_mutex_lock(&p_ctx->mutex);
17303 
17304  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
17305  {
17306  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
17307  __func__);
17309  LRETURN;
17310  }
17311 
17312  for (;;)
17313  {
17314  if (p_ctx->session_statistic.ui32RdBufAvailSize >= p_packet->data_len)
17315  {
17316  buf_info.buf_avail_size =
17318  ni_log2(p_ctx, NI_LOG_DEBUG,
17319  "Info ai read query success, available buf "
17320  "size %u >= frame size %u !\n",
17321  buf_info.buf_avail_size, p_packet->data_len);
17322  break;
17323  }
17325  "6K") >= 0)
17326  {
17328  &p_ctx->session_statistic);
17329  CHECK_ERR_RC(p_ctx, retval, &p_ctx->session_statistic,
17331  p_ctx->hw_id, &(p_ctx->session_id), OPT_2);
17332  buf_info.buf_avail_size =
17334  } else
17335  {
17337  NI_DEVICE_TYPE_AI, &buf_info);
17338  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
17339  p_ctx->device_type, p_ctx->hw_id,
17340  &(p_ctx->session_id), OPT_1);
17341  }
17342  ni_log2(p_ctx, NI_LOG_TRACE,
17343  "Info ai read query rc %d, available buf size %u, "
17344  "frame_num=%" PRIu64 ", pkt_num=%" PRIu64 "\n",
17345  retval, buf_info.buf_avail_size, p_ctx->frame_num,
17346  p_ctx->pkt_num);
17347 
17348  if (NI_RETCODE_SUCCESS != retval)
17349  {
17350  ni_log2(p_ctx, NI_LOG_DEBUG, "Buffer info query failed in ai read!!!!\n");
17351  LRETURN;
17352  } else if (0 == buf_info.buf_avail_size)
17353  {
17354  ni_log2(p_ctx, NI_LOG_DEBUG, "Info ai read available buf size %u, eos %u !\n",
17355  buf_info.buf_avail_size, p_packet->end_of_stream);
17356  retval = NI_RETCODE_SUCCESS;
17357  LRETURN;
17358  }
17359  }
17360  ni_log2(p_ctx, NI_LOG_DEBUG, "Ai read buf_avail_size %u\n", buf_info.buf_avail_size);
17361 
17362  assert(buf_info.buf_avail_size >= p_packet->data_len);
17363 
17364  ui32LBA = READ_INSTANCE_R(p_ctx->session_id, NI_DEVICE_TYPE_AI);
17365  actual_read_size = p_packet->data_len;
17366  if (actual_read_size & (NI_MEM_PAGE_ALIGNMENT - 1))
17367  {
17368  actual_read_size = (actual_read_size + (NI_MEM_PAGE_ALIGNMENT - 1)) &
17369  ~(NI_MEM_PAGE_ALIGNMENT - 1);
17370  }
17371 
17372  retval = ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
17373  p_packet->p_data, actual_read_size, ui32LBA);
17375  "6K") >= 0)
17376  {
17377  if (retval != NI_RETCODE_SUCCESS)
17378  {
17379  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
17381  LRETURN;
17382  }
17384  &p_ctx->session_statistic);
17385  CHECK_ERR_RC(p_ctx, retval, &p_ctx->session_statistic,
17387  p_ctx->hw_id, &(p_ctx->session_id), OPT_2);
17388  } else
17389  {
17390  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_read, p_ctx->device_type,
17391  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
17392  if (retval < 0)
17393  {
17394  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
17396  LRETURN;
17397  }
17398  }
17399 
17400  retval = p_packet->data_len;
17401 
17402 END:
17403 
17404  ni_pthread_mutex_unlock(&p_ctx->mutex);
17405 
17406  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
17407 
17408  return retval;
17409 }
17410 
17411 static void ni_unreference_network_data(ni_network_data_t *network_data)
17412 {
17413  if (network_data)
17414  {
17415  ni_memfree(network_data->inset);
17416  ni_memfree(network_data->linfo.in_param);
17417  }
17418 }
17419 
17421  ni_network_data_t *p_network)
17422 {
17423  void *p_buffer = NULL;
17424  void *p_info = NULL;
17426  uint32_t ui32LBA = 0;
17427  uint32_t dataLen;
17428  int32_t query_retry = 0;
17429  uint32_t l;
17430  ni_network_layer_params_t *layer_param;
17431  uint32_t buffer_size;
17432  uint32_t this_size;
17433  ni_network_data_t *network_data = NULL;
17434  uint32_t total_io_num;
17435 
17436  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
17437 
17438  if (!p_ctx || !p_network)
17439  {
17440  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
17441  __func__);
17442  retval = NI_RETCODE_INVALID_PARAM;
17443  LRETURN;
17444  }
17445 
17446  network_data = p_ctx->network_data;
17447  if (network_data->linfo.in_param != NULL)
17448  {
17449  ni_log2(p_ctx, NI_LOG_ERROR,
17450  "ERROR: %s(): network parameters data is already initialized\n",
17451  __func__);
17452  retval = NI_RETCODE_SUCCESS;
17453  LRETURN;
17454  }
17455 
17456  ni_pthread_mutex_lock(&p_ctx->mutex);
17457 
17458  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
17459  {
17460  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
17461  __func__);
17463  LRETURN;
17464  }
17465 
17466  /* query available size can be read. */
17467  dataLen = (sizeof(ni_instance_buf_info_t) + (NI_MEM_PAGE_ALIGNMENT - 1)) &
17468  ~(NI_MEM_PAGE_ALIGNMENT - 1);
17469  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), dataLen))
17470  {
17471  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
17472  NI_ERRNO, __func__);
17473  retval = NI_RETCODE_ERROR_MEM_ALOC;
17474  LRETURN;
17475  }
17476  memset(p_buffer, 0, dataLen);
17477 
17478  for (;;)
17479  {
17481  "6J") >= 0)
17482  {
17483  ui32LBA = QUERY_INSTANCE_NL_SIZE_V2_R(p_ctx->session_id,
17485  } else
17486  {
17487  ui32LBA =
17489  }
17490  retval =
17492  p_buffer, dataLen, ui32LBA);
17493  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_read, p_ctx->device_type,
17494  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
17495  if (retval != NI_RETCODE_SUCCESS)
17496  {
17497  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
17499  LRETURN;
17500  }
17501 
17502  if (((ni_instance_buf_info_t *)p_buffer)->buf_avail_size > 0)
17503  {
17504  break;
17505  }
17506 
17507  query_retry++;
17508  if (query_retry > 50000)
17509  {
17510  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): exceeded max query retries: %d\n",
17511  __func__, query_retry - 1);
17513  LRETURN;
17514  }
17515  ni_pthread_mutex_unlock(&p_ctx->mutex);
17516  ni_usleep(200);
17517  ni_pthread_mutex_lock(&p_ctx->mutex);
17518  continue;
17519  }
17520 
17521  if (((ni_instance_buf_info_t *)p_buffer)->buf_avail_size == 0)
17522  {
17524  LRETURN;
17525  }
17526 
17528  "6J") >= 0)
17529  {
17530  network_data->input_num =
17531  ((ni_instance_buf_info_t *)p_buffer)->buf_avail_size >> 16;
17532  network_data->output_num =
17533  ((ni_instance_buf_info_t *)p_buffer)->buf_avail_size & 0xFFFF;
17534  } else
17535  {
17536  network_data->input_num = 4;
17537  network_data->output_num = 4;
17538  }
17539 
17540  total_io_num = network_data->input_num + network_data->output_num;
17541 
17542  network_data->linfo.in_param = (ni_network_layer_params_t *)calloc(
17543  total_io_num, sizeof(ni_network_layer_params_t));
17544  if (!network_data->linfo.in_param)
17545  {
17546  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): Unable to allocate network layer params\n",
17547  __func__);
17548  retval = NI_RETCODE_ERROR_MEM_ALOC;
17549  LRETURN;
17550  }
17551  network_data->linfo.out_param =
17552  network_data->linfo.in_param + network_data->input_num;
17553 
17554  network_data->inset = (ni_network_layer_offset_t *)calloc(
17555  total_io_num, sizeof(ni_network_layer_offset_t));
17556  if (!network_data->inset)
17557  {
17558  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): Unable to allocate network offset\n",
17559  __func__);
17560  retval = NI_RETCODE_ERROR_MEM_ALOC;
17561  LRETURN;
17562  }
17563  network_data->outset = network_data->inset + network_data->input_num;
17564 
17566  "6J") >= 0)
17567  {
17568  /* query the real network layer data */
17569  this_size = sizeof(ni_network_layer_params_t) * total_io_num;
17570  dataLen = (this_size + (NI_MEM_PAGE_ALIGNMENT - 1)) &
17571  ~(NI_MEM_PAGE_ALIGNMENT - 1);
17572  if (ni_posix_memalign(&p_info, sysconf(_SC_PAGESIZE), dataLen))
17573  {
17574  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate info buffer\n",
17575  NI_ERRNO, __func__);
17576  retval = NI_RETCODE_ERROR_MEM_ALOC;
17577  LRETURN;
17578  }
17579  memset(p_info, 0, dataLen);
17580 
17582  retval = ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
17583  p_info, dataLen, ui32LBA);
17584  if ((int32_t)retval < 0)
17585  {
17586  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
17588  LRETURN;
17589  }
17590  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_read, p_ctx->device_type,
17591  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
17592  memcpy(network_data->linfo.in_param, p_info, this_size);
17593  } else
17594  {
17595  /* query the real network layer data */
17596  this_size = sizeof(ni_network_layer_params_t) * total_io_num;
17597  dataLen = (this_size + (NI_MEM_PAGE_ALIGNMENT - 1)) &
17598  ~(NI_MEM_PAGE_ALIGNMENT - 1);
17599  if (ni_posix_memalign(&p_info, sysconf(_SC_PAGESIZE), dataLen))
17600  {
17601  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate info buffer\n",
17602  NI_ERRNO, __func__);
17603  retval = NI_RETCODE_ERROR_MEM_ALOC;
17604  LRETURN;
17605  }
17606  memset(p_info, 0, dataLen);
17607 
17609  retval = ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
17610  p_info, dataLen, ui32LBA);
17611  if ((int32_t)retval < 0)
17612  {
17613  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
17615  LRETURN;
17616  }
17617  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_read, p_ctx->device_type,
17618  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
17619  memcpy(network_data->linfo.in_param, p_info, this_size);
17620 
17621  for (l = 0, network_data->input_num = 0; l < 4; l++)
17622  {
17623  layer_param = &network_data->linfo.in_param[l];
17624  if (layer_param->num_of_dims == 0)
17625  {
17626  break;
17627  }
17628  network_data->input_num++;
17629  }
17630 
17631  for (l = 0, network_data->output_num = 0; l < 4; l++)
17632  {
17633  layer_param = &network_data->linfo.out_param[l];
17634  if (layer_param->num_of_dims == 0)
17635  {
17636  break;
17637  }
17638  network_data->output_num++;
17639  }
17640  }
17641 
17642  for (l = 0, buffer_size = 0; l < network_data->input_num; l++)
17643  {
17644  layer_param = &network_data->linfo.in_param[l];
17645  this_size = ni_ai_network_layer_size(layer_param);
17646  this_size =
17647  (this_size + NI_AI_HW_ALIGN_SIZE - 1) & ~(NI_AI_HW_ALIGN_SIZE - 1);
17648  network_data->inset[l].offset = buffer_size;
17649  buffer_size += this_size;
17650 
17651  ni_log2(p_ctx,
17652  NI_LOG_DEBUG,
17653  "%s(): network input layer %d: dims %u, %u/%u/%u/%u, f %d, q %d\n",
17654  __func__, l, layer_param->num_of_dims, layer_param->sizes[0],
17655  layer_param->sizes[1], layer_param->sizes[2], layer_param->sizes[3],
17656  layer_param->data_format, layer_param->quant_format);
17657  }
17658 
17659  for (l = 0, buffer_size = 0; l < network_data->output_num; l++)
17660  {
17661  layer_param = &network_data->linfo.out_param[l];
17662  this_size = ni_ai_network_layer_size(layer_param);
17663  this_size =
17664  (this_size + NI_AI_HW_ALIGN_SIZE - 1) & ~(NI_AI_HW_ALIGN_SIZE - 1);
17665  network_data->outset[l].offset = buffer_size;
17666  buffer_size += this_size;
17667 
17668  ni_log2(p_ctx,
17669  NI_LOG_DEBUG,
17670  "%s(): network output layer %d: dims %u, %u/%u/%u/%u, f %d, q %d\n",
17671  __func__, l, layer_param->num_of_dims, layer_param->sizes[0],
17672  layer_param->sizes[1], layer_param->sizes[2], layer_param->sizes[3],
17673  layer_param->data_format, layer_param->quant_format);
17674  }
17675 
17676  memcpy(p_network, network_data, sizeof(ni_network_data_t));
17677 
17678  retval = NI_RETCODE_SUCCESS;
17679 
17680 END:
17681  ni_pthread_mutex_unlock(&p_ctx->mutex);
17682 
17683  if (retval != NI_RETCODE_SUCCESS)
17684  {
17685  ni_unreference_network_data(network_data);
17686  }
17687  ni_aligned_free(p_buffer);
17688  ni_aligned_free(p_info);
17689  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
17690  return retval;
17691 }
17692 
17694 {
17696  void *p_buffer = NULL;
17697  uint32_t ui32LBA = 0;
17698 
17699  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
17700 
17701  if (!p_ctx)
17702  {
17703  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
17704  __func__);
17705  retval = NI_RETCODE_INVALID_PARAM;
17706  LRETURN;
17707  }
17708 
17709  //Check if there is an instance or we need a new one
17710  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
17711  {
17712  p_ctx->device_type = NI_DEVICE_TYPE_AI;
17713  p_ctx->pts_table = NULL;
17714  p_ctx->dts_queue = NULL;
17715  p_ctx->buffer_pool = NULL;
17716  p_ctx->status = 0;
17717  p_ctx->key_frame_type = 0;
17718  p_ctx->keyframe_factor = 1;
17719  p_ctx->frame_num = 0;
17720  p_ctx->pkt_num = 0;
17721  p_ctx->rc_error_count = 0;
17722  p_ctx->force_frame_type = 0;
17723  p_ctx->ready_to_close = 0;
17724  //Sequence change tracking reated stuff
17725  p_ctx->active_video_width = 0;
17726  p_ctx->active_video_height = 0;
17727  p_ctx->p_all_zero_buf = NULL;
17728  p_ctx->actual_video_width = 0;
17730  memset(&(p_ctx->param_err_msg[0]), 0, sizeof(p_ctx->param_err_msg));
17731 
17732  //malloc zero data buffer
17733  if (ni_posix_memalign(&p_ctx->p_all_zero_buf, sysconf(_SC_PAGESIZE),
17735  {
17736  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() alloc all zero buffer failed\n",
17737  NI_ERRNO, __func__);
17738  retval = NI_RETCODE_ERROR_MEM_ALOC;
17739  LRETURN;
17740  }
17741  memset(p_ctx->p_all_zero_buf, 0, NI_DATA_BUFFER_LEN);
17742 
17743  //malloc data buffer
17744  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE),
17746  {
17747  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() alloc data buffer failed\n",
17748  NI_ERRNO, __func__);
17749  retval = NI_RETCODE_ERROR_MEM_ALOC;
17750  LRETURN;
17751  }
17752  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
17753 
17754  //Set session ID to be invalid. In case we cannot open session, the session id wold remain invalid.
17755  //In case we can open sesison, the session id would become valid.
17756  ((ni_session_stats_t *)p_buffer)->ui16SessionId =
17757  (uint16_t)NI_INVALID_SESSION_ID;
17758 
17759  // First uint32_t is either an invaild session ID or a valid session ID, depending on if session could be opened
17760  ui32LBA = OPEN_SESSION_CODEC(NI_DEVICE_TYPE_AI, 0, p_ctx->hw_action);
17761  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): LBA 0x%x, hw_action %d\n", __func__,
17762  ui32LBA, p_ctx->hw_action);
17763  retval =
17765  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
17766  if (retval != NI_RETCODE_SUCCESS)
17767  {
17768  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR ni_nvme_send_read_cmd\n");
17769  LRETURN;
17770  }
17771  //Open will return a session status structure with a valid session id if it worked.
17772  //Otherwise the invalid session id set before the open command will stay
17773  if ((uint16_t)NI_INVALID_SESSION_ID ==
17774  ni_ntohs(((ni_session_stats_t *)p_buffer)->ui16SessionId))
17775  {
17776  ni_log2(p_ctx, NI_LOG_ERROR,
17777  "ERROR %s(): p_ctx->device_handle=%" PRIx64
17778  ", p_ctx->hw_id=%d, p_ctx->session_id=%d\n",
17779  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
17780  p_ctx->session_id);
17781  ni_ai_session_close(p_ctx, 0);
17783  LRETURN;
17784  }
17785  p_ctx->session_id =
17786  ni_ntohs(((ni_session_stats_t *)p_buffer)->ui16SessionId);
17787  p_ctx->session_timestamp = ni_htonl(((ni_session_stats_t *)p_buffer)->ui32Session_timestamp_high);
17788  p_ctx->session_timestamp = (p_ctx->session_timestamp << 32) |
17789  ni_htonl(((ni_session_stats_t *)p_buffer)->ui32Session_timestamp_low);
17790  ni_log2(p_ctx, NI_LOG_DEBUG, "Ai open session ID:0x%x,timestamp:%" PRIu64 "\n",
17791  p_ctx->session_id, p_ctx->session_timestamp);
17792 
17793  ni_log2(p_ctx, NI_LOG_DEBUG, "Open session completed\n");
17794  ni_log2(p_ctx, NI_LOG_DEBUG,
17795  "%s(): p_ctx->device_handle=%" PRIx64
17796  ", p_ctx->hw_id=%d, p_ctx->session_id=%d\n",
17797  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
17798  p_ctx->session_id);
17799 
17800  //Send keep alive timeout Info
17801  uint64_t keep_alive_timeout =
17802  p_ctx->keep_alive_timeout * 1000000; //send us to FW
17803  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
17804  memcpy(p_buffer, &keep_alive_timeout, sizeof(keep_alive_timeout));
17805  ni_log2(p_ctx, NI_LOG_DEBUG, "%s keep_alive_timeout %" PRIx64 "\n", __func__,
17806  keep_alive_timeout);
17808  retval =
17810  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
17811  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
17812  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
17813  CHECK_VPU_RECOVERY(retval);
17814 
17815  if (NI_RETCODE_SUCCESS != retval)
17816  {
17817  ni_log2(p_ctx, NI_LOG_ERROR,
17818  "ERROR %s(): nvme write keep_alive_timeout command "
17819  "failed, blk_io_handle: %" PRIx64 ", hw_id, %d\n",
17820  __func__, (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id);
17822  LRETURN;
17823  }
17824 
17825  p_ctx->network_data =
17826  (ni_network_data_t *)calloc(1, sizeof(ni_network_data_t));
17827  if (!p_ctx->network_data)
17828  {
17829  ni_log2(p_ctx, NI_LOG_ERROR,
17830  "ERROR %s(): Unable to allocate network_data memory\n");
17831  retval = NI_RETCODE_ERROR_MEM_ALOC;
17832  LRETURN;
17833  }
17834  }
17835 
17836  //aio
17837 #ifdef __linux__
17838  if (ni_aio_setup(NI_MAX_SEGMENT_NUM, &p_ctx->aio_context) != 0) {
17839  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): failed to setup aio context\n", __func__);
17840  retval = NI_RETCODE_ERROR_MEM_ALOC;
17841  LRETURN;
17842  }
17843 
17844  p_ctx->iocbs = (ni_iocb_t **)malloc(sizeof(ni_iocb_t *) * NI_MAX_SEGMENT_NUM);
17845  if (!p_ctx->iocbs) {
17846  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): failed to allocate iocbs\n", __func__);
17847  retval = NI_RETCODE_ERROR_MEM_ALOC;
17848  LRETURN;
17849  } else {
17850  int i;
17851  for (i = 0; i < NI_MAX_SEGMENT_NUM; i++) {
17852  p_ctx->iocbs[i] = (ni_iocb_t *)malloc(sizeof(ni_iocb_t));
17853  if (!p_ctx->iocbs[i]) {
17854  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): failed to allocate iocb %d\n", __func__, i);
17855  retval = NI_RETCODE_ERROR_MEM_ALOC;
17856  LRETURN;
17857  }
17858  }
17859  }
17860 
17861  p_ctx->io_event = (ni_io_event_t *)malloc(sizeof(ni_io_event_t) * NI_MAX_SEGMENT_NUM);
17862  if (!p_ctx->io_event) {
17863  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): failed to allocate ioevent %d\n", __func__);
17864  retval = NI_RETCODE_ERROR_MEM_ALOC;
17865  LRETURN;
17866  }
17867 #endif
17868 
17869  // init for frame pts calculation
17870  p_ctx->is_first_frame = 1;
17871  p_ctx->last_pts = 0;
17872  p_ctx->last_dts = 0;
17873  p_ctx->active_video_width = 0;
17874  p_ctx->active_video_height = 0;
17875  p_ctx->actual_video_width = 0;
17876 
17877 END:
17878 
17879  ni_aligned_free(p_buffer);
17880  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
17881  return retval;
17882 }
17883 
17885 {
17887  void *p_buffer = NULL;
17888  uint32_t ui32LBA = 0;
17889 
17890  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
17891 
17892  if (!p_ctx)
17893  {
17894  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
17895  __func__);
17896  return NI_RETCODE_INVALID_PARAM;
17897  }
17898 
17899  ni_pthread_mutex_lock(&p_ctx->mutex);
17900 
17901  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
17902  {
17903  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): Invalid session ID, return.\n", __func__);
17904  retval = NI_RETCODE_SUCCESS;
17905  LRETURN;
17906  }
17907 
17908  //malloc data buffer
17909  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), NI_DATA_BUFFER_LEN))
17910  {
17911  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() alloc data buffer failed\n",
17912  NI_ERRNO, __func__);
17913  retval = NI_RETCODE_ERROR_MEM_ALOC;
17914  LRETURN;
17915  }
17916  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
17917 
17918  ui32LBA = CLOSE_SESSION_R(p_ctx->session_id, NI_DEVICE_TYPE_AI);
17919 
17920  int retry = 0;
17921  while (retry < NI_SESSION_CLOSE_RETRY_MAX)
17922  {
17923  ni_log2(p_ctx, NI_LOG_DEBUG,
17924  "%s(): p_ctx->blk_io_handle=%" PRIx64 ", p_ctx->hw_id=%d, "
17925  "p_ctx->session_id=%d, close_mode=1\n",
17926  __func__, (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id,
17927  p_ctx->session_id);
17928 
17930  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA) < 0)
17931  {
17932  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): command failed\n", __func__);
17935  break;
17936  } else
17937  {
17938  //Close should always succeed
17939  retval = NI_RETCODE_SUCCESS;
17941  break;
17942  }
17943  /*
17944  else if(((ni_session_closed_status_t *)p_buffer)->session_closed)
17945  {
17946  retval = NI_RETCODE_SUCCESS;
17947  p_ctx->session_id = NI_INVALID_SESSION_ID;
17948  break;
17949  }
17950  else
17951  {
17952  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): wait for close\n", __func__);
17953  ni_usleep(NI_SESSION_CLOSE_RETRY_INTERVAL_US);
17954  retval = NI_RETCODE_ERROR_NVME_CMD_FAILED;
17955  }
17956  */
17957  retry++;
17958  }
17959 
17960 END:
17961 
17962 #ifdef __linux__
17963  if (p_ctx->aio_context != 0) {
17964  if (ni_aio_destroy(p_ctx->aio_context) != 0) {
17965  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): failed to destroy aio context\n", __func__);
17966  } else {
17967  p_ctx->aio_context = 0;
17968  }
17969  }
17970  if (p_ctx->iocbs) {
17971  for (int i = 0; i < NI_MAX_SEGMENT_NUM; i++) {
17972  ni_memfree(p_ctx->iocbs[i]);
17973  }
17974  ni_memfree(p_ctx->iocbs);
17975  }
17976  ni_memfree(p_ctx->io_event);
17977 #endif
17978 
17979  ni_unreference_network_data(p_ctx->network_data);
17980  ni_memfree(p_ctx->network_data);
17981 
17982  ni_aligned_free(p_buffer);
17984 
17985  //Sequence change related stuff cleanup here
17986  p_ctx->active_video_width = 0;
17987  p_ctx->active_video_height = 0;
17988  p_ctx->actual_video_width = 0;
17989 
17990  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): CTX[Card:%" PRIx64 " / HW:%d / INST:%d]\n",
17991  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
17992  p_ctx->session_id);
17993  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
17994 
17995  ni_pthread_mutex_unlock(&p_ctx->mutex);
17996 
17997  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
17998 
17999  return retval;
18000 }
18001 
18003  ni_frame_config_t p_cfg_in[],
18004  int numInCfgs,
18005  ni_frame_config_t *p_cfg_out)
18006 {
18008  ni_network_buffer_info_t *p_data = NULL;
18009  void *p_read_data = NULL;
18010  uint32_t dataLen;
18011  uint32_t ui32LBA = 0;
18012 
18013  if (!p_ctx)
18014  {
18015  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
18016  __func__);
18017  return NI_RETCODE_INVALID_PARAM;
18018  }
18019 
18020  if (p_ctx->session_id == NI_INVALID_SESSION_ID)
18021  {
18022  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
18023  __func__);
18025  }
18026  ni_pthread_mutex_lock(&p_ctx->mutex);
18027  ni_instance_buf_info_t buf_info = {0};
18028  int32_t query_retry = 0;
18029  for (;;)
18030  {
18031  if (p_ctx->session_statistic.ui32WrBufAvailSize > 0)
18032  {
18033  buf_info.buf_avail_size =
18035  ni_log2(p_ctx, NI_LOG_DEBUG,
18036  "Info ai write query success, available buf "
18037  "size %u !\n",
18038  buf_info.buf_avail_size);
18039  break;
18040  }
18041 
18043  &p_ctx->session_statistic);
18044  CHECK_ERR_RC(p_ctx, retval, &p_ctx->session_statistic,
18046  p_ctx->hw_id, &(p_ctx->session_id), OPT_2);
18047  buf_info.buf_avail_size =
18049  if (NI_RETCODE_SUCCESS != retval ||
18050  buf_info.buf_avail_size == 0)
18051  {
18052  ni_log2(p_ctx, NI_LOG_TRACE,
18053  "AI write query failed or buf_size < frame_size. Retry %d\n",
18054  query_retry);
18055  // extend to 5000 retries for 8K encode on FPGA
18056  if (query_retry >= NI_MAX_ENCODER_QUERY_RETRIES)
18057  {
18058  ni_log2(p_ctx, NI_LOG_TRACE,
18059  "AI write query exceeded max retries: %d\n",
18062  retval = NI_RETCODE_SUCCESS;
18063  LRETURN;
18064  }
18065  ni_pthread_mutex_unlock(&p_ctx->mutex);
18067  ni_pthread_mutex_lock(&p_ctx->mutex);
18068  query_retry++;
18069  }
18070  }
18071 
18072  dataLen = (sizeof(ni_network_buffer_info_t) + NI_MEM_PAGE_ALIGNMENT - 1) &
18073  ~(NI_MEM_PAGE_ALIGNMENT - 1);
18074 
18075  if (ni_posix_memalign((void **)&p_data, sysconf(_SC_PAGESIZE), dataLen))
18076  {
18077  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
18078  NI_ERRNO, __func__);
18080  }
18081 
18082  memset(p_data, 0x00, dataLen);
18083  p_data->ui16Width = p_cfg_in[0].picture_width;
18084  p_data->ui16Height = p_cfg_in[0].picture_height;
18085  p_data->ui16Option = p_cfg_in[0].options;
18086  p_data->ui8PoolSize = p_cfg_in[0].rgba_color;
18087  p_data->ui8MultiIn = 1;
18088  for(int i = 0; i < numInCfgs; i++){
18089  p_data->ui16FrameIdx[i] = p_cfg_in[i].frame_index;
18090  ni_log2(p_ctx, NI_LOG_DEBUG, "Dev alloc frame[%d]: frame_index %u, hw=%d\n",
18091  i,p_data->ui16FrameIdx[i], p_data->ui8MultiIn);
18092  }
18093 
18094 
18095  ui32LBA =
18097 
18098  retval =
18100  p_data, dataLen, ui32LBA);
18101  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
18102  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id),
18103  OPT_1);
18104  if (NI_RETCODE_SUCCESS != retval)
18105  {
18106  ni_log2(p_ctx,
18107  NI_LOG_ERROR,
18108  "ERROR: ni_nvme_send_admin_cmd failed: blk_io_handle: %" PRIx64
18109  ", hw_id, %u, xcoder_inst_id: %d\n",
18110  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
18111  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed!\n",
18112  __func__);
18114  LRETURN;
18115  }
18116 
18117  END:
18118  ni_pthread_mutex_unlock(&p_ctx->mutex);
18119  ni_aligned_free(p_data);
18120  if (p_read_data != NULL)
18121  {
18122  ni_aligned_free(p_read_data);
18123  }
18124  return retval;
18125 }
18126 
18128  niFrameSurface1_t *p_out_surface)
18129 {
18131  ni_network_buffer_info_t *p_data = NULL;
18132  void *p_read_data = NULL;
18133  uint32_t dataLen;
18134  uint32_t ui32LBA = 0;
18135 
18136  if (!p_ctx)
18137  {
18138  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
18139  __func__);
18140  return NI_RETCODE_INVALID_PARAM;
18141  }
18142 
18143  if (p_ctx->session_id == NI_INVALID_SESSION_ID)
18144  {
18145  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
18146  __func__);
18148  }
18149 
18150  if (ni_cmp_fw_api_ver(
18152  "6rL") < 0)
18153  {
18154  ni_log2(p_ctx, NI_LOG_ERROR,
18155  "Error: %s function not supported on device with FW API version < 6rL\n",
18156  __func__);
18158  }
18159 
18160  ni_pthread_mutex_lock(&p_ctx->mutex);
18161 
18162  int query_retry = 0;
18163  dataLen =
18164  (sizeof(ni_instance_buf_info_t) + (NI_MEM_PAGE_ALIGNMENT - 1)) &
18165  ~(NI_MEM_PAGE_ALIGNMENT - 1);
18166  if (ni_posix_memalign(&p_read_data, sysconf(_SC_PAGESIZE), dataLen))
18167  {
18168  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
18169  NI_ERRNO, __func__);
18170  retval = NI_RETCODE_ERROR_MEM_ALOC;
18171  LRETURN;
18172  }
18173  memset(p_read_data, 0, dataLen);
18174 
18175  for (;;)
18176  {
18177  ui32LBA = QUERY_INSTANCE_HW_OUT_SIZE_R(p_ctx->session_id,
18179  retval =
18181  p_read_data, dataLen, ui32LBA);
18182  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_read,
18183  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id),
18184  OPT_1);
18185  if (retval != NI_RETCODE_SUCCESS)
18186  {
18187  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
18189  LRETURN;
18190  }
18191 
18192  if (((ni_instance_buf_info_t *)p_read_data)->hw_inst_ind.buffer_avail > 0)
18193  {
18194  p_out_surface->ui16FrameIdx = ((ni_instance_buf_info_t *)p_read_data)->hw_inst_ind.frame_index;
18195  p_out_surface->ui16session_ID = p_ctx->session_id;
18196  p_out_surface->device_handle =
18197  (int32_t)((int64_t)p_ctx->blk_io_handle & 0xFFFFFFFF);
18198  p_out_surface->bit_depth = p_ctx->bit_depth_factor;
18199  p_out_surface->src_cpu = (uint8_t)NI_DEVICE_TYPE_AI;
18200  p_out_surface->output_idx = 0;
18201  break;
18202  }
18203 
18204  query_retry++;
18205  if (query_retry > 2000)
18206  {
18207  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): exceeded max query retries: %d\n",
18208  __func__, query_retry - 1);
18209  retval = NI_RETCODE_EAGAIN;
18210  LRETURN;
18211  }
18212  ni_pthread_mutex_unlock(&p_ctx->mutex);
18213  ni_usleep(200);
18214  ni_pthread_mutex_lock(&p_ctx->mutex);
18215  continue;
18216  }
18217 
18218  if (((ni_instance_buf_info_t *)p_read_data)->hw_inst_ind.buffer_avail == 0)
18219  {
18221  LRETURN;
18222  }
18223 END:
18224  ni_pthread_mutex_unlock(&p_ctx->mutex);
18225  ni_aligned_free(p_data);
18226  if (p_read_data != NULL)
18227  {
18228  ni_aligned_free(p_read_data);
18229  }
18230  return retval;
18231 }
18232 
18234  int height, int options, int pool_size,
18235  int frame_index)
18236 {
18238  ni_network_buffer_info_t *p_data = NULL;
18239  void *p_read_data = NULL;
18240  uint32_t dataLen;
18241  uint32_t ui32LBA = 0;
18242 
18243  if (!p_ctx)
18244  {
18245  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
18246  __func__);
18247  return NI_RETCODE_INVALID_PARAM;
18248  }
18249 
18250  if (p_ctx->session_id == NI_INVALID_SESSION_ID)
18251  {
18252  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
18253  __func__);
18255  }
18256 
18257  ni_pthread_mutex_lock(&p_ctx->mutex);
18258 
18259  if (options == NI_AI_FLAG_IO)
18260  {
18261  int query_retry = 0;
18262  dataLen =
18263  (sizeof(ni_instance_buf_info_t) + (NI_MEM_PAGE_ALIGNMENT - 1)) &
18264  ~(NI_MEM_PAGE_ALIGNMENT - 1);
18265  if (ni_posix_memalign(&p_read_data, sysconf(_SC_PAGESIZE), dataLen))
18266  {
18267  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
18268  NI_ERRNO, __func__);
18269  retval = NI_RETCODE_ERROR_MEM_ALOC;
18270  LRETURN;
18271  }
18272  memset(p_read_data, 0, dataLen);
18273 
18274  for (;;)
18275  {
18276  ui32LBA = QUERY_INSTANCE_HW_OUT_SIZE_R(p_ctx->session_id,
18278  retval =
18280  p_read_data, dataLen, ui32LBA);
18281  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_read,
18282  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id),
18283  OPT_1);
18284  if (retval != NI_RETCODE_SUCCESS)
18285  {
18286  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
18288  LRETURN;
18289  }
18290 
18291  if (((ni_instance_buf_info_t *)p_read_data)->buf_avail_size > 0)
18292  {
18293  break;
18294  }
18295 
18296  query_retry++;
18297  if (query_retry > 2000)
18298  {
18299  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): exceeded max query retries: %d\n",
18300  __func__, query_retry - 1);
18301  retval = NI_RETCODE_EAGAIN;
18302  LRETURN;
18303  }
18304  ni_pthread_mutex_unlock(&p_ctx->mutex);
18305  ni_usleep(200);
18306  ni_pthread_mutex_lock(&p_ctx->mutex);
18307  continue;
18308  }
18309 
18310  if (((ni_instance_buf_info_t *)p_read_data)->buf_avail_size == 0)
18311  {
18313  LRETURN;
18314  }
18315  } else
18316  {
18317  ni_instance_buf_info_t buf_info = {0};
18318  int32_t query_retry = 0;
18319  for (;;)
18320  {
18321  if (p_ctx->session_statistic.ui32WrBufAvailSize > 0)
18322  {
18323  buf_info.buf_avail_size =
18325  ni_log2(p_ctx, NI_LOG_DEBUG,
18326  "Info ai write query success, available buf "
18327  "size %u !\n",
18328  buf_info.buf_avail_size);
18329  break;
18330  }
18331 
18333  &p_ctx->session_statistic);
18334  CHECK_ERR_RC(p_ctx, retval, &p_ctx->session_statistic,
18336  p_ctx->hw_id, &(p_ctx->session_id), OPT_2);
18337  buf_info.buf_avail_size =
18339  if (NI_RETCODE_SUCCESS != retval ||
18340  buf_info.buf_avail_size == 0)
18341  {
18342  ni_log2(p_ctx, NI_LOG_TRACE,
18343  "AI write query failed or buf_size < frame_size. Retry %d\n",
18344  query_retry);
18345  // extend to 5000 retries for 8K encode on FPGA
18346  if (query_retry >= NI_MAX_ENCODER_QUERY_RETRIES)
18347  {
18348  ni_log2(p_ctx, NI_LOG_TRACE,
18349  "AI write query exceeded max retries: %d\n",
18352  retval = NI_RETCODE_SUCCESS;
18353  LRETURN;
18354  }
18355  ni_pthread_mutex_unlock(&p_ctx->mutex);
18357  ni_pthread_mutex_lock(&p_ctx->mutex);
18358  query_retry++;
18359  }
18360  }
18361 
18362  dataLen = (sizeof(ni_network_buffer_info_t) + NI_MEM_PAGE_ALIGNMENT - 1) &
18363  ~(NI_MEM_PAGE_ALIGNMENT - 1);
18364 
18365  if (ni_posix_memalign((void **)&p_data, sysconf(_SC_PAGESIZE), dataLen))
18366  {
18367  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
18368  NI_ERRNO, __func__);
18370  }
18371 
18372  memset(p_data, 0x00, dataLen);
18373  p_data->ui16FrameIdx[0] = frame_index;
18374  p_data->ui16Width = width;
18375  p_data->ui16Height = height;
18376  p_data->ui16Option = options;
18377  p_data->ui8PoolSize = pool_size;
18378  p_data->ui8MultiIn = 0;
18379  ni_log2(p_ctx, NI_LOG_DEBUG, "Dev alloc frame: frame_index %u\n",
18380  p_data->ui16FrameIdx[0]);
18381 
18382  ui32LBA =
18384 
18385  retval =
18387  p_data, dataLen, ui32LBA);
18388  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
18389  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id),
18390  OPT_1);
18391  if (NI_RETCODE_SUCCESS != retval)
18392  {
18393  ni_log2(p_ctx,
18394  NI_LOG_ERROR,
18395  "ERROR: ni_nvme_send_admin_cmd failed: blk_io_handle: %" PRIx64
18396  ", hw_id, %u, xcoder_inst_id: %d\n",
18397  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
18398  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed!\n",
18399  __func__);
18401  LRETURN;
18402  }
18403  }
18404 
18405 END:
18406  ni_pthread_mutex_unlock(&p_ctx->mutex);
18407  ni_aligned_free(p_data);
18408  if (p_read_data != NULL)
18409  {
18410  ni_aligned_free(p_read_data);
18411  }
18412  return retval;
18413 }
18414 
18415 /*!******************************************************************************
18416  * \brief read a hardware descriptor from a scaler session
18417  *
18418  * \param[in] p_ctx pointer to session context
18419  * \param[out] p_frame pointer to frame to write hw descriptor
18420  *
18421  * \return NI_RETCODE_INVALID_PARAM
18422  * NI_RETCODE_ERROR_INVALID_SESSION
18423  * NI_RETCODE_ERROR_MEM_ALOC
18424  * NI_RETCODE_ERROR_NVME_CMD_FAILED
18425  * NI_RETCODE_FAILURE
18426  *******************************************************************************/
18428  ni_frame_t *p_frame)
18429 {
18430  int retval = NI_RETCODE_SUCCESS;
18431  int retry_count = 0;
18432 
18433  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
18434 
18435  ni_pthread_mutex_lock(&p_ctx->mutex);
18436 
18437  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
18438  {
18439  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
18440  __func__);
18442  LRETURN;
18443  }
18444 
18445  for (;;)
18446  {
18447  retry_count++;
18448  ni_instance_buf_info_t sInstanceBuf = {0};
18449  niFrameSurface1_t *pFrameSurface;
18451  NI_DEVICE_TYPE_AI, &sInstanceBuf);
18452  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
18453  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id),
18454  OPT_3);
18455 
18457  {
18458  if (retry_count >= 500)
18459  {
18460  ni_log2(p_ctx, NI_LOG_DEBUG, "Warning hwdesc read fail rc %d\n",
18461  retval);
18462  LRETURN;
18463  }
18464  ni_pthread_mutex_unlock(&p_ctx->mutex);
18465  ni_usleep(100);
18466  ni_pthread_mutex_lock(&p_ctx->mutex);
18467  }
18468  else if (retval != NI_RETCODE_SUCCESS)
18469  {
18470  LRETURN;
18471  } else
18472  {
18473  pFrameSurface = (niFrameSurface1_t *)p_frame->p_data[3];
18474  pFrameSurface->ui16FrameIdx = sInstanceBuf.hw_inst_ind.frame_index;
18475  pFrameSurface->ui16session_ID = p_ctx->session_id;
18476  pFrameSurface->device_handle =
18477  (int32_t)((int64_t)p_ctx->blk_io_handle & 0xFFFFFFFF);
18478  pFrameSurface->src_cpu = (uint8_t)NI_DEVICE_TYPE_AI;
18479  pFrameSurface->output_idx = 0;
18480 
18481  /* A frame index of zero is invalid, the memory acquisition failed */
18482  if (pFrameSurface->ui16FrameIdx == 0)
18483  {
18484  if (retry_count >= 500)
18485  {
18486  ni_log2(p_ctx, NI_LOG_DEBUG, "Warning hwdesc read fail rc %d\n",
18487  retval);
18488  retval = NI_RETCODE_EAGAIN;
18489  LRETURN;
18490  }
18491  ni_pthread_mutex_unlock(&p_ctx->mutex);
18492  ni_usleep(100);
18493  ni_pthread_mutex_lock(&p_ctx->mutex);
18494  continue;
18495  }
18496  LRETURN;
18497  }
18498  }
18499 
18500 END:
18501 
18502  ni_pthread_mutex_unlock(&p_ctx->mutex);
18503 
18504  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
18505 
18506  return retval;
18507 }
18508 
18509 /*!*****************************************************************************
18510  * \brief Get DDR configuration of Quadra device
18511  *
18512  * \param[in/out] p_ctx pointer to a session context with valid file handle
18513  *
18514  * \return On success NI_RETCODE_SUCCESS
18515  * On failure NI_RETCODE_INVALID_PARAM
18516  * NI_RETCODE_ERROR_MEM_ALOC
18517  * NI_RETCODE_ERROR_NVME_CMD_FAILED
18518  ******************************************************************************/
18520 {
18521  void *p_buffer = NULL;
18522  ni_nvme_identity_t *p_id_data;
18524  ni_event_handle_t event_handle = NI_INVALID_EVENT_HANDLE;
18525  uint32_t ui32LBA = IDENTIFY_DEVICE_R;
18526  ni_device_handle_t device_handle = p_ctx->blk_io_handle;
18527 
18528  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
18529 
18530  if (NI_INVALID_DEVICE_HANDLE == device_handle)
18531  {
18532  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s(): invalid passed parameters\n",
18533  __func__);
18534  retval = NI_RETCODE_INVALID_PARAM;
18535  LRETURN;
18536  }
18537 
18538  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE),
18540  {
18541  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer.\n",
18542  NI_ERRNO, __func__);
18543  retval = NI_RETCODE_ERROR_MEM_ALOC;
18544  LRETURN;
18545  }
18546 
18547  memset(p_buffer, 0, NI_NVME_IDENTITY_CMD_DATA_SZ);
18548 
18549  if (ni_nvme_send_read_cmd(device_handle, event_handle, p_buffer,
18550  NI_NVME_IDENTITY_CMD_DATA_SZ, ui32LBA) < 0)
18551  {
18553  LRETURN;
18554  }
18555 
18556  p_id_data = (ni_nvme_identity_t *) p_buffer;
18557 
18558  if (p_id_data->memory_cfg == NI_QUADRA_MEMORY_CONFIG_SR_4G)
18559  {
18560  p_ctx->ddr_config = 6;
18561  }
18562  else
18563  {
18564  if (ni_cmp_fw_api_ver(
18566  "6rJ") >= 0)
18567  {
18568  p_ctx->ddr_config = (p_id_data->memory_cfg == NI_QUADRA_MEMORY_CONFIG_SR)
18569  ? 3 : ((p_id_data->memory_cfg == NI_QUADRA_MEMORY_CONFIG_DR)? 4 : 5);
18570  }
18571  else if (ni_cmp_fw_api_ver(
18573  "6rD") >= 0)
18574  {
18575  p_ctx->ddr_config = (p_id_data->memory_cfg == NI_QUADRA_MEMORY_CONFIG_SR)
18576  ? 3 : 4;
18577  } else
18578  {
18579  p_ctx->ddr_config = (p_id_data->memory_cfg == NI_QUADRA_MEMORY_CONFIG_SR)
18580  ? 1 : 2;
18581  }
18582  }
18583 
18584  ni_log2(p_ctx, NI_LOG_DEBUG, "Memory configuration %d\n",p_ctx->ddr_config);
18585 END:
18586 
18587  ni_aligned_free(p_buffer);
18588  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): retval: %d\n", __func__, retval);
18589 
18590  return retval;
18591 }
18592 
18593 /*!*****************************************************************************
18594  * \brief Set DDR configuration of Quadra device
18595  *
18596  * \param[in] p_ctx pointer to a session context with valid file handle
18597  * \param[in] ddr_priority_mode ddr priority mode
18598  *
18599  * \return On success NI_RETCODE_SUCCESS
18600  * On failure NI_RETCODE_INVALID_PARAM
18601  * NI_RETCODE_ERROR_MEM_ALOC
18602  * NI_RETCODE_ERROR_NVME_CMD_FAILED
18603  ******************************************************************************/
18605  uint8_t ddr_priority_mode)
18606 {
18607  void *p_buffer = NULL;
18609  ni_ddr_priority_config_t *p_cfg = NULL;
18610  uint32_t ui32LBA = CONFIG_SESSION_DDR_PRIORITY_W(p_ctx->session_id);
18611  ni_device_handle_t device_handle = p_ctx->blk_io_handle;
18612 
18613  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
18614 
18615  ni_log2(p_ctx, NI_LOG_INFO, "set ddr priority %d\n",
18616  ddr_priority_mode);
18617 
18618  if (NI_INVALID_DEVICE_HANDLE == device_handle)
18619  {
18620  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s(): invalid passed parameters\n",
18621  __func__);
18622  retval = NI_RETCODE_INVALID_PARAM;
18623  LRETURN;
18624  }
18625 
18626  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE),
18628  {
18629  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer.\n",
18630  NI_ERRNO, __func__);
18631  retval = NI_RETCODE_ERROR_MEM_ALOC;
18632  LRETURN;
18633  }
18634 
18635  memset(p_buffer, 0, NI_NVME_IDENTITY_CMD_DATA_SZ);
18636  p_cfg = (ni_ddr_priority_config_t *)p_buffer;
18637  p_cfg->ddr_mode = ddr_priority_mode;
18638  if (ni_nvme_send_write_cmd(p_ctx->blk_io_handle, NI_INVALID_EVENT_HANDLE,
18639  p_buffer,NI_DATA_BUFFER_LEN, ui32LBA) < 0)
18640  {
18641  ni_log2(p_ctx, NI_LOG_ERROR, "DDR priority setting failed with mode %d\n",
18642  ddr_priority_mode);
18644  LRETURN;
18645  }
18646 END:
18647  ni_aligned_free(p_buffer);
18648  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): retval: %d\n", __func__, retval);
18649 
18650  return retval;
18651 }
18652 
18653 /*!*****************************************************************************
18654  * \brief Allocate memory for the metadata header and auxillary data for
18655  * encoder input data.
18656  *
18657  * \param[in] p_frame Pointer to a caller allocated ni_frame_t struct
18658  *
18659  * \param[in] extra_len Length header and auxillary data
18660  *
18661  * \return On success
18662  * NI_RETCODE_SUCCESS
18663  * On failure
18664  * NI_RETCODE_INVALID_PARAM
18665  * NI_RETCODE_ERROR_MEM_ALOC
18666  *****************************************************************************/
18668  int extra_len)
18669 {
18670  void *metadata_buffer = NULL;
18671  int retval = NI_RETCODE_SUCCESS;
18672 
18673  if ((!p_frame) || (extra_len <= 0))
18674  {
18676  "ERROR: %s passed parameters are null or not supported, "
18677  "p_frame %p, extra_len %d",
18678  __func__, p_frame, extra_len);
18679  return NI_RETCODE_INVALID_PARAM;
18680  }
18681 
18682  int buffer_size = extra_len;
18683  if (buffer_size % NI_MEM_PAGE_ALIGNMENT)
18684  {
18685  buffer_size = ((buffer_size / NI_MEM_PAGE_ALIGNMENT) + 1) *
18687  }
18688 
18689  // Check if previous metadata buffer needs to be freed
18690  if ((p_frame->metadata_buffer_size != buffer_size) &&
18691  (p_frame->metadata_buffer_size > 0))
18692  {
18694  "%s: free current p_frame metadata buffer, "
18695  "p_frame->buffer_size=%u\n",
18696  __func__, p_frame->metadata_buffer_size);
18697  p_frame->metadata_buffer_size = 0;
18699  }
18700 
18701  // Check if new metadata buffer needs to be allocated
18702  if (p_frame->metadata_buffer_size != buffer_size)
18703  {
18704  if (ni_posix_memalign(&metadata_buffer, sysconf(_SC_PAGESIZE),
18705  buffer_size))
18706  {
18708  "ERROR %d: %s() Cannot allocate metadata buffer.\n",
18709  NI_ERRNO, __func__);
18710  retval = NI_RETCODE_ERROR_MEM_ALOC;
18711  LRETURN;
18712  }
18713 
18714  // init once after allocation
18715  memset(metadata_buffer, 0, buffer_size);
18716  p_frame->metadata_buffer_size = buffer_size;
18717  p_frame->p_metadata_buffer = metadata_buffer;
18718 
18719  ni_log(NI_LOG_DEBUG, "%s: allocated new metadata buffer\n", __func__);
18720  } else
18721  {
18722  ni_log(NI_LOG_DEBUG, "%s: reuse metadata buffer\n", __func__);
18723  }
18724 
18726  "%s: success: p_frame->p_metadata_buffer %p "
18727  "p_frame->metadata_buffer_size=%u\n",
18728  __func__, p_frame->p_metadata_buffer, p_frame->metadata_buffer_size);
18729 
18730 END:
18731 
18732  if (NI_RETCODE_SUCCESS != retval)
18733  {
18734  ni_aligned_free(metadata_buffer);
18735  }
18736 
18737  return retval;
18738 }
18739 
18740 /*!*****************************************************************************
18741  * \brief Allocate memory for the non-4k-aligned part at the start of YUV data for
18742  * encoder input data.
18743  *
18744  * \param[in] p_frame Pointer to a caller allocated ni_frame_t struct
18745  *
18746  * \param[in] start_len Length of non-4k-aligned part at the start of YUV data
18747  *
18748  * \return On success
18749  * NI_RETCODE_SUCCESS
18750  * On failure
18751  * NI_RETCODE_INVALID_PARAM
18752  * NI_RETCODE_ERROR_MEM_ALOC
18753  *****************************************************************************/
18755 {
18756  void *start_buffer = NULL;
18757  int retval = NI_RETCODE_SUCCESS;
18758 
18759  if (!p_frame)
18760  {
18762  "ERROR: %s passed parameters are null or not supported, "
18763  "p_frame %p\n",
18764  __func__, p_frame);
18765  return NI_RETCODE_INVALID_PARAM;
18766  }
18767 
18768  // Check if new start buffer needs to be allocated
18769  if (!p_frame->start_buffer_size)
18770  {
18771  if (ni_posix_memalign(&start_buffer, sysconf(_SC_PAGESIZE),
18773  {
18775  "ERROR %d: %s() Cannot allocate start buffer.\n",
18776  NI_ERRNO, __func__);
18777  retval = NI_RETCODE_ERROR_MEM_ALOC;
18778  LRETURN;
18779  }
18780 
18781  // init once after allocation
18784  p_frame->p_start_buffer = start_buffer;
18785 
18786  ni_log(NI_LOG_DEBUG, "%s: allocated new start buffer\n", __func__);
18787  } else
18788  {
18789  ni_log(NI_LOG_DEBUG, "%s: reuse start buffer\n", __func__);
18790  }
18791 
18793  "%s: success: p_frame->p_start_buffer %p "
18794  "p_frame->start_buffer_size=%u\n",
18795  __func__, p_frame->p_start_buffer, p_frame->start_buffer_size);
18796 
18797 END:
18798 
18799  if (NI_RETCODE_SUCCESS != retval)
18800  {
18801  ni_aligned_free(start_buffer);
18802  }
18803 
18804  return retval;
18805 }
18806 
18808  ni_network_perf_metrics_t *p_metrics)
18809 {
18811  void *p_buffer = NULL;
18812  uint32_t dataLen;
18813  uint32_t ui32LBA = 0;
18814 
18815  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
18816  if (!p_ctx || !p_metrics)
18817  {
18818  ni_log2(p_ctx, NI_LOG_ERROR,
18819  "ERROR: %s() passed parameters are null!, return\n", __func__);
18820  retval = NI_RETCODE_INVALID_PARAM;
18821  LRETURN;
18822  }
18823 
18825  "6N") >= 0)
18826  {
18827  dataLen =
18829  ~(NI_MEM_PAGE_ALIGNMENT - 1);
18830  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), dataLen))
18831  {
18832  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
18833  NI_ERRNO, __func__);
18834  retval = NI_RETCODE_ERROR_MEM_ALOC;
18835  LRETURN;
18836  }
18837  memset(p_buffer, 0, sizeof(ni_network_perf_metrics_t));
18838 
18839  ui32LBA =
18841  retval =
18843  p_buffer, dataLen, ui32LBA);
18844  if ((int32_t)retval != NI_RETCODE_SUCCESS)
18845  {
18846  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
18848  LRETURN;
18849  }
18850  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_read, p_ctx->device_type,
18851  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
18852 
18853  *p_metrics = *((ni_network_perf_metrics_t *)p_buffer);
18854  } else
18855  {
18856  p_metrics->total_cycles = (uint32_t)(-1);
18857  p_metrics->total_idle_cycles = (uint32_t)(-1);
18858  }
18859 END:
18860 
18861  ni_aligned_free(p_buffer);
18862  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
18863 
18864  return retval;
18865 }
18866 
18867 /*!*****************************************************************************
18868  * \brief Send namespace num / Opmode and SRIOv index/value to the device with
18869  * specified logic block address.
18870  *
18871  * \param[in] device_handle Device handle obtained by calling ni_device_open
18872  * \param[in] Key Represents either namespace num or opmode
18873  * \param[in] Value Represents either SRIOv index or opmode value
18874  *
18875  * \return On success
18876  * NI_RETCODE_SUCCESS
18877  * On failure
18878  * NI_RETCODE_ERROR_MEM_ALOC
18879  * NI_RETCODE_ERROR_NVME_CMD_FAILED
18880  ******************************************************************************/
18881 ni_retcode_t ni_device_config_ns_qos(ni_device_handle_t device_handle,
18882  uint32_t key,
18883  uint32_t value)
18884 {
18885  char buf[NI_DATA_BUFFER_LEN] = {'\0'};
18886  uint32_t *u32_buf = (uint32_t *)buf;
18887  u32_buf[0] = key;
18888  u32_buf[1] = value;
18889  // event handle could be ignored
18890  return ni_nvme_send_write_cmd(device_handle, NI_INVALID_EVENT_HANDLE,
18891  (void *)buf, NI_DATA_BUFFER_LEN,
18893 }
18894 
18896 {
18897  char *CoreName;
18898  switch (eCoreType)
18899  {
18900  case ALL_CORE:
18901  CoreName = (char *)"all";
18902  break;
18903  case NVME_CORE:
18904  CoreName = (char *)"np";
18905  break;
18906  case EP_CORE:
18907  CoreName = (char *)"ep";
18908  break;
18909  case DP_CORE:
18910  CoreName = (char *)"dp";
18911  break;
18912  case TP_CORE:
18913  CoreName = (char *)"tp";
18914  break;
18915  case FP_CORE:
18916  CoreName = (char *)"fp";
18917  break;
18918  default:
18919  CoreName = (char *)"Not Found";
18920  break;
18921  }
18922  return CoreName;
18923 }
18924 
18925 uint32_t ni_get_log_lba(ni_core_type_t eCoreType)
18926 {
18927  uint32_t lba;
18928  switch (eCoreType)
18929  {
18930  case NVME_CORE:
18931  lba = NVME_LOG_OFFSET_IN_4K;
18932  break;
18933  case EP_CORE:
18934  lba = EP_LOG_OFFSET_IN_4K;
18935  break;
18936  case DP_CORE:
18937  lba = DP_LOG_OFFSET_IN_4K;
18938  break;
18939  case TP_CORE:
18940  lba = TP_LOG_OFFSET_IN_4K;
18941  break;
18942  case FP_CORE:
18943  lba = FP_LOG_OFFSET_IN_4K;
18944  break;
18945  default:
18946  ni_log(NI_LOG_ERROR, "%s:() Invalid core ID:%u\n", __func__, eCoreType);
18947  lba = 0;
18948  break;
18949  }
18950 
18951  return lba;
18952 }
18953 
18954 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)
18955 {
18956  int32_t rc;
18957  uint32_t lba = 0;
18958  uint32_t data_len = CPU_LOG_BUFFER_SIZE;
18959  char *core_name = NULL;
18960  FILE *p_file = NULL;
18962 
18963  if (!p_ctx || !p_data)
18964  {
18965  ni_log2(p_ctx, NI_LOG_ERROR, "%s:():%d: ERROR invalid pointer p_ctx %p p_data %p\n",
18966  __func__, __LINE__, p_ctx, p_data);
18967  return NI_RETCODE_INVALID_PARAM;
18968  }
18969 
18970  memset(p_data, 0, CPU_LOG_BUFFER_SIZE);
18971  *(uint8_t *)p_data = 0x55;
18972 
18973  lba = ni_get_log_lba((ni_core_type_t)core_id);
18974  if (lba == 0)
18975  {
18976  ni_log2(p_ctx, NI_LOG_ERROR, "%s:():%d: ERROR core_id %u\n",
18977  __func__, __LINE__, core_id);
18978  return NI_RETCODE_INVALID_PARAM;
18979  }
18980 
18981  core_name = ni_get_core_name((ni_core_type_t)core_id);
18982 
18983  rc = ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_data, data_len, lba);
18984 
18985  if (rc != NI_RETCODE_SUCCESS)
18986  {
18987  ni_log2(p_ctx, NI_LOG_ERROR, "%s:():%d: ERROR %d: nvme read %s core failed\n",
18988  __func__, __LINE__, rc, core_name);
18989  }
18990  else if (gen_log_file)
18991  {
18992  //generate log file e.g. raw_dp_slot_0_0000.bin
18993  char filename[32] = "raw_";
18994  ni_strcat(filename, 32, core_name);
18995  ni_strcat(filename, 32, "_slot_");
18996 #ifdef __linux__
18997  bool pcie_id_name = false;
18998  char devFilePath[1024] = {0};
18999  char devFDPath[1024] = {0};
19000  char pcie[64] = {0};
19001  char domain[5] = {0}, slot[3] = {0}, dev[3] = {0}, func[2] = {0};
19002  //p_ctx->blk_dev_name might be empty so look up the file name
19003  snprintf(devFDPath, sizeof(devFDPath), "/proc/self/fd/%d", p_ctx->blk_io_handle);
19004  ssize_t len = readlink(devFDPath, devFilePath, sizeof(devFilePath)-1);
19005  if (len != -1) {
19006  devFilePath[len] = '\0';
19007  }
19008  if (strstr(devFilePath, "/dev/nvme") != NULL)
19009  {
19010  get_dev_pcie_addr(devFilePath, pcie, domain, slot, dev, func);
19011  if (strlen(pcie) > 0 && strlen(slot) > 0 && strlen(domain) > 0)
19012  {
19013  ni_strcat(filename, 32, slot);
19014  ni_strcat(filename, 32, "_");
19015  ni_strcat(filename, 32, domain);
19016  pcie_id_name = true;
19017  }
19018  }
19019  if (!pcie_id_name)
19020  {
19021  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",
19022  __func__, __LINE__, p_ctx->blk_io_handle);
19023  char num[4] = {0};
19024  snprintf(num, 4, "%d", p_ctx->hw_id);
19025  ni_strcat(filename, 32, num);
19026  ni_strcat(filename, 32, "_0000");
19027  }
19028  ni_strcat(filename, 32, ".bin");
19029  ni_log2(p_ctx, NI_LOG_INFO, "For dev %d %s core %s creating file %s\n",
19030  p_ctx->blk_io_handle, devFilePath, core_name, filename);
19031 #else
19032  char num[4] = {0};
19033  snprintf(num, 4, "%02x", p_ctx->hw_id);
19034  ni_strcat(filename, 32, num);
19035  ni_strcat(filename, 32, "_0000");
19036  ni_strcat(filename, 32, ".bin");
19037  ni_log2(p_ctx, NI_LOG_INFO, "For dev %d core %s creating file %s\n",
19038  p_ctx->blk_io_handle, core_name, filename);
19039 #endif
19040  ni_fopen(&p_file, filename, "wb");
19041  if (p_file)
19042  {
19043  /* Write out the stream header */
19044  if (fwrite((uint8_t *)p_data ,
19045  data_len, 1, p_file) != 1)
19046  {
19047  ni_log2(p_ctx, NI_LOG_ERROR, "%s:():%d: Error: writing data %u bytes error!\n",
19048  __func__, __LINE__, data_len);
19049  ni_log2(p_ctx, NI_LOG_ERROR, "Error: ferror rc = %d\n", ferror(p_file));
19050  retval = NI_RETCODE_FAILURE;
19051  }
19052  if (fflush(p_file))
19053  {
19054  ni_log2(p_ctx, NI_LOG_ERROR, "Error: writing data frame flush failed! errno %d\n",
19055  ferror(p_file));
19056  retval = NI_RETCODE_FAILURE;
19057  }
19058  fclose(p_file);
19059  }
19060  else
19061  {
19062  ni_log2(p_ctx, NI_LOG_ERROR, "%s:():%d: Error: cannot open %s\n",
19063  __func__, __LINE__, filename);
19064  retval = NI_RETCODE_FAILURE;
19065  }
19066  }
19067 
19068  return retval;
19069 }
19070 
19071 ni_retcode_t ni_dump_log_all_cores(ni_session_context_t *p_ctx, void* p_data, bool gen_log_file)
19072 {
19073  int i = 1;
19074 
19075  if (!p_ctx || !p_data)
19076  {
19077  ni_log2(p_ctx, NI_LOG_ERROR, "%s:():%d: ERROR invalid pointer p_ctx %p p_data %p\n",
19078  __func__, __LINE__, p_ctx, p_data);
19079  return NI_RETCODE_INVALID_PARAM;
19080  }
19081 
19082  for (i = NVME_CORE; i< NUM_OF_CORES; i++)
19083  {
19084  ni_dump_log_single_core(p_ctx, p_data, i, gen_log_file);
19085  p_data = (void*)((uint8_t *)p_data + CPU_LOG_BUFFER_SIZE);
19086  }
19087 
19088  return NI_RETCODE_SUCCESS;
19089 }
19090 
19092  niFrameSurface1_t *source,
19093  uint64_t ui64DestAddr,
19094  uint32_t ui32FrameSize)
19095 {
19096  void *p_buffer = NULL;
19097  ni_retcode_t retval;
19098  uint32_t ui32LBA;
19099  uint8_t *p_data;
19100  uint16_t ui16Direction = NI_P2P_SEND;
19101 
19102  /* allocate memory aligned buffer */
19103  retval = ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), NI_DATA_BUFFER_LEN);
19104  if (retval != 0)
19105  {
19106  retval = NI_RETCODE_ERROR_MEM_ALOC;
19107  LRETURN;
19108  }
19109 
19110  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
19111 
19112  p_data = (uint8_t *) p_buffer;
19113 
19114  /* Add payload */
19115  memcpy(p_data, &ui64DestAddr, sizeof(uint64_t));
19116  memcpy(p_data + 8, &ui32FrameSize, sizeof(uint32_t));
19117  memcpy(p_data + 12, &source->ui16FrameIdx, sizeof(uint16_t));
19118  memcpy(p_data + 14, MAGIC_P2P_VALUE, 4);
19119  memcpy(p_data + 18, &ui16Direction, sizeof(uint16_t));
19120 
19122 
19123  retval = ni_nvme_send_write_cmd((ni_device_handle_t)(int64_t)source->device_handle,
19124  NI_INVALID_DEVICE_HANDLE, p_buffer,
19125  NI_DATA_BUFFER_LEN, ui32LBA);
19126 
19127  if (retval < 0)
19128  {
19129  ni_log2(p_ctx, NI_LOG_ERROR, "%s: NVME command failed %d\n", __func__, retval);
19130  ni_aligned_free(p_buffer);
19132  LRETURN;
19133  }
19134 
19135 END:
19136  if (p_buffer != NULL)
19137  ni_aligned_free(p_buffer);
19138  return retval;
19139 }
19140 
19142  ni_session_context_t *p_ctx,
19143  const ni_p2p_sgl_t *dmaAddrs,
19144  ni_frame_t *pDstFrame)
19145 {
19146  void *p_buffer = NULL;
19147  ni_retcode_t retval;
19148  uint32_t ui32LBA = 0;
19149  niFrameSurface1_t *pSurface;
19150  uint8_t *p_data;
19151  uint16_t ui16Direction = NI_P2P_RECV;
19152  uint32_t ui32Dummy = 0;
19153  uint32_t i;
19154 
19155  if (dmaAddrs->ui32NumEntries > NI_MAX_P2P_SGL_ENTRIES)
19156  {
19157  ni_log2(p_ctx, NI_LOG_ERROR, "Too many SGL entries\n");
19158  retval = NI_RETCODE_INVALID_PARAM;
19159  LRETURN;
19160  }
19161 
19162  /* allocate memory aligned buffer */
19163  retval = ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), NI_DATA_BUFFER_LEN);
19164  if (retval != 0)
19165  {
19166  retval = NI_RETCODE_ERROR_MEM_ALOC;
19167  LRETURN;
19168  }
19169 
19170  pSurface = (niFrameSurface1_t *) pDstFrame->p_data[3];
19171 
19172  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
19173 
19174  p_data = (uint8_t *) p_buffer;
19175 
19176  /* Add payload */
19177  memcpy(p_data, &dmaAddrs->ui64DMAAddr[0], 8); // dummy data, not used for p2p read
19178  memcpy(p_data + 8, &ui32Dummy, 4); // dummy data, not used for p2p read
19179  memcpy(p_data + 12, &pSurface->ui16FrameIdx, 2); // dummy data, not used for p2p read
19180  memcpy(p_data + 14, MAGIC_P2P_VALUE, 4);
19181  memcpy(p_data + 18, &ui16Direction, 2);
19182  memcpy(p_data + 20, &dmaAddrs->ui32NumEntries, 4);
19183 
19184  for (i = 0; i < dmaAddrs->ui32NumEntries; i++)
19185  {
19186  memcpy(&p_data[24] + (i*8), &dmaAddrs->ui64DMAAddr[i], 8);
19187  }
19188 
19189  for (i = 0; i < dmaAddrs->ui32NumEntries; i++)
19190  {
19191  memcpy(&p_data[24] + (dmaAddrs->ui32NumEntries * 8) + (i * 4), &dmaAddrs->ui32DMALen[i], 4);
19192  }
19193 
19195 
19196  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
19197  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
19198 
19199  if (retval < 0)
19200  {
19201  ni_log2(p_ctx, NI_LOG_ERROR, "%s: NVME command failed\n", __func__);
19202  ni_aligned_free(p_buffer);
19204  LRETURN;
19205  }
19206 
19207 END:
19208  if (p_buffer != NULL)
19209  ni_aligned_free(p_buffer);
19210  return retval;
19211 }
19212 
19213 int lower_pixel_rate(const ni_load_query_t *pQuery, uint32_t ui32CurrentLowest)
19214 {
19215  return (pQuery->total_pixel_load < ui32CurrentLowest) ? 1 : 0;
19216 }
19217 
19218 /*!******************************************************************************
19219  * \brief set cpu affinity based on numa node
19220  *
19221  * \param[in] p_ctx pointer to session context
19222  *
19223  * \return NI_RETCODE_SUCCESS
19224  * NI_RETCODE_ERROR_MEM_ALOC
19225  *******************************************************************************/
19227 {
19228 #if defined(__linux__) && defined(XCODER_ENABLE_CPU_AFFINITY)
19229  int numa_node;
19230  if (numa_available() == -1) {
19231  ni_log2(p_ctx, NI_LOG_INFO, "Warning: %s() NUMA not available.\n",
19232  __func__);
19233  }
19234  else
19235  {
19236  numa_node = ni_rsrc_get_numa_node(p_ctx->blk_xcoder_name);
19237 
19238  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): device name %s, numa node %d\n",
19239  __func__, p_ctx->blk_xcoder_name, numa_node);
19240 
19241  if (numa_node < 0)
19242  {
19243  ni_log2(p_ctx, NI_LOG_INFO, "Warning: %s() %s NUMA not available.\n",
19244  __func__, p_ctx->blk_dev_name);
19245  }
19246  else
19247  {
19248 #if defined(LIBNUMA_API_VERSION) && (LIBNUMA_API_VERSION == 2)
19249  struct bitmask *node_mask = numa_allocate_nodemask();
19250  if (!node_mask)
19251  {
19252  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() numa_allocate_cpumask failed.\n",
19253  __func__);
19255  }
19256  numa_bitmask_setbit(node_mask, numa_node);
19257  numa_bind(node_mask);
19258  numa_bitmask_free(node_mask);
19259 #else
19260  nodemask_t node_mask;
19261  nodemask_zero(&node_mask);
19262  nodemask_set(&node_mask, numa_node);
19263  numa_bind(&node_mask);
19264 #endif
19265  }
19266  }
19267 #endif
19268  return NI_RETCODE_SUCCESS;
19269 }
_ni_t408_config_t::maxQpB
int32_t maxQpB
Definition: ni_device_api_priv.h:494
_ni_metadata_enc_bstream::ui16psnr_y
uint16_t ui16psnr_y
Definition: ni_device_api_priv.h:299
_ni_metadata_enc_bstream::metadata_size
uint32_t metadata_size
Definition: ni_device_api_priv.h:277
TUNE_BFRAME_VISUAL_MEDIUM
#define TUNE_BFRAME_VISUAL_MEDIUM
Definition: ni_device_api_priv.h:320
ni_decoder_output_config_t::ui8CropMode
uint8_t ui8CropMode
Definition: ni_device_api_priv.h:940
_ni_encoder_config_t::ui16rootBufId
uint16_t ui16rootBufId
Definition: ni_device_api_priv.h:568
_ni_xcoder_params::reconf_demo_mode
int reconf_demo_mode
Definition: ni_device_api.h:2832
_ni_encoder_config_t::ui16hdr10_dx2
uint16_t ui16hdr10_dx2
Definition: ni_device_api_priv.h:601
NI_RETCODE_PARAM_ERROR_CONF_WIN_BOT
@ NI_RETCODE_PARAM_ERROR_CONF_WIN_BOT
Definition: ni_defs.h:481
NI_MAX_FRAMERATE
#define NI_MAX_FRAMERATE
Definition: ni_device_api.h:119
_ni_encoder_cfg_params::enable_mb_level_rc
int enable_mb_level_rc
Definition: ni_device_api.h:2459
_ni_encoder_cfg_params::frame_rate
int frame_rate
Definition: ni_device_api.h:2314
ni_decoder_output_picture_size::ui16Width
uint16_t ui16Width
Definition: ni_device_api_priv.h:934
_ni_encoder_config_t::ui8pastFrameMaxIntraRatio
uint8_t ui8pastFrameMaxIntraRatio
Definition: ni_device_api_priv.h:667
_ni_frame_config::picture_width
uint16_t picture_width
Definition: ni_device_api.h:2914
_ni_encoder_config_t::ui32hdr10_minluma
uint32_t ui32hdr10_minluma
Definition: ni_device_api_priv.h:606
NI_RETCODE_ERROR_VPU_RECOVERY
@ NI_RETCODE_ERROR_VPU_RECOVERY
Definition: ni_defs.h:529
_ni_xcoder_params::fps_denominator
uint32_t fps_denominator
Definition: ni_device_api.h:2820
_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:253
ni_decoder_output_config_t::ui8Force8Bit
uint8_t ui8Force8Bit
Definition: ni_device_api_priv.h:939
_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:610
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:19091
AVC_RDCOST_BYTE_OFFSET
#define AVC_RDCOST_BYTE_OFFSET
Definition: ni_device_api_priv.h:812
_ni_t408_config_t::saoEnable
int32_t saoEnable
Definition: ni_device_api_priv.h:409
_ni_xcoder_params::luma_linesize
int luma_linesize
Definition: ni_device_api.h:2887
ni_pthread_mutex_unlock
int ni_pthread_mutex_unlock(ni_pthread_mutex_t *mutex)
thread mutex unlock
Definition: ni_util.c:4712
NI_PIX_FMT_BGRA
@ NI_PIX_FMT_BGRA
Definition: ni_device_api.h:271
CLEAR_INSTANCE_BUF_W
#define CLEAR_INSTANCE_BUF_W(frame_id)
Definition: ni_nvme.h:879
_ni_t35_sei_mesg_type
_ni_t35_sei_mesg_type
Definition: ni_device_api_priv.c:91
_ni_encoder_config_t::u8customizeRoiQpLevel
uint8_t u8customizeRoiQpLevel
Definition: ni_device_api_priv.h:651
_ni_packet::psnr_v
double psnr_v
Definition: ni_device_api.h:3025
_ni_load_query::fw_p2p_mem_usage
uint32_t fw_p2p_mem_usage
Definition: ni_device_api.h:1225
_ni_sei_header::size
uint16_t size
Definition: ni_device_api.h:359
CONFIG_SESSION_KeepAliveTimeout_W
#define CONFIG_SESSION_KeepAliveTimeout_W(sid)
Definition: ni_nvme.h:883
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:7236
NI_CODEC_FORMAT_JPEG
@ NI_CODEC_FORMAT_JPEG
Definition: ni_device_api.h:928
_ni_encoder_cfg_params::ver_offset
int ver_offset
Definition: ni_device_api.h:2473
ni_decoder_output_picture_size::ui16Height
uint16_t ui16Height
Definition: ni_device_api_priv.h:933
NI_QUADRA_MEMORY_CONFIG_DR
#define NI_QUADRA_MEMORY_CONFIG_DR
Definition: ni_device_api_priv.h:1049
_ni_network_buffer_info::segment
ni_segment_t segment[NI_MAX_SEGMENT_NUM]
Definition: ni_device_api_priv.h:1004
_ni_dec_mastering_display_colour_volume_bytes::display_primaries
uint16_t display_primaries[3][2]
Definition: ni_device_api.h:1077
_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:1055
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:18519
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:4217
_ni_device_capability::fw_commit_time
uint8_t fw_commit_time[26]
Definition: ni_device_api.h:1180
_ni_t408_config_t::pu16IntraDcDeltaRate
int32_t pu16IntraDcDeltaRate
Definition: ni_device_api_priv.h:474
_ni_session_statistic_t::ui32InstErrors
uint32_t ui32InstErrors
Definition: ni_defs.h:398
NI_MAX_RESOLUTION_HEIGHT
#define NI_MAX_RESOLUTION_HEIGHT
Definition: ni_device_api.h:104
_ni_decoder_input_params_t::mcmode
int mcmode
Definition: ni_device_api.h:2594
_ni_t408_config_t::losslessEnable
int32_t losslessEnable
Definition: ni_device_api_priv.h:382
AI_MODEL_TYPE_HVSPLUS_FILTER
#define AI_MODEL_TYPE_HVSPLUS_FILTER
Definition: ni_device_api_priv.c:89
_ni_encoder_config_t::ui32ltrFirstGap
uint32_t ui32ltrFirstGap
Definition: ni_device_api_priv.h:577
NI_DEVICE_TYPE_ENCODER
@ NI_DEVICE_TYPE_ENCODER
Definition: ni_defs.h:361
_ni_t408_config_t::tier
int32_t tier
Definition: ni_device_api_priv.h:380
_ni_frame::separate_start
uint8_t separate_start
Definition: ni_device_api.h:2804
ni_strerror
ni_retcode_t ni_strerror(char *dest, size_t dmax, int errnum)
Definition: ni_util.c:656
_ni_nvme_identity::xcoder_cnt
uint8_t xcoder_cnt[14]
Definition: ni_nvme.h:231
ni_encoder_cu_info::costIntraSatd
uint32_t costIntraSatd
Definition: ni_device_api_priv.h:873
_ni_packet::p_custom_sei_set
ni_custom_sei_set_t * p_custom_sei_set
Definition: ni_device_api.h:3021
_ni_encoder_config_t::ui8enable2PassGopPatern
uint8_t ui8enable2PassGopPatern
Definition: ni_device_api_priv.h:620
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:413
_ni_p2p_sgl_t
Definition: ni_defs.h:417
_ni_t408_config_t::strongIntraSmoothEnable
uint32_t strongIntraSmoothEnable
Definition: ni_device_api_priv.h:452
_ni_instance_mgr_general_status::fw_model_load
uint32_t fw_model_load
Definition: ni_device_api_priv.h:91
PresetConfig::gopPresetIndex
int gopPresetIndex
Definition: ni_device_api_priv.c:108
_ni_frame::buffer_size
uint32_t buffer_size
Definition: ni_device_api.h:2764
_ni_encoder_config_t::i32spatialLayerBitrate
int32_t i32spatialLayerBitrate[NI_MAX_SPATIAL_LAYERS]
Definition: ni_device_api_priv.h:669
_ni_encoder_cfg_params::adaptiveLamdaMode
int adaptiveLamdaMode
Definition: ni_device_api.h:2531
_ni_network_data
Definition: ni_device_api.h:1377
NI_CC_SEI_BYTE0
#define NI_CC_SEI_BYTE0
Definition: ni_device_api.h:457
_ni_session_context::decoder_low_delay
int decoder_low_delay
Definition: ni_device_api.h:1625
_ni_decoder_input_params_t::error_ratio_threshold
int error_ratio_threshold
Definition: ni_device_api.h:2630
_ni_session_context::p_all_zero_buf
void * p_all_zero_buf
Definition: ni_device_api.h:1602
NI_HDR10P_SEI_BYTE1
#define NI_HDR10P_SEI_BYTE1
Definition: ni_device_api.h:467
_ni_session_context::required_buf_size
uint32_t required_buf_size
Definition: ni_device_api.h:1751
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:7434
_ni_metadata_enc_bstream::pass1Cost
float pass1Cost
Definition: ni_device_api_priv.h:310
_ni_session_context::auto_dl_handle
ni_device_handle_t auto_dl_handle
Definition: ni_device_api.h:1500
_ni_thread_arg_struct_t::p_mutex
ni_pthread_mutex_t * p_mutex
Definition: ni_device_api.h:1281
_ni_metadata_enc_bstream::gop_size
uint8_t gop_size
Definition: ni_device_api_priv.h:296
_ni_encoder_cfg_params::skip_frame_interval
int skip_frame_interval
Definition: ni_device_api.h:2495
NI_MIN_RESOLUTION_WIDTH
#define NI_MIN_RESOLUTION_WIDTH
Definition: ni_device_api.h:89
_ni_decoder_config_t::ui8MaxExtraHwFrameCnt
uint8_t ui8MaxExtraHwFrameCnt
Definition: ni_device_api_priv.h:964
NI_POOL_TYPE_P2P
@ NI_POOL_TYPE_P2P
Definition: ni_device_api.h:507
NI_H265_USERDATA_FLAG_MASTERING_COLOR_VOL
@ NI_H265_USERDATA_FLAG_MASTERING_COLOR_VOL
Definition: ni_device_api.h:383
NI_FW_ENC_BITSTREAM_META_DATA_SIZE
#define NI_FW_ENC_BITSTREAM_META_DATA_SIZE
Definition: ni_device_api_priv.h:1064
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:275
_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:1154
_ni_encoder_cfg_params::enable_dynamic_32x32_merge
int enable_dynamic_32x32_merge
Definition: ni_device_api.h:2427
_ni_encoder_cfg_params::hor_offset
int hor_offset
Definition: ni_device_api.h:2472
_ni_encoder_cfg_params::inLoopDSRatio
int inLoopDSRatio
Definition: ni_device_api.h:2410
_ni_session_context::max_retry_fail_count
int max_retry_fail_count[2]
Definition: ni_device_api.h:1732
_ni_metadata_enc_bstream_rev61::recycle_index
uint32_t recycle_index
Definition: ni_device_api_priv.h:271
_ni_network_buffer_info::ui16Option
uint16_t ui16Option
Definition: ni_device_api_priv.h:993
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:7034
_ni_content_light_level_info_bytes
payload format of HDR SEI content light level info
Definition: ni_device_api.h:1088
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:14710
_ni_encoder_config_t::ui8tuneBframeVisual
uint8_t ui8tuneBframeVisual
Definition: ni_device_api_priv.h:649
_ni_encoder_cfg_params::reset_dts_offset
int reset_dts_offset
Definition: ni_device_api.h:2530
_ni_scaler_config
Definition: ni_device_api_priv.h:1009
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:3725
QUERY_INSTANCE_UPLOAD_ID_R
#define QUERY_INSTANCE_UPLOAD_ID_R(sid, instance)
Definition: ni_nvme.h:739
_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:552
NI_MAX_4K_FPS_QUADRA
#define NI_MAX_4K_FPS_QUADRA
Definition: ni_defs.h:249
_ni_t408_config_t::minQpP
int32_t minQpP
Definition: ni_device_api_priv.h:491
_ni_frame::vui_len
unsigned int vui_len
Definition: ni_device_api.h:2747
_ni_frame::sei_hdr_content_light_level_info_offset
unsigned int sei_hdr_content_light_level_info_offset
Definition: ni_device_api.h:2734
QUERY_SESSION_STATS_R
#define QUERY_SESSION_STATS_R(sid, instance)
Definition: ni_nvme.h:707
GC620_ABGR8888
#define GC620_ABGR8888
Definition: ni_device_api.h:206
_ni_encoder_frame_params::picture_type
uint16_t picture_type
Definition: ni_device_api_priv.h:201
AVC_INTER_PRED_IDC_BIT_POSITION
#define AVC_INTER_PRED_IDC_BIT_POSITION
Definition: ni_device_api_priv.h:818
_ni_decoder_config_t
Definition: ni_device_api_priv.h:954
_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:2617
_ni_encoder_cfg_params::pbRatio
float pbRatio
Definition: ni_device_api.h:2480
_ni_session_statistic_t::ui16ErrorCount
uint16_t ui16ErrorCount
Definition: ni_defs.h:401
_ni_encoder_config_t::i32tolCtbRcInter
int32_t i32tolCtbRcInter
Definition: ni_device_api_priv.h:584
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:5971
_ni_decoder_input_params_t::enable_out1
int enable_out1
Definition: ni_device_api.h:2592
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:7127
ni_encoder_cu_info::cost
uint32_t cost
Definition: ni_device_api_priv.h:871
_ni_network_layer_info::in_param
ni_network_layer_params_t * in_param
Definition: ni_device_api.h:1368
_ni_t408_config_t::disableDeblk
int32_t disableDeblk
Definition: ni_device_api_priv.h:404
_ni_packet::pts
long long pts
Definition: ni_device_api.h:2996
_ni_instance_mgr_stream_complete
Definition: ni_device_api_priv.h:176
_ni_encoder_cfg_params::vbvBufferReencode
int vbvBufferReencode
Definition: ni_device_api.h:2518
_ni_encoder_config_t::i32vbvBufferSize
int32_t i32vbvBufferSize
Definition: ni_device_api_priv.h:523
_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:439
QUERY_INSTANCE_WBUFF_SIZE_R_BY_EP
#define QUERY_INSTANCE_WBUFF_SIZE_R_BY_EP(sid, instance)
Definition: ni_nvme.h:727
_ni_encoder_cfg_params::enable_filler
int enable_filler
Definition: ni_device_api.h:2451
NI_RETCODE_PARAM_ERROR_RCENABLE
@ NI_RETCODE_PARAM_ERROR_RCENABLE
Definition: ni_defs.h:490
_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:1447
_ni_t408_config_t::cu32MergeDeltaRate
int32_t cu32MergeDeltaRate
Definition: ni_device_api_priv.h:487
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:150
_ni_session_context::force_low_delay
bool force_low_delay
Definition: ni_device_api.h:1730
QUERY_INSTANCE_HW_OUT_SIZE_R
#define QUERY_INSTANCE_HW_OUT_SIZE_R(sid, instance)
Definition: ni_nvme.h:747
NI_CC_SEI_BYTE7
#define NI_CC_SEI_BYTE7
Definition: ni_device_api.h:464
GOP_PRESET_IDX_NONE
@ GOP_PRESET_IDX_NONE
Definition: ni_device_api_priv.h:325
NI_VQ_FAST
@ NI_VQ_FAST
Definition: ni_device_api.h:1877
_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:16812
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:18127
_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:460
NI_RETCODE_NVME_SC_RESOURCE_UNAVAILABLE
@ NI_RETCODE_NVME_SC_RESOURCE_UNAVAILABLE
Definition: ni_defs.h:550
_ni_encoder_cfg_params::gopLowdelay
int gopLowdelay
Definition: ni_device_api.h:2399
CONFIG_SESSION_DDR_PRIORITY_W
#define CONFIG_SESSION_DDR_PRIORITY_W(sid)
Definition: ni_nvme.h:900
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:1996
_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:701
_ni_network_buffer_info::ui32IovecNum
uint32_t ui32IovecNum
Definition: ni_device_api_priv.h:1002
ni_decode_cropping_rectangle::ui16W
uint16_t ui16W
Definition: ni_device_api_priv.h:928
_ni_frame_config::rectangle_x
int16_t rectangle_x
Definition: ni_device_api.h:2920
LBA_BIT_OFFSET
#define LBA_BIT_OFFSET
Definition: ni_nvme.h:625
_ni_encoder_config_t::ui32hdr10_maxluma
uint32_t ui32hdr10_maxluma
Definition: ni_device_api_priv.h:605
_ni_gop_rps::ref_pic_used
int ref_pic_used
Definition: ni_device_api.h:2009
_ni_load_query::current_load
uint32_t current_load
Definition: ni_device_api.h:1212
_ni_metadata_common::num_frame_dropped
uint8_t num_frame_dropped
Definition: ni_device_api_priv.h:222
_ni_packet::ssim_u
double ssim_u
Definition: ni_device_api.h:3028
_ni_session_context::dev_xcoder_name
char dev_xcoder_name[MAX_CHAR_IN_DEVICE_NAME]
Definition: ni_device_api.h:1519
_ni_encoder_config_t::ui8sceneChangeDetectLevel
uint8_t ui8sceneChangeDetectLevel
Definition: ni_device_api_priv.h:654
_ni_packet::recycle_index
int recycle_index
Definition: ni_device_api.h:3004
_ni_load_query::total_contexts
uint32_t total_contexts
Definition: ni_device_api.h:1215
RDCOST_OTHER_BYTE_OFFSET
#define RDCOST_OTHER_BYTE_OFFSET
Definition: ni_device_api_priv.h:786
_ni_t408_config_t::cu08IntraDeltaRate
int32_t cu08IntraDeltaRate
Definition: ni_device_api_priv.h:479
_ni_session_context::frame_time_q
void * frame_time_q
Definition: ni_device_api.h:1440
_ni_network_buffer_info::ui16FrameIdx
uint16_t ui16FrameIdx[4]
Definition: ni_device_api_priv.h:998
NI_CC_SEI_BYTE2
#define NI_CC_SEI_BYTE2
Definition: ni_device_api.h:459
_ni_encoder_cfg_params::statistic_output_level
int statistic_output_level
Definition: ni_device_api.h:2492
_ni_dec_mastering_display_colour_volume_bytes::white_point_y
uint16_t white_point_y
Definition: ni_device_api.h:1079
_ni_session_statistic_t
Definition: ni_defs.h:388
_ni_metadata_enc_bstream::ssimY
uint32_t ssimY
Definition: ni_device_api_priv.h:285
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:18881
_ni_encoder_config_t::ui16gdrDuration
uint16_t ui16gdrDuration
Definition: ni_device_api_priv.h:560
_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:1449
_ni_t408_config_t::numTicksPocDiffOne
uint32_t numTicksPocDiffOne
Definition: ni_device_api_priv.h:424
_ni_t408_config_t::enable_cu_level_rate_control
int32_t enable_cu_level_rate_control
Definition: ni_device_api_priv.h:413
_ni_dec_mastering_display_colour_volume_bytes
decoded payload format of HDR SEI mastering display colour volume
Definition: ni_device_api.h:1075
_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:2615
_ni_t408_config_t::customMDEnable
uint32_t customMDEnable
Definition: ni_device_api_priv.h:462
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:2793
_ni_encoder_config_t::i8cuTreeFactor
int8_t i8cuTreeFactor
Definition: ni_device_api_priv.h:633
_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:2620
_ni_t408_config_t::dependSliceModeArg
int32_t dependSliceModeArg
Definition: ni_device_api_priv.h:395
_ni_t408_config_t::pu32IntraPlanarDeltaRate
int32_t pu32IntraPlanarDeltaRate
Definition: ni_device_api_priv.h:476
_ni_packet::psnr_u
double psnr_u
Definition: ni_device_api.h:3024
_ni_encoder_cfg_params::temporal_layers_enable
int temporal_layers_enable
Definition: ni_device_api.h:2467
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:14658
NI_NUM_OF_PIXELS_1080P
#define NI_NUM_OF_PIXELS_1080P
Definition: ni_device_api.h:80
_ni_encoder_cfg_params::adaptiveCrfMode
int adaptiveCrfMode
Definition: ni_device_api.h:2532
_ni_session_context::fn
unsigned short fn
Definition: ni_device_api.h:1653
_ni_packet::flags
int flags
Definition: ni_device_api.h:3019
NI_DEVICE_TYPE_DECODER
@ NI_DEVICE_TYPE_DECODER
Definition: ni_defs.h:360
_niFrameSurface1::ui16height
uint16_t ui16height
Definition: ni_device_api.h:2902
NI_GET_MAX_HWDESC_P2P_BUF_ID
#define NI_GET_MAX_HWDESC_P2P_BUF_ID(x)
Definition: ni_defs.h:283
_ni_session_context::enc_pts_w_idx
int64_t enc_pts_w_idx
Definition: ni_device_api.h:1470
_ni_session_context::key_frame_type
int key_frame_type
Definition: ni_device_api.h:1556
_ni_encoder_config_t::ui32ltrNextInterval
uint32_t ui32ltrNextInterval
Definition: ni_device_api_priv.h:578
READ_INSTANCE_R
#define READ_INSTANCE_R(sid, instance)
Definition: ni_nvme.h:680
_ni_session_context::input_frame_fifo
ni_input_frame input_frame_fifo[120]
encoder:calculate PSNR start
Definition: ni_device_api.h:1740
_ni_encoder_config_t::ui8hrdEnable
uint8_t ui8hrdEnable
Definition: ni_device_api_priv.h:561
_ni_session_context::current_frame_delay
int current_frame_delay
Definition: ni_device_api.h:1724
ni_encoder_cu_info
Definition: ni_device_api_priv.h:865
_ni_encoder_config_t::i8crfMax
int8_t i8crfMax
Definition: ni_device_api_priv.h:628
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:1511
_ni_nvme_identity::hw0_codec_format
uint8_t hw0_codec_format
Definition: ni_nvme.h:169
ni_strcpy
ni_retcode_t ni_strcpy(char *dest, size_t dmax, const char *src)
Definition: ni_util.c:453
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:16328
_ni_t408_config_t::enable_hvs_qp
int32_t enable_hvs_qp
Definition: ni_device_api_priv.h:414
_ni_t408_config_t::avcIdrPeriod
int32_t avcIdrPeriod
Definition: ni_device_api_priv.h:497
_ni_xcoder_params::source_width
int source_width
Definition: ni_device_api.h:2824
_ni_packet::start_of_stream
uint32_t start_of_stream
Definition: ni_device_api.h:3000
NI_PARAM_AV1_MAX_WIDTH
#define NI_PARAM_AV1_MAX_WIDTH
Definition: ni_device_api.h:148
_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:2752
NI_MAX_PACKET_SZ
#define NI_MAX_PACKET_SZ
Definition: ni_defs.h:239
_ni_t408_config_t
Definition: ni_device_api_priv.h:376
_ni_input_frame::usable
int8_t usable
Definition: ni_device_api.h:1419
_ni_decoder_input_params_t::enable_ppu_scale_limit
int enable_ppu_scale_limit
Definition: ni_device_api.h:2618
_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:596
_ni_encoder_cfg_params::ltrRefInterval
int ltrRefInterval
Definition: ni_device_api.h:2360
_ni_encoder_config_t::i8enableipRatio
int8_t i8enableipRatio
Definition: ni_device_api_priv.h:609
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:11891
_ni_nvme_identity::hw0_id
uint8_t hw0_id
Definition: ni_nvme.h:166
INTERCOST_BYTE_OFFSET
#define INTERCOST_BYTE_OFFSET
Definition: ni_device_api_priv.h:796
_ni_device_capability::xcoder_devices
ni_hw_capability_t xcoder_devices[NI_MAX_DEVICES_PER_HW_INSTANCE]
Definition: ni_device_api.h:1173
AVC_MB_MODE_BYTE_OFFSET
#define AVC_MB_MODE_BYTE_OFFSET
Definition: ni_device_api_priv.h:807
_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:16540
_ni_device_capability::fw_branch_name
uint8_t fw_branch_name[256]
Definition: ni_device_api.h:1179
NI_HDR10P_SEI_HDR_H264_LEN
#define NI_HDR10P_SEI_HDR_H264_LEN
Definition: ni_device_api.h:477
_ni_session_context::netint_fd
int netint_fd
Definition: ni_device_api.h:1649
_ni_hw_capability::hw_id
uint8_t hw_id
Definition: ni_device_api.h:1150
_ni_frame_config::frame_index
uint16_t frame_index
Definition: ni_device_api.h:2923
_ni_session_context::keyframe_factor
int keyframe_factor
Definition: ni_device_api.h:1561
_ni_scaler_config::scaler_param_c
uint16_t scaler_param_c
Definition: ni_device_api_priv.h:1014
_ni_encoder_config_t::ui8crfMaxIframeEnable
uint8_t ui8crfMaxIframeEnable
Definition: ni_device_api_priv.h:645
OPT_2
@ OPT_2
Definition: ni_device_api_priv.c:100
NI_CODEC_HW_NONE
@ NI_CODEC_HW_NONE
Definition: ni_device_api.h:953
NI_PIX_FMT_YUV420P
@ NI_PIX_FMT_YUV420P
Definition: ni_device_api.h:266
_ni_uploader_config_t
Definition: ni_device_api_priv.h:890
_ni_encoder_config_t::ui16aspectRatioWidth
uint16_t ui16aspectRatioWidth
Definition: ni_device_api_priv.h:566
_ni_resolution
Definition: ni_device_api_priv.h:903
_ni_encoder_config_t::i32maxIntraSize
int32_t i32maxIntraSize
Definition: ni_device_api_priv.h:526
ni_device_type_t
ni_device_type_t
Definition: ni_defs.h:355
_niFrameSurface1::ui32nodeAddress
uint32_t ui32nodeAddress
Definition: ni_device_api.h:2903
_ni_session_context::isP2P
int32_t isP2P
Definition: ni_device_api.h:1648
_ni_packet::end_of_stream
uint32_t end_of_stream
Definition: ni_device_api.h:2999
_ni_encoder_cfg_params::enable_hvs_qp_scale
int enable_hvs_qp_scale
Definition: ni_device_api.h:2456
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:2286
_ni_thread_arg_struct_t::close_thread
bool close_thread
Definition: ni_device_api.h:1276
_ni_session_context::p_leftover
uint8_t * p_leftover
Definition: ni_device_api.h:1537
_ni_metadata_enc_bstream_rev61::frame_tstamp
uint64_t frame_tstamp
Definition: ni_device_api_priv.h:268
_ni_t408_config_t::useLongTerm
uint32_t useLongTerm
Definition: ni_device_api_priv.h:448
NI_MAX_RESOLUTION_RGBA_WIDTH
#define NI_MAX_RESOLUTION_RGBA_WIDTH
Definition: ni_device_api.h:106
ni_decoder_output_config_t::sOutputPictureSize
ni_decoder_output_picture_size sOutputPictureSize
Definition: ni_device_api_priv.h:951
_ni_xcoder_params::enable2PassGop
int enable2PassGop
Definition: ni_device_api.h:2885
_ni_t408_config_t::roiEnable
int32_t roiEnable
Definition: ni_device_api_priv.h:420
_ni_device_capability::model_number
uint8_t model_number[40]
Definition: ni_device_api.h:1176
_ni_t408_config_t::cu16InterDeltaRate
int32_t cu16InterDeltaRate
Definition: ni_device_api_priv.h:483
_ni_packet::dts
long long dts
Definition: ni_device_api.h:2995
_ni_encoder_cfg_params::crf_max_iframe_enable
int crf_max_iframe_enable
Definition: ni_device_api.h:2499
WRITE_INSTANCE_W
#define WRITE_INSTANCE_W(sid, instance)
Definition: ni_nvme.h:674
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:8718
NI_RETCODE_SUCCESS
@ NI_RETCODE_SUCCESS
Definition: ni_defs.h:441
_ni_session_context::active_video_width
uint32_t active_video_width
Definition: ni_device_api.h:1580
_ni_t408_config_t::gop_preset_index
int32_t gop_preset_index
Definition: ni_device_api_priv.h:384
_ni_t408_config_t::intraRefreshMode
int32_t intraRefreshMode
Definition: ni_device_api_priv.h:396
_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:1342
_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:1345
_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:896
_ni_gop_params::poc_offset
int poc_offset
Definition: ni_device_api.h:2014
_ni_gop_params::qp_offset
int qp_offset
Definition: ni_device_api.h:2015
_ni_framerate::framerate_denom
int32_t framerate_denom
Definition: ni_device_api.h:715
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:494
NI_DEVICE_TYPE_UPLOAD
@ NI_DEVICE_TYPE_UPLOAD
Definition: ni_defs.h:367
_ni_scaler_config::numInputs
uint8_t numInputs
Definition: ni_device_api_priv.h:1012
QUERY_INSTANCE_AI_INFO_R
#define QUERY_INSTANCE_AI_INFO_R(sid, instance)
Definition: ni_nvme.h:714
_ni_decoder_input_params_t::enable_user_data_sei_passthru
int enable_user_data_sei_passthru
Definition: ni_device_api.h:2612
NI_CC_SEI_BYTE1
#define NI_CC_SEI_BYTE1
Definition: ni_device_api.h:458
_ni_t408_config_t::hvs_qp_scale
int32_t hvs_qp_scale
Definition: ni_device_api_priv.h:415
_ni_session_context::pts_correction_last_dts
int64_t pts_correction_last_dts
Definition: ni_device_api.h:1472
ni_ai_session_open
ni_retcode_t ni_ai_session_open(ni_session_context_t *p_ctx)
Definition: ni_device_api_priv.c:17693
_ni_session_context::status
int status
Definition: ni_device_api.h:1555
_ni_decoder_config_t::ui8EcPolicy
uint8_t ui8EcPolicy
Definition: ni_device_api_priv.h:967
_ni_session_context::domain
unsigned short domain
Definition: ni_device_api.h:1650
_ni_session_context::is_dec_pkt_512_aligned
int is_dec_pkt_512_aligned
Definition: ni_device_api.h:1490
_ni_session_context::pool_type
ni_frame_pool_type_t pool_type
Definition: ni_device_api.h:1728
_ni_t408_config_t::nrNoiseSigmaY
uint32_t nrNoiseSigmaY
Definition: ni_device_api_priv.h:444
INTER_PRED_IDC_BYTE_OFFSET
#define INTER_PRED_IDC_BYTE_OFFSET
Definition: ni_device_api_priv.h:711
NI_INVALID_SESSION_ID
#define NI_INVALID_SESSION_ID
Definition: ni_device_api.h:114
_ni_t408_config_t::maxQpI
int32_t maxQpI
Definition: ni_device_api_priv.h:490
_ni_t408_config_t::level
int32_t level
Definition: ni_device_api_priv.h:379
_ni_encoder_cfg_params::enable_ssim
int enable_ssim
Definition: ni_device_api.h:2462
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:1559
_ni_device_capability::xcoder_cnt
uint8_t xcoder_cnt[NI_DEVICE_TYPE_XCODER_MAX]
Definition: ni_device_api.h:1172
_ni_session_context::ori_chroma_linesize
int ori_chroma_linesize
Definition: ni_device_api.h:1712
_ni_encoder_cfg_params::enable_compensate_qp
int enable_compensate_qp
Definition: ni_device_api.h:2513
_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:1372
_ni_frame::total_start_len
uint32_t total_start_len
Definition: ni_device_api.h:2802
_ni_session_context::last_bitrate
int32_t last_bitrate
Definition: ni_device_api.h:1705
current_time
struct timeval current_time
Definition: ni_p2p_read_test.c:80
_ni_network_buffer_info::ui16Height
uint16_t ui16Height
Definition: ni_device_api_priv.h:992
_ni_input_frame::video_width
uint32_t video_width
Definition: ni_device_api.h:1414
_ni_session_context::pts_correction_num_faulty_dts
int pts_correction_num_faulty_dts
Definition: ni_device_api.h:1471
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:3328
GC620_ARGB8888
#define GC620_ARGB8888
Definition: ni_device_api.h:207
_ni_session_context::actual_video_width
uint32_t actual_video_width
Definition: ni_device_api.h:1584
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:7368
ni_gettime_ns
uint64_t ni_gettime_ns(void)
Definition: ni_util.c:2622
_ni_encoder_config_t::ui8compressor
uint8_t ui8compressor
Definition: ni_device_api_priv.h:612
_ni_encoder_cfg_params::chromaQpOffset
int chromaQpOffset
Definition: ni_device_api.h:2403
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:1563
_ni_session_context::last_frame_dropped
uint32_t last_frame_dropped
Definition: ni_device_api.h:1763
_ni_frame::iovec
ni_iovec_t * iovec
Definition: ni_device_api.h:2811
_ni_encoder_config_t::ui8colorPrimaries
uint8_t ui8colorPrimaries
Definition: ni_device_api_priv.h:563
NI_PIX_FMT_BGR0
@ NI_PIX_FMT_BGR0
Definition: ni_device_api.h:274
_ni_t408_config_t::fixedBitRatio
int32_t fixedBitRatio[NI_MAX_GOP_NUM]
Definition: ni_device_api_priv.h:412
_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:629
_ni_instance_mgr_general_status::process_load_percent
uint8_t process_load_percent
Definition: ni_device_api_priv.h:82
ni_encoder_cu_info::cuMode
uint8_t cuMode
Definition: ni_device_api_priv.h:870
_ni_frameclone_desc
Definition: ni_device_api.h:1389
NI_MIN_RESOLUTION_HEIGHT_SCALER
#define NI_MIN_RESOLUTION_HEIGHT_SCALER
Definition: ni_device_api.h:98
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:6632
NI_EC_POLICY_LIMITED_ERROR
#define NI_EC_POLICY_LIMITED_ERROR
Definition: ni_device_api.h:336
_ni_xcoder_params::rootBufId
int rootBufId
Definition: ni_device_api.h:2878
_ni_encoder_config_t::ui32ltrRefInterval
uint32_t ui32ltrRefInterval
Definition: ni_device_api_priv.h:575
ni_config_instance_set_decoder_ppu_params
ni_retcode_t ni_config_instance_set_decoder_ppu_params(ni_session_context_t *p_ctx, void *p_dec_ppu_config, int buffer_size)
Send a p_config command to configure decoding parameters.
Definition: ni_device_api_priv.c:16240
OPT_3
@ OPT_3
Definition: ni_device_api_priv.c:101
ni_encoder_cu_info::qp
uint8_t qp
Definition: ni_device_api_priv.h:884
_ni_t408_config_t::pu04DeltaRate
int32_t pu04DeltaRate
Definition: ni_device_api_priv.h:463
_ni_ddr_priority_config::ddr_mode
uint8_t ddr_mode
Definition: ni_device_api_priv.h:1021
_ni_frame::p_start_buffer
uint8_t * p_start_buffer
Definition: ni_device_api.h:2799
_ni_session_context::event_handle
ni_event_handle_t event_handle
Definition: ni_device_api.h:1605
_ni_encoder_config_t::i16bitrateWindow
int16_t i16bitrateWindow
Definition: ni_device_api_priv.h:586
QUERY_INSTANCE_WBUFF_SIZE_BUSY_R
#define QUERY_INSTANCE_WBUFF_SIZE_BUSY_R(sid, instance)
Definition: ni_nvme.h:734
QUERY_INSTANCE_ACQUIRE_BUF
#define QUERY_INSTANCE_ACQUIRE_BUF(sid, instance)
Definition: ni_nvme.h:741
NI_XCODER_FAILURES_MAX
#define NI_XCODER_FAILURES_MAX
Definition: ni_device_api_priv.c:194
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:8472
NI_XCODER_REVISION
#define NI_XCODER_REVISION
Definition: ni_defs.h:98
QUERY_DETAIL_GET_STATUS_R
#define QUERY_DETAIL_GET_STATUS_R(instance)
Definition: ni_nvme.h:785
_ni_t408_config_t::maxQpP
int32_t maxQpP
Definition: ni_device_api_priv.h:492
_ni_device_capability::hw_elements_cnt
uint8_t hw_elements_cnt
Definition: ni_device_api.h:1170
_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:5550
ni_create_frame
int ni_create_frame(ni_frame_t *p_frame, uint32_t read_length, uint64_t *p_frame_offset, uint32_t *p_frame_dropped, bool is_hw_frame)
Get info from received p_frame.
Definition: ni_device_api_priv.c:7672
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:2710
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:13097
_ni_encoder_cfg_params::enable_acq_limit
int enable_acq_limit
Definition: ni_device_api.h:2505
_ni_encoder_cfg_params::noMbtree
int noMbtree
Definition: ni_device_api.h:2476
_ni_t408_config_t::nrNoiseSigmaCb
uint32_t nrNoiseSigmaCb
Definition: ni_device_api_priv.h:445
AVC_MB_MODE_BIT_POSITION
#define AVC_MB_MODE_BIT_POSITION
Definition: ni_device_api_priv.h:808
_ni_scaler_config::scaler_param_b
uint16_t scaler_param_b
Definition: ni_device_api_priv.h:1013
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:6093
_ni_session_context::last_dts
int64_t last_dts
Definition: ni_device_api.h:1467
_ni_session_context::blk_io_handle
ni_device_handle_t blk_io_handle
Definition: ni_device_api.h:1496
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:17420
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:2046
_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:347
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:2801
OPEN_SESSION_CODEC
#define OPEN_SESSION_CODEC(instance, codec, param)
Definition: ni_nvme.h:694
NI_HDR10P_SEI_HDR_HEVC_LEN
#define NI_HDR10P_SEI_HDR_HEVC_LEN
Definition: ni_device_api.h:476
_ni_segment::ui32RelLba
uint32_t ui32RelLba
Definition: ni_device_api_priv.h:984
_ni_network_buffer_info::ui8PoolSize
uint8_t ui8PoolSize
Definition: ni_device_api_priv.h:996
NI_RETCODE_PARAM_ERROR_AREA_TOO_BIG
@ NI_RETCODE_PARAM_ERROR_AREA_TOO_BIG
Definition: ni_defs.h:516
NI_RETCODE_PARAM_ERROR_CUSIZE_MODE_32X32_EN
@ NI_RETCODE_PARAM_ERROR_CUSIZE_MODE_32X32_EN
Definition: ni_defs.h:502
_ni_encoder_cfg_params::roi_enable
int roi_enable
Definition: ni_device_api.h:2332
_ni_session_context::frame_pkt_offset
uint64_t frame_pkt_offset
Definition: ni_device_api.h:1678
_ni_encoder_cfg_params::pastFrameMaxIntraRatio
int pastFrameMaxIntraRatio
Definition: ni_device_api.h:2523
NI_MAX_DEVICES_PER_HW_INSTANCE
#define NI_MAX_DEVICES_PER_HW_INSTANCE
Definition: ni_defs.h:251
_ni_encoder_cfg_params::intra_period
int intra_period
Definition: ni_device_api.h:2335
_ni_session_context::pts_correction_last_pts
int64_t pts_correction_last_pts
Definition: ni_device_api.h:1474
_ni_decoder_config_t::ui32ErrRatioThreshold
uint32_t ui32ErrRatioThreshold
Definition: ni_device_api_priv.h:973
_ni_encoder_cfg_params::EnableRdoQuant
int EnableRdoQuant
Definition: ni_device_api.h:2377
_ni_encoder_cfg_params::spatialLayerBitrate
int spatialLayerBitrate[NI_MAX_SPATIAL_LAYERS]
Definition: ni_device_api.h:2525
_ni_frame_config::rectangle_height
uint16_t rectangle_height
Definition: ni_device_api.h:2919
_ni_decoder_input_params_t::min_packets_delay
bool min_packets_delay
Definition: ni_device_api.h:2623
INTER_PRED_IDC_BITS
#define INTER_PRED_IDC_BITS
Definition: ni_device_api_priv.h:713
_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:2795
_ni_nvme_identity::hw3_video_profile
uint8_t hw3_video_profile
Definition: ni_nvme.h:214
NI_FW_ENC_BITSTREAM_META_DATA_SIZE_UNDER_MAJOR_6_MINOR_sM
#define NI_FW_ENC_BITSTREAM_META_DATA_SIZE_UNDER_MAJOR_6_MINOR_sM
Definition: ni_device_api_priv.h:1070
_ni_encoder_cfg_params::enable_pic_skip
int enable_pic_skip
Definition: ni_device_api.h:2445
_ni_network_data::outset
ni_network_layer_offset_t * outset
Definition: ni_device_api.h:1385
_ni_network_perf_metrics
Definition: ni_device_api.h:1398
_ni_hw_capability::max_4k_fps
uint8_t max_4k_fps
Definition: ni_device_api.h:1152
_ni_t408_config_t::betaOffsetDiv2
int32_t betaOffsetDiv2
Definition: ni_device_api_priv.h:406
_ni_encoder_cfg_params::use_recommend_enc_params
int use_recommend_enc_params
Definition: ni_device_api.h:2421
_ni_thread_arg_struct_t::p_buffer
void * p_buffer
Definition: ni_device_api.h:1280
_ni_packet::frame_type
uint32_t frame_type
Definition: ni_device_api.h:3003
_ni_encoder_config_t::ui8disableBframeRDOQ
uint8_t ui8disableBframeRDOQ
Definition: ni_device_api_priv.h:647
_ni_encoder_config_t::ui8planarFormat
uint8_t ui8planarFormat
Definition: ni_device_api_priv.h:569
CONFIG_INSTANCE_SetEncRoiQpMap_W
#define CONFIG_INSTANCE_SetEncRoiQpMap_W(sid, instance)
Definition: ni_nvme.h:847
_ni_hw_capability::min_video_height
uint16_t min_video_height
Definition: ni_device_api.h:1158
_ni_load_query::fw_share_mem_usage
uint32_t fw_share_mem_usage
Definition: ni_device_api.h:1224
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:6800
_ni_gop_params::pic_type
int pic_type
Definition: ni_device_api.h:2018
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:725
_ni_encoder_config_t::ui8LookAheadDepth
uint8_t ui8LookAheadDepth
Definition: ni_device_api_priv.h:548
_ni_encoder_cfg_params::min_qp
int min_qp
Definition: ni_device_api.h:2441
_ni_encoder_config_t::i32userMinDeltaQp
int32_t i32userMinDeltaQp
Definition: ni_device_api_priv.h:528
NI_SCALER_OPCODE_AI_ALIGN
@ NI_SCALER_OPCODE_AI_ALIGN
Definition: ni_defs.h:599
_ni_frame::error_ratio
uint32_t error_ratio
Definition: ni_device_api.h:2807
_ni_encoder_cfg_params::gop_preset_index
int gop_preset_index
Definition: ni_device_api.h:2326
_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:602
_ni_frame::sei_user_data_unreg_len
unsigned int sei_user_data_unreg_len
Definition: ni_device_api.h:2741
_ni_encoder_cfg_params::max_qp
int max_qp
Definition: ni_device_api.h:2442
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:5734
_ni_frame_config
Definition: ni_device_api.h:2912
_ni_encoder_cfg_params::ipRatio
float ipRatio
Definition: ni_device_api.h:2479
_ni_session_statistic_t::ui32FramesCorrupted
uint32_t ui32FramesCorrupted
Definition: ni_defs.h:412
NI_SCALER_FLAG_IO
#define NI_SCALER_FLAG_IO
Definition: ni_device_api.h:303
ni_decoder_output_config_t::ui8Enabled
uint8_t ui8Enabled
Definition: ni_device_api_priv.h:938
_ni_encoder_config_t::ui8av1ErrResilientMode
uint8_t ui8av1ErrResilientMode
Definition: ni_device_api_priv.h:614
_ni_metadata_enc_bstream::cuInfoSize
uint32_t cuInfoSize
Definition: ni_device_api_priv.h:311
_ni_session_context::force_frame_type
int force_frame_type
Definition: ni_device_api.h:1573
_ni_encoder_cfg_params::blockRCSize
int blockRCSize
Definition: ni_device_api.h:2411
NI_SCALER_OPCODE_IPOVLY
@ NI_SCALER_OPCODE_IPOVLY
Definition: ni_defs.h:595
ni_pthread_cond_signal
int ni_pthread_cond_signal(ni_pthread_cond_t *cond)
signal a condition
Definition: ni_util.c:4880
_ni_encoder_cfg_params::av1OpLevel
int av1OpLevel[NI_MAX_SPATIAL_LAYERS]
Definition: ni_device_api.h:2527
_ni_encoder_config_t::i32meBlkMode
int32_t i32meBlkMode
Definition: ni_device_api_priv.h:520
_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:443
_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:390
_ni_xcoder_params::roi_demo_mode
int roi_demo_mode
Definition: ni_device_api.h:2831
_ni_sei_header::status
uint8_t status
Definition: ni_device_api.h:358
_niFrameSurface1::src_cpu
int8_t src_cpu
Definition: ni_device_api.h:2908
_ni_network_perf_metrics::total_idle_cycles
uint32_t total_idle_cycles
Definition: ni_device_api.h:1401
_ni_metadata_enc_bstream::frameCropTopOffset
uint16_t frameCropTopOffset
Definition: ni_device_api_priv.h:304
_ni_session_context::src_endian
int src_endian
Definition: ni_device_api.h:1524
_ni_session_context::low_delay_sync_mutex
ni_pthread_mutex_t low_delay_sync_mutex
Definition: ni_device_api.h:1696
_ni_encoder_config_t::i32userQpMin
int32_t i32userQpMin
Definition: ni_device_api_priv.h:529
AVC_INTER_PRED_IDC_BYTE_OFFSET
#define AVC_INTER_PRED_IDC_BYTE_OFFSET
Definition: ni_device_api_priv.h:817
_ni_encoder_cfg_params::intra_mb_refresh_mode
int intra_mb_refresh_mode
Definition: ni_device_api.h:2336
nvme_config_xcoder_config_set_sequence_change
@ nvme_config_xcoder_config_set_sequence_change
Definition: ni_nvme.h:437
_ni_encoder_config_t::ui8lowLatencyMode
uint8_t ui8lowLatencyMode
Definition: ni_device_api_priv.h:535
_ni_encoder_config_t::ui8spatialLayersRefBaseLayer
uint8_t ui8spatialLayersRefBaseLayer
Definition: ni_device_api_priv.h:661
NI_MIN_RESOLUTION_WIDTH_SCALER
#define NI_MIN_RESOLUTION_WIDTH_SCALER
Definition: ni_device_api.h:97
DOWNLOAD_FRAMEIDX_R
#define DOWNLOAD_FRAMEIDX_R(frame_id)
Definition: ni_nvme.h:683
_ni_frame::orignal_pts
long long orignal_pts
Definition: ni_device_api.h:2806
NI_RETCODE_PARAM_ERROR_CONF_WIN_TOP
@ NI_RETCODE_PARAM_ERROR_CONF_WIN_TOP
Definition: ni_defs.h:479
_ni_encoder_config_t::ui8totalCuTreeDepth
uint8_t ui8totalCuTreeDepth
Definition: ni_device_api_priv.h:663
_ni_encoder_cfg_params::vbv_buffer_size
int vbv_buffer_size
Definition: ni_device_api.h:2452
_ni_t408_config_t::nrIntraWeightCb
uint32_t nrIntraWeightCb
Definition: ni_device_api_priv.h:437
_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:949
PresetConfig::lookAheadDepth
int lookAheadDepth
Definition: ni_device_api_priv.c:107
_ni_frame::sei_total_len
unsigned int sei_total_len
Definition: ni_device_api.h:2726
_ni_t408_config_t::conf_win_right
int32_t conf_win_right
Definition: ni_device_api_priv.h:391
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:1480
_ni_frame::p_buffer
uint8_t * p_buffer
Definition: ni_device_api.h:2763
NUM_OF_CORES
@ NUM_OF_CORES
Definition: ni_defs.h:385
_ni_metadata_enc_bstream::max_mv_x
int16_t max_mv_x[2]
Definition: ni_device_api_priv.h:289
_ni_encoder_config_t::i32ipRatio
int32_t i32ipRatio
Definition: ni_device_api_priv.h:635
_ni_encoder_cfg_params::noHWMultiPassSupport
int noHWMultiPassSupport
Definition: ni_device_api.h:2477
_ni_session_statistic_t::ui32FramesCompleted
uint32_t ui32FramesCompleted
Definition: ni_defs.h:395
_ni_encoder_cfg_params::gdrDuration
int gdrDuration
Definition: ni_device_api.h:2359
_ni_packet::psnr_y
double psnr_y
Definition: ni_device_api.h:3023
NI_RETCODE_PARAM_ERROR_CONF_WIN_L
@ NI_RETCODE_PARAM_ERROR_CONF_WIN_L
Definition: ni_defs.h:483
_ni_xcoder_params::video_full_range_flag
int video_full_range_flag
Definition: ni_device_api.h:2864
NI_FIFO_SZ
#define NI_FIFO_SZ
Definition: ni_defs.h:305
_ni_frame::dts
long long dts
Definition: ni_device_api.h:2703
_ni_decoder_input_params_t::force_low_delay
bool force_low_delay
Definition: ni_device_api.h:2610
_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:337
_ni_encoder_cfg_params::aspectRatioWidth
int aspectRatioWidth
Definition: ni_device_api.h:2315
NI_RETCODE_INVALID_PARAM
@ NI_RETCODE_INVALID_PARAM
Definition: ni_defs.h:443
_ni_metadata_enc_bstream::reconLumaWidth
uint16_t reconLumaWidth
Definition: ni_device_api_priv.h:302
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:1888
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:16960
_ni_encoder_config_t::ui8avccHvcc
uint8_t ui8avccHvcc
Definition: ni_device_api_priv.h:660
_ni_session_context::ppu_reconfig_pkt_pos
uint64_t ppu_reconfig_pkt_pos
Definition: ni_device_api.h:1760
_ni_frame::pkt_pos
uint64_t pkt_pos
Definition: ni_device_api.h:2796
ni_buffer_pool_free
void ni_buffer_pool_free(ni_queue_buffer_pool_t *p_buffer_pool)
Definition: ni_util.c:1412
NI_AV1_INVALID_BUFFER_INDEX
#define NI_AV1_INVALID_BUFFER_INDEX
Definition: ni_device_api.h:182
_ni_session_context::buffered_frame_index
int16_t buffered_frame_index
Definition: ni_device_api.h:1748
_ni_encoder_config_t::i32picHeight
int32_t i32picHeight
Definition: ni_device_api_priv.h:519
_ni_device_capability::fw_commit_hash
uint8_t fw_commit_hash[41]
Definition: ni_device_api.h:1181
_ni_frame::crop_left
uint32_t crop_left
Definition: ni_device_api.h:2712
_ni_metadata_enc_frame::inconsecutive_transfer
uint8_t inconsecutive_transfer
Definition: ni_device_api_priv.h:259
_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:287
_ni_instance_mgr_stream_info
Definition: ni_device_api_priv.h:122
_ni_packet::ssim_y
double ssim_y
Definition: ni_device_api.h:3027
_ni_encoder_cfg_params::maxFrameSizeRatio
int maxFrameSizeRatio
Definition: ni_device_api.h:2319
_ni_decoder_input_params_t::enable_advanced_ec
int enable_advanced_ec
Definition: ni_device_api.h:2616
_ni_t408_config_t::bgThrMeanDiff
uint32_t bgThrMeanDiff
Definition: ni_device_api_priv.h:457
_ni_encoder_config_t::ui8adaptiveCuTree
uint8_t ui8adaptiveCuTree
Definition: ni_device_api_priv.h:664
_ni_encoder_cfg_params::HDR10AveLight
int HDR10AveLight
Definition: ni_device_api.h:2346
_ni_encoder_config_t::ui32verOffset
uint32_t ui32verOffset
Definition: ni_device_api_priv.h:626
_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:2384
_ni_decoder_config_t::ui8HWFrame
uint8_t ui8HWFrame
Definition: ni_device_api_priv.h:956
_ni_encoder_cfg_params::cu_size_mode
int cu_size_mode
Definition: ni_device_api.h:2423
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:7298
_ni_decoder_config_t::ui16MaxSeiDataSize
uint16_t ui16MaxSeiDataSize
Definition: ni_device_api_priv.h:958
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:6456
_ni_encoder_cfg_params::hrdEnable
int hrdEnable
Definition: ni_device_api.h:2378
_ni_encoder_config_t::i32bitRate
int32_t i32bitRate
Definition: ni_device_api_priv.h:530
_ni_t408_config_t::pu04IntraPlanarDeltaRate
int32_t pu04IntraPlanarDeltaRate
Definition: ni_device_api_priv.h:467
SwapSWBytes
void SwapSWBytes(uint8_t *buf, uint32_t bytes)
Definition: ni_device_api_priv.c:203
_ni_frame_config::session_id
uint16_t session_id
Definition: ni_device_api.h:2924
_ni_encoder_config_t::ui8AiEnhanceLevel
uint8_t ui8AiEnhanceLevel
Definition: ni_device_api_priv.h:627
_ni_t408_config_t::pu32IntraAngleDeltaRate
int32_t pu32IntraAngleDeltaRate
Definition: ni_device_api_priv.h:478
AVC_INTRA_PART_MODE_BITS
#define AVC_INTRA_PART_MODE_BITS
Definition: ni_device_api_priv.h:854
NI_PIX_FMT_8_TILED4X4
@ NI_PIX_FMT_8_TILED4X4
Definition: ni_device_api.h:279
_ni_session_context::bit_depth_factor
int bit_depth_factor
Definition: ni_device_api.h:1525
_ni_session_statistic_t::ui32FramesBuffered
uint32_t ui32FramesBuffered
Definition: ni_defs.h:394
_ni_uploader_config_t::ui32chromaLinesize
uint32_t ui32chromaLinesize
Definition: ni_device_api_priv.h:899
_ni_encoder_config_t
Definition: ni_device_api_priv.h:515
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:223
_ni_encoder_config_t::ui8av1OpLevel
uint8_t ui8av1OpLevel[NI_MAX_SPATIAL_LAYERS]
Definition: ni_device_api_priv.h:671
_ni_encoder_cfg_params::encMallocStrategy
int encMallocStrategy
Definition: ni_device_api.h:2511
_ni_encoder_config_t::ui32setLongTermInterval
uint32_t ui32setLongTermInterval
Definition: ni_device_api_priv.h:581
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:859
ni_decoder_output_config_t::ui8ScaleEnabled
uint8_t ui8ScaleEnabled
Definition: ni_device_api_priv.h:941
_ni_frame::force_pic_qp
uint16_t force_pic_qp
Definition: ni_device_api.h:2756
_ni_encoder_cfg_params::high_tier
int high_tier
Definition: ni_device_api.h:2419
_ni_session_context::mutex
ni_pthread_mutex_t mutex
Definition: ni_device_api.h:1596
_ni_encoder_cfg_params::max_num_merge
int max_num_merge
Definition: ni_device_api.h:2424
_ni_gop_params::num_ref_pics
int num_ref_pics
Definition: ni_device_api.h:2019
_ni_decoder_input_params_t::crop_mode
int crop_mode[NI_MAX_NUM_OF_DECODER_OUTPUTS]
Definition: ni_device_api.h:2598
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:16014
_ni_encoder_cfg_params::crf
int crf
Definition: ni_device_api.h:2344
_ni_session_context::hw_id
int hw_id
Definition: ni_device_api.h:1509
ni_dec_fme_buffer_pool_free
void ni_dec_fme_buffer_pool_free(ni_buf_pool_t *p_buffer_pool)
Definition: ni_util.c:1358
ni_encoder_cu_info::costOfOtherMode
uint32_t costOfOtherMode
Definition: ni_device_api_priv.h:872
_ni_encoder_config_t::i8pass1Qp
int8_t i8pass1Qp
Definition: ni_device_api_priv.h:641
_ni_hw_capability::video_level
uint8_t video_level
Definition: ni_device_api.h:1160
NI_MAX_DEC_SESSION_READ_QUERY_EOS_RETRIES
#define NI_MAX_DEC_SESSION_READ_QUERY_EOS_RETRIES
Definition: ni_device_api_priv.h:1059
CU_SIZE_BIT_POSITION
#define CU_SIZE_BIT_POSITION
Definition: ni_device_api_priv.h:697
_ni_network_layer_params_t
Definition: ni_device_api.h:1340
_ni_encoder_config_t::ui8fixedframerate
uint8_t ui8fixedframerate
Definition: ni_device_api_priv.h:613
NVME_CORE
@ NVME_CORE
Definition: ni_defs.h:380
_ni_encoder_config_t::ui8mallocStrategy
uint8_t ui8mallocStrategy
Definition: ni_device_api_priv.h:655
_ni_encoder_config_t::ui8colorTrc
uint8_t ui8colorTrc
Definition: ni_device_api_priv.h:564
_ni_session_context::ori_width
int ori_width
Definition: ni_device_api.h:1593
_ni_xcoder_params::fps_number
uint32_t fps_number
Definition: ni_device_api.h:2819
QUERY_INSTANCE_RBUFF_SIZE_R
#define QUERY_INSTANCE_RBUFF_SIZE_R(sid, instance)
Definition: ni_nvme.h:723
MOTION_CONSTRAINED_QUALITY_MODE
#define MOTION_CONSTRAINED_QUALITY_MODE
Definition: ni_device_api.h:498
_ni_session_context::ddr_config
uint8_t ddr_config
Definition: ni_device_api.h:1657
_ni_t408_config_t::bgLambdaQp
uint32_t bgLambdaQp
Definition: ni_device_api_priv.h:458
_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:4302
_ni_xcoder_params::hdrEnableVUI
int hdrEnableVUI
Definition: ni_device_api.h:2846
_ni_network_buffer_info
Definition: ni_device_api_priv.h:988
NI_HDR10P_SEI_BYTE5
#define NI_HDR10P_SEI_BYTE5
Definition: ni_device_api.h:471
_ni_load_query::fw_video_shared_mem_usage
uint32_t fw_video_shared_mem_usage
Definition: ni_device_api.h:1221
_ni_scaler_params_t
Definition: ni_device_api.h:2658
_ni_t408_config_t::conf_win_bottom
int32_t conf_win_bottom
Definition: ni_device_api_priv.h:389
QP_BIT_POSITION
#define QP_BIT_POSITION
Definition: ni_device_api_priv.h:782
_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:435
NI_RETCODE_PARAM_ERROR_CU_LVL_RC_EN
@ NI_RETCODE_PARAM_ERROR_CU_LVL_RC_EN
Definition: ni_defs.h:469
_ni_t408_config_t::use_recommend_enc_params
int32_t use_recommend_enc_params
Definition: ni_device_api_priv.h:398
_ni_device_capability::fw_build_id
uint8_t fw_build_id[256]
Definition: ni_device_api.h:1183
CONFIG_GLOBAL_NAMESPACE_NUM
#define CONFIG_GLOBAL_NAMESPACE_NUM
Definition: ni_nvme.h:895
_ni_p2p_sgl_t::ui64DMAAddr
uint64_t ui64DMAAddr[NI_MAX_P2P_SGL_ENTRY]
Definition: ni_defs.h:421
RDCOST_OTHER_BITS
#define RDCOST_OTHER_BITS
Definition: ni_device_api_priv.h:788
NI_RETCODE_PARAM_ERROR_HEIGHT_TOO_BIG
@ NI_RETCODE_PARAM_ERROR_HEIGHT_TOO_BIG
Definition: ni_defs.h:512
_ni_timestamp_table_t::list
ni_queue_t list
Definition: ni_device_api.h:1337
_ni_metadata_enc_frame::start_len
uint16_t start_len[3]
Definition: ni_device_api_priv.h:258
NI_RETCODE_PARAM_ERROR_MX_QP
@ NI_RETCODE_PARAM_ERROR_MX_QP
Definition: ni_defs.h:475
_ni_encoder_cfg_params::enable_timecode
int enable_timecode
Definition: ni_device_api.h:2515
STD_AV1
@ STD_AV1
Definition: ni_device_api_priv.h:371
NI_RETCODE_ERROR_MEM_ALOC
@ NI_RETCODE_ERROR_MEM_ALOC
Definition: ni_defs.h:445
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:439
_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:255
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:8128
_ni_load_query::fw_model_load
uint32_t fw_model_load
Definition: ni_device_api.h:1213
_ni_metadata_enc_bstream::inter_total_count
uint16_t inter_total_count
Definition: ni_device_api_priv.h:294
_ni_t408_config_t::forcedHeaderEnable
uint32_t forcedHeaderEnable
Definition: ni_device_api_priv.h:512
_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:807
SESSION_RUN_STATE_SEQ_CHANGE_DRAINING
@ SESSION_RUN_STATE_SEQ_CHANGE_DRAINING
Definition: ni_device_api.h:1192
NI_FW_META_DATA_SZ
#define NI_FW_META_DATA_SZ
Definition: ni_defs.h:320
NI_CC_SEI_HDR_H264_LEN
#define NI_CC_SEI_HDR_H264_LEN
Definition: ni_device_api.h:478
_ni_decoder_config_t::ui32SourceHeight
uint32_t ui32SourceHeight
Definition: ni_device_api_priv.h:972
ni_nvme_send_write_cmd
int32_t ni_nvme_send_write_cmd(ni_device_handle_t handle, ni_event_handle_t event_handle, void *p_data, uint32_t data_len, uint32_t lba)
Compose a io write command.
Definition: ni_nvme.c:660
_ni_session_context::fw_rev
uint8_t fw_rev[8]
Definition: ni_device_api.h:1656
ni_encoder_cu_info::costInterSatd
uint32_t costInterSatd
Definition: ni_device_api_priv.h:874
nvme_cmd_xcoder_read
@ nvme_cmd_xcoder_read
Definition: ni_defs.h:582
_ni_network_data::output_num
uint32_t output_num
Definition: ni_device_api.h:1380
_ni_encoder_cfg_params::intra_mb_refresh_arg
int intra_mb_refresh_arg
Definition: ni_device_api.h:2337
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:1191
_ni_metadata_enc_frame::force_pic_qp_i
uint16_t force_pic_qp_i
Definition: ni_device_api_priv.h:251
nvme_cmd_xcoder_write
@ nvme_cmd_xcoder_write
Definition: ni_defs.h:581
_ni_session_context::decoder_last_drop_frame_num
uint32_t decoder_last_drop_frame_num
Definition: ni_device_api.h:1754
_ni_encoder_config_t::ui8disableAv1TimingInfo
uint8_t ui8disableAv1TimingInfo
Definition: ni_device_api_priv.h:670
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:3877
NI_MAX_REF_PIC
#define NI_MAX_REF_PIC
Definition: ni_device_api.h:60
_ni_session_context::pts_correction_num_faulty_pts
int pts_correction_num_faulty_pts
Definition: ni_device_api.h:1473
_ni_encoder_cfg_params::intraCompensateMode
int intraCompensateMode
Definition: ni_device_api.h:2533
_ni_encoder_cfg_params::pps_init_qp
int pps_init_qp
Definition: ni_device_api.h:2482
_ni_session_context::pkt_pos
uint64_t pkt_pos[NI_FIFO_SZ]
Definition: ni_device_api.h:1484
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:18807
NI_RETCODE_PARAM_INVALID_VALUE
@ NI_RETCODE_PARAM_INVALID_VALUE
Definition: ni_defs.h:451
MAGIC_P2P_VALUE
#define MAGIC_P2P_VALUE
Definition: ni_device_api_priv.c:88
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:2732
_ni_frame::sei_cc_offset
unsigned int sei_cc_offset
Definition: ni_device_api.h:2729
_ni_packet::pkt_pos
uint64_t pkt_pos
Definition: ni_device_api.h:2998
_ni_encoder_cfg_params::gopSize
int gopSize
Definition: ni_device_api.h:2398
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:1744
ni_decode_cropping_rectangle::ui16X
uint16_t ui16X
Definition: ni_device_api_priv.h:926
_ni_frame_config::options
uint16_t options
Definition: ni_device_api.h:2917
_ni_session_context::framerate
ni_framerate_t framerate
Definition: ni_device_api.h:1635
_ni_nvme_identity::fw_build_time
uint8_t fw_build_time[26]
Definition: ni_nvme.h:221
_ni_encoder_config_t::ui8preIntraHandling
uint8_t ui8preIntraHandling
Definition: ni_device_api_priv.h:665
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:1660
_ni_session_context::pext_mutex
ni_pthread_mutex_t * pext_mutex
Definition: ni_device_api.h:1701
_ni_frame::iovec_num
uint32_t iovec_num
Definition: ni_device_api.h:2810
_ni_frame_config::picture_format
uint16_t picture_format
Definition: ni_device_api.h:2916
_ni_p2p_sgl_t::ui32NumEntries
uint32_t ui32NumEntries
Definition: ni_defs.h:419
_ni_thread_arg_struct_t::device_handle
ni_device_handle_t device_handle
Definition: ni_device_api.h:1278
_ni_decoder_input_params_t::crop_whxy
int crop_whxy[NI_MAX_NUM_OF_DECODER_OUTPUTS][4]
Definition: ni_device_api.h:2599
NI_RETCODE_PARAM_ERROR_VBV_BUFFER_SIZE
@ NI_RETCODE_PARAM_ERROR_VBV_BUFFER_SIZE
Definition: ni_defs.h:458
_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:1417
SwapSW16
void SwapSW16(uint16_t *buf, uint32_t bytes)
Definition: ni_device_api_priv.c:214
_ni_encoder_cfg_params::qlevel
int qlevel
Definition: ni_device_api.h:2393
GOP_PRESET_IDX_SP
@ GOP_PRESET_IDX_SP
Definition: ni_device_api_priv.h:337
NI_MAX_SPATIAL_LAYERS
#define NI_MAX_SPATIAL_LAYERS
Definition: ni_device_api.h:183
_ni_encoder_config_t::ui8rcEnable
uint8_t ui8rcEnable
Definition: ni_device_api_priv.h:532
NI_ERRNO_LEN
#define NI_ERRNO_LEN
Definition: ni_log.h:51
NI_MAX_CONTEXTS_PER_HW_INSTANCE
#define NI_MAX_CONTEXTS_PER_HW_INSTANCE
Definition: ni_defs.h:248
_ni_session_statistic_t::ui32FramesInput
uint32_t ui32FramesInput
Definition: ni_defs.h:393
_ni_custom_gop_params::custom_gop_size
int custom_gop_size
Definition: ni_device_api.h:2105
XCODER_TEST_RECONF_OFF
@ XCODER_TEST_RECONF_OFF
Definition: ni_device_api.h:1781
_ni_session_statistic_t::ui32LastTransactionCompletionStatus
uint32_t ui32LastTransactionCompletionStatus
Definition: ni_defs.h:403
_ni_t408_config_t::internalBitDepth
int32_t internalBitDepth
Definition: ni_device_api_priv.h:381
ni_encoder_cu_info::cuSize
uint8_t cuSize
Definition: ni_device_api_priv.h:869
_ni_t408_config_t::conf_win_top
int32_t conf_win_top
Definition: ni_device_api_priv.h:388
NI_PARAM_MAX_HEIGHT
#define NI_PARAM_MAX_HEIGHT
Definition: ni_device_api.h:140
_niFrameSurface1::ui16FrameIdx
uint16_t ui16FrameIdx
Definition: ni_device_api.h:2899
_ni_encoder_cfg_params::preset_enabled
int preset_enabled
Definition: ni_device_api.h:2528
NI_VQ_SLOW
@ NI_VQ_SLOW
Definition: ni_device_api.h:1879
_ni_encoder_config_t::i8chromaQpOffset
int8_t i8chromaQpOffset
Definition: ni_device_api_priv.h:583
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:3361
_ni_encoder_cfg_params::HDR10dx2
int HDR10dx2
Definition: ni_device_api.h:2353
_ni_session_statistic_t::ui32LastErrorStatus
uint32_t ui32LastErrorStatus
Definition: ni_defs.h:405
_ni_encoder_config_t::i32cplxDecay
int32_t i32cplxDecay
Definition: ni_device_api_priv.h:637
_ni_metadata_enc_frame
Definition: ni_device_api_priv.h:241
_ni_encoder_config_t::ui8enableCompensateQp
uint8_t ui8enableCompensateQp
Definition: ni_device_api_priv.h:657
ni_decoder_output_config_t::ui8EnablePpuScaleLimit
uint8_t ui8EnablePpuScaleLimit
Definition: ni_device_api_priv.h:948
_ni_metadata_enc_frame::force_pic_qp_p
uint16_t force_pic_qp_p
Definition: ni_device_api_priv.h:252
_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:1743
NI_LOG_INFO
@ NI_LOG_INFO
Definition: ni_log.h:63
_ni_encoder_config_t::i8intraQpDelta
int8_t i8intraQpDelta
Definition: ni_device_api_priv.h:572
_ni_frameclone_desc::ui16DstIdx
uint16_t ui16DstIdx
Definition: ni_device_api.h:1392
_ni_uploader_config_t::ui8PixelFormat
uint8_t ui8PixelFormat
Definition: ni_device_api_priv.h:895
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:15923
NI_AI_FLAG_IO
#define NI_AI_FLAG_IO
Definition: ni_device_api.h:312
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:2054
CONFIG_SESSION_Read_W
#define CONFIG_SESSION_Read_W(sid)
Definition: ni_nvme.h:874
DP_LOG_OFFSET_IN_4K
#define DP_LOG_OFFSET_IN_4K
Definition: ni_nvme.h:663
INTERCOST_BITS
#define INTERCOST_BITS
Definition: ni_device_api_priv.h:798
_ni_metadata_dec_frame
Definition: ni_device_api_priv.h:228
_ni_metadata_enc_frame::use_long_term_ref
uint8_t use_long_term_ref
Definition: ni_device_api_priv.h:256
_ni_nvme_identity
Definition: ni_nvme.h:89
CONFIG_INSTANCE_SetPktSize_W
#define CONFIG_INSTANCE_SetPktSize_W(sid, instance)
Definition: ni_nvme.h:843
_ni_encoder_cfg_params::HDR10wx
int HDR10wx
Definition: ni_device_api.h:2355
_ni_session_context::io_event
ni_io_event_t * io_event
Definition: ni_device_api.h:1759
_ni_xcoder_params::source_height
int source_height
Definition: ni_device_api.h:2829
_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:845
_ni_encoder_config_t::i32srcBitDepth
int32_t i32srcBitDepth
Definition: ni_device_api_priv.h:533
_ni_encoder_cfg_params::rcQpDeltaRange
int rcQpDeltaRange
Definition: ni_device_api.h:2415
ni_encoder_cu_info::interPredIdc
uint8_t interPredIdc
Definition: ni_device_api_priv.h:875
_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:2749
_ni_metadata_enc_bstream::avg_frame_qp
uint32_t avg_frame_qp
Definition: ni_device_api_priv.h:281
_ni_encoder_cfg_params::hvsBaseMbComplexity
int hvsBaseMbComplexity
Definition: ni_device_api.h:2486
_ni_metadata_enc_bstream::ssimU
uint32_t ssimU
Definition: ni_device_api_priv.h:286
NI_LOG_ERROR
@ NI_LOG_ERROR
Definition: ni_log.h:62
_ni_encoder_config_t::i8statisticOutputLevel
int8_t i8statisticOutputLevel
Definition: ni_device_api_priv.h:644
_ni_metadata_enc_frame::enc_reconfig_data_size
uint32_t enc_reconfig_data_size
Definition: ni_device_api_priv.h:247
IS_XCODER_DEVICE_TYPE
#define IS_XCODER_DEVICE_TYPE(t)
Definition: ni_defs.h:424
_ni_encoder_config_t::ui8setLongTermCount
uint8_t ui8setLongTermCount
Definition: ni_device_api_priv.h:593
_ni_encoder_config_t::i32tolCtbRcIntra
int32_t i32tolCtbRcIntra
Definition: ni_device_api_priv.h:585
_ni_session_context::src_bit_depth
int src_bit_depth
Definition: ni_device_api.h:1523
_ni_frame::ni_pict_type
ni_pic_type_t ni_pict_type
Definition: ni_device_api.h:2724
_ni_encoder_config_t::i32bitRateBL
int32_t i32bitRateBL
Definition: ni_device_api_priv.h:531
_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:103
_ni_framerate::framerate_num
int32_t framerate_num
Definition: ni_device_api.h:712
DP_CORE
@ DP_CORE
Definition: ni_defs.h:382
_ni_packet::ssim_v
double ssim_v
Definition: ni_device_api.h:3029
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:5242
_ni_encoder_config_t::ui8stillImageDetectLevel
uint8_t ui8stillImageDetectLevel
Definition: ni_device_api_priv.h:653
_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:607
GOP_PRESET_IDX_DEFAULT
@ GOP_PRESET_IDX_DEFAULT
Definition: ni_device_api_priv.h:326
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:1052
_ni_encoder_cfg_params::HDR10maxluma
int HDR10maxluma
Definition: ni_device_api.h:2357
_ni_decoder_config_t::ui8ReduceDpbDelay
uint8_t ui8ReduceDpbDelay
Definition: ni_device_api_priv.h:970
_ni_metadata_enc_bstream::ui8StillImage
uint8_t ui8StillImage
Definition: ni_device_api_priv.h:308
_ni_network_data::linfo
ni_network_layer_info_t linfo
Definition: ni_device_api.h:1381
NI_VQ_SLOWER
@ NI_VQ_SLOWER
Definition: ni_device_api.h:1880
_niFrameSurface1::ui16session_ID
uint16_t ui16session_ID
Definition: ni_device_api.h:2900
NI_SCALER_OPCODE_OVERLAY
@ NI_SCALER_OPCODE_OVERLAY
Definition: ni_defs.h:591
NI_RETCODE_ERROR_UNSUPPORTED_FW_VERSION
@ NI_RETCODE_ERROR_UNSUPPORTED_FW_VERSION
Definition: ni_defs.h:535
_ni_instance_buf_info::buf_avail_size
uint32_t buf_avail_size
Definition: ni_device_api_priv.h:192
ni_rsrc_get_numa_node
int ni_rsrc_get_numa_node(char *device_name)
get linux numa_node
Definition: ni_rsrc_api.cpp:2850
_ni_xcoder_params::cfg_enc_params
ni_encoder_cfg_params_t cfg_enc_params
Definition: ni_device_api.h:2868
_ni_session_context::last_pkt_pos
uint64_t last_pkt_pos
Definition: ni_device_api.h:1485
_ni_t408_config_t::skipIntraTrans
int32_t skipIntraTrans
Definition: ni_device_api_priv.h:408
_ni_encoder_cfg_params::vbv_max_rate
int vbv_max_rate
Definition: ni_device_api.h:2453
_ni_xcoder_params::enableCpuAffinity
int enableCpuAffinity
Definition: ni_device_api.h:2894
_ni_gop_params::qp_factor
float qp_factor
Definition: ni_device_api.h:2016
_ni_encoder_config_t::ui16aspectRatioHeight
uint16_t ui16aspectRatioHeight
Definition: ni_device_api_priv.h:567
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:97
_ni_encoder_cfg_params::preset_index
int preset_index
Definition: ni_device_api.h:2529
_ni_xcoder_params::force_frame_type
int force_frame_type
Definition: ni_device_api.h:2843
_ni_gop_rps::ref_pic
int ref_pic
Definition: ni_device_api.h:2008
NI_PIX_FMT_NV12
@ NI_PIX_FMT_NV12
Definition: ni_device_api.h:268
_ni_encoder_config_t::ui8fillerEnable
uint8_t ui8fillerEnable
Definition: ni_device_api_priv.h:573
NI_P2P_RECV
#define NI_P2P_RECV
Definition: ni_device_api_priv.h:1054
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:6223
_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:379
_ni_frame::data_len
uint32_t data_len[NI_MAX_NUM_DATA_POINTERS]
Definition: ni_device_api.h:2761
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:13977
NI_MAX_SLICE_SIZE
#define NI_MAX_SLICE_SIZE
Definition: ni_device_api.h:223
_ni_session_context::pixel_format_changed
int pixel_format_changed
Definition: ni_device_api.h:1736
_ni_frame::extra_data_len
unsigned int extra_data_len
Definition: ni_device_api.h:2754
_ni_encoder_config_t::ui8adaptiveCrfMode
uint8_t ui8adaptiveCrfMode
Definition: ni_device_api_priv.h:674
DP_IPC_PASSTHRU
#define DP_IPC_PASSTHRU
Definition: ni_device_api_priv.c:195
_ni_encoder_cfg_params::intra_qp
int intra_qp
Definition: ni_device_api.h:2443
_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:398
_ni_encoder_cfg_params::enable_rate_control
int enable_rate_control
Definition: ni_device_api.h:2440
_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:579
INTRA_PART_MODE_BYTE_OFFSET
#define INTRA_PART_MODE_BYTE_OFFSET
Definition: ni_device_api_priv.h:746
CONFIG_SESSION_Write_W
#define CONFIG_SESSION_Write_W(sid)
Definition: ni_nvme.h:876
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:2494
PresetConfig
Definition: ni_device_api_priv.c:105
NI_PIXEL_PLANAR_MAX
@ NI_PIXEL_PLANAR_MAX
Definition: ni_device_api.h:937
_ni_decoder_input_params_t::semi_planar
int semi_planar[NI_MAX_NUM_OF_DECODER_OUTPUTS]
Definition: ni_device_api.h:2597
_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:2625
_ni_encoder_config_t::ui16hdr10_wx
uint16_t ui16hdr10_wx
Definition: ni_device_api_priv.h:603
_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:416
_ni_encoder_config_t::ui16hdr10_dx1
uint16_t ui16hdr10_dx1
Definition: ni_device_api_priv.h:599
_ni_decoder_input_params_t::hwframes
int hwframes
Definition: ni_device_api.h:2591
NI_POOL_TYPE_NORMAL
@ NI_POOL_TYPE_NORMAL
Definition: ni_device_api.h:506
NI_DEVICE_TYPE_AI
@ NI_DEVICE_TYPE_AI
Definition: ni_defs.h:363
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:14572
_ni_ai_config_t
Definition: ni_device_api_priv.h:977
NI_RETCODE_PARAM_ERROR_DECODING_REFRESH_TYPE
@ NI_RETCODE_PARAM_ERROR_DECODING_REFRESH_TYPE
Definition: ni_defs.h:498
_ni_encoder_config_t::ui8PixelFormat
uint8_t ui8PixelFormat
Definition: ni_device_api_priv.h:570
_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:2893
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:5462
ni_network_buffer_info_t
struct _ni_network_buffer_info ni_network_buffer_info_t
_ni_uploader_config_t::ui32lumaLinesize
uint32_t ui32lumaLinesize
Definition: ni_device_api_priv.h:898
NI_RETCODE_PARAM_ERROR_CUSIZE_MODE_16X16_EN
@ NI_RETCODE_PARAM_ERROR_CUSIZE_MODE_16X16_EN
Definition: ni_defs.h:501
_ni_encoder_cfg_params::multicoreJointMode
int multicoreJointMode
Definition: ni_device_api.h:2364
CHECK_ERR_RC
#define CHECK_ERR_RC(ctx, rc, info, opcode, type, hw_id, inst_id, opt)
Definition: ni_device_api_priv.c:817
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:1931
FP_CORE
@ FP_CORE
Definition: ni_defs.h:384
_ni_hw_capability::max_video_height
uint16_t max_video_height
Definition: ni_device_api.h:1156
_ni_xcoder_params::enable_ai_enhance
int enable_ai_enhance
Definition: ni_device_api.h:2884
NETINT_PCI_VENDOR_ID
#define NETINT_PCI_VENDOR_ID
Definition: ni_defs.h:129
ni_calculate_sha256
void ni_calculate_sha256(const uint8_t aui8Data[], size_t ui32DataLength, uint8_t aui8Hash[])
Definition: ni_util.c:4176
NI_RETCODE_ERROR_NVME_CMD_FAILED
@ NI_RETCODE_ERROR_NVME_CMD_FAILED
Definition: ni_defs.h:446
_ni_t408_config_t::cu16MergeDeltaRate
int32_t cu16MergeDeltaRate
Definition: ni_device_api_priv.h:484
_ni_thread_arg_struct_t::device_type
uint32_t device_type
Definition: ni_device_api.h:1277
NI_HDR10P_SEI_BYTE3
#define NI_HDR10P_SEI_BYTE3
Definition: ni_device_api.h:469
_ni_decoder_config_t::ui8MCMode
uint8_t ui8MCMode
Definition: ni_device_api_priv.h:961
_ni_decoder_input_params_t
Definition: ni_device_api.h:2536
_ni_t408_config_t::nrCbEnable
uint32_t nrCbEnable
Definition: ni_device_api_priv.h:432
_ni_encoder_config_t::ui32vbvMinRate
uint32_t ui32vbvMinRate
Definition: ni_device_api_priv.h:646
_ni_encoder_config_t::i32frameRateInfo
int32_t i32frameRateInfo
Definition: ni_device_api_priv.h:522
_ni_t408_config_t::bgThrDiff
uint32_t bgThrDiff
Definition: ni_device_api_priv.h:456
GOP_PRESET_IDX_CUSTOM
@ GOP_PRESET_IDX_CUSTOM
Definition: ni_device_api_priv.h:328
NI_CUS_ROI_DISABLE
@ NI_CUS_ROI_DISABLE
Definition: ni_device_api.h:294
_ni_encoder_config_t::ui8AiEnhanceMode
uint8_t ui8AiEnhanceMode
Definition: ni_device_api_priv.h:619
QUERY_INSTANCE_EOS_R
#define QUERY_INSTANCE_EOS_R(sid, instance)
Definition: ni_nvme.h:721
_ni_t408_config_t::dependSliceMode
int32_t dependSliceMode
Definition: ni_device_api_priv.h:394
_ni_encoder_cfg_params::HDR10dy0
int HDR10dy0
Definition: ni_device_api.h:2350
_ni_load_query::fw_video_mem_usage
uint32_t fw_video_mem_usage
Definition: ni_device_api.h:1217
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:4900
_ni_encoder_cfg_params::enable_dynamic_8x8_merge
int enable_dynamic_8x8_merge
Definition: ni_device_api.h:2425
_ni_t408_config_t::profile
int32_t profile
Definition: ni_device_api_priv.h:378
_ni_scaler_params_t::nb_inputs
int nb_inputs
Definition: ni_device_api.h:2661
_ni_encoder_cfg_params::ctbRcMode
int ctbRcMode
Definition: ni_device_api.h:2397
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:506
_ni_thread_arg_struct_t::keep_alive_timeout
uint32_t keep_alive_timeout
Definition: ni_device_api.h:1282
NI_MEM_PAGE_ALIGNMENT
#define NI_MEM_PAGE_ALIGNMENT
Definition: ni_defs.h:263
NI_LOG_TRACE
@ NI_LOG_TRACE
Definition: ni_log.h:65
_ni_encoder_cfg_params::long_term_ref_interval
int long_term_ref_interval
Definition: ni_device_api.h:2366
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:1252
_ni_frame::end_of_stream
uint32_t end_of_stream
Definition: ni_device_api.h:2705
_ni_session_context::network_data
ni_network_data_t * network_data
Definition: ni_device_api.h:1709
NI_T35_SEI_HDR10_PLUS
@ NI_T35_SEI_HDR10_PLUS
Definition: ni_device_api_priv.c:94
_ni_session_context::headers_length
uint32_t headers_length
Definition: ni_device_api.h:1762
_ni_frame_config::orientation
uint8_t orientation
Definition: ni_device_api.h:2926
NI_HWDESC_UNIFIED_MEMBIN_SIZE
#define NI_HWDESC_UNIFIED_MEMBIN_SIZE
Definition: ni_device_api_priv.h:1047
CONFIG_SESSION_SWVersion_W
#define CONFIG_SESSION_SWVersion_W(sid)
Definition: ni_nvme.h:889
INTER_PRED_IDC_BIT_POSITION
#define INTER_PRED_IDC_BIT_POSITION
Definition: ni_device_api_priv.h:712
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:1159
NI_CODEC_HW_DOWNLOAD
@ NI_CODEC_HW_DOWNLOAD
Definition: ni_device_api.h:955
_ni_encoder_config_t::ui8colorSpace
uint8_t ui8colorSpace
Definition: ni_device_api_priv.h:565
_ni_metadata_enc_bstream::frame_size
uint16_t frame_size
Definition: ni_device_api_priv.h:293
_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:4686
_niFrameSurface1::encoding_type
int8_t encoding_type
Definition: ni_device_api.h:2906
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:15613
_ni_encoder_config_t::i32hwframes
int32_t i32hwframes
Definition: ni_device_api_priv.h:546
_ni_frameclone_desc::ui16SrcIdx
uint16_t ui16SrcIdx
Definition: ni_device_api.h:1391
_ni_xcoder_params::hwframes
int hwframes
Definition: ni_device_api.h:2877
_ni_session_context::prev_size
int prev_size
Definition: ni_device_api.h:1538
_ni_encoder_cfg_params::newRcEnable
int newRcEnable
Definition: ni_device_api.h:2466
_ni_session_context::device_handle
ni_device_handle_t device_handle
Definition: ni_device_api.h:1493
_ni_session_context::p_session_config
void * p_session_config
Definition: ni_device_api.h:1504
NI_CODEC_FORMAT_AV1
@ NI_CODEC_FORMAT_AV1
Definition: ni_device_api.h:929
NI_MAX_TX_RETRIES
#define NI_MAX_TX_RETRIES
Definition: ni_device_api.h:67
_ni_encoder_cfg_params::skip_frame_enable
int skip_frame_enable
Definition: ni_device_api.h:2493
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:1336
_ni_frame::sei_hdr_plus_len
unsigned int sei_hdr_plus_len
Definition: ni_device_api.h:2738
_ni_xcoder_params::preset
int preset
Definition: ni_device_api.h:2817
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:1068
_ni_metadata_dec_frame::sei_header
uint32_t sei_header
Definition: ni_device_api_priv.h:232
_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:587
_ni_session_context::dec_fme_buf_pool
ni_buf_pool_t * dec_fme_buf_pool
Definition: ni_device_api.h:1590
NI_NOPTS_VALUE
#define NI_NOPTS_VALUE
Definition: ni_device_api.h:52
AVC_INTRA_PART_MODE_BYTE_OFFSET
#define AVC_INTRA_PART_MODE_BYTE_OFFSET
Definition: ni_device_api_priv.h:852
_ni_metadata_enc_bstream_rev61::avg_frame_qp
uint32_t avg_frame_qp
Definition: ni_device_api_priv.h:270
_ni_custom_sei_set
Definition: ni_device_api.h:1139
_ni_encoder_cfg_params::entropy_coding_mode
int entropy_coding_mode
Definition: ni_device_api.h:2382
_ni_metadata_common::frame_offset
uint64_t frame_offset
Definition: ni_device_api_priv.h:216
AVC_INTRA_PART_MODE_BIT_POSITION
#define AVC_INTRA_PART_MODE_BIT_POSITION
Definition: ni_device_api_priv.h:853
_ni_packet::p_data
void * p_data
Definition: ni_device_api.h:3005
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:1157
_ni_session_data_io
Definition: ni_device_api.h:3034
PresetConfig::vbvBufferSize
int vbvBufferSize
Definition: ni_device_api_priv.c:109
_ni_t408_config_t::conf_win_left
int32_t conf_win_left
Definition: ni_device_api_priv.h:390
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:10439
_ni_encoder_config_t::ui8temporalLayersEnable
uint8_t ui8temporalLayersEnable
Definition: ni_device_api_priv.h:618
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:13776
_ni_metadata_enc_bstream::reconLumaSize
uint32_t reconLumaSize
Definition: ni_device_api_priv.h:300
NI_RETCODE_NVME_SC_VPU_RECOVERY
@ NI_RETCODE_NVME_SC_VPU_RECOVERY
Definition: ni_defs.h:561
QUERY_INSTANCE_RBUFF_SIZE_BUSY_R
#define QUERY_INSTANCE_RBUFF_SIZE_BUSY_R(sid, instance)
Definition: ni_nvme.h:729
_ni_encoder_cfg_params::ltrFirstGap
int ltrFirstGap
Definition: ni_device_api.h:2362
TP_CORE
@ TP_CORE
Definition: ni_defs.h:383
_ni_encoder_frame_params::force_picture_type
uint16_t force_picture_type
Definition: ni_device_api_priv.h:199
INTRA_PART_MODE_BITS
#define INTRA_PART_MODE_BITS
Definition: ni_device_api_priv.h:748
_ni_content_light_level_info_bytes::max_pic_average_light_level
uint16_t max_pic_average_light_level
Definition: ni_device_api.h:1091
_ni_encoder_cfg_params::ltrRefQpOffset
int ltrRefQpOffset
Definition: ni_device_api.h:2361
_ni_t408_config_t::chromaCrQpOffset
int32_t chromaCrQpOffset
Definition: ni_device_api_priv.h:427
_ni_metadata_common::frame_type
uint8_t frame_type
Definition: ni_device_api_priv.h:221
NI_PIX_FMT_YUV420P10LE
@ NI_PIX_FMT_YUV420P10LE
Definition: ni_device_api.h:267
_ni_frame_config::rgba_color
uint32_t rgba_color
Definition: ni_device_api.h:2922
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:17884
_ni_metadata_common::pkt_delay_cnt
uint8_t pkt_delay_cnt
Definition: ni_device_api_priv.h:224
NI_XCODER_REVISION_API_MAJOR_VER_IDX
#define NI_XCODER_REVISION_API_MAJOR_VER_IDX
Definition: ni_defs.h:99
_ni_t408_config_t::slice_arg
int32_t slice_arg
Definition: ni_device_api_priv.h:502
_ni_encoder_cfg_params::enable_transform_8x8
int enable_transform_8x8
Definition: ni_device_api.h:2431
_ni_session_context::sender_handle
ni_device_handle_t sender_handle
Definition: ni_device_api.h:1499
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:15805
_ni_scaler_config::filterblit
uint8_t filterblit
Definition: ni_device_api_priv.h:1011
CONFIG_INSTANCE_SetP2P_W
#define CONFIG_INSTANCE_SetP2P_W(sid, instance)
Definition: ni_nvme.h:865
_ni_session_statistic_t::ui32FramesOutput
uint32_t ui32FramesOutput
Definition: ni_defs.h:396
_ni_dec_mastering_display_colour_volume_bytes::min_display_mastering_luminance
uint32_t min_display_mastering_luminance
Definition: ni_device_api.h:1081
NI_MAX_GOP_NUM
#define NI_MAX_GOP_NUM
Definition: ni_device_api.h:58
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:495
_ni_t408_config_t::monochromeEnable
uint32_t monochromeEnable
Definition: ni_device_api_priv.h:451
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:6903
_ni_encoder_cfg_params::enable_smooth_crf
int enable_smooth_crf
Definition: ni_device_api.h:2512
SwapSW32
void SwapSW32(uint32_t *buf, uint32_t bytes)
Definition: ni_device_api_priv.c:226
_ni_encoder_config_t::i8crfDecimal
int8_t i8crfDecimal
Definition: ni_device_api_priv.h:642
MAX_BDF_LEN
#define MAX_BDF_LEN
Definition: ni_device_api_priv.c:1067
INTRACOST_BITS
#define INTRACOST_BITS
Definition: ni_device_api_priv.h:793
_ni_session_context::keep_alive_timeout
uint32_t keep_alive_timeout
Definition: ni_device_api.h:1552
_ni_encoder_config_t::ui8bitstreamFormat
uint8_t ui8bitstreamFormat
Definition: ni_device_api_priv.h:517
_ni_session_statistic_t::ui8AdditionalFramesDelay
uint8_t ui8AdditionalFramesDelay
Definition: ni_defs.h:410
_ni_encoder_cfg_params::totalCuTreeDepth
int totalCuTreeDepth
Definition: ni_device_api.h:2519
NI_HDR10P_SEI_BYTE2
#define NI_HDR10P_SEI_BYTE2
Definition: ni_device_api.h:468
NI_RETCODE_PARAM_ERROR_CUSIZE_MODE_8X8_EN
@ NI_RETCODE_PARAM_ERROR_CUSIZE_MODE_8X8_EN
Definition: ni_defs.h:500
_ni_encoder_cfg_params::customize_roi_qp_level
int customize_roi_qp_level
Definition: ni_device_api.h:2507
_ni_encoder_config_t::ui8enableTimecode
uint8_t ui8enableTimecode
Definition: ni_device_api_priv.h:659
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:17284
NI_SCALER_OPCODE_STACK
@ NI_SCALER_OPCODE_STACK
Definition: ni_defs.h:592
_ni_packet::scene_change_detected
uint8_t scene_change_detected
Definition: ni_device_api.h:3031
_ni_encoder_cfg_params
Definition: ni_device_api.h:2113
AVC_INTER_PRED_IDC_BITS
#define AVC_INTER_PRED_IDC_BITS
Definition: ni_device_api_priv.h:819
GC620_BGRX8888
#define GC620_BGRX8888
Definition: ni_device_api.h:205
NI_RETCODE_PARAM_WARN
@ NI_RETCODE_PARAM_WARN
Definition: ni_defs.h:540
_ni_encoder_config_t::ui8repeatHeaders
uint8_t ui8repeatHeaders
Definition: ni_device_api_priv.h:555
_ni_encoder_cfg_params::EnableAUD
int EnableAUD
Definition: ni_device_api.h:2376
_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:1482
_ni_encoder_cfg_params::spatial_layers
int spatial_layers
Definition: ni_device_api.h:2514
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:4272
FRAME_CHUNK_INDEX_SIZE
#define FRAME_CHUNK_INDEX_SIZE
Definition: ni_device_api_priv.h:1042
ni_usleep
void ni_usleep(int64_t usec)
Definition: ni_util.c:362
_ni_encoder_config_t::ui8useLowDelayPocType
uint8_t ui8useLowDelayPocType
Definition: ni_device_api_priv.h:558
_ni_encoder_cfg_params::scene_change_detect_level
int scene_change_detect_level
Definition: ni_device_api.h:2510
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:18002
CONFIG_SESSION_KeepAlive_W
#define CONFIG_SESSION_KeepAlive_W(sid)
Definition: ni_nvme.h:872
_ni_session_context::device_type
uint32_t device_type
Definition: ni_device_api.h:1515
_ni_decoder_config_t::ui32MaxPktSize
uint32_t ui32MaxPktSize
Definition: ni_device_api_priv.h:965
niFrameSurface1_t
struct _niFrameSurface1 niFrameSurface1_t
NI_RETCODE_PARAM_ERROR_MX_DELTA_QP
@ NI_RETCODE_PARAM_ERROR_MX_DELTA_QP
Definition: ni_defs.h:477
_ni_t408_config_t::intra_period
int32_t intra_period
Definition: ni_device_api_priv.h:387
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:10042
NI_ENC_MIN_RESOLUTION_WIDTH
#define NI_ENC_MIN_RESOLUTION_WIDTH
Definition: ni_device_api.h:91
QUERY_INSTANCE_CUR_STATUS_INFO_R
#define QUERY_INSTANCE_CUR_STATUS_INFO_R(sid, instance)
Definition: ni_nvme.h:712
NI_AI_HW_ALIGN_SIZE
#define NI_AI_HW_ALIGN_SIZE
Definition: ni_device_api_priv.h:1483
ni_encoder_cu_info::intraPartMode
uint8_t intraPartMode
Definition: ni_device_api_priv.h:879
ni_decoder_output_config_t::ui8SemiPlanarEnabled
uint8_t ui8SemiPlanarEnabled
Definition: ni_device_api_priv.h:942
_ni_t408_config_t::minQpB
int32_t minQpB
Definition: ni_device_api_priv.h:493
_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:753
GC620_RGBA8888
#define GC620_RGBA8888
Definition: ni_device_api.h:203
_ni_frame::force_key_frame
int force_key_frame
Definition: ni_device_api.h:2721
NI_GET_MAX_HWDESC_FRAME_INDEX
#define NI_GET_MAX_HWDESC_FRAME_INDEX(x)
Definition: ni_defs.h:292
_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:2662
LRETURN
#define LRETURN
Definition: ni_defs.h:337
INTRACOST_BIT_POSITION
#define INTRACOST_BIT_POSITION
Definition: ni_device_api_priv.h:792
NI_PIXEL_PLANAR_FORMAT_SEMIPLANAR
@ NI_PIXEL_PLANAR_FORMAT_SEMIPLANAR
Definition: ni_device_api.h:934
calculate_psnr
void calculate_psnr(ni_session_context_t *p_ctx, ni_packet_t *p_packet)
Definition: ni_device_api_priv.c:476
_ni_t408_config_t::numUnitsInTick
uint32_t numUnitsInTick
Definition: ni_device_api_priv.h:422
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:2347
_ni_metadata_enc_frame::force_headers
uint16_t force_headers
Definition: ni_device_api_priv.h:254
_ni_scaler_params_t::enable_scaler_params
bool enable_scaler_params
Definition: ni_device_api.h:2664
PresetConfig::rdoLevel
int rdoLevel
Definition: ni_device_api_priv.c:106
_ni_encoder_config_t::ui8EnableRdoQuant
uint8_t ui8EnableRdoQuant
Definition: ni_device_api_priv.h:554
_ni_t408_config_t::pu08IntraPlanarDeltaRate
int32_t pu08IntraPlanarDeltaRate
Definition: ni_device_api_priv.h:470
_ni_frameclone_desc::ui32Offset
uint32_t ui32Offset
Definition: ni_device_api.h:1393
_ni_encoder_config_t::ui16hdr10_dy1
uint16_t ui16hdr10_dy1
Definition: ni_device_api_priv.h:600
NI_HDR10P_SEI_BYTE0
#define NI_HDR10P_SEI_BYTE0
Definition: ni_device_api.h:466
_ni_content_light_level_info_bytes::max_content_light_level
uint16_t max_content_light_level
Definition: ni_device_api.h:1090
_ni_packet::video_height
uint32_t video_height
Definition: ni_device_api.h:3002
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:8441
_ni_session_context::buffer_pool
ni_queue_buffer_pool_t * buffer_pool
Definition: ni_device_api.h:1589
_ni_xcoder_params::chroma_linesize
int chroma_linesize
Definition: ni_device_api.h:2888
NI_MINIMUM_CROPPED_LENGTH
#define NI_MINIMUM_CROPPED_LENGTH
Definition: ni_device_api_priv.h:918
_ni_xcoder_params::minFramesDelay
int minFramesDelay
Definition: ni_device_api.h:2891
_ni_xcoder_params::sar_num
int sar_num
Definition: ni_device_api.h:2862
_ni_metadata_enc_frame::metadata_common
ni_metadata_common_t metadata_common
Definition: ni_device_api_priv.h:243
_ni_t408_config_t::pu16IntraAngleDeltaRate
int32_t pu16IntraAngleDeltaRate
Definition: ni_device_api_priv.h:475
_ni_t408_config_t::constIntraPredFlag
int32_t constIntraPredFlag
Definition: ni_device_api_priv.h:383
_ni_metadata_enc_frame::frame_roi_map_size
uint32_t frame_roi_map_size
Definition: ni_device_api_priv.h:245
NI_RETCODE_PARAM_ERROR_HVS_QP_EN
@ NI_RETCODE_PARAM_ERROR_HVS_QP_EN
Definition: ni_defs.h:471
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:7181
_ni_packet
Definition: ni_device_api.h:2993
NI_DATA_BUFFER_LEN
#define NI_DATA_BUFFER_LEN
Definition: ni_nvme.h:624
_ni_session_statistic_t::ui32RdBufAvailSize
uint32_t ui32RdBufAvailSize
Definition: ni_defs.h:391
_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:1056
_ni_session_context::frame_num
uint64_t frame_num
Definition: ni_device_api.h:1562
_ni_frame::sei_cc_len
unsigned int sei_cc_len
Definition: ni_device_api.h:2730
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:262
_ni_encoder_cfg_params::HDR10Enable
int HDR10Enable
Definition: ni_device_api.h:2348
CONFIG_INSTANCE_SetEncFramePara_W
#define CONFIG_INSTANCE_SetEncFramePara_W(sid, instance)
Definition: ni_nvme.h:841
CU_SIZE_BITS
#define CU_SIZE_BITS
Definition: ni_device_api_priv.h:698
_ni_queue_t::count
uint32_t count
Definition: ni_device_api.h:1330
_ni_encoder_cfg_params::intra_qp_delta
int intra_qp_delta
Definition: ni_device_api.h:2444
_ni_session_context::ori_luma_linesize
int ori_luma_linesize
Definition: ni_device_api.h:1711
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:14018
_ni_xcoder_params::ai_enhance_level
int ai_enhance_level
Definition: ni_device_api.h:2889
_ni_network_layer_params_t::sizes
uint32_t sizes[6]
Definition: ni_device_api.h:1343
_ni_t408_config_t::enable_mb_level_rc
int32_t enable_mb_level_rc
Definition: ni_device_api_priv.h:505
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:1615
_ni_network_layer_offset::offset
int32_t offset
Definition: ni_device_api.h:1374
_ni_t408_config_t::cu16IntraDeltaRate
int32_t cu16IntraDeltaRate
Definition: ni_device_api_priv.h:482
_ni_instance_mgr_stream_info::is_flushed
uint16_t is_flushed
Definition: ni_device_api_priv.h:129
CU_MODE_BYTE_OFFSET
#define CU_MODE_BYTE_OFFSET
Definition: ni_device_api_priv.h:701
_ni_encoder_cfg_params::colorDescPresent
int colorDescPresent
Definition: ni_device_api.h:2386
_ni_t408_config_t::wppEnable
int32_t wppEnable
Definition: ni_device_api_priv.h:402
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:837
_ni_overall_load_query::overall_fw_model_load
uint32_t overall_fw_model_load
Definition: ni_device_api.h:1239
_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:959
_ni_session_context::last_gop_size
int last_gop_size
Definition: ni_device_api.h:1722
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:2746
_ni_encoder_cfg_params::tolCtbRcInter
float tolCtbRcInter
Definition: ni_device_api.h:2404
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:19141
_ni_frame::p_data
uint8_t * p_data[NI_MAX_NUM_DATA_POINTERS]
Definition: ni_device_api.h:2760
_ni_encoder_cfg_params::custom_gop_params
ni_custom_gop_params_t custom_gop_params
Definition: ni_device_api.h:2330
CU_SIZE_BYTE_OFFSET
#define CU_SIZE_BYTE_OFFSET
Definition: ni_device_api_priv.h:696
_ni_encoder_cfg_params::HDR10dx0
int HDR10dx0
Definition: ni_device_api.h:2349
NVME_LOG_OFFSET_IN_4K
#define NVME_LOG_OFFSET_IN_4K
Definition: ni_nvme.h:661
_ni_xcoder_params::zerocopy_mode
int zerocopy_mode
Definition: ni_device_api.h:2886
NI_EC_POLICY_DEFAULT
#define NI_EC_POLICY_DEFAULT
Definition: ni_device_api.h:338
_ni_xcoder_params::sar_denom
int sar_denom
Definition: ni_device_api.h:2863
_ni_t408_config_t::cu32IntraDeltaRate
int32_t cu32IntraDeltaRate
Definition: ni_device_api_priv.h:485
_ni_iovec::size
uint32_t size
Definition: ni_device_api.h:2694
ni_decoder_output_config_t::sCroppingRectable
ni_decode_cropping_rectangle sCroppingRectable
Definition: ni_device_api_priv.h:950
_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:2619
NI_RETCODE_PARAM_ERROR_MAXNUMMERGE
@ NI_RETCODE_PARAM_ERROR_MAXNUMMERGE
Definition: ni_defs.h:491
_ni_device_capability::fw_build_time
uint8_t fw_build_time[26]
Definition: ni_device_api.h:1182
_ni_encoder_config_t::ui32sourceEndian
uint32_t ui32sourceEndian
Definition: ni_device_api_priv.h:540
_ni_decoder_config_t::asOutputConfig
ni_decoder_output_config_t asOutputConfig[NI_MAX_NUM_OF_DECODER_OUTPUTS]
Definition: ni_device_api_priv.h:966
_ni_iovec::ptr
void * ptr
Definition: ni_device_api.h:2695
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:13382
_ni_t408_config_t::nrIntraWeightCr
uint32_t nrIntraWeightCr
Definition: ni_device_api_priv.h:438
_ni_session_context::pkt_index
int pkt_index
Definition: ni_device_api.h:1481
_ni_encoder_cfg_params::disableBframeRdoq
int disableBframeRdoq
Definition: ni_device_api.h:2502
NI_CODEC_FORMAT_VP9
@ NI_CODEC_FORMAT_VP9
Definition: ni_device_api.h:927
GOP_PRESET_IDX_HIERARCHICAL_IPPPP
@ GOP_PRESET_IDX_HIERARCHICAL_IPPPP
Definition: ni_device_api_priv.h:339
_ni_t408_config_t::nrCrEnable
uint32_t nrCrEnable
Definition: ni_device_api_priv.h:433
ni_core_type_t
ni_core_type_t
Definition: ni_defs.h:377
NI_DEVICE_TYPE_SCALER
@ NI_DEVICE_TYPE_SCALER
Definition: ni_defs.h:362
_ni_segment::ui32Size
uint32_t ui32Size
Definition: ni_device_api_priv.h:985
_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:2405
_ni_t408_config_t::cu08MergeDeltaRate
int32_t cu08MergeDeltaRate
Definition: ni_device_api_priv.h:481
_ni_session_statistic_t::ui32LastErrorTransactionId
uint32_t ui32LastErrorTransactionId
Definition: ni_defs.h:404
_ni_instance_mgr_stream_info::picture_height
uint16_t picture_height
Definition: ni_device_api_priv.h:125
NI_VQ_VERYFAST
@ NI_VQ_VERYFAST
Definition: ni_device_api.h:1875
XCODER_MAX_ENC_PIC_HEIGHT
#define XCODER_MAX_ENC_PIC_HEIGHT
Definition: ni_util.h:115
_ni_iovec
Definition: ni_device_api.h:2693
INTRA_PART_MODE_BIT_POSITION
#define INTRA_PART_MODE_BIT_POSITION
Definition: ni_device_api_priv.h:747
NI_DDR_PRIORITY_RESET
@ NI_DDR_PRIORITY_RESET
Definition: ni_device_api.h:1865
_ni_frame::roi_len
unsigned int roi_len
Definition: ni_device_api.h:2750
_ni_encoder_config_t::ui8gopSize
uint8_t ui8gopSize
Definition: ni_device_api_priv.h:557
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:1249
_ni_session_context::last_pts_interval
int64_t last_pts_interval
Definition: ni_device_api.h:1671
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:3944
NI_RETCODE_PARAM_ERROR_CONF_WIN_R
@ NI_RETCODE_PARAM_ERROR_CONF_WIN_R
Definition: ni_defs.h:485
_ni_metadata_common::crop_top
uint16_t crop_top
Definition: ni_device_api_priv.h:212
NI_MAX_SEGMENT_NUM
#define NI_MAX_SEGMENT_NUM
Definition: ni_device_api_priv.h:1001
NI_ERRNO
#define NI_ERRNO
Definition: ni_defs.h:229
_ni_frame_config::picture_height
uint16_t picture_height
Definition: ni_device_api.h:2915
NI_RETCODE_PARAM_ERROR_WIDTH_TOO_BIG
@ NI_RETCODE_PARAM_ERROR_WIDTH_TOO_BIG
Definition: ni_defs.h:508
ni_set_cpu_affinity
ni_retcode_t ni_set_cpu_affinity(ni_session_context_t *p_ctx)
set cpu affinity based on numa node
Definition: ni_device_api_priv.c:19226
_ni_encoder_config_t::ui8LowDelay
uint8_t ui8LowDelay
Definition: ni_device_api_priv.h:592
_ni_frame::sei_hdr_plus_offset
unsigned int sei_hdr_plus_offset
Definition: ni_device_api.h:2737
_ni_encoder_cfg_params::enable_dynamic_16x16_merge
int enable_dynamic_16x16_merge
Definition: ni_device_api.h:2426
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:531
_ni_encoder_config_t::ui32cropHeight
uint32_t ui32cropHeight
Definition: ni_device_api_priv.h:624
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:473
_ni_t408_config_t::enable_transform_8x8
int32_t enable_transform_8x8
Definition: ni_device_api_priv.h:500
_ni_session_context::hw_action
int hw_action
Definition: ni_device_api.h:1640
_ni_input_frame::video_height
uint32_t video_height
Definition: ni_device_api.h:1415
_ni_encoder_config_t::niParamT408
ni_t408_config_t niParamT408
Definition: ni_device_api_priv.h:537
nvme_admin_cmd_xcoder_query
@ nvme_admin_cmd_xcoder_query
Definition: ni_nvme.h:320
INTRACOST_BYTE_OFFSET
#define INTRACOST_BYTE_OFFSET
Definition: ni_device_api_priv.h:791
_ni_session_context
Definition: ni_device_api.h:1435
_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:1446
QUERY_DETAIL_GET_STATUS_V1_R
#define QUERY_DETAIL_GET_STATUS_V1_R(instance)
Definition: ni_nvme.h:787
_ni_encoder_cfg_params::iframe_size_ratio
int iframe_size_ratio
Definition: ni_device_api.h:2498
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:16476
NI_PIX_FMT_P010LE
@ NI_PIX_FMT_P010LE
Definition: ni_device_api.h:269
ni_memfree
#define ni_memfree(p_memptr)
Definition: ni_util.h:410
_ni_session_context::bus
unsigned short bus
Definition: ni_device_api.h:1651
_ni_thread_arg_struct_t::hw_id
int hw_id
Definition: ni_device_api.h:1273
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:10748
NI_CODEC_HW_UPLOAD
@ NI_CODEC_HW_UPLOAD
Definition: ni_device_api.h:956
_niFrameSurface1
Definition: ni_device_api.h:2897
NI_EC_ERR_THRESHOLD_DEFAULT
#define NI_EC_ERR_THRESHOLD_DEFAULT
Definition: ni_device_api.h:339
NI_CODEC_HW_ENABLE
@ NI_CODEC_HW_ENABLE
Definition: ni_device_api.h:954
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:488
_ni_metadata_enc_bstream_rev61::frame_type
uint32_t frame_type
Definition: ni_device_api_priv.h:267
_ni_t408_config_t::cu08InterDeltaRate
int32_t cu08InterDeltaRate
Definition: ni_device_api_priv.h:480
NI_PARAM_AV1_MAX_HEIGHT
#define NI_PARAM_AV1_MAX_HEIGHT
Definition: ni_device_api.h:149
ni_decode_cropping_rectangle::ui16H
uint16_t ui16H
Definition: ni_device_api_priv.h:929
_ni_session_context::last_dts_interval
int64_t last_dts_interval
Definition: ni_device_api.h:1670
ni_nvme_send_read_cmd
int32_t ni_nvme_send_read_cmd(ni_device_handle_t handle, ni_event_handle_t event_handle, void *p_data, uint32_t data_len, uint32_t lba)
Compose an io read command.
Definition: ni_nvme.c:554
_ni_metadata_dec_frame::metadata_common
ni_metadata_common_t metadata_common
Definition: ni_device_api_priv.h:230
_ni_ddr_priority_config
Definition: ni_device_api_priv.h:1019
_ni_encoder_cfg_params::adaptiveCuTree
int adaptiveCuTree
Definition: ni_device_api.h:2520
_ni_encoder_config_t::ui32QLevel
uint32_t ui32QLevel
Definition: ni_device_api_priv.h:582
_ni_t408_config_t::pu16DeltaRate
int32_t pu16DeltaRate
Definition: ni_device_api_priv.h:465
_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:1521
_ni_frame
Definition: ni_device_api.h:2698
_ni_decoder_config_t::ui8DisablePictureReordering
uint8_t ui8DisablePictureReordering
Definition: ni_device_api_priv.h:962
_ni_metadata_dec_frame::sei_number
uint16_t sei_number
Definition: ni_device_api_priv.h:235
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:4934
_ni_t408_config_t::intraNxNEnable
int32_t intraNxNEnable
Definition: ni_device_api_priv.h:410
_ni_network_data::input_num
uint32_t input_num
Definition: ni_device_api.h:1379
_ni_packet::sent_size
int sent_size
Definition: ni_device_api.h:3007
_ni_encoder_cfg_params::HDR10dy1
int HDR10dy1
Definition: ni_device_api.h:2352
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:819
CONFIG_INSTANCE_SetScalerPara_W
#define CONFIG_INSTANCE_SetScalerPara_W(sid, instance)
Definition: ni_nvme.h:814
NI_PIX_FMT_RGBA
@ NI_PIX_FMT_RGBA
Definition: ni_device_api.h:270
_ni_encoder_cfg_params::conf_win_bottom
int conf_win_bottom
Definition: ni_device_api.h:2373
_ni_metadata_enc_bstream_rev61
Definition: ni_device_api_priv.h:264
_ni_decoder_input_params_t::scale_round
int scale_round[NI_MAX_NUM_OF_DECODER_OUTPUTS]
Definition: ni_device_api.h:2627
_ni_overall_load_query::overall_instance_count
uint32_t overall_instance_count
Definition: ni_device_api.h:1240
_ni_session_context::roi_len
uint32_t roi_len
Definition: ni_device_api.h:1527
_ni_encoder_cfg_params::linkFrameMaxIntraRatio
int linkFrameMaxIntraRatio
Definition: ni_device_api.h:2524
NI_DDR_PRIORITY_NONE
@ NI_DDR_PRIORITY_NONE
Definition: ni_device_api.h:1864
_ni_encoder_cfg_params::profile
int profile
Definition: ni_device_api.h:2323
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:662
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:2389
_ni_session_context::max_frame_delay
int max_frame_delay
Definition: ni_device_api.h:1725
_ni_gop_params::rps
ni_gop_rps_t rps[NI_MAX_REF_PIC]
Definition: ni_device_api.h:2020
CONFIG_INSTANCE_SetEOS_W
#define CONFIG_INSTANCE_SetEOS_W(sid, instance)
Definition: ni_nvme.h:805
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:237
NI_VQ_MEDIUM
@ NI_VQ_MEDIUM
Definition: ni_device_api.h:1878
NI_DEC_CROP_MODE_MANUAL
@ NI_DEC_CROP_MODE_MANUAL
Definition: ni_device_api.h:944
_ni_t408_config_t::pu32IntraDcDeltaRate
int32_t pu32IntraDcDeltaRate
Definition: ni_device_api_priv.h:477
NI_RETCODE_PARAM_ERROR_USR_RMD_ENC_PARAM
@ NI_RETCODE_PARAM_ERROR_USR_RMD_ENC_PARAM
Definition: ni_defs.h:487
NI_RETCODE_NVME_SC_INVALID_PARAMETER
@ NI_RETCODE_NVME_SC_INVALID_PARAMETER
Definition: ni_defs.h:558
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:1066
_ni_packet::average_psnr
double average_psnr
Definition: ni_device_api.h:3026
_ni_encoder_cfg_params::bitrateWindow
int bitrateWindow
Definition: ni_device_api.h:2406
NI_RETCODE_ERROR_INVALID_SESSION
@ NI_RETCODE_ERROR_INVALID_SESSION
Definition: ni_defs.h:447
_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:1483
_ni_t408_config_t::scalingListEnable
int32_t scalingListEnable
Definition: ni_device_api_priv.h:399
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:14215
NI_PIX_FMT_ABGR
@ NI_PIX_FMT_ABGR
Definition: ni_device_api.h:273
_ni_t408_config_t::bgDetectEnable
uint32_t bgDetectEnable
Definition: ni_device_api_priv.h:455
hwdl_frame
int hwdl_frame(ni_session_context_t *p_ctx, ni_session_data_io_t *p_session_data, ni_frame_t *p_src_frame, int output_format)
Download hw frames by HwDesc.
Definition: ni_generic_utils.c:708
ni_session_statistic_t
struct _ni_session_statistic_t ni_session_statistic_t
NI_MAX_ENCODER_QUERY_RETRIES
#define NI_MAX_ENCODER_QUERY_RETRIES
Definition: ni_device_api.h:69
NI_PRESETS_NONE
@ NI_PRESETS_NONE
Definition: ni_device_api.h:1874
_ni_load_query::total_pixel_load
uint32_t total_pixel_load
Definition: ni_device_api.h:1231
QUERY_GENERAL_GET_STATUS_R
#define QUERY_GENERAL_GET_STATUS_R(instance)
Definition: ni_nvme.h:782
NI_PIX_FMT_ARGB
@ NI_PIX_FMT_ARGB
Definition: ni_device_api.h:272
_ni_encoder_config_t::i32pbRatio
int32_t i32pbRatio
Definition: ni_device_api_priv.h:636
_ni_xcoder_params
Definition: ni_device_api.h:2814
TP_LOG_OFFSET_IN_4K
#define TP_LOG_OFFSET_IN_4K
Definition: ni_nvme.h:664
NI_RETRY_INTERVAL_100US
#define NI_RETRY_INTERVAL_100US
Definition: ni_device_api_priv.h:1061
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:260
_ni_frame::sei_hdr_content_light_level_info_len
unsigned int sei_hdr_content_light_level_info_len
Definition: ni_device_api.h:2735
_ni_encoder_config_t::ui8enablePTS
uint8_t ui8enablePTS
Definition: ni_device_api_priv.h:534
_ni_encoder_config_t::i32userMaxDeltaQp
int32_t i32userMaxDeltaQp
Definition: ni_device_api_priv.h:527
EP_CORE
@ EP_CORE
Definition: ni_defs.h:381
NI_RETCODE_PARAM_ERROR_WIDTH_TOO_SMALL
@ NI_RETCODE_PARAM_ERROR_WIDTH_TOO_SMALL
Definition: ni_defs.h:510
_ni_encoder_config_t::i8crf
int8_t i8crf
Definition: ni_device_api_priv.h:550
_ni_decoder_input_params_t::reduce_dpb_delay
int reduce_dpb_delay
Definition: ni_device_api.h:2632
_ni_metadata_enc_bstream::min_mv_x
int16_t min_mv_x[2]
Definition: ni_device_api_priv.h:290
_ni_encoder_cfg_params::max_delta_qp
int max_delta_qp
Definition: ni_device_api.h:2457
_ni_xcoder_params::staticMmapThreshold
int staticMmapThreshold
Definition: ni_device_api.h:2883
_ni_t408_config_t::tcOffsetDiv2
int32_t tcOffsetDiv2
Definition: ni_device_api_priv.h:407
_ni_encoder_cfg_params::ctbRowQpStep
int ctbRowQpStep
Definition: ni_device_api.h:2465
_ni_encoder_config_t::ui32cropWidth
uint32_t ui32cropWidth
Definition: ni_device_api_priv.h:623
_ni_encoder_config_t::ui8HDR10CLLEnable
uint8_t ui8HDR10CLLEnable
Definition: ni_device_api_priv.h:553
_ni_encoder_config_t::ui8enableSmoothCrf
uint8_t ui8enableSmoothCrf
Definition: ni_device_api_priv.h:656
CONFIG_INSTANCE_SetAiPara_W
#define CONFIG_INSTANCE_SetAiPara_W(sid, instance)
Definition: ni_nvme.h:849
_ni_session_context::low_delay_sync_cond
ni_pthread_cond_t low_delay_sync_cond
Definition: ni_device_api.h:1697
_ni_frame::sei_hdr_mastering_display_color_vol_len
unsigned int sei_hdr_mastering_display_color_vol_len
Definition: ni_device_api.h:2733
_ni_encoder_cfg_params::motionConstrainedMode
int motionConstrainedMode
Definition: ni_device_api.h:2508
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:14110
_ni_encoder_config_t::ui8intraCompensateMode
uint8_t ui8intraCompensateMode
Definition: ni_device_api_priv.h:675
NI_SESSION_CLOSE_RETRY_MAX
#define NI_SESSION_CLOSE_RETRY_MAX
Definition: ni_device_api_priv.h:1043
_ni_session_context::ready_to_close
uint32_t ready_to_close
Definition: ni_device_api.h:1575
_ni_session_context::overall_load_query
ni_overall_load_query_t overall_load_query
Definition: ni_device_api.h:1534
OPEN_ADD_CODEC
#define OPEN_ADD_CODEC(instance, codec, param)
Definition: ni_nvme.h:696
_ni_encoder_config_t::ui8vbvBufferReencode
uint8_t ui8vbvBufferReencode
Definition: ni_device_api_priv.h:662
_ni_encoder_config_t::i8ppsInitQp
int8_t i8ppsInitQp
Definition: ni_device_api_priv.h:639
_ni_encoder_cfg_params::decoding_refresh_type
int decoding_refresh_type
Definition: ni_device_api.h:2435
_ni_encoder_config_t::ui8picSkipEnable
uint8_t ui8picSkipEnable
Definition: ni_device_api_priv.h:574
NI_RETCODE_NVME_SC_VPU_RSRC_INSUFFICIENT
@ NI_RETCODE_NVME_SC_VPU_RSRC_INSUFFICIENT
Definition: ni_defs.h:562
_ni_encoder_config_t::ui8blockRCSize
uint8_t ui8blockRCSize
Definition: ni_device_api_priv.h:589
_ni_device_capability::xcoder_devices_cnt
uint8_t xcoder_devices_cnt
Definition: ni_device_api.h:1171
NI_ENC_MIN_RESOLUTION_HEIGHT
#define NI_ENC_MIN_RESOLUTION_HEIGHT
Definition: ni_device_api.h:92
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:3236
_ni_encoder_config_t::ui32chromaLinesize
uint32_t ui32chromaLinesize
Definition: ni_device_api_priv.h:622
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:4108
_ni_load_query::fw_load
uint32_t fw_load
Definition: ni_device_api.h:1214
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:18754
_ni_device_capability::fw_rev
uint8_t fw_rev[8]
Definition: ni_device_api.h:1178
_ni_encoder_cfg_params::disableAv1TimingInfo
int disableAv1TimingInfo
Definition: ni_device_api.h:2526
_ni_encoder_cfg_params::crop_width
int crop_width
Definition: ni_device_api.h:2470
GOP_PRESET_IDX_IBBBP
@ GOP_PRESET_IDX_IBBBP
Definition: ni_device_api_priv.h:333
_ni_thread_arg_struct_t
Definition: ni_device_api.h:1271
_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:2432
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:373
_ni_encoder_cfg_params::HDR10minluma
int HDR10minluma
Definition: ni_device_api.h:2358
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:18667
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:15987
_ni_session_context::codec_format
uint32_t codec_format
Definition: ni_device_api.h:1517
_ni_device_capability
device capability type
Definition: ni_device_api.h:1167
NI_RETRY_INTERVAL_200US
#define NI_RETRY_INTERVAL_200US
Definition: ni_device_api_priv.h:1060
NI_MAX_NUM_OF_DECODER_OUTPUTS
#define NI_MAX_NUM_OF_DECODER_OUTPUTS
Definition: ni_defs.h:255
_ni_metadata_enc_frame::frame_force_type
uint16_t frame_force_type
Definition: ni_device_api_priv.h:249
_ni_decoder_config_t::ui8DisableAdaptiveBuffers
uint8_t ui8DisableAdaptiveBuffers
Definition: ni_device_api_priv.h:969
_ni_encoder_config_t::ui8intraResetRefresh
uint8_t ui8intraResetRefresh
Definition: ni_device_api_priv.h:615
_ni_t408_config_t::max_num_merge
int32_t max_num_merge
Definition: ni_device_api_priv.h:403
ALL_CORE
@ ALL_CORE
Definition: ni_defs.h:379
NI_VQ_VERYSLOW
@ NI_VQ_VERYSLOW
Definition: ni_device_api.h:1881
_ni_encoder_config_t::i8hvsBaseMbComplexity
int8_t i8hvsBaseMbComplexity
Definition: ni_device_api_priv.h:643
_ni_session_context::scaler_operation
uint32_t scaler_operation
Definition: ni_device_api.h:1641
ni_posix_memalign
int ni_posix_memalign(void **memptr, size_t alignment, size_t size)
Allocate aligned memory.
Definition: ni_util.c:202
_ni_t408_config_t::intraRefreshArg
int32_t intraRefreshArg
Definition: ni_device_api_priv.h:397
_ni_session_context::stream_dir_name
char stream_dir_name[256]
Definition: ni_device_api.h:1639
_ni_encoder_config_t::ui32lumaLinesize
uint32_t ui32lumaLinesize
Definition: ni_device_api_priv.h:621
NI_RETCODE_ERROR_UNSUPPORTED_FEATURE
@ NI_RETCODE_ERROR_UNSUPPORTED_FEATURE
Definition: ni_defs.h:536
_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:1448
_ni_encoder_config_t::i8maxConsecutiveSkipFrameNum
int8_t i8maxConsecutiveSkipFrameNum
Definition: ni_device_api_priv.h:608
QUERY_INSTANCE_NL_V2_R
#define QUERY_INSTANCE_NL_V2_R(sid, instance)
Definition: ni_nvme.h:770
_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:1741
NI_RETCODE_PARAM_ERROR_PIC_HEIGHT
@ NI_RETCODE_PARAM_ERROR_PIC_HEIGHT
Definition: ni_defs.h:496
_ni_encoder_cfg_params::colorPrimaries
int colorPrimaries
Definition: ni_device_api.h:2387
_ni_decoder_input_params_t::scale_wh
int scale_wh[NI_MAX_NUM_OF_DECODER_OUTPUTS][2]
Definition: ni_device_api.h:2603
CONFIG_INSTANCE_SetDecPara_W
#define CONFIG_INSTANCE_SetDecPara_W(sid, instance)
Definition: ni_nvme.h:810
_ni_t408_config_t::customLambdaEnable
uint32_t customLambdaEnable
Definition: ni_device_api_priv.h:461
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:454
NI_AI_FLAG_IOVEC
#define NI_AI_FLAG_IOVEC
Definition: ni_device_api.h:315
_ni_encoder_cfg_params::forceBframeQpfactor
float forceBframeQpfactor
Definition: ni_device_api.h:2503
_ni_metadata_enc_frame::frame_sei_data_size
uint32_t frame_sei_data_size
Definition: ni_device_api_priv.h:246
_ni_network_layer_params_t::quant_format
int32_t quant_format
Definition: ni_device_api.h:1347
_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:1210
_ni_session_context::is_auto_dl
uint8_t is_auto_dl
Definition: ni_device_api.h:1501
FP_LOG_OFFSET_IN_4K
#define FP_LOG_OFFSET_IN_4K
Definition: ni_nvme.h:665
_ni_thread_arg_struct_t::session_id
uint32_t session_id
Definition: ni_device_api.h:1274
_ni_encoder_cfg_params::crfFloat
float crfFloat
Definition: ni_device_api.h:2485
_ni_metadata_enc_bstream::recycle_index
uint32_t recycle_index
Definition: ni_device_api_priv.h:282
_ni_t408_config_t::nrIntraWeightY
uint32_t nrIntraWeightY
Definition: ni_device_api_priv.h:436
_ni_encoder_cfg_params::long_term_ref_count
int long_term_ref_count
Definition: ni_device_api.h:2367
_ni_t408_config_t::cu32InterDeltaRate
int32_t cu32InterDeltaRate
Definition: ni_device_api_priv.h:486
_ni_encoder_config_t::ui32vbvMaxRate
uint32_t ui32vbvMaxRate
Definition: ni_device_api_priv.h:638
NI_CC_SEI_HDR_HEVC_LEN
#define NI_CC_SEI_HDR_HEVC_LEN
Definition: ni_device_api.h:475
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:419
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:6291
_niFrameSurface1::ui16width
uint16_t ui16width
Definition: ni_device_api.h:2901
ni_strncpy
ni_retcode_t ni_strncpy(char *dest, size_t dmax, const char *src, size_t slen)
Definition: ni_util.c:518
_ni_session_context::flags_array
int flags_array[NI_FIFO_SZ]
Definition: ni_device_api.h:1675
_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:758
AVC_MB_MODE_BITS
#define AVC_MB_MODE_BITS
Definition: ni_device_api_priv.h:809
_ni_metadata_enc_bstream::max_mv_y
int16_t max_mv_y[2]
Definition: ni_device_api_priv.h:291
_ni_thread_arg_struct_t::thread_event_handle
ni_event_handle_t thread_event_handle
Definition: ni_device_api.h:1279
_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:576
_ni_session_context::load_query
ni_load_query_t load_query
Definition: ni_device_api.h:1531
_ni_session_statistic_t::ui32FramesDropped
uint32_t ui32FramesDropped
Definition: ni_defs.h:397
_ni_session_data_io::frame
ni_frame_t frame
Definition: ni_device_api.h:3038
_ni_network_buffer_info::ui8MultiIn
uint8_t ui8MultiIn
Definition: ni_device_api_priv.h:997
_ni_encoder_config_t::ui16hdr10_dx0
uint16_t ui16hdr10_dx0
Definition: ni_device_api_priv.h:597
_ni_t408_config_t::initialRcQp
int32_t initialRcQp
Definition: ni_device_api_priv.h:429
_ni_metadata_enc_bstream::frame_tstamp
uint64_t frame_tstamp
Definition: ni_device_api_priv.h:279
_ni_decoder_input_params_t::enable_out2
int enable_out2
Definition: ni_device_api.h:2593
_ni_encoder_config_t::ui8rdoLevel
uint8_t ui8rdoLevel
Definition: ni_device_api_priv.h:549
_ni_encoder_cfg_params::level_idc
int level_idc
Definition: ni_device_api.h:2324
_ni_scaler_params_t::filterblit
int filterblit
Definition: ni_device_api.h:2660
_ni_encoder_cfg_params::HDR10dy2
int HDR10dy2
Definition: ni_device_api.h:2354
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:503
NI_NUM_OF_PIXELS_1440P
#define NI_NUM_OF_PIXELS_1440P
Definition: ni_device_api.h:81
_ni_uploader_config_t::ui16picHeight
uint16_t ui16picHeight
Definition: ni_device_api_priv.h:893
_niFrameSurface1::output_idx
int8_t output_idx
Definition: ni_device_api.h:2907
NI_H265_USER_DATA_FLAG_CONTENT_LIGHT_LEVEL_INFO
@ NI_H265_USER_DATA_FLAG_CONTENT_LIGHT_LEVEL_INFO
Definition: ni_device_api.h:388
NI_SCALER_FLAG_P2
#define NI_SCALER_FLAG_P2
Definition: ni_device_api.h:306
_ni_segment
Definition: ni_device_api_priv.h:983
_ni_thread_arg_struct_t::session_timestamp
uint64_t session_timestamp
Definition: ni_device_api.h:1275
_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:596
_ni_session_context::pts_table
ni_timestamp_table_t * pts_table
Definition: ni_device_api.h:1546
_ni_metadata_enc_frame::frame_force_type_enable
uint16_t frame_force_type_enable
Definition: ni_device_api_priv.h:248
_ni_metadata_enc_bstream::min_mv_y
int16_t min_mv_y[2]
Definition: ni_device_api_priv.h:292
QP_BITS
#define QP_BITS
Definition: ni_device_api_priv.h:783
COMPILE_ASSERT
#define COMPILE_ASSERT(condition)
Definition: ni_util.h:102
NI_MAX_NUM_SW_FRAME_DATA_POINTERS
#define NI_MAX_NUM_SW_FRAME_DATA_POINTERS
Definition: ni_defs.h:246
CONFIG_SESSION_FRAME_COPY_W
#define CONFIG_SESSION_FRAME_COPY_W(sid)
Definition: ni_nvme.h:906
NI_RETCODE_PARAM_ERROR_CUSTOM_GOP
@ NI_RETCODE_PARAM_ERROR_CUSTOM_GOP
Definition: ni_defs.h:492
CONFIG_INSTANCE_SetAiHVSPlus_W
#define CONFIG_INSTANCE_SetAiHVSPlus_W(sid, instance)
Definition: ni_nvme.h:854
_ni_encoder_cfg_params::preIntraHandling
int preIntraHandling
Definition: ni_device_api.h:2521
AVC_RDCOST_BITS
#define AVC_RDCOST_BITS
Definition: ni_device_api_priv.h:814
_ni_frameclone_desc::ui32Size
uint32_t ui32Size
Definition: ni_device_api.h:1394
_ni_t408_config_t::timeScale
uint32_t timeScale
Definition: ni_device_api_priv.h:423
_ni_encoder_config_t::ui16hdr10_dy0
uint16_t ui16hdr10_dy0
Definition: ni_device_api_priv.h:598
_ni_encoder_cfg_params::baseLayerOnly
int baseLayerOnly
Definition: ni_device_api.h:2522
_ni_encoder_config_t::ui8linkFrameMaxIntraRatio
uint8_t ui8linkFrameMaxIntraRatio
Definition: ni_device_api_priv.h:668
_ni_encoder_cfg_params::maxFrameSize
int maxFrameSize
Definition: ni_device_api.h:2318
_ni_encoder_cfg_params::hvs_qp_scale
int hvs_qp_scale
Definition: ni_device_api.h:2450
_ni_metadata_enc_bstream::intra_total_count
uint16_t intra_total_count
Definition: ni_device_api_priv.h:295
_ni_encoder_cfg_params::cplxDecay
float cplxDecay
Definition: ni_device_api.h:2481
_ni_packet::av1_show_frame
int av1_show_frame
Definition: ni_device_api.h:3017
_ni_instance_mgr_general_status
Definition: ni_device_api_priv.h:79
atoi
#define atoi(p_str)
Definition: ni_device_api.c:7531
_ni_network_buffer_info::ui16Width
uint16_t ui16Width
Definition: ni_device_api_priv.h:991
_ni_encoder_config_t::ui8getCuInfo
uint8_t ui8getCuInfo
Definition: ni_device_api_priv.h:673
_ni_t408_config_t::nrYEnable
uint32_t nrYEnable
Definition: ni_device_api_priv.h:431
_ni_uploader_config_t::ui16picWidth
uint16_t ui16picWidth
Definition: ni_device_api_priv.h:892
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:2664
_ni_frame::sei_user_data_unreg_offset
unsigned int sei_user_data_unreg_offset
Definition: ni_device_api.h:2740
NI_PARAM_MAX_WIDTH
#define NI_PARAM_MAX_WIDTH
Definition: ni_device_api.h:138
_ni_t408_config_t::bitAllocMode
int32_t bitAllocMode
Definition: ni_device_api_priv.h:411
END
#define END
Definition: ni_defs.h:338
PresetConfig::tolCtbRcIntra
float tolCtbRcIntra
Definition: ni_device_api_priv.c:112
_ni_encoder_cfg_params::enable_hvs_qp
int enable_hvs_qp
Definition: ni_device_api.h:2449
_ni_t408_config_t::bgDeltaQp
int32_t bgDeltaQp
Definition: ni_device_api_priv.h:459
_ni_frame::video_height
uint32_t video_height
Definition: ni_device_api.h:2708
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:16117
_ni_t408_config_t::pu08IntraDcDeltaRate
int32_t pu08IntraDcDeltaRate
Definition: ni_device_api_priv.h:471
_ni_network_data::inset
ni_network_layer_offset_t * inset
Definition: ni_device_api.h:1383
_ni_decoder_input_params_t::force_8_bit
int force_8_bit[NI_MAX_NUM_OF_DECODER_OUTPUTS]
Definition: ni_device_api.h:2596
_ni_t408_config_t::cu_size_mode
int32_t cu_size_mode
Definition: ni_device_api_priv.h:400
_ni_encoder_cfg_params::qcomp
float qcomp
Definition: ni_device_api.h:2475
_ni_session_context::low_delay_sync_flag
int low_delay_sync_flag
Definition: ni_device_api.h:1695
PRESET_DEFAULT_BITRATE
#define PRESET_DEFAULT_BITRATE
Definition: ni_device_api.h:56
NI_CUSTOMIZE_ROI_QPOFFSET_LEVEL
#define NI_CUSTOMIZE_ROI_QPOFFSET_LEVEL
Max number of lines supported for qpoffset level.
Definition: ni_device_api.h:493
_ni_packet::pos
long long pos
Definition: ni_device_api.h:2997
RDCOST_OTHER_BIT_POSITION
#define RDCOST_OTHER_BIT_POSITION
Definition: ni_device_api_priv.h:787
_ni_t408_config_t::lfCrossSliceBoundaryEnable
int32_t lfCrossSliceBoundaryEnable
Definition: ni_device_api_priv.h:405
_ni_frame_config::output_index
uint8_t output_index
Definition: ni_device_api.h:2925
_ni_encoder_cfg_params::avcc_hvcc
int avcc_hvcc
Definition: ni_device_api.h:2516
_ni_encoder_config_t::ui8sliceMode
uint8_t ui8sliceMode
Definition: ni_device_api_priv.h:521
NI_CC_SEI_BYTE4
#define NI_CC_SEI_BYTE4
Definition: ni_device_api.h:461
_ni_input_frame::p_input_buffer
uint8_t * p_input_buffer
Definition: ni_device_api.h:1412
_ni_xcoder_params::ddr_priority_mode
ni_ddr_priority_mode_t ddr_priority_mode
Definition: ni_device_api.h:2890
_ni_encoder_cfg_params::colorTrc
int colorTrc
Definition: ni_device_api.h:2388
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:3899
_ni_session_context::hvsplus_level
int hvsplus_level
encoder:calculate PSNR end
Definition: ni_device_api.h:1746
PresetConfig::tolCtbRcInter
float tolCtbRcInter
Definition: ni_device_api_priv.c:111
_ni_session_context::active_video_height
uint32_t active_video_height
Definition: ni_device_api.h:1582
_ni_encoder_config_t::ui8gopLowdelay
uint8_t ui8gopLowdelay
Definition: ni_device_api_priv.h:559
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:19071
NI_MAX_SEI_DATA
#define NI_MAX_SEI_DATA
Definition: ni_device_api.h:440
_ni_session_context::pkt_delay_cnt
uint32_t pkt_delay_cnt
Definition: ni_device_api.h:1733
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:2434
_ni_t408_config_t::intra_qp
int32_t intra_qp
Definition: ni_device_api_priv.h:386
_ni_encoder_cfg_params::av1_error_resilient_mode
int av1_error_resilient_mode
Definition: ni_device_api.h:2463
NI_CC_SEI_BYTE6
#define NI_CC_SEI_BYTE6
Definition: ni_device_api.h:463
_ni_session_context::enc_pts_r_idx
int64_t enc_pts_r_idx
Definition: ni_device_api.h:1469
_ni_encoder_cfg_params::tune_bframe_visual
int tune_bframe_visual
Definition: ni_device_api.h:2504
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:13559
ni_instance_mgr_stream_complete_t
struct _ni_instance_mgr_stream_complete ni_instance_mgr_stream_complete_t
_ni_encoder_cfg_params::conf_win_left
int conf_win_left
Definition: ni_device_api.h:2374
NI_MAX_FRAME_SIZE
#define NI_MAX_FRAME_SIZE
Definition: ni_device_api.h:181
_ni_encoder_cfg_params::pass1_qp
int pass1_qp
Definition: ni_device_api.h:2484
_ni_session_context::rc_error_count
int rc_error_count
Definition: ni_device_api.h:1564
_ni_encoder_cfg_params::conf_win_right
int conf_win_right
Definition: ni_device_api.h:2375
_ni_encoder_config_t::ui8rcQpDeltaRange
uint8_t ui8rcQpDeltaRange
Definition: ni_device_api_priv.h:591
CONFIG_INSTANCE_UploadModel_W
#define CONFIG_INSTANCE_UploadModel_W(sid, instance)
Definition: ni_nvme.h:912
_ni_session_context::blk_dev_name
char blk_dev_name[NI_MAX_DEVICE_NAME_LEN]
Definition: ni_device_api.h:1690
_ni_decoder_input_params_t::enable_low_delay_check
int enable_low_delay_check
Definition: ni_device_api.h:2611
NI_POOL_TYPE_NONE
@ NI_POOL_TYPE_NONE
Definition: ni_device_api.h:505
_ni_session_context::biggest_bitstream_buffer_allocated
uint32_t biggest_bitstream_buffer_allocated
Definition: ni_device_api.h:1586
_ni_session_context::session_run_state
ni_session_run_state_t session_run_state
Definition: ni_device_api.h:1578
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:6373
_ni_session_context::enable_low_delay_check
int enable_low_delay_check
Definition: ni_device_api.h:1626
_ni_t408_config_t::lambdaScalingEnable
int32_t lambdaScalingEnable
Definition: ni_device_api_priv.h:499
_ni_session_context::sent_size
uint32_t sent_size
Definition: ni_device_api.h:1539
_niFrameSurface1::device_handle
int32_t device_handle
Definition: ni_device_api.h:2904
_ni_encoder_cfg_params::crfMax
int crfMax
Definition: ni_device_api.h:2474
NI_CC_SEI_TRAILER_LEN
#define NI_CC_SEI_TRAILER_LEN
Definition: ni_device_api.h:479
_ni_encoder_config_t::ui8bitrateMode
uint8_t ui8bitrateMode
Definition: ni_device_api_priv.h:640
_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:2905
_ni_t408_config_t::weightPredEnable
uint32_t weightPredEnable
Definition: ni_device_api_priv.h:454
_ni_decoder_config_t::fps_denominator
uint32_t fps_denominator
Definition: ni_device_api_priv.h:960
_ni_metadata_enc_bstream::frameCropLeftOffset
uint16_t frameCropLeftOffset
Definition: ni_device_api_priv.h:305
_ni_metadata_enc_bstream_rev61::av1_show_frame
uint32_t av1_show_frame
Definition: ni_device_api_priv.h:272
_ni_p2p_sgl_t::ui32DMALen
uint32_t ui32DMALen[NI_MAX_P2P_SGL_ENTRY]
Definition: ni_defs.h:420
_ni_t408_config_t::nrInterWeightCb
uint32_t nrInterWeightCb
Definition: ni_device_api_priv.h:440
_ni_encoder_cfg_params::cuTreeFactor
int cuTreeFactor
Definition: ni_device_api.h:2478
_ni_network_layer_info::out_param
ni_network_layer_params_t * out_param
Definition: ni_device_api.h:1369
_ni_encoder_cfg_params::enableipRatio
int enableipRatio
Definition: ni_device_api.h:2496
NI_CODEC_HW_PAYLOAD_OFFSET
@ NI_CODEC_HW_PAYLOAD_OFFSET
Definition: ni_device_api.h:958
_ni_decoder_input_params_t::scale_resolution_ceil
int scale_resolution_ceil[NI_MAX_NUM_OF_DECODER_OUTPUTS]
Definition: ni_device_api.h:2626
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:1175
_ni_t408_config_t::rdoSkip
int32_t rdoSkip
Definition: ni_device_api_priv.h:498
NI_MAX_RESOLUTION_RGBA_HEIGHT
#define NI_MAX_RESOLUTION_RGBA_HEIGHT
Definition: ni_device_api.h:107
_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:1669
_ni_encoder_cfg_params::vbv_min_rate
int vbv_min_rate
Definition: ni_device_api.h:2500
NI_DEVICE_TYPE_XCODER_MAX
@ NI_DEVICE_TYPE_XCODER_MAX
Definition: ni_defs.h:364
CONFIG_INSTANCE_SetSOS_W
#define CONFIG_INSTANCE_SetSOS_W(sid, instance)
Definition: ni_nvme.h:803
_ni_t408_config_t::pu04IntraDcDeltaRate
int32_t pu04IntraDcDeltaRate
Definition: ni_device_api_priv.h:468
ni_get_core_name
char * ni_get_core_name(ni_core_type_t eCoreType)
Definition: ni_device_api_priv.c:18895
_ni_encoder_cfg_params::HDR10MaxLight
int HDR10MaxLight
Definition: ni_device_api.h:2345
AVC_RDCOST_BIT_POSITION
#define AVC_RDCOST_BIT_POSITION
Definition: ni_device_api_priv.h:813
_ni_encoder_config_t::ui8ctbRcMode
uint8_t ui8ctbRcMode
Definition: ni_device_api_priv.h:556
_ni_xcoder_params::cacheRoi
int cacheRoi
Definition: ni_device_api.h:2848
_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:1513
_ni_session_context::psnr_u
double psnr_u
Definition: ni_device_api.h:1742
_ni_dec_mastering_display_colour_volume_bytes::white_point_x
uint16_t white_point_x
Definition: ni_device_api.h:1078
_ni_encoder_config_t::ui8motionConstrainedMode
uint8_t ui8motionConstrainedMode
Definition: ni_device_api_priv.h:652
_ni_encoder_cfg_params::still_image_detect_level
int still_image_detect_level
Definition: ni_device_api.h:2509
_ni_encoder_cfg_params::crop_height
int crop_height
Definition: ni_device_api.h:2471
_ni_hw_capability::codec_format
uint8_t codec_format
Definition: ni_device_api.h:1153
_ni_frame_config::rectangle_width
uint16_t rectangle_width
Definition: ni_device_api.h:2918
_ni_session_context::max_nvme_io_size
uint32_t max_nvme_io_size
Definition: ni_device_api.h:1507
_ni_session_context::enable_user_data_sei_passthru
int enable_user_data_sei_passthru
Definition: ni_device_api.h:1645
_ni_encoder_config_t::ui8videoFullRange
uint8_t ui8videoFullRange
Definition: ni_device_api_priv.h:580
_ni_encoder_cfg_params::aspectRatioHeight
int aspectRatioHeight
Definition: ni_device_api.h:2316
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:7603
NI_RETCODE_EAGAIN
@ NI_RETCODE_EAGAIN
Definition: ni_defs.h:543
_ni_timestamp_table_t
Definition: ni_device_api.h:1335
_ni_dec_mastering_display_colour_volume_bytes::max_display_mastering_luminance
uint32_t max_display_mastering_luminance
Definition: ni_device_api.h:1080
NI_MIN_HEIGHT
#define NI_MIN_HEIGHT
Definition: ni_device_api.h:129
_ni_overall_load_query::admin_queried
uint32_t admin_queried
Definition: ni_device_api.h:1241
_ni_t408_config_t::minQpI
int32_t minQpI
Definition: ni_device_api_priv.h:489
NI_RETCODE_FAILURE
@ NI_RETCODE_FAILURE
Definition: ni_defs.h:442
_ni_encoder_cfg_params::HDR10dx1
int HDR10dx1
Definition: ni_device_api.h:2351
_ni_nvme_identity::hw2_codec_format
uint8_t hw2_codec_format
Definition: ni_nvme.h:195
ni_strcat
ni_retcode_t ni_strcat(char *dest, size_t dmax, const char *src)
Definition: ni_util.c:689
_ni_encoder_config_t::ui32VuiDataSizeBytes
uint32_t ui32VuiDataSizeBytes
Definition: ni_device_api_priv.h:545
_ni_session_context::is_first_frame
int is_first_frame
Definition: ni_device_api.h:1465
_ni_t408_config_t::tmvpEnable
int32_t tmvpEnable
Definition: ni_device_api_priv.h:401
NI_HDR10P_SEI_BYTE4
#define NI_HDR10P_SEI_BYTE4
Definition: ni_device_api.h:470
_ni_encoder_config_t::ui8enableSSIM
uint8_t ui8enableSSIM
Definition: ni_device_api_priv.h:595
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:18233
_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:1155
_ni_decoder_config_t::ui32SourceWidth
uint32_t ui32SourceWidth
Definition: ni_device_api_priv.h:971
_ni_encoder_config_t::ui16maxFrameSize
uint16_t ui16maxFrameSize
Definition: ni_device_api_priv.h:594
_ni_encoder_config_t::ui32horOffset
uint32_t ui32horOffset
Definition: ni_device_api_priv.h:625
NI_MIN_BITRATE
#define NI_MIN_BITRATE
Definition: ni_device_api.h:117
_ni_encoder_cfg_params::planar
int planar
Definition: ni_device_api.h:2317
_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:2706
_ni_packet::buffer_size
uint32_t buffer_size
Definition: ni_device_api.h:3010
_ni_t408_config_t::decoding_refresh_type
int32_t decoding_refresh_type
Definition: ni_device_api_priv.h:385
_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:462
_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:514
_ni_sei_header::type
uint8_t type
Definition: ni_device_api.h:360
_ni_session_context::dts_queue
ni_timestamp_table_t * dts_queue
Definition: ni_device_api.h:1549
_ni_encoder_frame_params::video_height
uint16_t video_height
Definition: ni_device_api_priv.h:203
CONFIG_INSTANCE_SetDecPpuPara_W
#define CONFIG_INSTANCE_SetDecPpuPara_W(sid, instance)
Definition: ni_nvme.h:812
_ni_frame::pts
long long pts
Definition: ni_device_api.h:2704
_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:2881
_ni_decoder_config_t::ui8EnablelowDelayCheck
uint8_t ui8EnablelowDelayCheck
Definition: ni_device_api_priv.h:963
_ni_frame::inconsecutive_transfer
uint8_t inconsecutive_transfer
Definition: ni_device_api.h:2805
_ni_overall_load_query::overall_current_load
uint32_t overall_current_load
Definition: ni_device_api.h:1238
NI_RETCODE_PARAM_ERROR_FRATE
@ NI_RETCODE_PARAM_ERROR_FRATE
Definition: ni_defs.h:452
_ni_frame::crop_right
uint32_t crop_right
Definition: ni_device_api.h:2713
_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:1706
NI_SCALER_FLAG_PC
#define NI_SCALER_FLAG_PC
Definition: ni_device_api.h:304
NI_RETCODE_PARAM_ERROR_CU_SIZE_MODE
@ NI_RETCODE_PARAM_ERROR_CU_SIZE_MODE
Definition: ni_defs.h:464
_ni_encoder_config_t::ui16HDR10MaxLight
uint16_t ui16HDR10MaxLight
Definition: ni_device_api_priv.h:551
_ni_encoder_cfg_params::rdoLevel
int rdoLevel
Definition: ni_device_api.h:2343
_ni_t408_config_t::slice_mode
int32_t slice_mode
Definition: ni_device_api_priv.h:501
_ni_encoder_cfg_params::long_term_ref_enable
int long_term_ref_enable
Definition: ni_device_api.h:2334
_ni_frame::pixel_format
int pixel_format
Definition: ni_device_api.h:2769
_ni_encoder_config_t::i32frameRateDenominator
int32_t i32frameRateDenominator
Definition: ni_device_api_priv.h:571
_ni_encoder_config_t::i32picWidth
int32_t i32picWidth
Definition: ni_device_api_priv.h:518
NI_T35_SEI_CLOSED_CAPTION
@ NI_T35_SEI_CLOSED_CAPTION
Definition: ni_device_api_priv.c:93
_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:504
_ni_decoder_config_t::ui8SurviveStreamErr
uint8_t ui8SurviveStreamErr
Definition: ni_device_api_priv.h:974
_ni_encoder_cfg_params::get_psnr_mode
int get_psnr_mode
Definition: ni_device_api.h:2506
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:813
QUERY_INSTANCE_STREAM_INFO_R
#define QUERY_INSTANCE_STREAM_INFO_R(sid, instance)
Definition: ni_nvme.h:719
_ni_encoder_config_t::i32userQpMax
int32_t i32userQpMax
Definition: ni_device_api_priv.h:524
ni_util.h
Utility definitions.
_ni_session_context::av1_pkt_num
uint64_t av1_pkt_num
Definition: ni_device_api.h:1726
ni_decoder_output_config_t::ui8EnablePpuScaleAdapt
uint8_t ui8EnablePpuScaleAdapt
Definition: ni_device_api_priv.h:947
_ni_encoder_cfg_params::ltrNextInterval
int ltrNextInterval
Definition: ni_device_api.h:2363
NI_VQ_FASTER
@ NI_VQ_FASTER
Definition: ni_device_api.h:1876
_ni_decoder_input_params_t::survive_stream_err
int survive_stream_err
Definition: ni_device_api.h:2631
_ni_encoder_cfg_params::videoFullRange
int videoFullRange
Definition: ni_device_api.h:2365
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:1466
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:8563
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:16513
_ni_encoder_cfg_params::intra_reset_refresh
int intra_reset_refresh
Definition: ni_device_api.h:2464
_ni_uploader_config_t::ui8poolSize
uint8_t ui8poolSize
Definition: ni_device_api_priv.h:894
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:927
_ni_network_buffer_info::ui32RelOffset
uint32_t ui32RelOffset
Definition: ni_device_api_priv.h:1003
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:18604
NI_PIX_FMT_10_TILED4X4
@ NI_PIX_FMT_10_TILED4X4
Definition: ni_device_api.h:280
_ni_encoder_cfg_params::slice_arg
int slice_arg
Definition: ni_device_api.h:2433
NI_H265_USERDATA_FLAG_UNREGISTERED_SUF
@ NI_H265_USERDATA_FLAG_UNREGISTERED_SUF
Definition: ni_device_api.h:381
_ni_frame::p_metadata_buffer
uint8_t * p_metadata_buffer
Definition: ni_device_api.h:2792
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:604
_ni_encoder_config_t::i16ctbRowQpStep
int16_t i16ctbRowQpStep
Definition: ni_device_api_priv.h:616
WRITE_METADATA_W
#define WRITE_METADATA_W(sid, instance)
Definition: ni_nvme.h:676
lower_pixel_rate
int lower_pixel_rate(const ni_load_query_t *pQuery, uint32_t ui32CurrentLowest)
Definition: ni_device_api_priv.c:19213
NI_GET_MIN_HWDESC_P2P_BUF_ID
#define NI_GET_MIN_HWDESC_P2P_BUF_ID(x)
Definition: ni_defs.h:282
_ni_nvme_identity::xcoder_num_elements
uint8_t xcoder_num_elements
Definition: ni_nvme.h:229
_ni_t408_config_t::nrNoiseSigmaCr
uint32_t nrNoiseSigmaCr
Definition: ni_device_api_priv.h:446
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:2556
_ni_encoder_cfg_params::lookAheadDepth
int lookAheadDepth
Definition: ni_device_api.h:2342
NI_MAX_AI_NETWORK_BINARY_BUFFER_QUERY_RETRIES
#define NI_MAX_AI_NETWORK_BINARY_BUFFER_QUERY_RETRIES
Definition: ni_device_api_priv.h:1058
NI_RETCODE_ERROR_INVALID_HANDLE
@ NI_RETCODE_ERROR_INVALID_HANDLE
Definition: ni_defs.h:525
_ni_encoder_config_t::ui8baseLayerOnly
uint8_t ui8baseLayerOnly
Definition: ni_device_api_priv.h:666
NI_RETCODE_PARAM_ERROR_INTRA_QP
@ NI_RETCODE_PARAM_ERROR_INTRA_QP
Definition: ni_defs.h:461
INTERCOST_BIT_POSITION
#define INTERCOST_BIT_POSITION
Definition: ni_device_api_priv.h:797
_ni_encoder_config_t::ui8adaptiveLamdaMode
uint8_t ui8adaptiveLamdaMode
Definition: ni_device_api_priv.h:672
_ni_packet::still_image_detected
uint8_t still_image_detected
Definition: ni_device_api.h:3030
_ni_frame_config::rectangle_y
int16_t rectangle_y
Definition: ni_device_api.h:2921
NI_RETCODE_NVME_SC_WRITE_BUFFER_FULL
@ NI_RETCODE_NVME_SC_WRITE_BUFFER_FULL
Definition: ni_defs.h:548
_ni_t408_config_t::independSliceMode
int32_t independSliceMode
Definition: ni_device_api_priv.h:392
AV_CODEC_DEFAULT_BITRATE
#define AV_CODEC_DEFAULT_BITRATE
Definition: ni_device_api.h:55
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:18954
_ni_custom_gop_params::pic_param
ni_gop_params_t pic_param[NI_MAX_GOP_NUM]
Definition: ni_device_api.h:2106
_ni_session_context::pic_reorder_delay
int pic_reorder_delay
Definition: ni_device_api.h:1672
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:4733
_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:400
_ni_session_context::dev
unsigned short dev
Definition: ni_device_api.h:1652
_ni_xcoder_params::low_delay_mode
int low_delay_mode
Definition: ni_device_api.h:2834
_ni_decoder_input_params_t::decoder_low_delay
int decoder_low_delay
Definition: ni_device_api.h:2609
_ni_metadata_enc_bstream::av1_show_frame
uint32_t av1_show_frame
Definition: ni_device_api_priv.h:283
_ni_metadata_enc_bstream::reconChromaWidth
uint16_t reconChromaWidth
Definition: ni_device_api_priv.h:303
_ni_decoder_input_params_t::nb_save_pkt
int nb_save_pkt
Definition: ni_device_api.h:2595
_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:99
_ni_packet::video_width
uint32_t video_width
Definition: ni_device_api.h:3001
NI_MIN_RESOLUTION_HEIGHT
#define NI_MIN_RESOLUTION_HEIGHT
Definition: ni_device_api.h:90
QUERY_INSTANCE_METRICS_R
#define QUERY_INSTANCE_METRICS_R(sid, instance)
Definition: ni_nvme.h:776
_ni_encoder_config_t::ui8colorDescPresent
uint8_t ui8colorDescPresent
Definition: ni_device_api_priv.h:562
_ni_metadata_enc_frame::frame_roi_avg_qp
uint32_t frame_roi_avg_qp
Definition: ni_device_api_priv.h:244
_ni_session_context::pkt_num
uint64_t pkt_num
Definition: ni_device_api.h:1563
_ni_encoder_cfg_params::enable_cu_level_rate_control
int enable_cu_level_rate_control
Definition: ni_device_api.h:2448
_ni_session_context::iocbs
ni_iocb_t ** iocbs
Definition: ni_device_api.h:1758
_ni_decoder_input_params_t::disable_adaptive_buffers
int disable_adaptive_buffers
Definition: ni_device_api.h:2629
NI_SCALER_OPCODE_ROTATE
@ NI_SCALER_OPCODE_ROTATE
Definition: ni_defs.h:593
NI_CODEC_FORMAT_H264
@ NI_CODEC_FORMAT_H264
Definition: ni_device_api.h:925
_ni_frame::use_cur_src_as_long_term_pic
uint8_t use_cur_src_as_long_term_pic
Definition: ni_device_api.h:2718
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:18427
_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:1283
_ni_frame::start_buffer_size
uint32_t start_buffer_size
Definition: ni_device_api.h:2800
_ni_decoder_config_t::ui8UduSeiEnabled
uint8_t ui8UduSeiEnabled
Definition: ni_device_api_priv.h:957
_ni_t408_config_t::independSliceModeArg
int32_t independSliceModeArg
Definition: ni_device_api_priv.h:393
NI_MAX_BITRATE
#define NI_MAX_BITRATE
Definition: ni_device_api.h:116
NI_RETCODE_PARAM_ERROR_OOR
@ NI_RETCODE_PARAM_ERROR_OOR
Definition: ni_defs.h:507
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:2707
_ni_t408_config_t::pu32DeltaRate
int32_t pu32DeltaRate
Definition: ni_device_api_priv.h:466
NI_QUADRA_MEMORY_CONFIG_SR
#define NI_QUADRA_MEMORY_CONFIG_SR
Definition: ni_device_api_priv.h:1050
_ni_session_context::initial_frame_delay
int initial_frame_delay
Definition: ni_device_api.h:1723
_ni_xcoder_params::bitrate
int bitrate
Definition: ni_device_api.h:2830
_ni_encoder_cfg_params::HDR10wy
int HDR10wy
Definition: ni_device_api.h:2356
NI_MIN_WIDTH
#define NI_MIN_WIDTH
Definition: ni_device_api.h:127
_ni_encoder_config_t::hdrEnableVUI
uint32_t hdrEnableVUI
Definition: ni_device_api_priv.h:543
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:162
_ni_encoder_config_t::ui16iFrameSizeRatio
uint16_t ui16iFrameSizeRatio
Definition: ni_device_api_priv.h:611
_ni_encoder_cfg_params::preferred_transfer_characteristics
int preferred_transfer_characteristics
Definition: ni_device_api.h:2340
_ni_t408_config_t::intra_mb_refresh_arg
int32_t intra_mb_refresh_arg
Definition: ni_device_api_priv.h:504
_ni_session_context::pixel_format
int pixel_format
Definition: ni_device_api.h:1647
_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:2837
_ni_frame::flags
int flags
Definition: ni_device_api.h:2789
_ni_encoder_cfg_params::conf_win_top
int conf_win_top
Definition: ni_device_api.h:2372
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:13436
_ni_encoder_config_t::ui8EnableAcqLimit
uint8_t ui8EnableAcqLimit
Definition: ni_device_api_priv.h:650
_ni_t408_config_t::nrInterWeightCr
uint32_t nrInterWeightCr
Definition: ni_device_api_priv.h:441
_ni_metadata_enc_bstream::reconChromaSize
uint32_t reconChromaSize
Definition: ni_device_api_priv.h:301
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:16905
_ni_session_context::sei_trailer
uint8_t sei_trailer[NI_CC_SEI_TRAILER_LEN]
Definition: ni_device_api.h:1451
_ni_encoder_config_t::ui8noMbtree
uint8_t ui8noMbtree
Definition: ni_device_api_priv.h:631
QUADRA
#define QUADRA
Definition: ni_defs.h:123
ni_ai_network_layer_size
uint32_t ni_ai_network_layer_size(ni_network_layer_params_t *p_param)
Definition: ni_util.c:3989
CHECK_VPU_RECOVERY
#define CHECK_VPU_RECOVERY(ret)
Definition: ni_device_api_priv.c:823
_ni_t408_config_t::chromaCbQpOffset
int32_t chromaCbQpOffset
Definition: ni_device_api_priv.h:426
PresetConfig::enableCuLevelRateControl
int enableCuLevelRateControl
Definition: ni_device_api_priv.c:110
_ni_metadata_enc_bstream::frame_type
uint32_t frame_type
Definition: ni_device_api_priv.h:278
STD_JPEG
@ STD_JPEG
Definition: ni_device_api_priv.h:372
_ni_nvme_identity::hw2_codec_type
uint8_t hw2_codec_type
Definition: ni_nvme.h:196
CU_MODE_BITS
#define CU_MODE_BITS
Definition: ni_device_api_priv.h:703
_ni_encoder_config_t::ui32VuiDataSizeBits
uint32_t ui32VuiDataSizeBits
Definition: ni_device_api_priv.h:544
_ni_encoder_cfg_params::bitrateMode
int bitrateMode
Definition: ni_device_api.h:2483
_ni_encoder_config_t::ui8inLoopDSRatio
uint8_t ui8inLoopDSRatio
Definition: ni_device_api_priv.h:588
_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:236
_ni_session_context::pkt_custom_sei_set
ni_custom_sei_set_t * pkt_custom_sei_set[NI_FIFO_SZ]
Definition: ni_device_api.h:1487
NI_PIXEL_PLANAR_FORMAT_TILED4X4
@ NI_PIXEL_PLANAR_FORMAT_TILED4X4
Definition: ni_device_api.h:936
NI_LOG_DEBUG
@ NI_LOG_DEBUG
Definition: ni_log.h:64
NI_CC_SEI_BYTE5
#define NI_CC_SEI_BYTE5
Definition: ni_device_api.h:462
_ni_metadata_enc_frame::force_pic_qp_enable
uint16_t force_pic_qp_enable
Definition: ni_device_api_priv.h:250
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:2711
NI_ENC_REPEAT_HEADERS_ALL_I_FRAMES
#define NI_ENC_REPEAT_HEADERS_ALL_I_FRAMES
Definition: ni_device_api.h:2110
_ni_t408_config_t::pu04IntraAngleDeltaRate
int32_t pu04IntraAngleDeltaRate
Definition: ni_device_api_priv.h:469
_ni_xcoder_params::dec_input_params
ni_decoder_input_params_t dec_input_params
Definition: ni_device_api.h:2869
_ni_network_perf_metrics::total_cycles
uint32_t total_cycles
Definition: ni_device_api.h:1400
_ni_decoder_config_t::ui8EnableAdvancedEc
uint8_t ui8EnableAdvancedEc
Definition: ni_device_api_priv.h:968
STD_HEVC
@ STD_HEVC
Definition: ni_device_api_priv.h:367
_ni_metadata_dec_frame::first_sei_header
ni_sei_header_t first_sei_header
Definition: ni_device_api_priv.h:233
STD_AVC
@ STD_AVC
Definition: ni_device_api_priv.h:356
CU_MODE_BIT_POSITION
#define CU_MODE_BIT_POSITION
Definition: ni_device_api_priv.h:702
_ni_sei_header
Definition: ni_device_api.h:357
_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:307
_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:2017
_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:18925
NI_PIXEL_PLANAR_FORMAT_PLANAR
@ NI_PIXEL_PLANAR_FORMAT_PLANAR
Definition: ni_device_api.h:935
_ni_encoder_config_t::ui8noHWMultiPassSupport
uint8_t ui8noHWMultiPassSupport
Definition: ni_device_api_priv.h:632
_ni_frame::p_custom_sei_set
ni_custom_sei_set_t * p_custom_sei_set
Definition: ni_device_api.h:2771
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:5199
ni_fopen
ni_retcode_t ni_fopen(FILE **fp, const char *filename, const char *mode)
Definition: ni_util.c:983
_ni_t408_config_t::pu08IntraAngleDeltaRate
int32_t pu08IntraAngleDeltaRate
Definition: ni_device_api_priv.h:472
_ni_packet::avg_frame_qp
uint32_t avg_frame_qp
Definition: ni_device_api.h:3011
_ni_encoder_config_t::ui8spatialLayersMinusOne
uint8_t ui8spatialLayersMinusOne
Definition: ni_device_api_priv.h:658
_ni_session_context::aio_context
ni_aio_context_t aio_context
Definition: ni_device_api.h:1757
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:3808
_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:1528
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:1705
_ni_encoder_config_t::ui8EnableAUD
uint8_t ui8EnableAUD
Definition: ni_device_api_priv.h:547
_ni_device_capability::device_is_xcoder
uint8_t device_is_xcoder
Definition: ni_device_api.h:1169
ni_frameclone_desc_t
struct _ni_frameclone_desc ni_frameclone_desc_t
QP_BYTE_OFFSET
#define QP_BYTE_OFFSET
Definition: ni_device_api_priv.h:781
_ni_session_context::force_low_delay_cnt
uint32_t force_low_delay_cnt
Definition: ni_device_api.h:1731
NI_MAX_RESOLUTION_AREA
#define NI_MAX_RESOLUTION_AREA
Definition: ni_device_api.h:105
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:448
NI_RETCODE_PARAM_ERROR_MN_QP
@ NI_RETCODE_PARAM_ERROR_MN_QP
Definition: ni_defs.h:473
_ni_session_context::last_frame_offset
uint64_t last_frame_offset
Definition: ni_device_api.h:1486
_ni_encoder_cfg_params::forced_header_enable
int forced_header_enable
Definition: ni_device_api.h:2333
_ni_hw_capability::max_number_of_contexts
uint8_t max_number_of_contexts
Definition: ni_device_api.h:1151
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:16447
_ni_session_context::session_statistic
ni_session_statistic_t session_statistic
Definition: ni_device_api.h:1681
_ni_encoder_cfg_params::spatial_layers_ref_base_layer
int spatial_layers_ref_base_layer
Definition: ni_device_api.h:2517
_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:1694
IDENTIFY_DEVICE_R
#define IDENTIFY_DEVICE_R
Definition: ni_nvme.h:687
_ni_scaler_params_t::scaler_param_c
double scaler_param_c
Definition: ni_device_api.h:2663
ni_assert
#define ni_assert(expression)
Definition: ni_defs.h:341
_ni_t408_config_t::pu08DeltaRate
int32_t pu08DeltaRate
Definition: ni_device_api_priv.h:464
_ni_encoder_frame_params
Definition: ni_device_api_priv.h:197
NI_CODEC_FORMAT_H265
@ NI_CODEC_FORMAT_H265
Definition: ni_device_api.h:926
_ni_encoder_config_t::i32forceBframeQpFactor
int32_t i32forceBframeQpFactor
Definition: ni_device_api_priv.h:648
_ni_load_query::active_hwuploaders
uint32_t active_hwuploaders
Definition: ni_device_api.h:1227
_ni_frame::use_long_term_ref
uint8_t use_long_term_ref
Definition: ni_device_api.h:2719
NI_RETCODE_PARAM_ERROR_INTRA_PERIOD
@ NI_RETCODE_PARAM_ERROR_INTRA_PERIOD
Definition: ni_defs.h:459
_ni_encoder_config_t::ui8NewRCEnable
uint8_t ui8NewRCEnable
Definition: ni_device_api_priv.h:617
_ni_session_context::last_access_time
volatile uint64_t last_access_time
Definition: ni_device_api.h:1714
QUERY_INSTANCE_NL_SIZE_V2_R
#define QUERY_INSTANCE_NL_SIZE_V2_R(sid, instance)
Definition: ni_nvme.h:764
_ni_packet::data_len
uint32_t data_len
Definition: ni_device_api.h:3006