libxcoder  5.2.0
ni_encode_utils.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 encode_utils.c
24  *
25  * \brief Video encoding utility functions shared by Libxcoder API examples
26  ******************************************************************************/
27 
28 #include "ni_generic_utils.h"
29 #include "ni_encode_utils.h"
30 #include "ni_log.h"
31 #include "ni_av_codec.h"
32 #include "ni_util.h"
33 
34 /*!*****************************************************************************
35  * \brief Set up hard coded demo ROI map
36  *
37  * \param
38  *
39  * \return none
40  ******************************************************************************/
42 {
43  ni_xcoder_params_t *p_param =
44  (ni_xcoder_params_t *)(p_enc_ctx->p_session_config);
45  uint32_t i, j, sumQp = 0;
46  uint32_t mbWidth, mbHeight, numMbs;
47  // mode 1: Set QP for center 1/3 of picture to highest - lowest quality
48  // the rest to lowest - highest quality;
49  // mode non-1: reverse of mode 1
50  int importanceLevelCentre = p_param->roi_demo_mode == 1 ? 40 : 10;
51  int importanceLevelRest = p_param->roi_demo_mode == 1 ? 10 : 40;
52  int32_t width, height;
53 
54  if (!p_enc_ctx->roi_map)
55  {
56  p_enc_ctx->roi_map =
57  (ni_enc_quad_roi_custom_map *)calloc(1, p_enc_ctx->roi_len);
58  }
59  if (!p_enc_ctx->roi_map)
60  {
61  return;
62  }
63  uint32_t roiMapBlockUnitSize = 64; // HEVC
64  uint32_t max_cu_size = 64; // HEVC
65  if (NI_CODEC_FORMAT_H264 == p_enc_ctx->codec_format)
66  {
67  max_cu_size = 16;
68  roiMapBlockUnitSize = 16;
69  }
70 
71  width = p_param->source_width;
72  height = p_param->source_height;
73  // AV1 non-8x8-aligned resolution is implicitly cropped due to Quadra HW limitation
74  if (NI_CODEC_FORMAT_AV1 == p_enc_ctx->codec_format)
75  {
76  width = (width / 8) * 8;
77  height = (height / 8) * 8;
78  }
79 
80  mbWidth =
81  ((width + max_cu_size - 1) & (~(max_cu_size - 1))) /
82  roiMapBlockUnitSize;
83  mbHeight =
84  ((height + max_cu_size - 1) & (~(max_cu_size - 1))) /
85  roiMapBlockUnitSize;
86  numMbs = mbWidth * mbHeight;
87 
88  // copy roi MBs QPs into custom map
89  bool bIsCenter;
90  // number of qp info (8x8) per mb or ctb
91  uint32_t entryPerMb = (roiMapBlockUnitSize / 8) * (roiMapBlockUnitSize / 8);
92 
93  for (i = 0; i < numMbs; i++)
94  {
95  if ((i % mbWidth > mbWidth / 3) && (i % mbWidth < mbWidth * 2 / 3))
96  bIsCenter = 1;
97  else
98  bIsCenter = 0;
99 
100  for (j = 0; j < entryPerMb; j++)
101  {
102  /*
103  g_quad_roi_map[i*4+j].field.skip_flag = 0; // don't force
104  skip mode g_quad_roi_map[i*4+j].field.roiAbsQp_flag = 1; //
105  absolute QP g_quad_roi_map[i*4+j].field.qp_info = bIsCenter
106  ? importanceLevelCentre : importanceLevelRest;
107  */
108  p_enc_ctx->roi_map[i * entryPerMb + j].field.ipcm_flag =
109  0; // don't force skip mode
110  p_enc_ctx->roi_map[i * entryPerMb + j].field.roiAbsQp_flag =
111  1; // absolute QP
112  p_enc_ctx->roi_map[i * entryPerMb + j].field.qp_info =
113  bIsCenter ? importanceLevelCentre : importanceLevelRest;
114  }
115  sumQp += p_enc_ctx->roi_map[i * entryPerMb].field.qp_info;
116  }
117  p_enc_ctx->roi_avg_qp =
118  // NOLINTNEXTLINE(clang-analyzer-core.DivideZero)
119  (sumQp + (numMbs >> 1)) / numMbs; // round off
120 }
121 
122 // convert various reconfig and demo modes (stored in encoder configuration) to
123 // aux data and store them in frame
125 {
126  ni_xcoder_params_t *api_param =
127  (ni_xcoder_params_t *)p_enc_ctx->p_session_config;
128  ni_aux_data_t *aux_data = NULL;
129 
130  switch (api_param->reconf_demo_mode)
131  {
133  if (p_enc_ctx->frame_num ==
134  api_param->reconf_hash[p_ctx->reconfig_count][0])
135  {
136  aux_data = ni_frame_new_aux_data(
137  frame, NI_FRAME_AUX_DATA_BITRATE, sizeof(int32_t));
138  if (!aux_data)
139  {
141  "Error %s(): no mem for reconf BR aux_data\n",
142  __func__);
143  return;
144  }
145  *((int32_t *)aux_data->data) =
146  api_param->reconf_hash[p_ctx->reconfig_count][1];
147  ni_log(NI_LOG_DEBUG, "%s(): frame #%lu reconfig BR %d by frame aux data\n",
148  __func__, p_enc_ctx->frame_num,
149  api_param->reconf_hash[p_ctx->reconfig_count][1]);
150 
151  p_ctx->reconfig_count++;
152  }
153  break;
155  if (p_enc_ctx->frame_num ==
156  api_param->reconf_hash[p_ctx->reconfig_count][0])
157  {
158  aux_data = ni_frame_new_aux_data(
159  frame, NI_FRAME_AUX_DATA_INTRAPRD, sizeof(int32_t));
160  if (!aux_data)
161  {
162  return;
163  }
164  int32_t intraprd = *((int32_t *)aux_data->data) =
165  api_param->reconf_hash[p_ctx->reconfig_count][1];
167  "xcoder_send_frame: frame #%lu reconf "
168  "intraPeriod %d\n",
169  p_enc_ctx->frame_num,
170  intraprd);
171  p_ctx->reconfig_count++;
172  }
173  break;
175  if (p_enc_ctx->frame_num ==
176  api_param->reconf_hash[p_ctx->reconfig_count][0])
177  {
178  p_enc_ctx->enc_change_params->enable_option |=
180  p_enc_ctx->enc_change_params->colorDescPresent =
181  api_param->reconf_hash[p_ctx->reconfig_count][1];
182  p_enc_ctx->enc_change_params->colorPrimaries =
183  api_param->reconf_hash[p_ctx->reconfig_count][2];
184  p_enc_ctx->enc_change_params->colorTrc =
185  api_param->reconf_hash[p_ctx->reconfig_count][3];
186  p_enc_ctx->enc_change_params->colorSpace =
187  api_param->reconf_hash[p_ctx->reconfig_count][4];
188  p_enc_ctx->enc_change_params->aspectRatioWidth =
189  api_param->reconf_hash[p_ctx->reconfig_count][5];
191  api_param->reconf_hash[p_ctx->reconfig_count][6];
192  p_enc_ctx->enc_change_params->videoFullRange =
193  api_param->reconf_hash[p_ctx->reconfig_count][7];
194 
195  // frame reconf_len needs to be set here
196  frame->reconf_len = sizeof(ni_encoder_change_params_t);
197  p_ctx->reconfig_count++;
198  }
199  break;
201  // the reconf file data line format for this is:
202  // <frame-number>:useCurSrcAsLongtermPic,useLongtermRef where
203  // values will stay the same on every frame until changed.
204  if (p_enc_ctx->frame_num ==
205  api_param->reconf_hash[p_ctx->reconfig_count][0])
206  {
207  aux_data = ni_frame_new_aux_data(
209  sizeof(ni_long_term_ref_t));
210  if (!aux_data)
211  {
213  "Error %s(): no mem for reconf LTR aux_data\n",
214  __func__);
215  return;
216  }
217  ni_long_term_ref_t *ltr = (ni_long_term_ref_t *)aux_data->data;
219  (uint8_t)api_param->reconf_hash[p_ctx->reconfig_count][1];
220  ltr->use_long_term_ref =
221  (uint8_t)api_param->reconf_hash[p_ctx->reconfig_count][2];
222 
224  "%s(): frame #%lu reconf LTR "
225  "use_cur_src_as_long_term_pic %u use_long_term_ref "
226  "%u\n",
227  __func__, p_enc_ctx->frame_num,
229 
230  p_ctx->reconfig_count++;
231  }
232  break;
235  if (p_enc_ctx->frame_num ==
236  api_param->reconf_hash[p_ctx->reconfig_count][0])
237  {
238  aux_data = ni_frame_new_aux_data(
240  if (!aux_data) {
242  "Error %s(): no mem for reconf max&min QP aux_data\n",
243  __func__);
244  return;
245  }
246  ni_rc_min_max_qp *qp_info = (ni_rc_min_max_qp *)aux_data->data;
247  qp_info->minQpI = api_param->reconf_hash[p_ctx->reconfig_count][1];
248  qp_info->maxQpI = api_param->reconf_hash[p_ctx->reconfig_count][2];
249  qp_info->maxDeltaQp = api_param->reconf_hash[p_ctx->reconfig_count][3];
250  qp_info->minQpPB = api_param->reconf_hash[p_ctx->reconfig_count][4];
251  qp_info->maxQpPB = api_param->reconf_hash[p_ctx->reconfig_count][5];
252 
253  p_ctx->reconfig_count++;
254  }
255  break;
257  if (p_enc_ctx->frame_num ==
258  api_param->reconf_hash[p_ctx->reconfig_count][0])
259  {
260  aux_data = ni_frame_new_aux_data(
261  frame, NI_FRAME_AUX_DATA_LTR_INTERVAL, sizeof(int32_t));
262  if (!aux_data)
263  {
264  ni_log(NI_LOG_ERROR, "Error %s(): no mem for reconf LTR interval "
265  "aux_data\n",
266  __func__);
267  return;
268  }
269  *((int32_t *)aux_data->data) =
270  api_param->reconf_hash[p_ctx->reconfig_count][1];
271  ni_log(NI_LOG_DEBUG, "%s(): frame #%lu reconf LTR interval %d\n",
272  __func__, p_enc_ctx->frame_num,
273  *((int32_t *)aux_data->data));
274 
275  p_ctx->reconfig_count++;
276  }
277  break;
279  if (p_enc_ctx->frame_num ==
280  api_param->reconf_hash[p_ctx->reconfig_count][0])
281  {
282  aux_data = ni_frame_new_aux_data(
284  sizeof(int32_t));
285  if (!aux_data)
286  {
287  ni_log(NI_LOG_ERROR, "Error %s(): no mem for reconf invalid ref "
288  "frame aux_data\n",
289  __func__);
290  return;
291  }
292  *((int32_t *)aux_data->data) =
293  api_param->reconf_hash[p_ctx->reconfig_count][1];
294  ni_log(NI_LOG_DEBUG, "%s(): frame #%lu reconf invalid ref frame %d\n",
295  __func__, p_enc_ctx->frame_num,
296  *((int32_t *)aux_data->data));
297 
298  p_ctx->reconfig_count++;
299  }
300  break;
302  if (p_enc_ctx->frame_num ==
303  api_param->reconf_hash[p_ctx->reconfig_count][0])
304  {
305  aux_data = ni_frame_new_aux_data(
307  if (!aux_data)
308  {
310  "Error %s(): no mem for reconf framerate aux_data\n",
311  __func__);
312  return;
313  }
314  ni_framerate_t *framerate = (ni_framerate_t *)aux_data->data;
315  framerate->framerate_num =
316  (int32_t)api_param->reconf_hash[p_ctx->reconfig_count][1];
317  framerate->framerate_denom =
318  (int32_t)api_param->reconf_hash[p_ctx->reconfig_count][2];
319 
321  "%s(): frame #%lu reconfig framerate (%d/%d) by frame aux data\n",
322  __func__, p_enc_ctx->frame_num, framerate->framerate_num,
323  framerate->framerate_denom);
324 
325  p_ctx->reconfig_count++;
326  }
327  break;
329  if (p_enc_ctx->frame_num ==
330  api_param->reconf_hash[p_ctx->reconfig_count][0])
331  {
332  aux_data = ni_frame_new_aux_data(
333  frame, NI_FRAME_AUX_DATA_MAX_FRAME_SIZE, sizeof(int32_t));
334  if (!aux_data)
335  {
337  "Error %s(): no mem for reconf maxFrameSize aux_data\n",
338  __func__);
339  return;
340  }
341  *((int32_t *)aux_data->data) =
342  api_param->reconf_hash[p_ctx->reconfig_count][1];
343  ni_log(NI_LOG_DEBUG, "%s(): frame #%lu reconfig maxFrameSize %d by frame aux data\n",
344  __func__, p_enc_ctx->frame_num,
345  api_param->reconf_hash[p_ctx->reconfig_count][1]);
346 
347  p_ctx->reconfig_count++;
348  }
349  break;
351  if (p_enc_ctx->frame_num ==
352  api_param->reconf_hash[p_ctx->reconfig_count][0])
353  {
354  aux_data = ni_frame_new_aux_data(
355  frame, NI_FRAME_AUX_DATA_CRF, sizeof(int32_t));
356  if (!aux_data) {
358  "Error %s(): no mem for reconf crf aux_data\n",
359  __func__);
360  return;
361  }
362  *((int32_t *)aux_data->data) =
363  api_param->reconf_hash[p_ctx->reconfig_count][1];
364  ni_log(NI_LOG_DEBUG, "%s(): frame #%lu reconfig crf %d by frame aux data\n",
365  __func__, p_enc_ctx->frame_num,
366  api_param->reconf_hash[p_ctx->reconfig_count][1]);
367 
368  p_ctx->reconfig_count++;
369  }
370  break;
372  if (p_enc_ctx->frame_num ==
373  api_param->reconf_hash[p_ctx->reconfig_count][0])
374  {
375  aux_data = ni_frame_new_aux_data(
376  frame, NI_FRAME_AUX_DATA_CRF_FLOAT, sizeof(float));
377  if (!aux_data) {
379  "Error %s(): no mem for reconf crf aux_data\n",
380  __func__);
381  return;
382  }
383  float crf = (float)(api_param->reconf_hash[p_ctx->reconfig_count][1] +
384  (float)api_param->reconf_hash[p_ctx->reconfig_count][2] / 100.0);
385  *((float *)aux_data->data) = crf;
387  "%s(): frame #%lu reconfig float type crf %f by frame "
388  "aux data\n", __func__, p_enc_ctx->frame_num, crf);
389  p_ctx->reconfig_count++;
390  }
391  break;
392 
394  if (p_enc_ctx->frame_num ==
395  api_param->reconf_hash[p_ctx->reconfig_count][0])
396  {
397  aux_data = ni_frame_new_aux_data(
398  frame, NI_FRAME_AUX_DATA_VBV_MAX_RATE, sizeof(int32_t));
399  if (!aux_data) {
401  "Error %s(): no mem for reconf vbfMaxRate aux_data\n",
402  __func__);
403  return;
404  }
405  *((int32_t *)aux_data->data) =
406  api_param->reconf_hash[p_ctx->reconfig_count][1];
407  aux_data = ni_frame_new_aux_data(
408  frame, NI_FRAME_AUX_DATA_VBV_BUFFER_SIZE, sizeof(int32_t));
409  if (!aux_data) {
411  "Error %s(): no mem for reconf vbvBufferSize aux_data\n",
412  __func__);
413  return;
414  }
415  *((int32_t *)aux_data->data) =
416  api_param->reconf_hash[p_ctx->reconfig_count][2];
418  "%s(): frame #%lu reconfig vbfMaxRate %d vbvBufferSize "
419  "%d by frame aux data\n",
420  __func__, p_enc_ctx->frame_num,
421  api_param->reconf_hash[p_ctx->reconfig_count][1],
422  api_param->reconf_hash[p_ctx->reconfig_count][2]);
423 
424  p_ctx->reconfig_count++;
425  }
426  break;
428  if (p_enc_ctx->frame_num ==
429  api_param->reconf_hash[p_ctx->reconfig_count][0]) {
430  int maxFrameSizeRatio = api_param->reconf_hash[p_ctx->reconfig_count][1];
431  if (maxFrameSizeRatio < 1) {
432  ni_log(NI_LOG_ERROR, "maxFrameSizeRatio %d cannot < 1\n",
433  maxFrameSizeRatio);
434  return;
435  }
436  aux_data = ni_frame_new_aux_data(
437  frame, NI_FRAME_AUX_DATA_MAX_FRAME_SIZE, sizeof(int32_t));
438  if (!aux_data) {
440  "Error %s(): no mem for reconf maxFrameSizeRatio aux_data\n",
441  __func__);
442  return;
443  }
444 
445  int32_t bitrate, framerate_num, framerate_denom;
446  uint32_t min_maxFrameSize, maxFrameSize;
447  bitrate = (p_enc_ctx->target_bitrate > 0) ? p_enc_ctx->target_bitrate : api_param->bitrate;
448 
449  if ((p_enc_ctx->framerate.framerate_num > 0) && (p_enc_ctx->framerate.framerate_denom > 0))
450  {
451  framerate_num = p_enc_ctx->framerate.framerate_num;
452  framerate_denom = p_enc_ctx->framerate.framerate_denom;
453  }
454  else
455  {
456  framerate_num = (int32_t) api_param->fps_number;
457  framerate_denom = (int32_t) api_param->fps_denominator;
458  }
459 
460  min_maxFrameSize = ((uint32_t)bitrate / framerate_num * framerate_denom) / 8;
461  maxFrameSize = min_maxFrameSize * maxFrameSizeRatio > NI_MAX_FRAME_SIZE ?
462  NI_MAX_FRAME_SIZE : min_maxFrameSize * maxFrameSizeRatio;
463  *((int32_t *)aux_data->data) = maxFrameSize;
465  "xcoder_send_frame: frame #%lu reconf "
466  "maxFrameSizeRatio %d maxFrameSize %d\n",
467  p_enc_ctx->frame_num, maxFrameSizeRatio, maxFrameSize);
468 
469  p_ctx->reconfig_count++;
470  }
471  break;
473  if (p_enc_ctx->frame_num ==
474  api_param->reconf_hash[p_ctx->reconfig_count][0]) {
475  aux_data = ni_frame_new_aux_data(
476  frame, NI_FRAME_AUX_DATA_SLICE_ARG, sizeof(int16_t));
477  if (!aux_data) {
479  "Error %s(): no mem for reconf sliceArg aux_data\n",
480  __func__);
481  return;
482  }
483  *((int16_t *)aux_data->data) =
484  api_param->reconf_hash[p_ctx->reconfig_count][1];
485  ni_log2(p_enc_ctx, NI_LOG_TRACE,
486  "xcoder_send_frame: frame #%lu reconf "
487  "sliceArg %d\n",
488  p_enc_ctx->frame_num,
489  api_param->reconf_hash[p_ctx->reconfig_count][1]);
490 
491  p_ctx->reconfig_count++;
492  }
493  break;
494 
496  if (p_enc_ctx->frame_num ==
497  api_param->reconf_hash[p_ctx->reconfig_count][0])
498  {
499  ni_force_idr_frame_type(p_enc_ctx);
500  ni_log(NI_LOG_DEBUG, "%s(): frame #%lu force IDR frame\n", __func__,
501  p_enc_ctx->frame_num);
502  p_ctx->reconfig_count++;
503  }
504  break;
506  if (p_enc_ctx->frame_num ==
507  api_param->reconf_hash[p_ctx->reconfig_count][0])
508  {
509  ni_reconfig_bitrate(p_enc_ctx,
510  api_param->reconf_hash[p_ctx->reconfig_count][1]);
511  ni_log(NI_LOG_DEBUG, "%s(): frame #%lu API reconfig BR %d\n",
512  __func__, p_enc_ctx->frame_num,
513  api_param->reconf_hash[p_ctx->reconfig_count][1]);
514  p_ctx->reconfig_count++;
515  }
516  break;
518  if (p_enc_ctx->frame_num ==
519  api_param->reconf_hash[p_ctx->reconfig_count][0]) {
520  int32_t intraprd =
521  api_param->reconf_hash[p_ctx->reconfig_count][1];
522  ni_reconfig_intraprd(p_enc_ctx, intraprd);
524  "xcoder_send_frame: frame #%lu API reconfig intraPeriod %d\n",
525  p_enc_ctx->frame_num,
526  intraprd);
527 
528  p_ctx->reconfig_count++;
529  }
530  break;
532  if (p_enc_ctx->frame_num ==
533  api_param->reconf_hash[p_ctx->reconfig_count][0])
534  {
535  ni_vui_hrd_t vui;
536  vui.colorDescPresent =
537  (uint8_t)api_param->reconf_hash[p_ctx->reconfig_count][1];
538  vui.colorPrimaries =
539  (uint8_t)api_param->reconf_hash[p_ctx->reconfig_count][2];
540  vui.colorTrc =
541  (uint8_t)api_param->reconf_hash[p_ctx->reconfig_count][3];
542  vui.colorSpace =
543  (uint8_t)api_param->reconf_hash[p_ctx->reconfig_count][4];
544  vui.aspectRatioWidth =
545  (uint16_t)api_param->reconf_hash[p_ctx->reconfig_count][5];
546  vui.aspectRatioHeight =
547  (uint16_t)api_param->reconf_hash[p_ctx->reconfig_count][6];
548  vui.videoFullRange =
549  (uint8_t)api_param->reconf_hash[p_ctx->reconfig_count][7];
550 
551  ni_reconfig_vui(p_enc_ctx, &vui);
552  ni_log(NI_LOG_DEBUG, "%s(): frame #%lu API reconfig VUI HRD "
553  "colorDescPresent %d colorPrimaries %d "
554  "colorTrc %d colorSpace %d aspectRatioWidth %d "
555  "aspectRatioHeight %d videoFullRange %d\n",
556  __func__, p_enc_ctx->frame_num,
557  api_param->reconf_hash[p_ctx->reconfig_count][1],
558  api_param->reconf_hash[p_ctx->reconfig_count][2],
559  api_param->reconf_hash[p_ctx->reconfig_count][3],
560  api_param->reconf_hash[p_ctx->reconfig_count][4],
561  api_param->reconf_hash[p_ctx->reconfig_count][5],
562  api_param->reconf_hash[p_ctx->reconfig_count][6],
563  api_param->reconf_hash[p_ctx->reconfig_count][7]);
564  p_ctx->reconfig_count++;
565  }
566  break;
568  if (p_enc_ctx->frame_num ==
569  api_param->reconf_hash[p_ctx->reconfig_count][0])
570  {
571  ni_long_term_ref_t ltr;
573  (uint8_t)api_param->reconf_hash[p_ctx->reconfig_count][1];
574  ltr.use_long_term_ref =
575  (uint8_t)api_param->reconf_hash[p_ctx->reconfig_count][2];
576 
577  ni_set_ltr(p_enc_ctx, &ltr);
578  ni_log(NI_LOG_DEBUG, "%s(): frame #%lu API set LTR\n", __func__,
579  p_enc_ctx->frame_num);
580  p_ctx->reconfig_count++;
581  }
582  break;
585  if (p_enc_ctx->frame_num ==
586  api_param->reconf_hash[p_ctx->reconfig_count][0])
587  {
588  ni_rc_min_max_qp qp_info;
589  qp_info.minQpI = (int32_t)api_param->reconf_hash[p_ctx->reconfig_count][1];
590  qp_info.maxQpI = (int32_t)api_param->reconf_hash[p_ctx->reconfig_count][2];
591  qp_info.maxDeltaQp = (int32_t)api_param->reconf_hash[p_ctx->reconfig_count][3];
592  qp_info.minQpPB = (int32_t)api_param->reconf_hash[p_ctx->reconfig_count][4];
593  qp_info.maxQpPB = (int32_t)api_param->reconf_hash[p_ctx->reconfig_count][5];
594  ni_reconfig_min_max_qp(p_enc_ctx, &qp_info);
596  "%s(): frame %llu minQpI %d maxQpI %d maxDeltaQp %d minQpPB %d maxQpPB %d\n",
597  __func__, p_enc_ctx->frame_num,
598  qp_info.minQpI, qp_info.maxQpI, qp_info.maxDeltaQp, qp_info.minQpPB, qp_info.maxQpPB);
599  p_ctx->reconfig_count++;
600  }
601  break;
603  if (p_enc_ctx->frame_num ==
604  api_param->reconf_hash[p_ctx->reconfig_count][0])
605  {
606  ni_set_ltr_interval(p_enc_ctx,
607  api_param->reconf_hash[p_ctx->reconfig_count][1]);
608  ni_log(NI_LOG_DEBUG, "%s(): frame #%lu API set LTR interval %d\n",
609  __func__, p_enc_ctx->frame_num,
610  api_param->reconf_hash[p_ctx->reconfig_count][1]);
611  p_ctx->reconfig_count++;
612  }
613  break;
615  if (p_enc_ctx->frame_num ==
616  api_param->reconf_hash[p_ctx->reconfig_count][0])
617  {
619  p_enc_ctx, api_param->reconf_hash[p_ctx->reconfig_count][1]);
620  ni_log(NI_LOG_DEBUG, "%s(): frame #%lu API set frame ref invalid "
621  "%d\n",
622  __func__, p_enc_ctx->frame_num,
623  api_param->reconf_hash[p_ctx->reconfig_count][1]);
624  p_ctx->reconfig_count++;
625  }
626  break;
628  if (p_enc_ctx->frame_num ==
629  api_param->reconf_hash[p_ctx->reconfig_count][0])
630  {
631  ni_framerate_t framerate;
632  framerate.framerate_num =
633  (int32_t)api_param->reconf_hash[p_ctx->reconfig_count][1];
634  framerate.framerate_denom =
635  (int32_t)api_param->reconf_hash[p_ctx->reconfig_count][2];
636  ni_reconfig_framerate(p_enc_ctx, &framerate);
638  "%s(): frame #%lu API reconfig framerate (%d/%d)\n",
639  __func__, p_enc_ctx->frame_num,
640  api_param->reconf_hash[p_ctx->reconfig_count][1],
641  api_param->reconf_hash[p_ctx->reconfig_count][2]);
642  p_ctx->reconfig_count++;
643  }
644  break;
646  if (p_enc_ctx->frame_num ==
647  api_param->reconf_hash[p_ctx->reconfig_count][0])
648  {
649  ni_reconfig_max_frame_size(p_enc_ctx,
650  api_param->reconf_hash[p_ctx->reconfig_count][1]);
651  ni_log(NI_LOG_DEBUG, "%s(): frame #%lu API reconfig maxFrameSize %d\n",
652  __func__, p_enc_ctx->frame_num,
653  api_param->reconf_hash[p_ctx->reconfig_count][1]);
654  p_ctx->reconfig_count++;
655  }
656  break;
657  case XCODER_TEST_CRF_API:
658  if (p_enc_ctx->frame_num ==
659  api_param->reconf_hash[p_ctx->reconfig_count][0])
660  {
661  ni_reconfig_crf(p_enc_ctx,
662  api_param->reconf_hash[p_ctx->reconfig_count][1]);
663  ni_log(NI_LOG_DEBUG, "%s(): frame #%lu API reconfig crf %d\n",
664  __func__, p_enc_ctx->frame_num,
665  api_param->reconf_hash[p_ctx->reconfig_count][1]);
666  p_ctx->reconfig_count++;
667  }
668  break;
670  if (p_enc_ctx->frame_num ==
671  api_param->reconf_hash[p_ctx->reconfig_count][0])
672  {
673  float crf = (float)(api_param->reconf_hash[p_ctx->reconfig_count][1] +
674  (float)api_param->reconf_hash[p_ctx->reconfig_count][2] / 100.0);
675  ni_reconfig_crf2(p_enc_ctx, crf);
676  ni_log(NI_LOG_DEBUG, "%s(): frame #%lu API reconfig crf %f\n",
677  __func__, p_enc_ctx->frame_num, crf);
678  p_ctx->reconfig_count++;
679  }
680  break;
682  if (p_enc_ctx->frame_num ==
683  api_param->reconf_hash[p_ctx->reconfig_count][0])
684  {
686  p_enc_ctx, api_param->reconf_hash[p_ctx->reconfig_count][1],
687  api_param->reconf_hash[p_ctx->reconfig_count][2]);
688  ni_log(NI_LOG_DEBUG, "%s(): frame #%lu API reconfig vbvMaxRate %d vbvBufferSize %d\n",
689  __func__, p_enc_ctx->frame_num,
690  api_param->reconf_hash[p_ctx->reconfig_count][1],
691  api_param->reconf_hash[p_ctx->reconfig_count][2]);
692  p_ctx->reconfig_count++;
693  }
694  break;
696  if (p_enc_ctx->frame_num ==
697  api_param->reconf_hash[p_ctx->reconfig_count][0])
698  {
700  p_enc_ctx, api_param->reconf_hash[p_ctx->reconfig_count][1]);
702  "xcoder_send_frame: frame #%lu reconf maxFrameSizeRatio %d\n",
703  p_enc_ctx->frame_num, api_param->reconf_hash[p_ctx->reconfig_count][1]);
704 
705  p_ctx->reconfig_count++;
706  }
707  break;
709  if (p_enc_ctx->frame_num ==
710  api_param->reconf_hash[p_ctx->reconfig_count][0]) {
712  p_enc_ctx, api_param->reconf_hash[p_ctx->reconfig_count][1]);
714  "xcoder_send_frame: frame #%lu API reconfig sliceArg %d\n",
715  p_enc_ctx->frame_num,
716  api_param->reconf_hash[p_ctx->reconfig_count][1]);
717 
718  p_ctx->reconfig_count++;
719  }
720  break;
722  default:;
723  }
724 }
725 
726 /*!*****************************************************************************
727  * \brief Send encoder input data, read from input file
728  *
729  * Note: For optimal performance, yuv_buf should be 4k aligned
730  *
731  * \param
732  *
733  * \return
734  ******************************************************************************/
736  ni_session_data_io_t *p_in_data, void *yuv_buf,
737  int input_video_width, int input_video_height,
738  int is_last_input)
739 {
740  int oneSent;
741  ni_frame_t *p_in_frame = &p_in_data->data.frame;
742  uint8_t enc_id = p_ctx->curr_enc_index;
743 
744  ni_log(NI_LOG_DEBUG, "===> encoder_send_data <===\n");
745 
747  {
748  ni_log(NI_LOG_DEBUG, "encoder_send_data: Sequence Change - waiting "
749  "for previous session to end\n");
751  }
752 
753  if (p_ctx->enc_eos_sent[enc_id] == 1)
754  {
755  ni_log(NI_LOG_DEBUG, "encoder_send_data: ALL data (incl. eos) sent "
756  "already!\n");
758  }
759 
760  if (p_ctx->enc_resend[enc_id])
761  {
762  goto send_frame;
763  }
764 
765  p_in_frame->start_of_stream = 0;
766  if (!p_ctx->enc_sos_sent[enc_id] ||
768  {
769  p_ctx->enc_sos_sent[enc_id] = 1;
770  p_in_frame->start_of_stream = 1;
771  }
772  p_in_frame->end_of_stream = 0;
773  p_in_frame->force_key_frame = 0;
774 
775  p_in_frame->video_width = input_video_width;
776  p_in_frame->video_height = input_video_height;
777 
778  // reset encoder change data buffer
779  memset(p_enc_ctx->enc_change_params, 0, sizeof(ni_encoder_change_params_t));
780 
781  // reset various aux data size
782  p_in_frame->roi_len = 0;
783  p_in_frame->reconf_len = 0;
784  p_in_frame->sei_total_len = 0;
785 
786  // collect encode reconfig and demo info and save them as aux data in
787  // the input frame struct.
788  prep_reconf_demo_data(p_ctx, p_enc_ctx, p_in_frame);
789 
790  if (yuv_buf == NULL)
791  {
792  if (is_last_input)
793  {
794  p_in_frame->end_of_stream = 1;
795  ni_log(NI_LOG_DEBUG, "encoder_send_data: read chunk size 0, eos!\n");
796  }
797  else
798  {
799  ni_log(NI_LOG_DEBUG, "encoder_send_data: exit to get next input\n");
801  }
802  }
803 
804 send_frame:
805  // simulate FFmpeg -re option, which controls process input rate to simualte real time environment
806  if (p_ctx->read_framerate > 0)
807  {
808  uint64_t abs_time_ns;
809  abs_time_ns = ni_gettime_ns();
810  if ((abs_time_ns - p_ctx->start_time) < (uint64_t)(1000000000LL / p_ctx->read_framerate * p_ctx->num_frames_sent[enc_id]))
811  {
812  if (!p_ctx->enc_resend[enc_id])
813  p_ctx->enc_resend[enc_id] = 2;
814  if (p_ctx->enc_resend[enc_id] == 2)
815  return NI_TEST_RETCODE_EAGAIN;
816  }
817  else
818  {
819  p_ctx->num_frames_sent[enc_id]++;
820  }
821  }
822 
823  oneSent = ni_enc_write_from_yuv_buffer(p_enc_ctx, p_in_frame, yuv_buf);
824  if (oneSent < 0)
825  {
827  "Error: failed ni_device_session_write() for encoder\n");
828  p_ctx->enc_resend[enc_id] = 1;
830  } else if (oneSent == 0 && !p_enc_ctx->ready_to_close)
831  {
832  p_ctx->enc_resend[enc_id] = 1;
833  return NI_TEST_RETCODE_EAGAIN;
834  } else
835  {
836  p_ctx->enc_resend[enc_id] = 0;
837 
838  p_ctx->enc_total_bytes_sent[enc_id] += p_in_frame->data_len[0] +
839  p_in_frame->data_len[1] + p_in_frame->data_len[2] + p_in_frame->data_len[3];
840  ni_log(NI_LOG_DEBUG, "encoder_send_data: total sent data size=%lu\n",
841  p_ctx->enc_total_bytes_sent[enc_id]);
842 
843  ni_log(NI_LOG_DEBUG, "encoder_send_data: success\n");
844 
845  if (p_enc_ctx->ready_to_close)
846  {
848  {
849  p_ctx->enc_eos_sent[enc_id] = 1;
850  }
851  }
852 
854  {
857  "encoder_send_data: session_run_state change to %d\n",
858  p_enc_ctx->session_run_state);
859  }
860  }
861 
862  ni_frame_wipe_aux_data(p_in_frame);
864 }
865 
866 /*******************************************************************************
867  * @brief Send encoder input data, directly after receiving from decoder
868  *
869  * @param p_enc_ctx encoder context
870  * p_dec_ctx decoder context
871  * p_dec_out_data frame returned by decoder
872  * p_enc_in_data frame to be sent to encoder
873  *
874  * @return
875  ******************************************************************************/
877  ni_session_data_io_t *p_dec_out_data,
878  ni_session_data_io_t *p_enc_in_data,
879  int input_video_width, int input_video_height)
880 {
881  int oneSent;
882  int data_len_to_send;
883  // frame pointer to data frame struct to be sent
884  ni_frame_t *p_in_frame = &(p_enc_in_data->data.frame);
885  ni_xcoder_params_t *api_params =
886  (ni_xcoder_params_t *)p_enc_ctx->p_session_config;
887  int is_semiplanar = get_pixel_planar(p_enc_ctx->pixel_format) == NI_PIXEL_PLANAR_FORMAT_SEMIPLANAR;
888  int is_hwframe = p_enc_ctx->hw_action != NI_CODEC_HW_NONE;
889  uint8_t enc_id = p_ctx->curr_enc_index;
890 
891  ni_log(NI_LOG_DEBUG, "===> encoder_send_data2 <===\n");
892 
893  if (p_ctx->enc_eos_sent[enc_id] == 1)
894  {
895  ni_log(NI_LOG_DEBUG, "encoder_send_data2: ALL data (incl. eos) sent "
896  "already!\n");
898  }
899 
900  // don't send new data before flushing completed
902  {
903  return NI_TEST_RETCODE_EAGAIN;
904  }
905 
906 //#define ENCODER_FLUSH_INJECT
907 // Note that this demo only supports in multi threads transcoding mode
908 // For examples: sudo ./xcoder -c 0 -i input.h265 -m h2h -b 8 -o output.h265 -t
909 #ifdef ENCODER_FLUSH_INJECT
910  if ((p_enc_ctx->frame_num > 0 && p_enc_ctx->frame_num % 30 == 0) &&
911  !p_dec_out_data->data.frame.end_of_stream)
912  {
913  // send the encoder flush command
915  {
916  // need to change the state
918  ni_log(NI_LOG_INFO, "encoder_send_data2 flush encoder successfully\n");
919  return NI_TEST_RETCODE_EAGAIN;
920  }
921  else
922  {
923  ni_log(NI_LOG_ERROR, "Error: encoder_send_data2 flush encoder\n");
924  return -1;
925  }
926  }
927 #endif
928 
929  // frame resend
930  if (p_ctx->enc_resend[enc_id])
931  {
932  goto send_frame;
933  }
934 
935  // copy input frame to a new frame struct and prep for the aux data
936  p_in_frame->end_of_stream = p_dec_out_data->data.frame.end_of_stream;
937  p_in_frame->ni_pict_type = 0;
938 
939  // reset encoder change data buffer
940  memset(p_enc_ctx->enc_change_params, 0,
942 
943  // extra data starts with metadata header, and reset various aux data
944  // size
946  p_in_frame->roi_len = 0;
947  p_in_frame->reconf_len = 0;
948  p_in_frame->sei_total_len = 0;
949  p_in_frame->force_pic_qp = 0;
950 
951  // collect encode reconfig and demo info and save them in the decode out
952  // frame, to be used in the aux data prep and copy later
953  prep_reconf_demo_data(p_ctx, p_enc_ctx, &(p_dec_out_data->data.frame));
954 
955  int dst_stride[NI_MAX_NUM_DATA_POINTERS] = {0};
956  int dst_height_aligned[NI_MAX_NUM_DATA_POINTERS] = {0};
957  bool alignment_2pass_wa = (
958  (api_params->cfg_enc_params.lookAheadDepth ||
959  api_params->cfg_enc_params.crf >= 0 ||
960  api_params->cfg_enc_params.crfFloat >= 0) &&
961  (p_enc_ctx->codec_format == NI_CODEC_FORMAT_H265 ||
962  p_enc_ctx->codec_format == NI_CODEC_FORMAT_AV1));
963  ni_get_hw_yuv420p_dim(input_video_width, input_video_height,
964  p_enc_ctx->bit_depth_factor, is_semiplanar,
965  dst_stride, dst_height_aligned);
966 
967  if (alignment_2pass_wa && !is_hwframe) {
968  if (is_semiplanar) {
969  // for 2-pass encode output mismatch WA, need to extend (and
970  // pad) CbCr plane height, because 1st pass assume input 32
971  // align
972  dst_height_aligned[1] = (((dst_height_aligned[0] + 31) / 32) * 32) / 2;
973  } else {
974  // for 2-pass encode output mismatch WA, need to extend (and
975  // pad) Cr plane height, because 1st pass assume input 32 align
976  dst_height_aligned[2] = (((dst_height_aligned[0] + 31) / 32) * 32) / 2;
977  }
978  }
979 
980  // ROI demo mode takes higher priority over aux data
981  // Note: when ROI demo modes enabled, supply ROI map for the specified
982  // range frames, and 0 map for others
983  if (api_params->roi_demo_mode && api_params->cfg_enc_params.roi_enable)
984  {
985  if (p_enc_ctx->frame_num > 90 && p_enc_ctx->frame_num < 300)
986  {
987  p_in_frame->roi_len = p_enc_ctx->roi_len;
988  } else
989  {
990  p_in_frame->roi_len = 0;
991  }
992  // when ROI enabled, always have a data buffer for ROI
993  // Note: this is handled separately from ROI through side/aux data
994  p_in_frame->extra_data_len += p_enc_ctx->roi_len;
995  }
996 
997  int should_send_sei_with_frame = ni_should_send_sei_with_frame(
998  p_enc_ctx, p_in_frame->ni_pict_type, api_params);
999 
1000  // data buffer for various SEI: HDR mastering display color volume, HDR
1001  // content light level, close caption, User data unregistered, HDR10+
1002  // etc.
1003  uint8_t mdcv_data[NI_MAX_SEI_DATA];
1004  uint8_t cll_data[NI_MAX_SEI_DATA];
1005  uint8_t cc_data[NI_MAX_SEI_DATA];
1006  uint8_t udu_data[NI_MAX_SEI_DATA];
1007  uint8_t hdrp_data[NI_MAX_SEI_DATA];
1008 
1009  // prep for auxiliary data (various SEI, ROI) in p_in_frame, based on
1010  // the data returned in decoded frame and also reconfig and demo modes
1011  // collected in prep_reconf_demo_data
1013  p_enc_ctx, p_in_frame, &(p_dec_out_data->data.frame),
1014  p_enc_ctx->codec_format, should_send_sei_with_frame, mdcv_data,
1015  cll_data, cc_data, udu_data, hdrp_data);
1016 
1017  p_in_frame->extra_data_len += p_in_frame->sei_total_len;
1018 
1019  // data layout requirement: leave space for reconfig data if at least
1020  // one of reconfig, SEI or ROI is present
1021  // Note: ROI is present when enabled, so use encode config flag instead
1022  // of frame's roi_len as it can be 0 indicating a 0'd ROI map
1023  // setting !
1024  if (p_in_frame->reconf_len || p_in_frame->sei_total_len ||
1025  (api_params->roi_demo_mode &&
1026  api_params->cfg_enc_params.roi_enable))
1027  {
1028  p_in_frame->extra_data_len += sizeof(ni_encoder_change_params_t);
1029  }
1030 
1031  if (!is_hwframe)
1032  {
1034  p_in_frame, input_video_width, input_video_height, dst_stride,
1035  p_enc_ctx->codec_format == NI_CODEC_FORMAT_H264,
1036  (int)(p_in_frame->extra_data_len), alignment_2pass_wa);
1037  if (!p_in_frame->p_data[0])
1038  {
1039  ni_log(NI_LOG_ERROR, "Error: cannot allocate YUV frame buffer!");
1040  return NI_TEST_RETCODE_FAILURE;
1041  }
1042  } else
1043  {
1044  ni_frame_buffer_alloc_hwenc(p_in_frame, input_video_width,
1045  input_video_height,
1046  (int)(p_in_frame->extra_data_len));
1047  if (!p_in_frame->p_data[3])
1048  {
1049  ni_log(NI_LOG_ERROR, "Error: cannot allocate YUV frame buffer!");
1050  return NI_TEST_RETCODE_FAILURE;
1051  }
1052  }
1053 
1055  "p_dst alloc linesize = %d/%d/%d src height=%d "
1056  "dst height aligned = %d/%d/%d force_key_frame=%d, "
1057  "extra_data_len=%u"
1058  " sei_size=%u (hdr_content_light_level %u hdr_mastering_display_"
1059  "color_vol %u hdr10+ %u hrd %d) reconf_size=%u roi_size=%u "
1060  "force_pic_qp=%u udu_sei_size=%u "
1061  "use_cur_src_as_long_term_pic %u use_long_term_ref %u\n",
1062  dst_stride[0], dst_stride[1], dst_stride[2], input_video_height,
1063  dst_height_aligned[0], dst_height_aligned[1], dst_height_aligned[2],
1064  p_in_frame->force_key_frame, p_in_frame->extra_data_len,
1065  p_in_frame->sei_total_len,
1068  p_in_frame->sei_hdr_plus_len, 0, /* hrd is 0 size for now */
1069  p_in_frame->reconf_len, p_in_frame->roi_len,
1070  p_in_frame->force_pic_qp, p_in_frame->sei_user_data_unreg_len,
1071  p_in_frame->use_cur_src_as_long_term_pic,
1072  p_in_frame->use_long_term_ref);
1073 
1074  uint8_t *p_src[NI_MAX_NUM_DATA_POINTERS];
1075  int src_stride[NI_MAX_NUM_DATA_POINTERS];
1076  int src_height[NI_MAX_NUM_DATA_POINTERS];
1077 
1078  src_height[0] = p_dec_out_data->data.frame.video_height;
1079  src_height[1] = src_height[2] = src_height[0] / 2;
1080  src_height[3] = 0;
1081 
1082  src_stride[0] =
1083  (int)(p_dec_out_data->data.frame.data_len[0]) / src_height[0];
1084  src_stride[1] =
1085  (int)(p_dec_out_data->data.frame.data_len[1]) / src_height[1];
1086  src_stride[2] = src_stride[1];
1087  if (is_semiplanar)
1088  {
1089  src_height[2] = 0;
1090  src_stride[2] = 0;
1091  }
1092  src_stride[3] = 0;
1093 
1094  p_src[0] = p_dec_out_data->data.frame.p_data[0];
1095  p_src[1] = p_dec_out_data->data.frame.p_data[1];
1096  p_src[2] = p_dec_out_data->data.frame.p_data[2];
1097  p_src[3] = p_dec_out_data->data.frame.p_data[3];
1098 
1099  if (!is_hwframe)
1100  { // YUV part of the encoder input data layout
1102  (uint8_t **)(p_in_frame->p_data), p_src, input_video_width,
1103  input_video_height, p_enc_ctx->bit_depth_factor, is_semiplanar,
1104  ((ni_xcoder_params_t *)p_enc_ctx->p_session_config)
1105  ->cfg_enc_params.conf_win_right,
1106  dst_stride, dst_height_aligned, src_stride, src_height);
1107  } else
1108  {
1109  ni_copy_hw_descriptors((uint8_t **)(p_in_frame->p_data), p_src);
1110  }
1111  // auxiliary data part of the encoder input data layout
1112  ni_enc_copy_aux_data(p_enc_ctx, p_in_frame,
1113  &(p_dec_out_data->data.frame),
1114  p_enc_ctx->codec_format, mdcv_data, cll_data,
1115  cc_data, udu_data, hdrp_data, is_hwframe,
1116  is_semiplanar);
1117 
1118  p_in_frame->video_width = input_video_width;
1119  p_in_frame->video_height = input_video_height;
1120 
1121  p_in_frame->start_of_stream = 0;
1122  if (!p_ctx->enc_sos_sent[enc_id])
1123  {
1124  p_ctx->enc_sos_sent[enc_id] = 1;
1125  p_in_frame->start_of_stream = 1;
1126  }
1127 
1128 send_frame:
1129  data_len_to_send = (int)(p_in_frame->data_len[0] + p_in_frame->data_len[1] +
1130  p_in_frame->data_len[2] + p_in_frame->data_len[3]);
1131 
1132  if (data_len_to_send > 0 || p_in_frame->end_of_stream)
1133  {
1134  oneSent = ni_device_session_write(p_enc_ctx, p_enc_in_data,
1136  p_in_frame->end_of_stream = 0;
1137  } else
1138  {
1139  return NI_TEST_RETCODE_SUCCESS;
1140  }
1141 
1142  if (oneSent < 0)
1143  {
1144  ni_log(NI_LOG_ERROR, "Error: encoder_send_data2\n");
1145  p_ctx->enc_resend[enc_id] = 1;
1146  return NI_TEST_RETCODE_FAILURE;
1147  } else if (oneSent == 0)
1148  {
1149  if (p_ctx->enc_eos_sent[enc_id] == 0 && p_enc_ctx->ready_to_close)
1150  {
1151  p_ctx->enc_resend[enc_id] = 0;
1152  p_ctx->enc_eos_sent[enc_id] = 1;
1153  } else
1154  {
1155  p_ctx->enc_resend[enc_id] = 1;
1156  return NI_TEST_RETCODE_EAGAIN;
1157  }
1158  } else
1159  {
1160  p_ctx->enc_resend[enc_id] = 0;
1161 
1162  if (p_enc_ctx->ready_to_close)
1163  {
1164  p_ctx->enc_eos_sent[enc_id] = 1;
1165  }
1166  ni_log(NI_LOG_DEBUG, "encoder_send_data2: success\n");
1167  }
1168 
1169  return NI_TEST_RETCODE_SUCCESS;
1170 }
1171 
1172 /*!*****************************************************************************
1173  * \brief Send encoder input data, read from uploader instance hwframe
1174  *
1175  * \param
1176  *
1177  * \return
1178  ******************************************************************************/
1180  ni_session_data_io_t *p_in_data,
1181  int input_video_width, int input_video_height, int eos)
1182 {
1183  int oneSent;
1184  ni_frame_t *p_in_frame = &(p_in_data->data.frame);
1185  uint8_t enc_id = p_ctx->curr_enc_index;
1186 
1187  ni_log(NI_LOG_DEBUG, "===> encoder_send_data3 <===\n");
1188 
1189  if (p_ctx->enc_eos_sent[enc_id] == 1)
1190  {
1191  ni_log(NI_LOG_DEBUG, "encoder_send_data3: ALL data (incl. eos) sent "
1192  "already!\n");
1193  return 0;
1194  }
1195 
1196  if (p_ctx->enc_resend[enc_id])
1197  {
1198  goto send_frame;
1199  }
1200 
1201  p_in_frame->start_of_stream = 0;
1202  if (!p_ctx->enc_sos_sent[enc_id])
1203  {
1204  p_ctx->enc_sos_sent[enc_id] = 1;
1205  p_in_frame->start_of_stream = 1;
1206  }
1207  p_in_frame->end_of_stream = eos;
1208  p_in_frame->force_key_frame = 0;
1209  p_in_frame->video_width = input_video_width;
1210  p_in_frame->video_height = input_video_height;
1211  if (eos)
1212  {
1213  ni_log(NI_LOG_DEBUG, "encoder_send_data3: read chunk size 0, eos!\n");
1214  }
1215 
1216  // only metadata header for now
1218 
1219 send_frame:
1220  oneSent =
1221  ni_device_session_write(p_enc_ctx, p_in_data, NI_DEVICE_TYPE_ENCODER);
1222  if (oneSent < 0)
1223  {
1225  "Error: failed ni_device_session_write() for encoder\n");
1226  p_ctx->enc_resend[enc_id] = 1;
1227  return -1;
1228  } else if (oneSent == 0 && !p_enc_ctx->ready_to_close)
1229  {
1230  p_ctx->enc_resend[enc_id] = 1;
1231  ni_log(NI_LOG_DEBUG, "NEEDED TO RESEND");
1232  return NI_TEST_RETCODE_EAGAIN;
1233  } else
1234  {
1235  p_ctx->enc_resend[enc_id] = 0;
1236 
1237  ni_log(NI_LOG_DEBUG, "encoder_send_data3: total sent data size=%u\n",
1238  p_in_frame->data_len[3]);
1239 
1240  ni_log(NI_LOG_DEBUG, "encoder_send_data3: success\n");
1241 
1242  if (p_enc_ctx->ready_to_close)
1243  {
1244  p_ctx->enc_eos_sent[enc_id] = 1;
1245  }
1246  }
1247 
1248  return 0;
1249 }
1250 
1251 /*!*****************************************************************************
1252  * \brief Encoder session open
1253  *
1254  * \param
1255  *
1256  * \return 0 if successful, < 0 otherwise
1257  ******************************************************************************/
1258 int encoder_open_session(ni_session_context_t *p_enc_ctx, int dst_codec_format,
1259  int iXcoderGUID, ni_xcoder_params_t *p_enc_params,
1260  int width, int height, ni_pix_fmt_t pix_fmt,
1261  bool check_zerocopy)
1262 {
1263  int ret = 0;
1264  bool isrgba = false;
1265 
1266  p_enc_ctx->p_session_config = p_enc_params;
1267  p_enc_ctx->session_id = NI_INVALID_SESSION_ID;
1268  p_enc_ctx->codec_format = dst_codec_format;
1269 
1270  // assign the card GUID in the encoder context and let session open
1271  // take care of the rest
1272  p_enc_ctx->device_handle = NI_INVALID_DEVICE_HANDLE;
1273  p_enc_ctx->blk_io_handle = NI_INVALID_DEVICE_HANDLE;
1274  p_enc_ctx->hw_id = iXcoderGUID;
1275 
1276  // default: little endian
1277  p_enc_ctx->src_endian = NI_FRAME_LITTLE_ENDIAN;
1278 
1279  switch (pix_fmt)
1280  {
1281  case NI_PIX_FMT_YUV420P:
1282  case NI_PIX_FMT_NV12:
1283  p_enc_ctx->src_bit_depth = 8;
1284  p_enc_ctx->bit_depth_factor = 1;
1285  break;
1287  case NI_PIX_FMT_P010LE:
1288  p_enc_ctx->src_bit_depth = 10;
1289  p_enc_ctx->bit_depth_factor = 2;
1290  break;
1291  case NI_PIX_FMT_ABGR:
1292  case NI_PIX_FMT_ARGB:
1293  case NI_PIX_FMT_RGBA:
1294  case NI_PIX_FMT_BGRA:
1295  p_enc_ctx->src_bit_depth = 8;
1296  p_enc_ctx->bit_depth_factor = 4;
1297  isrgba = true;
1298  break;
1299  default:
1300  p_enc_ctx->src_bit_depth = 8;
1301  p_enc_ctx->bit_depth_factor = 1;
1302  pix_fmt = NI_PIX_FMT_YUV420P;
1303  break;
1304  //ni_log(NI_LOG_ERROR, "%s: Invalid pixel format %s\n", __func__,
1305  // ni_pixel_format_name(pix_fmt));
1306  //return NI_RETCODE_INVALID_PARAM;
1307  }
1308 
1309  // original resolution this stream started with, this is used by encoder sequence change
1310  p_enc_ctx->ori_width = width;
1311  p_enc_ctx->ori_height = height;
1312  p_enc_ctx->ori_bit_depth_factor = p_enc_ctx->bit_depth_factor;
1313  p_enc_ctx->ori_pix_fmt = pix_fmt;
1314  p_enc_ctx->pixel_format = pix_fmt;
1315 
1316  int linesize_aligned = width;
1317  if (!isrgba)
1318  {
1319  if (linesize_aligned < NI_MIN_WIDTH)
1320  {
1321  p_enc_params->cfg_enc_params.conf_win_right +=
1322  (NI_MIN_WIDTH - width) / 2 * 2;
1323  linesize_aligned = NI_MIN_WIDTH;
1324  } else
1325  {
1326  linesize_aligned = ((width + 1) / 2) * 2;
1327  p_enc_params->cfg_enc_params.conf_win_right +=
1328  (linesize_aligned - width) / 2 * 2;
1329  }
1330  }
1331  p_enc_params->source_width = linesize_aligned;
1332 
1333  int height_aligned = height;
1334  if (!isrgba)
1335  {
1336  if (height_aligned < NI_MIN_HEIGHT)
1337  {
1338  p_enc_params->cfg_enc_params.conf_win_bottom +=
1339  (NI_MIN_HEIGHT - height) / 2 * 2;
1340  height_aligned = NI_MIN_HEIGHT;
1341  } else
1342  {
1343  height_aligned = ((height + 1) / 2) * 2;
1344  p_enc_params->cfg_enc_params.conf_win_bottom +=
1345  (height_aligned - height) / 2 * 2;
1346  }
1347  }
1348  p_enc_params->source_height = height_aligned;
1349 
1350  // default planar encoder input data
1351  p_enc_params->cfg_enc_params.planar = get_pixel_planar(pix_fmt);
1352 
1353  if (check_zerocopy)
1354  {
1355 
1356  // config linesize for zero copy (if input resolution is zero copy compatible)
1357  int src_stride[NI_MAX_NUM_DATA_POINTERS];
1358 
1359  // NOTE - FFmpeg / Gstreamer users should use linesize array in frame structure instead of src_stride in the following sample code
1360  src_stride[0] = width * p_enc_ctx->bit_depth_factor;
1361 
1362  if (isrgba)
1363  src_stride[1] = src_stride[2] = 0;
1364  else
1365  {
1366  bool isnv12frame = (p_enc_params->cfg_enc_params.planar == NI_PIXEL_PLANAR_FORMAT_SEMIPLANAR) ? true : false;
1367  src_stride[1] = isnv12frame ? src_stride[0] : src_stride[0] / 2;
1368  src_stride[2] = isnv12frame ? 0 : src_stride[0] / 2;
1369  }
1370 
1372  p_enc_params, width, height,
1373  (const int *)src_stride, true);
1374  }
1375 
1377  if (ret != NI_RETCODE_SUCCESS)
1378  {
1379  ni_log(NI_LOG_ERROR, "Error: %s failure!\n", __func__);
1380  } else
1381  {
1382  ni_log(NI_LOG_INFO, "Encoder device %d session open successful.\n", iXcoderGUID);
1383  }
1384 
1385  // set up ROI QP map for ROI demo modes if enabled
1386  if (p_enc_params->cfg_enc_params.roi_enable &&
1387  (1 == p_enc_params->roi_demo_mode || 2 == p_enc_params->roi_demo_mode))
1388  {
1389  set_demo_roi_map(p_enc_ctx);
1390  }
1391 
1392  return ret;
1393 }
1394 
1395 /*!*****************************************************************************
1396  * \brief Reopen or reconfig encoder upon sequence change
1397  *
1398  * \param
1399  *
1400  * \return 0 - success got packet
1401  * 1 - received eos
1402  * 2 - got nothing, need retry
1403  * -1 - failure
1404  ******************************************************************************/
1406  ni_session_data_io_t *p_in_data,
1407  ni_session_data_io_t *p_out_data)
1408 {
1409  int ret = NI_TEST_RETCODE_SUCCESS;
1410  int new_stride, ori_stride;
1411  bool bIsSmallPicture = false;
1412  ni_frame_t *p_buffered_frame = &(p_in_data->data.frame);
1413  ni_xcoder_params_t *p_api_param = (ni_xcoder_params_t *)p_enc_ctx->p_session_config;
1414  int new_width, new_height;
1415  ni_pix_fmt_t new_pix_fmt;
1416  bool is_semiplanar;
1417  bool is_rgba;
1418  int src_stride[NI_MAX_NUM_DATA_POINTERS];
1419 
1420  new_width = p_buffered_frame->video_width;
1421  new_height = p_buffered_frame->video_height;
1422  new_pix_fmt = p_buffered_frame->pixel_format;
1423 
1424  // check if resolution is zero copy compatible and set linesize according to new resolution
1425  is_semiplanar = (new_pix_fmt == NI_PIX_FMT_NV12 || new_pix_fmt == NI_PIX_FMT_P010LE);
1426 
1427  is_rgba = (new_pix_fmt == NI_PIX_FMT_ABGR || new_pix_fmt == NI_PIX_FMT_ARGB
1428  || new_pix_fmt == NI_PIX_FMT_RGBA || new_pix_fmt == NI_PIX_FMT_BGRA);
1429 
1430  // NOTE - FFmpeg / Gstreamer users should use linesize array in frame structure instead of src_stride in the following sample code
1431  src_stride[0] = new_width * p_enc_ctx->bit_depth_factor;
1432  if (is_rgba)
1433  {
1434  src_stride[1] = 0;
1435  src_stride[2] = 0;
1436  }
1437  else
1438  {
1439  src_stride[1] = is_semiplanar ? src_stride[0] : src_stride[0] / 2;
1440  src_stride[2] = is_semiplanar ? 0 : src_stride[0] / 2;
1441  }
1442 
1443  if (ni_encoder_frame_zerocopy_check(p_enc_ctx,
1444  p_api_param, new_width, new_height,
1445  (const int *)src_stride, true) == NI_RETCODE_SUCCESS)
1446  {
1447  new_stride = p_api_param->luma_linesize; // new sequence is zero copy compatible
1448  }
1449  else
1450  {
1451  new_stride = NI_ALIGN(new_width * p_enc_ctx->bit_depth_factor, 128);
1452  }
1453 
1454  if (p_enc_ctx->ori_luma_linesize && p_enc_ctx->ori_chroma_linesize)
1455  {
1456  ori_stride = p_enc_ctx->ori_luma_linesize; // previous sequence was zero copy compatible
1457  }
1458  else
1459  {
1460  ori_stride = NI_ALIGN(p_enc_ctx->ori_width * p_enc_ctx->bit_depth_factor, 128);
1461  }
1462 
1463  if (p_api_param->cfg_enc_params.lookAheadDepth) {
1464  ni_log(NI_LOG_DEBUG, "xcoder_encode_reinit 2-pass "
1465  "lookaheadDepth %d\n",
1466  p_api_param->cfg_enc_params.lookAheadDepth);
1467  if ((new_width < NI_2PASS_ENCODE_MIN_WIDTH) ||
1468  (new_height < NI_2PASS_ENCODE_MIN_HEIGHT)) {
1469  bIsSmallPicture = true;
1470  }
1471  }
1472  else {
1473  if ((new_width < NI_MIN_WIDTH) ||
1474  (new_height < NI_MIN_HEIGHT)) {
1475  bIsSmallPicture = true;
1476  }
1477  }
1478 
1479  if (p_api_param->cfg_enc_params.multicoreJointMode) {
1480  ni_log(NI_LOG_DEBUG, "xcoder_encode_reinit multicore "
1481  "joint mode\n");
1482  if ((new_width < NI_MULTICORE_ENCODE_MIN_WIDTH) ||
1483  (new_height < NI_MULTICORE_ENCODE_MIN_HEIGHT)) {
1484  bIsSmallPicture = true;
1485  }
1486  }
1487 
1488  if (p_api_param->cfg_enc_params.crop_width || p_api_param->cfg_enc_params.crop_height) {
1489  ni_log(NI_LOG_DEBUG, "xcoder_encode_reinit needs to close and re-open "
1490  "due to crop width x height\n");
1491  bIsSmallPicture = true;
1492  }
1493 
1494  ni_log(NI_LOG_DEBUG, "xcoder_encode_reinit resolution: %dx%d->%dx%d "
1495  "pix fmt: %d->%d bIsSmallPicture %d codec %d\n",
1496  ori_stride, p_enc_ctx->ori_height, new_stride, new_height,
1497  p_enc_ctx->ori_pix_fmt, new_pix_fmt, bIsSmallPicture,
1498  p_enc_ctx->codec_format);
1499 
1500  // fast sequence change without close / open only if new resolution < original resolution
1501  if (ori_stride*p_enc_ctx->ori_height < new_stride*new_height ||
1502  p_enc_ctx->ori_pix_fmt != new_pix_fmt ||
1503  bIsSmallPicture ||
1504  p_enc_ctx->codec_format == NI_CODEC_FORMAT_JPEG)
1505  {
1506  ni_log(NI_LOG_INFO, "XCoder encode sequence change by close / re-open session\n");
1507  encoder_close_session(p_enc_ctx, p_in_data, p_out_data);
1508  ret = encoder_open_session(p_enc_ctx, p_enc_ctx->codec_format,
1509  p_enc_ctx->hw_id, p_api_param, new_width,
1510  new_height, new_pix_fmt, true);
1511  if (NI_RETCODE_SUCCESS != ret)
1512  {
1513  ni_log(NI_LOG_ERROR, "Failed to Re-open Encoder Session upon Sequence Change (status = %d)\n", ret);
1514  return ret;
1515  }
1516  p_out_data->data.packet.end_of_stream = 0;
1517  p_in_data->data.frame.start_of_stream = 1;
1518  // clear crop parameters upon sequence change because cropping values may not be compatible to new resolution
1519  p_api_param->cfg_enc_params.crop_width = p_api_param->cfg_enc_params.crop_height = 0;
1520  p_api_param->cfg_enc_params.hor_offset = p_api_param->cfg_enc_params.ver_offset = 0;
1521  }
1522  else
1523  {
1524  if (p_enc_ctx->codec_format == NI_CODEC_FORMAT_AV1) {
1525  // AV1 8x8 alignment HW limitation is now worked around by FW cropping input resolution
1526  if (new_width % NI_PARAM_AV1_ALIGN_WIDTH_HEIGHT)
1528  "resolution change: AV1 Picture Width not aligned to %d - picture will be cropped\n",
1530 
1531  if (new_height % NI_PARAM_AV1_ALIGN_WIDTH_HEIGHT)
1533  "resolution change: AV1 Picture Height not aligned to %d - picture will be cropped\n",
1535  }
1536  ni_log(NI_LOG_INFO, "XCoder encode sequence change by re-config session (fast path)\n");
1537  ret = encoder_sequence_change(p_enc_ctx, p_in_data, p_out_data, new_width, new_height, new_pix_fmt);
1538  }
1539 
1540  // this state is referenced when sending first frame after sequence change
1542 
1543  ni_log(NI_LOG_DEBUG, "%s: session_run_state change to %d \n", __func__,
1544  p_enc_ctx->session_run_state);
1545 
1546  return ret;
1547 }
1548 
1549 void write_av1_ivf_header(ni_demo_context_t *p_ctx, uint32_t width, uint32_t height, uint32_t frame_num,
1550  uint32_t frame_denom, FILE *p_file)
1551 {
1552  // write the global ivf start header
1553  if (!p_ctx->ivf_header_written && p_file != NULL && !p_ctx->av1_output_obu)
1554  {
1555  uint8_t start_header[32] = {
1556  0x44, 0x4b, 0x49, 0x46, /* signature: 'DKIF' */
1557  0x00, 0x00, /* version: 0 */
1558  0x20, 0x00, /* length of header in bytes: 32 */
1559  0x41, 0x56, 0x30, 0x31, /* codec FourCC: AV01 */
1560  0x00, 0x07, /* width in pixels(little endian), default 1280 */
1561  0xd0, 0x02, /* height in pixels(little endian), default 720 */
1562  0x1e, 0x00, 0x00, 0x00, /* time base numerator, default 30 */
1563  0x01, 0x00, 0x00, 0x00, /* time base denominator, default 1 */
1564  0x00, 0x00, 0x00, 0x00, /* number of frames in file */
1565  0x00, 0x00, 0x00, 0x00 /* reserved */
1566  };
1567 
1568  if (width && height)
1569  {
1570  start_header[12] = width & 0xff;
1571  start_header[13] = ((width >> 8) & 0xff);
1572  start_header[14] = height & 0xff;
1573  start_header[15] = ((height >> 8) & 0xff);
1574  }
1575 
1576  if (frame_num && frame_denom)
1577  {
1578  start_header[16] = frame_num & 0xff;
1579  start_header[17] = ((frame_num >> 8) & 0xff);
1580  start_header[18] = ((frame_num >> 16) & 0xff);
1581  start_header[19] = ((frame_num >> 24) & 0xff);
1582  start_header[20] = frame_denom & 0xff;
1583  start_header[21] = ((frame_denom >> 8) & 0xff);
1584  start_header[22] = ((frame_denom >> 16) & 0xff);
1585  start_header[23] = ((frame_denom >> 24) & 0xff);
1586  }
1587 
1588  if (fwrite(start_header, sizeof(start_header), 1, p_file) != 1)
1589  {
1590  ni_log(NI_LOG_ERROR, "Error: writing ivf start header fail!\n");
1591  ni_log(NI_LOG_ERROR, "Error: ferror rc = %d\n", ferror(p_file));
1592  }
1593 
1594  p_ctx->ivf_header_written = 1;
1595  }
1596 }
1597 
1598 void write_av1_ivf_packet(ni_demo_context_t *p_ctx, ni_packet_t *p_out_pkt, uint32_t meta_size, FILE *p_file)
1599 {
1600  int i;
1601  uint8_t enc_id = p_ctx->curr_enc_index;
1602 
1603  if (!p_file)
1604  {
1605  return;
1606  }
1607 
1608  // write ivf frame header
1609  if (!p_ctx->av1_output_obu)
1610  {
1611  uint32_t pts = p_ctx->av1_muxed_num_packets[enc_id];
1612  uint32_t pkt_size = p_out_pkt->data_len - meta_size;
1613 
1614  if (p_ctx->av1_seq_header_len[enc_id] > 0)
1615  {
1616  pkt_size += p_ctx->av1_seq_header_len[enc_id] - meta_size;
1617  }
1618  for (i = 0; i < p_out_pkt->av1_buffer_index; i++)
1619  {
1620  pkt_size += p_out_pkt->av1_data_len[i] - meta_size;
1621  }
1622 
1623  // ivf frame header
1624  // bytes 0-3: size of frame in bytes(not including the 12-byte header
1625  // byte 4-11: 64-bit pts (here pts=num_of_packets(32-bit), thus here only saves 32-bit
1626  uint8_t ivf_frame_header[12] = {((pkt_size & 0xff)),
1627  ((pkt_size >> 8) & 0xff),
1628  ((pkt_size >> 16) & 0xff),
1629  ((pkt_size >> 24) & 0xff),
1630  ((pts & 0xff)),
1631  ((pts >> 8) & 0xff),
1632  ((pts >> 16) & 0xff),
1633  ((pts >> 24) & 0xff),
1634  0x00, 0x00, 0x00, 0x00};
1635  if (fwrite(ivf_frame_header, 12, 1, p_file) != 1)
1636  {
1637  ni_log(NI_LOG_ERROR, "Error: writing ivf frame header fail!\n");
1638  ni_log(NI_LOG_ERROR, "Error: ferror rc = %d\n", ferror(p_file));
1639  }
1640  }
1641 
1642  // write the leftover sequence header if there is any
1643  if (p_ctx->av1_seq_header_len[enc_id] > 0)
1644  {
1645  if (fwrite(p_ctx->p_av1_seq_header[enc_id] + meta_size,
1646  p_ctx->av1_seq_header_len[enc_id] - meta_size, 1, p_file) != 1)
1647  {
1648  ni_log(NI_LOG_ERROR, "Error: writing av1 sequence header fail!\n");
1649  ni_log(NI_LOG_ERROR, "Error: ferror rc = %d\n", ferror(p_file));
1650  }
1651  ni_aligned_free(p_ctx->p_av1_seq_header[enc_id]);
1652  p_ctx->av1_seq_header_len[enc_id] = 0;
1653  }
1654 
1655  // write the leftover av1 packets
1656  for (i = 0; i < p_out_pkt->av1_buffer_index; i++)
1657  {
1658  if (fwrite((uint8_t *)p_out_pkt->av1_p_data[i] + meta_size,
1659  p_out_pkt->av1_data_len[i] - meta_size, 1, p_file) != 1)
1660  {
1661  ni_log(NI_LOG_ERROR, "Error: writing av1 packets fail!\n");
1662  ni_log(NI_LOG_ERROR, "Error: ferror rc = %d\n", ferror(p_file));
1663  }
1664  }
1665 
1666  // write the current packet
1667  if (fwrite((uint8_t *)p_out_pkt->p_data + meta_size,
1668  p_out_pkt->data_len - meta_size, 1, p_file) != 1)
1669  {
1670  ni_log(NI_LOG_ERROR, "Error: writing av1 packets fail!\n");
1671  ni_log(NI_LOG_ERROR, "Error: ferror rc = %d\n", ferror(p_file));
1672  }
1673 
1674  p_ctx->av1_muxed_num_packets[enc_id]++;
1675 }
1676 
1677 int write_av1_ivf_trailer(ni_demo_context_t *p_ctx, ni_packet_t *p_out_pkt, uint32_t meta_size, FILE *p_file)
1678 {
1679  uint32_t muxed_num_packets = p_ctx->av1_muxed_num_packets[p_ctx->curr_enc_index];
1680  if (p_file)
1681  {
1682  // write the leftover packets
1683  if (p_out_pkt->av1_buffer_index > 0)
1684  {
1685  write_av1_ivf_packet(p_ctx, p_out_pkt, meta_size, p_file);
1686  }
1687 
1688  // update frame_count in ivf start header
1689  if (muxed_num_packets && !p_ctx->av1_output_obu)
1690  {
1691  uint8_t frame_cnt[4] = {
1692  (muxed_num_packets & 0xff),
1693  ((muxed_num_packets >> 8) & 0xff),
1694  ((muxed_num_packets >> 16) & 0xff),
1695  ((muxed_num_packets >> 24) & 0xff)};
1696  fseek(p_file, 24, SEEK_SET);
1697  if (fwrite(frame_cnt, 4, 1, p_file) != 1)
1698  {
1699  ni_log(NI_LOG_ERROR, "Error: failed to update frame_cnt in ivf "
1700  "header!\n");
1701  ni_log(NI_LOG_ERROR, "Error: ferror rc = %d\n", ferror(p_file));
1702  return -1;
1703  }
1704  }
1705  }
1706 
1707  return 0;
1708 }
1709 
1710 /*!*****************************************************************************
1711  * \brief Receive output data from encoder
1712  *
1713  * \param p_in_data is passed in to specify new frame resolution upon sequence
1714  * change
1715  *
1716  * \return 0 - success got packet
1717  * 1 - received eos
1718  * 2 - got nothing, need retry
1719  * -1 - failure
1720  ******************************************************************************/
1722  ni_session_data_io_t *p_out_data, int output_video_width,
1723  int output_video_height, FILE *p_file, ni_session_data_io_t * p_in_data)
1724 {
1725  int packet_size = NI_MAX_TX_SZ;
1726  int rc = 0;
1727  int end_flag = 0;
1728  int rx_size = 0;
1729  uint8_t enc_id = p_ctx->curr_enc_index;
1730  ni_packet_t *p_out_pkt = &(p_out_data->data.packet);
1731  int meta_size = p_enc_ctx->meta_size;
1732  ni_xcoder_params_t *p_api_param = (ni_xcoder_params_t *)p_enc_ctx->p_session_config;
1733 
1734  ni_log(NI_LOG_DEBUG, "===> encoder_receive_data <===\n");
1735  if (NI_INVALID_SESSION_ID == p_enc_ctx->session_id)
1736  {
1737  // keep-alive-thread timeout will set session_id to invalid, should exit
1739  "encode session id invalid, the session should be closed\n");
1740  return NI_TEST_RETCODE_FAILURE;
1741  }
1742 
1743 receive_data:
1744  rc = ni_packet_buffer_alloc(p_out_pkt, packet_size);
1745  if (rc != NI_RETCODE_SUCCESS)
1746  {
1747  ni_log(NI_LOG_ERROR, "Error: malloc packet failed, ret = %d!\n", rc);
1748  return NI_TEST_RETCODE_FAILURE;
1749  }
1750 
1751  rc = ni_device_session_read(p_enc_ctx, p_out_data, NI_DEVICE_TYPE_ENCODER);
1752 
1753  end_flag = p_out_pkt->end_of_stream;
1754  rx_size = rc;
1755 
1756  ni_log(NI_LOG_DEBUG, "encoder_receive_data: received data size=%d\n", rx_size);
1757 
1758  if (rx_size > meta_size)
1759  {
1760  if (p_enc_ctx->codec_format == NI_CODEC_FORMAT_AV1)
1761  {
1762  if (p_enc_ctx->pkt_num == 0)
1763  {
1764  write_av1_ivf_header(p_ctx, output_video_width, output_video_height,
1765  p_api_param->fps_number,
1766  p_api_param->fps_denominator, p_file);
1767  // store the sequence header for next packet writing
1768  p_ctx->p_av1_seq_header[enc_id] = (uint8_t *)p_out_pkt->p_data;
1769  p_ctx->av1_seq_header_len[enc_id] = p_out_pkt->data_len;
1770  p_out_pkt->p_buffer = NULL;
1771  p_out_pkt->p_data = NULL;
1772  p_out_pkt->buffer_size = 0;
1773  p_out_pkt->data_len = 0;
1774  } else
1775  {
1776  // store the av1 unshown frames for next packet writing
1777  if (!p_out_pkt->av1_show_frame)
1778  {
1779  p_out_pkt->av1_p_buffer[p_out_pkt->av1_buffer_index] =
1780  p_out_pkt->p_buffer;
1781  p_out_pkt->av1_p_data[p_out_pkt->av1_buffer_index] =
1782  p_out_pkt->p_data;
1783  p_out_pkt->av1_buffer_size[p_out_pkt->av1_buffer_index] =
1784  p_out_pkt->buffer_size;
1785  p_out_pkt->av1_data_len[p_out_pkt->av1_buffer_index] =
1786  p_out_pkt->data_len;
1787  p_out_pkt->av1_buffer_index++;
1788  p_out_pkt->p_buffer = NULL;
1789  p_out_pkt->p_data = NULL;
1790  p_out_pkt->buffer_size = 0;
1791  p_out_pkt->data_len = 0;
1792  if (p_out_pkt->av1_buffer_index >= MAX_AV1_ENCODER_GOP_NUM)
1793  {
1794  ni_log(NI_LOG_ERROR, "Error: recv AV1 not shown frame "
1795  "number %d >= %d\n", p_out_pkt->av1_buffer_index,
1797  return NI_TEST_RETCODE_FAILURE;
1798  }
1799  } else
1800  {
1801  ni_log(NI_LOG_DEBUG, "AV1 output packet "
1802  "pts %lld dts %lld\n", p_out_pkt->pts, p_out_pkt->dts);
1803  write_av1_ivf_packet(p_ctx, p_out_pkt, p_enc_ctx->meta_size, p_file);
1804  ni_packet_buffer_free_av1(p_out_pkt);
1805  }
1806 
1807  // recycle hw frame before next read
1808  if (p_enc_ctx->hw_action)
1809  {
1810  // encoder only returns valid recycle index
1811  // when there's something to recycle.
1812  // This range is suitable for all memory bins
1813  if (p_out_pkt->recycle_index > 0 &&
1814  p_out_pkt->recycle_index <
1816  {
1817  ni_hw_frame_unref(p_out_pkt->recycle_index);
1818  p_out_pkt->recycle_index = 0; //clear to not double count
1819  }
1820  }
1821  }
1822  } else
1823  {
1824  if (p_file &&
1825  (fwrite((uint8_t *)p_out_pkt->p_data + meta_size,
1826  p_out_pkt->data_len - meta_size, 1, p_file) != 1))
1827  {
1828  ni_log(NI_LOG_ERROR, "Error: writing data %u bytes error!\n",
1829  p_out_pkt->data_len - meta_size);
1830  ni_log(NI_LOG_ERROR, "Error: ferror rc = %d\n", ferror(p_file));
1831  }
1832  }
1833 
1834  p_ctx->enc_total_bytes_received[enc_id] += rx_size - meta_size;
1835 
1836  if (0 == p_enc_ctx->pkt_num)
1837  {
1838  p_enc_ctx->pkt_num = 1;
1839  ni_log(NI_LOG_DEBUG, "got encoded stream header, keep reading ..\n");
1840  goto receive_data;
1841  }
1842  (p_ctx->num_packets_received[enc_id])++;
1843 
1844  ni_log(NI_LOG_DEBUG, "Got: Packets= %u\n", p_ctx->num_packets_received[enc_id]);
1845  } else if (rx_size != 0)
1846  {
1847  ni_log(NI_LOG_ERROR, "Error: received %d bytes, <= metadata size %d!\n",
1848  rx_size, meta_size);
1849  return NI_TEST_RETCODE_FAILURE;
1850  } else if (end_flag)
1851  {
1853  p_enc_ctx->session_run_state)
1854  {
1855  // after sequence change completes, reset codec state
1856  ni_log(NI_LOG_INFO, "encoder_receive_data: sequence "
1857  "change completed, return SEQ_CHANGE_DONE and will reopen "
1858  "or reconfig codec!\n");
1859  rc = encoder_reinit_session(p_enc_ctx, p_in_data, p_out_data);
1860  ni_log(NI_LOG_TRACE, "encoder_receive_data: encoder_reinit_session ret %d\n", rc);
1861  if (rc == NI_RETCODE_SUCCESS)
1862  {
1864  }
1865  else
1866  {
1867  return NI_TEST_RETCODE_FAILURE;
1868  }
1869  }
1870 
1872  {
1873  // restart the encoder after sending flushing command
1874  rc = ni_device_session_restart(p_enc_ctx,
1875  p_in_data->data.frame.video_width,
1876  p_in_data->data.frame.video_height,
1878  ni_log(NI_LOG_INFO, "ni_device_session_restart() ret %d\n", rc);
1879  if (rc == NI_RETCODE_SUCCESS)
1880  {
1881  // need to reset the packet end_of_stream and run state
1882  p_out_pkt->end_of_stream = 0;
1884  return NI_TEST_RETCODE_EAGAIN;
1885  }
1886  else
1887  {
1888  return NI_TEST_RETCODE_FAILURE;
1889  }
1890  }
1891 
1892  if (p_enc_ctx->codec_format == NI_CODEC_FORMAT_AV1)
1893  {
1894  rc = write_av1_ivf_trailer(p_ctx, p_out_pkt, p_enc_ctx->meta_size, p_file);
1895  ni_packet_buffer_free_av1(p_out_pkt);
1896  if (rc < 0)
1897  {
1898  return NI_TEST_RETCODE_FAILURE;
1899  }
1900  }
1901  ni_log(NI_LOG_INFO, "Encoder Receiving done.\n");
1903  } else if (p_api_param->low_delay_mode && p_enc_ctx->frame_num >= p_enc_ctx->pkt_num) {
1904  ni_log(NI_LOG_DEBUG, "low delay mode and NO pkt, keep reading ..\n");
1905  ni_usleep(200);
1906  goto receive_data;
1907  } else { //rx_size == 0 && !end_flag
1908  ni_log(NI_LOG_DEBUG, "no packet received from encoder, return EAGAIN and retry\n");
1909  return NI_TEST_RETCODE_EAGAIN;
1910  }
1911 
1912  ni_log(NI_LOG_DEBUG, "encoder_receive_data: success\n");
1913 
1914  return NI_TEST_RETCODE_SUCCESS;
1915 }
1916 
1917 /*!*****************************************************************************
1918  * \brief encoder session close
1919  *
1920  * \param
1921  *
1922  * \return 0 if successful, < 0 otherwise
1923  ******************************************************************************/
1925  ni_session_data_io_t *p_in_data,
1926  ni_session_data_io_t *p_out_data)
1927 {
1928  int ret = 0;
1930 
1931  ni_log(NI_LOG_DEBUG, "encoder_close_session - close encoder blk_io_handle %d device_handle %d\n", p_enc_ctx->blk_io_handle, p_enc_ctx->device_handle);
1932 #ifdef _WIN32
1933  ni_device_close(p_enc_ctx->device_handle);
1934 #elif __linux__
1935  ni_device_close(p_enc_ctx->device_handle);
1936  ni_device_close(p_enc_ctx->blk_io_handle);
1937 #endif
1938 
1939  if (p_enc_ctx->codec_format == NI_CODEC_FORMAT_AV1 &&
1940  p_out_data->data.packet.av1_buffer_index)
1941  {
1942  ni_packet_buffer_free_av1(&(p_out_data->data.packet));
1943  }
1944  ni_frame_buffer_free(&(p_in_data->data.frame));
1945  ni_packet_buffer_free(&(p_out_data->data.packet));
1946  return ret;
1947 }
1948 
1950  ni_session_data_io_t *p_in_data,
1951  ni_session_data_io_t *p_out_data,
1952  int width, int height, ni_pix_fmt_t pix_fmt)
1953 {
1954  ni_retcode_t ret = 0;
1955  int bit_depth;
1956  int bit_depth_factor;
1957 
1958  ni_log(NI_LOG_DEBUG, "XCoder encode sequence change (reconfig): session_run_state %d\n", p_enc_ctx->session_run_state);
1959 
1960  switch (pix_fmt)
1961  {
1962  case NI_PIX_FMT_YUV420P:
1963  case NI_PIX_FMT_NV12:
1964  bit_depth = 8;
1965  bit_depth_factor = 1;
1966  break;
1968  case NI_PIX_FMT_P010LE:
1969  bit_depth = 10;
1970  bit_depth_factor = 2;
1971  break;
1972  default:
1973  bit_depth = 8;
1974  bit_depth_factor = 1;
1975  break;
1976  }
1977 
1978  ret = ni_device_session_sequence_change(p_enc_ctx, width, height, bit_depth_factor, NI_DEVICE_TYPE_ENCODER);
1979  if (NI_RETCODE_SUCCESS != ret)
1980  {
1981  ni_log(NI_LOG_ERROR, "Failed to send Sequence Change to Encoder Session (status = %d)\n", ret);
1982  return ret;
1983  }
1984 
1985  // update session context
1986  p_enc_ctx->bit_depth_factor = bit_depth_factor;
1987  p_enc_ctx->src_bit_depth = bit_depth;
1988  // xcoder demo only support little endian (for 10-bit pixel format)
1989  p_enc_ctx->src_endian = NI_FRAME_LITTLE_ENDIAN;
1990  p_enc_ctx->ready_to_close = 0;
1991  p_enc_ctx->frame_num = 0; // need to reset frame_num because pkt_num is set to 1 when header received after sequnce change, and low delay mode compares frame_num and pkt_num
1992  p_enc_ctx->pkt_num = 0; // also need to reset pkt_num because before header received, pkt_num > frame_num will also cause low delay mode stuck
1993  p_enc_ctx->pixel_format = p_in_data->data.frame.pixel_format;
1994  p_out_data->data.packet.end_of_stream = 0;
1995  p_in_data->data.frame.start_of_stream = 1;
1996  return ret;
1997 }
1998 
2000  ni_xcoder_params_t *p_api_param_list,
2001  int output_total, char p_enc_conf_params[][2048],
2002  char p_enc_conf_gop[][2048],
2003  ni_frame_t *p_ni_frame, int width, int height,
2004  int fps_num, int fps_den, int bitrate,
2005  int codec_format, ni_pix_fmt_t pix_fmt,
2006  int aspect_ratio_idc, int xcoder_guid,
2007  niFrameSurface1_t *p_surface, int multi_thread,
2008  bool check_zerocopy)
2009 {
2010  int i, ret = 0;
2011  int color_prim = NI_COL_PRI_UNSPECIFIED;
2012  int color_trc = NI_COL_TRC_UNSPECIFIED;
2013  int color_space = NI_COL_SPC_UNSPECIFIED;
2014  int sar_num = 0;
2015  int sar_den = 0;
2016  int video_full_range_flag = 0;
2017 
2018  if (p_ni_frame != NULL)
2019  {
2020  // open the encode session when the first frame arrives and the session
2021  // is not opened yet, with the source stream and user-configured encode
2022  // info both considered when constructing VUI in the stream headers
2023  color_prim = p_ni_frame->color_primaries;
2024  color_trc = p_ni_frame->color_trc;
2025  color_space = p_ni_frame->color_space;
2026  sar_num = p_ni_frame->sar_width;
2027  sar_den = p_ni_frame->sar_height;
2028  video_full_range_flag = p_ni_frame->video_full_range_flag;
2029 
2030  // calculate the source fps and set it as the default target fps, based
2031  // on the timing_info passed in from the decoded frame
2032  if (p_ni_frame->vui_num_units_in_tick && p_ni_frame->vui_time_scale)
2033  {
2034  if (NI_CODEC_FORMAT_H264 == p_ni_frame->src_codec)
2035  {
2036  if (0 == p_ni_frame->vui_time_scale % 2)
2037  {
2038  fps_num = (int)(p_ni_frame->vui_time_scale / 2);
2039  fps_den = (int)(p_ni_frame->vui_num_units_in_tick);
2040  } else
2041  {
2042  fps_num = (int)(p_ni_frame->vui_time_scale);
2043  fps_den = (int)(2 * p_ni_frame->vui_num_units_in_tick);
2044  }
2045  } else if (NI_CODEC_FORMAT_H265 == p_ni_frame->src_codec)
2046  {
2047  fps_num = p_ni_frame->vui_time_scale;
2048  fps_den = p_ni_frame->vui_num_units_in_tick;
2049  }
2050  }
2051  }
2052 
2053  for (i = 0; i < output_total; i++)
2054  {
2055  // set up encoder p_config, using some info from source
2056  ret = ni_encoder_init_default_params(&p_api_param_list[i], fps_num,
2057  fps_den, bitrate, width, height,
2058  enc_ctx_list[i].codec_format);
2059  if (ret < 0)
2060  {
2061  ni_log(NI_LOG_ERROR, "Error encoder[%d] init default set up error\n", i);
2062  return -1;
2063  }
2064 
2065  // check and set ni_encoder_params from --xcoder-params
2066  // Note: the parameter setting has to be in this order so that user
2067  // configured values can overwrite the source/default ones if
2068  // desired.
2069  if (ni_retrieve_xcoder_params(p_enc_conf_params[i],
2070  &p_api_param_list[i], &enc_ctx_list[i]))
2071  {
2072  ni_log(NI_LOG_ERROR, "Error: encoder[%d] p_config parsing error\n", i);
2073  return -1;
2074  }
2075 
2076  if (ni_retrieve_xcoder_gop(p_enc_conf_gop[i],
2077  &p_api_param_list[i], &enc_ctx_list[i]))
2078  {
2079  ni_log(NI_LOG_ERROR, "Error: encoder[%d] p_config_gop parsing error\n", i);
2080  return -1;
2081  }
2082 
2083  // set async mode in enc_ctx if encoding is multi-threaded
2084  if (multi_thread)
2085  {
2086  ni_log(NI_LOG_INFO, "Encoder[%d] is multi-threaded, set async mode "
2087  "in the session context!\n", i);
2088  enc_ctx_list[i].async_mode = 1;
2089  p_api_param_list[i].cfg_enc_params.enable_acq_limit = 1;
2090  }
2091 
2092  // check color primaries configuration
2093  if (color_prim != p_api_param_list[i].color_primaries &&
2094  NI_COL_PRI_UNSPECIFIED != p_api_param_list[i].color_primaries)
2095  {
2096  ni_log(NI_LOG_DEBUG, "Encoder[%d] user-configured color primaries "
2097  "%d to overwrite source %d\n",
2098  i, p_api_param_list[i].color_primaries, color_prim);
2099  color_prim = p_api_param_list[i].color_primaries;
2100  }
2101 
2102  // check color transfer characteristic configuration
2103  if (color_trc != p_api_param_list[i].color_transfer_characteristic &&
2104  NI_COL_TRC_UNSPECIFIED != p_api_param_list[i].color_transfer_characteristic)
2105  {
2106  ni_log(NI_LOG_DEBUG, "Encoder[%d] user-configured color trc %d to "
2107  "overwrite source %d\n", i,
2108  p_api_param_list[i].color_transfer_characteristic, color_trc);
2109  color_trc = p_api_param_list[i].color_transfer_characteristic;
2110  }
2111 
2112  // check color space configuration
2113  if (color_space != p_api_param_list[i].color_space &&
2114  NI_COL_SPC_UNSPECIFIED != p_api_param_list[i].color_space)
2115  {
2116  ni_log(NI_LOG_DEBUG, "Encoder[%d] user-configured color space %d "
2117  "to overwrite source %d\n",
2118  i, p_api_param_list[i].color_space, color_space);
2119  color_space = p_api_param_list[i].color_space;
2120  }
2121 
2122  // check video full range flag configuration
2123  if (p_api_param_list[i].video_full_range_flag >= 0)
2124  {
2125  ni_log(NI_LOG_DEBUG, "Encoder[%d] user-configured video full range "
2126  "flag %d\n", i, p_api_param_list[i].video_full_range_flag);
2127  video_full_range_flag = p_api_param_list[i].video_full_range_flag;
2128  }
2129 
2130  // check aspect ratio indicator configuration
2131  if (aspect_ratio_idc > 0 && aspect_ratio_idc < NI_NUM_PIXEL_ASPECT_RATIO)
2132  {
2133  sar_num = ni_h264_pixel_aspect_list[aspect_ratio_idc].num;
2134  sar_den = ni_h264_pixel_aspect_list[aspect_ratio_idc].den;
2135  } else if (p_api_param_list[i].sar_denom)
2136  {
2137  sar_num = p_api_param_list[i].sar_num;
2138  sar_den = p_api_param_list[i].sar_denom;
2139  }
2140 
2141  // check hwframe configuration
2142  if (p_surface != NULL)
2143  {
2144  //Items in this else condition could be improved by being handled in libxcoder
2145  enc_ctx_list[i].hw_action = NI_CODEC_HW_ENABLE;
2146  p_api_param_list[i].hwframes = 1;
2147  enc_ctx_list[i].sender_handle =
2148  (ni_device_handle_t)(int64_t)p_surface->device_handle;
2149  p_api_param_list[i].rootBufId = p_surface->ui16FrameIdx;
2150  }
2151 
2152  // VUI setting including color setting is done by specifying them in the
2153  // encoder config
2154  p_api_param_list[i].cfg_enc_params.colorDescPresent = 0;
2155  if ((color_prim != NI_COL_PRI_UNSPECIFIED) ||
2156  (color_space != NI_COL_SPC_UNSPECIFIED) ||
2157  (color_trc != NI_COL_TRC_UNSPECIFIED))
2158  {
2159  p_api_param_list[i].cfg_enc_params.colorDescPresent = 1;
2160  }
2161  p_api_param_list[i].cfg_enc_params.colorPrimaries = color_prim;
2162  p_api_param_list[i].cfg_enc_params.colorTrc = color_trc;
2163  p_api_param_list[i].cfg_enc_params.colorSpace = color_space;
2164  p_api_param_list[i].cfg_enc_params.videoFullRange = video_full_range_flag;
2165  p_api_param_list[i].cfg_enc_params.aspectRatioWidth = sar_num;
2166  p_api_param_list[i].cfg_enc_params.aspectRatioHeight = sar_den;
2167 
2168  ret = encoder_open_session(&enc_ctx_list[i], codec_format, xcoder_guid,
2169  &p_api_param_list[i], width, height,
2170  pix_fmt, check_zerocopy);
2171  if (ret != 0)
2172  {
2173  ni_log(NI_LOG_ERROR, "Error encoder[%d] open session failed!\n", i);
2174  return -1;
2175  }
2176  }
2177 
2178  return ret;
2179 }
2180 
2182  ni_session_context_t *enc_ctx_list,
2183  ni_session_data_io_t *in_frame,
2184  ni_session_data_io_t *pkt, int width, int height,
2185  int output_total, FILE **pfs_list)
2186 {
2187  int i, recycle_index;
2188  int recv_fin_flag = NI_TEST_RETCODE_SUCCESS;
2189  uint32_t prev_num_pkt[MAX_OUTPUT_FILES] = {0};
2190  ni_session_data_io_t *p_out_pkt = pkt;
2191 
2192  for (i = 0; i < output_total; i++)
2193  {
2194  if (enc_ctx_list[i].codec_format == NI_CODEC_FORMAT_AV1)
2195  {
2196  p_out_pkt = &pkt[i];
2197  }
2198  p_out_pkt->data.packet.end_of_stream = 0;
2199  prev_num_pkt[i] = p_ctx->num_packets_received[i];
2200 
2201  p_ctx->curr_enc_index = i;
2202  recv_fin_flag = encoder_receive_data(p_ctx, &enc_ctx_list[i], p_out_pkt, width,
2203  height, pfs_list[i], in_frame);
2204 
2205  recycle_index = p_out_pkt->data.packet.recycle_index;
2206  if (prev_num_pkt[i] < p_ctx->num_packets_received[i] &&
2207  enc_ctx_list[i].hw_action && recycle_index > 0 &&
2208  recycle_index < NI_GET_MAX_HWDESC_FRAME_INDEX(enc_ctx_list[i].ddr_config))
2209  {
2210  //encoder only returns valid recycle index
2211  //when there's something to recycle.
2212  //This range is suitable for all memory bins
2213  ni_hw_frame_unref(recycle_index);
2214  } else
2215  {
2216  ni_log(NI_LOG_DEBUG, "enc %d recv, prev_num_pkt %u "
2217  "number_of_packets_list %u recycle_index %u\n", i,
2218  prev_num_pkt[i], p_ctx->num_packets_received[i], recycle_index);
2219  }
2220 
2221  if (prev_num_pkt[i] < p_ctx->num_packets_received[i] &&
2222  enc_ctx_list[i].codec_format == NI_CODEC_FORMAT_AV1)
2223  {
2224  // For low delay mode encoding, only one packet is received for one
2225  // frame sent. For non low delay mode, there will be multiple
2226  // packets received for one frame sent. So we need to read out all
2227  // the packets encoded.
2228  ni_xcoder_params_t *p_api_param =
2229  (ni_xcoder_params_t *)enc_ctx_list[i].p_session_config;
2230  if (!p_api_param->low_delay_mode)
2231  {
2232  i--;
2233  continue;
2234  }
2235  }
2236 
2237  p_ctx->enc_eos_received[i] = p_out_pkt->data.packet.end_of_stream;
2238 
2239  if (recv_fin_flag < 0)
2240  {
2241  ni_log(NI_LOG_DEBUG, "enc %d error, quit !\n", i);
2242  break;
2243  } else if (recv_fin_flag == NI_TEST_RETCODE_EAGAIN)
2244  {
2245  ni_usleep(100);
2246  }
2247  }
2248 
2249  return recv_fin_flag;
2250 }
2251 
2252 void encoder_stat_report_and_close(ni_demo_context_t *p_ctx, ni_session_context_t *p_enc_ctx_list, int output_total)
2253 {
2254  int i;
2255  int nb_recycled;
2256  uint64_t current_time;
2257 
2259 
2260  nb_recycled = scan_and_clean_hwdescriptors();
2261 
2262  for (i = 0; i < output_total; i++)
2263  {
2264  ni_log(NI_LOG_ERROR, "Encoder %d closing, Got: Packets=%u FPS=%.2f Total bytes %llu\n",
2265  (int)i, p_ctx->num_packets_received[i],
2266  (float)p_enc_ctx_list[i].frame_num / (float)(current_time - p_ctx->start_time) * (float)1000000000,
2267  p_ctx->enc_total_bytes_received[i]);
2268 
2269  ni_device_session_close(&p_enc_ctx_list[i], 1, NI_DEVICE_TYPE_ENCODER);
2270  }
2271 
2272  ni_log(NI_LOG_DEBUG, "Cleanup recycled %d internal buffers\n",
2273  nb_recycled);
2274 }
2275 
2276 void *encoder_send_thread(void *args)
2277 {
2278  enc_send_param_t *p_enc_send_param = args;
2279  ni_demo_context_t *p_ctx = p_enc_send_param->p_ctx;
2280  ni_session_context_t *p_enc_ctx_list = p_enc_send_param->p_enc_ctx;
2281  ni_test_frame_list_t *frame_list = p_enc_send_param->frame_list;
2282  ni_session_data_io_t *p_dec_frame = NULL;
2283  ni_session_data_io_t enc_in_frame = {0};
2284  ni_frame_t *p_ni_frame = NULL;
2285  niFrameSurface1_t *p_surface;
2286  int i, ret = 0;
2287 
2288  ni_log(NI_LOG_INFO, "%s start\n", __func__);
2289 
2290  for (;;)
2291  {
2292  while (frame_list_is_empty(frame_list) && !p_ctx->end_all_threads)
2293  {
2294  ni_usleep(100);
2295  }
2296 
2297  if (p_ctx->end_all_threads)
2298  {
2299  break;
2300  }
2301 
2302  p_dec_frame = &frame_list->frames[frame_list->head];
2303  p_ni_frame = &p_dec_frame->data.frame;
2304 
2305  for (i = 0; i < p_enc_send_param->output_total; i++)
2306  {
2307  p_ctx->curr_enc_index = i;
2308  ret = encoder_send_data2(p_ctx, &p_enc_ctx_list[i], p_dec_frame,
2309  &enc_in_frame,
2310  p_enc_send_param->output_width,
2311  p_enc_send_param->output_height);
2312  if (ret < 0) //Error
2313  {
2314  if (p_enc_ctx_list[i].hw_action)
2315  {
2316  //pre close cleanup will clear it out
2317  p_surface = (niFrameSurface1_t *)p_ni_frame->p_data[3];
2318  ni_hw_frame_ref(p_surface);
2319  } else
2320  {
2321  ni_decoder_frame_buffer_free(p_ni_frame);
2322  }
2323  frame_list_drain(frame_list);
2324  ni_log(NI_LOG_ERROR, "Error: encoder send frame failed\n");
2325  break;
2326  } else if (ret == NI_TEST_RETCODE_EAGAIN) {
2327  ni_usleep(100);
2328  i--; //resend frame
2329  continue;
2330  } else if (p_enc_ctx_list[0].hw_action && !p_ctx->enc_eos_sent[i])
2331  {
2332  p_surface = (niFrameSurface1_t *)p_ni_frame->p_data[3];
2333  ni_hw_frame_ref(p_surface);
2334  }
2335  }
2336 
2337  if (ret < 0)
2338  {
2339  break;
2340  } else if (p_enc_ctx_list[0].hw_action)
2341  {
2342  ni_frame_wipe_aux_data(p_ni_frame); //reuse buffer
2343  } else
2344  {
2345  ni_decoder_frame_buffer_free(p_ni_frame);
2346  }
2347  frame_list_drain(frame_list);
2348 
2349  if (p_enc_send_param->p_ctx->enc_eos_sent[0]) // eos
2350  {
2351  ni_log(NI_LOG_INFO, "%s EOS sent\n", __func__);
2352  break;
2353  }
2354  }
2355 
2356  ni_frame_buffer_free(&enc_in_frame.data.frame);
2357 
2358  // Broadcast all codec threads to quit on exception such as NVMe IO
2359  if (ret < 0)
2360  {
2361  p_ctx->end_all_threads = 1;
2362  }
2363 
2364  ni_log(NI_LOG_TRACE, "%s exit\n", __func__);
2365  return (void *)(long)ret;
2366 }
2367 
2368 void *encoder_receive_thread(void *args)
2369 {
2370  enc_recv_param_t *p_enc_recv_param = args;
2371  ni_demo_context_t *p_ctx = p_enc_recv_param->p_ctx;
2372  ni_session_context_t *p_enc_ctx = p_enc_recv_param->p_enc_ctx;
2373  ni_session_data_io_t out_packet[MAX_OUTPUT_FILES] = {0};
2374  int i, ret = 0;
2375  int end_of_all_streams = 0;
2376  uint64_t current_time, previous_time = p_ctx->start_time;
2377 
2378  ni_log(NI_LOG_INFO, "encoder_receive_thread start\n");
2379 
2380  while (!end_of_all_streams && ret >= 0 && !p_ctx->end_all_threads)
2381  {
2382  ret = encoder_receive(p_ctx, p_enc_ctx,
2383  &p_enc_recv_param->frame_list->frames[p_enc_recv_param->frame_list->head],
2384  out_packet, p_enc_recv_param->output_width,
2385  p_enc_recv_param->output_height, p_enc_recv_param->output_total,
2386  p_enc_recv_param->p_file);
2387  for (i = 0; ret >= 0 && i < p_enc_recv_param->output_total; i++)
2388  {
2389  if (!p_ctx->enc_eos_received[i])
2390  {
2391  ni_log(NI_LOG_DEBUG, "enc %d continues to read!\n", i);
2392  end_of_all_streams = 0;
2393  break;
2394  } else
2395  {
2396  ni_log(NI_LOG_DEBUG, "enc %d eos !\n", i);
2397  end_of_all_streams = 1;
2398  }
2399  }
2400 
2402  if (current_time - previous_time >= (uint64_t)1000000000) {
2403  for (i = 0; i < p_enc_recv_param->output_total; i++)
2404  {
2405  ni_log(NI_LOG_INFO, "Encoder %d stats: received %u packets, fps %.2f, total bytes %u\n",
2406  i, p_ctx->num_packets_received[i],
2407  (float)p_enc_ctx[i].frame_num / (float)(current_time - p_ctx->start_time) * (float)1000000000,
2408  p_ctx->enc_total_bytes_received[i]);
2409  }
2411  }
2412  }
2413 
2414  for (i = 0; i < p_enc_recv_param->output_total; i++)
2415  {
2416  ni_packet_buffer_free(&out_packet[i].data.packet);
2417  }
2418 
2419  // Broadcast all codec threads to quit on exception such as NVMe IO.
2420  if (ret < 0)
2421  {
2422  p_ctx->end_all_threads = 1;
2423  }
2424 
2425  ni_log(NI_LOG_TRACE, "%s exit\n", __func__);
2426  return (void *)(long)ret;
2427 }
XCODER_TEST_RECONF_LTR_INTERVAL_API
@ XCODER_TEST_RECONF_LTR_INTERVAL_API
Definition: ni_device_api.h:1765
_ni_xcoder_params::reconf_demo_mode
int reconf_demo_mode
Definition: ni_device_api.h:2731
_ni_long_term_ref::use_long_term_ref
uint8_t use_long_term_ref
Definition: ni_device_api.h:691
_ni_xcoder_params::fps_denominator
uint32_t fps_denominator
Definition: ni_device_api.h:2719
_ni_xcoder_params::luma_linesize
int luma_linesize
Definition: ni_device_api.h:2784
NI_PIX_FMT_BGRA
@ NI_PIX_FMT_BGRA
Definition: ni_device_api.h:267
NI_CODEC_FORMAT_JPEG
@ NI_CODEC_FORMAT_JPEG
Definition: ni_device_api.h:914
NI_MAX_TX_SZ
#define NI_MAX_TX_SZ
Definition: ni_defs.h:249
_ni_encoder_cfg_params::ver_offset
int ver_offset
Definition: ni_device_api.h:2398
_ni_demo_context::ivf_header_written
uint8_t ivf_header_written
Definition: ni_generic_utils.h:139
NI_FRAME_AUX_DATA_MAX_FRAME_SIZE
@ NI_FRAME_AUX_DATA_MAX_FRAME_SIZE
Definition: ni_device_api.h:566
encoder_send_data
int encoder_send_data(ni_demo_context_t *p_ctx, ni_session_context_t *p_enc_ctx, ni_session_data_io_t *p_in_data, void *yuv_buf, int input_video_width, int input_video_height, int is_last_input)
Send encoder input data, read from input file.
Definition: ni_encode_utils.c:735
NI_DEVICE_TYPE_ENCODER
@ NI_DEVICE_TYPE_ENCODER
Definition: ni_defs.h:347
XCODER_TEST_RECONF_CRF_FLOAT
@ XCODER_TEST_RECONF_CRF_FLOAT
Definition: ni_device_api.h:1755
encoder_reinit_session
int encoder_reinit_session(ni_session_context_t *p_enc_ctx, ni_session_data_io_t *p_in_data, ni_session_data_io_t *p_out_data)
Reopen or reconfig encoder upon sequence change.
Definition: ni_encode_utils.c:1405
_ni_demo_context::av1_muxed_num_packets
uint32_t av1_muxed_num_packets[MAX_OUTPUT_FILES]
Definition: ni_generic_utils.h:135
ni_pix_fmt_t
ni_pix_fmt_t
Definition: ni_device_api.h:260
_ni_encoder_cfg_params::hor_offset
int hor_offset
Definition: ni_device_api.h:2397
XCODER_TEST_CRF_API
@ XCODER_TEST_CRF_API
Definition: ni_device_api.h:1770
ni_frame_buffer_free
ni_retcode_t ni_frame_buffer_free(ni_frame_t *p_frame)
Free frame buffer that was previously allocated with either ni_frame_buffer_alloc or ni_encoder_frame...
Definition: ni_device_api.c:3561
_ni_packet::pts
long long pts
Definition: ni_device_api.h:2828
_ni_frame::sar_width
uint16_t sar_width
Definition: ni_device_api.h:2687
XCODER_TEST_RECONF_MAX_FRAME_SIZE
@ XCODER_TEST_RECONF_MAX_FRAME_SIZE
Definition: ni_device_api.h:1752
ni_hw_frame_ref
void ni_hw_frame_ref(const niFrameSurface1_t *p_surface)
Definition: ni_generic_utils.c:658
ni_retrieve_xcoder_gop
int ni_retrieve_xcoder_gop(char xcoderGop[], ni_xcoder_params_t *params, ni_session_context_t *ctx)
Retrieve custom gop config values from –xcoder-gop.
Definition: ni_util.c:3861
encoder_stat_report_and_close
void encoder_stat_report_and_close(ni_demo_context_t *p_ctx, ni_session_context_t *p_enc_ctx_list, int output_total)
Definition: ni_encode_utils.c:2252
_ni_packet::recycle_index
int recycle_index
Definition: ni_device_api.h:2836
enc_recv_param::p_file
FILE ** p_file
Definition: ni_encode_utils.h:53
ni_encoder_change_params_t
struct _ni_encoder_change_params_t ni_encoder_change_params_t
This is a data structure for encoding parameters that have changed.
XCODER_TEST_RECONF_LTR_INTERVAL
@ XCODER_TEST_RECONF_LTR_INTERVAL
Definition: ni_device_api.h:1749
XCODER_TEST_RECONF_FRAMERATE_API
@ XCODER_TEST_RECONF_FRAMERATE_API
Definition: ni_device_api.h:1767
ni_enc_copy_aux_data
void ni_enc_copy_aux_data(ni_session_context_t *p_enc_ctx, ni_frame_t *p_enc_frame, ni_frame_t *p_dec_frame, ni_codec_format_t codec_format, const uint8_t *mdcv_data, const uint8_t *cll_data, const uint8_t *cc_data, const uint8_t *udu_data, const uint8_t *hdrp_data, int is_hwframe, int is_semiplanar)
Copy auxiliary data that should be sent together with this frame to encoder.
Definition: ni_av_codec.c:2267
_ni_vui_hrd
Definition: ni_device_api.h:654
_ni_rc_min_max_qp::minQpI
int32_t minQpI
Definition: ni_device_api.h:706
ni_device_close
void ni_device_close(ni_device_handle_t device_handle)
Close device and release resources.
Definition: ni_device_api.c:503
SESSION_RUN_STATE_FLUSHING
@ SESSION_RUN_STATE_FLUSHING
Definition: ni_device_api.h:1181
encoder_send_thread
void * encoder_send_thread(void *args)
Definition: ni_encode_utils.c:2276
ni_force_idr_frame_type
ni_retcode_t ni_force_idr_frame_type(ni_session_context_t *p_ctx)
Force next frame to be IDR frame during encoding.
Definition: ni_device_api.c:9992
XCODER_TEST_RECONF_RC_MIN_MAX_QP_API
@ XCODER_TEST_RECONF_RC_MIN_MAX_QP_API
Definition: ni_device_api.h:1769
_ni_session_context::session_id
uint32_t session_id
Definition: ni_device_api.h:1480
ni_hw_frame_unref
void ni_hw_frame_unref(uint16_t hwframe_index)
Definition: ni_generic_utils.c:679
_ni_xcoder_params::source_width
int source_width
Definition: ni_device_api.h:2723
_ni_frame::reconf_len
unsigned int reconf_len
Definition: ni_device_api.h:2655
ni_reconfig_intraprd
ni_retcode_t ni_reconfig_intraprd(ni_session_context_t *p_ctx, int32_t intra_period)
Reconfigure intraPeriod dynamically during encoding.
Definition: ni_device_api.c:9909
ni_generic_utils.h
_ni_session_context::target_bitrate
int32_t target_bitrate
Definition: ni_device_api.h:1599
ni_device_session_sequence_change
ni_retcode_t ni_device_session_sequence_change(ni_session_context_t *p_ctx, int width, int height, int bit_depth_factor, ni_device_type_t device_type)
Send sequence change information to device.
Definition: ni_device_api.c:11322
NI_CODEC_HW_NONE
@ NI_CODEC_HW_NONE
Definition: ni_device_api.h:939
NI_PIX_FMT_YUV420P
@ NI_PIX_FMT_YUV420P
Definition: ni_device_api.h:262
_ni_rc_min_max_qp
Definition: ni_device_api.h:704
_ni_packet::end_of_stream
uint32_t end_of_stream
Definition: ni_device_api.h:2831
_ni_session_data_io::packet
ni_packet_t packet
Definition: ni_device_api.h:2871
_ni_frame::src_codec
ni_codec_format_t src_codec
Definition: ni_device_api.h:2604
_ni_packet::dts
long long dts
Definition: ni_device_api.h:2827
NI_RETCODE_SUCCESS
@ NI_RETCODE_SUCCESS
Definition: ni_defs.h:427
XCODER_TEST_RECONF_SLICE_ARG
@ XCODER_TEST_RECONF_SLICE_ARG
Definition: ni_device_api.h:1758
_ni_framerate::framerate_denom
int32_t framerate_denom
Definition: ni_device_api.h:701
ni_encoder_sw_frame_buffer_alloc
ni_retcode_t ni_encoder_sw_frame_buffer_alloc(bool planar, ni_frame_t *p_frame, int video_width, int video_height, int linesize[], int alignment, int extra_len, bool alignment_2pass_wa)
This API is a wrapper for ni_encoder_frame_buffer_alloc(), used for planar pixel formats,...
Definition: ni_device_api.c:3531
ni_reconfig_framerate
ni_retcode_t ni_reconfig_framerate(ni_session_context_t *p_ctx, ni_framerate_t *framerate)
Reconfigure framerate dynamically during encoding.
Definition: ni_device_api.c:10107
_ni_frame::color_trc
uint8_t color_trc
Definition: ni_device_api.h:2683
NI_2PASS_ENCODE_MIN_WIDTH
#define NI_2PASS_ENCODE_MIN_WIDTH
Definition: ni_device_api.h:128
ni_encode_utils.h
XCODER_TEST_RECONF_CRF
@ XCODER_TEST_RECONF_CRF
Definition: ni_device_api.h:1754
NI_INVALID_SESSION_ID
#define NI_INVALID_SESSION_ID
Definition: ni_device_api.h:111
NI_FRAME_AUX_DATA_CRF
@ NI_FRAME_AUX_DATA_CRF
Definition: ni_device_api.h:574
_ni_xcoder_params::color_primaries
int color_primaries
Definition: ni_device_api.h:2756
_ni_session_context::ori_chroma_linesize
int ori_chroma_linesize
Definition: ni_device_api.h:1681
current_time
struct timeval current_time
Definition: ni_p2p_read_test.c:80
ni_retrieve_xcoder_params
int ni_retrieve_xcoder_params(char xcoderParams[], ni_xcoder_params_t *params, ni_session_context_t *ctx)
retrieve encoder config parameter values from –xcoder-params
Definition: ni_util.c:3787
prep_reconf_demo_data
void prep_reconf_demo_data(ni_demo_context_t *p_ctx, ni_session_context_t *p_enc_ctx, ni_frame_t *frame)
Definition: ni_encode_utils.c:124
XCODER_TEST_RECONF_VUI_HRD
@ XCODER_TEST_RECONF_VUI_HRD
Definition: ni_device_api.h:1744
ni_gettime_ns
uint64_t ni_gettime_ns(void)
Definition: ni_util.c:1998
MAX_OUTPUT_FILES
#define MAX_OUTPUT_FILES
Definition: ni_generic_utils.h:48
ni_enc_write_from_yuv_buffer
int ni_enc_write_from_yuv_buffer(ni_session_context_t *p_ctx, ni_frame_t *p_enc_frame, uint8_t *p_yuv_buffer)
Send an input data frame to the encoder with YUV data given in the inputs.
Definition: ni_av_codec.c:2625
XCODER_TEST_RECONF_RC_MIN_MAX_QP_REDUNDANT
@ XCODER_TEST_RECONF_RC_MIN_MAX_QP_REDUNDANT
Definition: ni_device_api.h:1753
_ni_xcoder_params::rootBufId
int rootBufId
Definition: ni_device_api.h:2775
_ni_frame::vui_num_units_in_tick
uint32_t vui_num_units_in_tick
Definition: ni_device_api.h:2689
ni_set_frame_ref_invalid
ni_retcode_t ni_set_frame_ref_invalid(ni_session_context_t *p_ctx, int32_t frame_num)
Set frame reference invalidation.
Definition: ni_device_api.c:10082
XCODER_TEST_RECONF_FRAMERATE
@ XCODER_TEST_RECONF_FRAMERATE
Definition: ni_device_api.h:1751
ni_av_codec.h
Audio/video related utility definitions.
NI_2PASS_ENCODE_MIN_HEIGHT
#define NI_2PASS_ENCODE_MIN_HEIGHT
Definition: ni_device_api.h:129
ni_reconfig_crf
ni_retcode_t ni_reconfig_crf(ni_session_context_t *p_ctx, int32_t crf)
Reconfigure crf value dynamically during encoding.
Definition: ni_device_api.c:10301
_ni_encoder_cfg_params::enable_acq_limit
int enable_acq_limit
Definition: ni_device_api.h:2429
XCODER_TEST_RECONF_INTRAPRD
@ XCODER_TEST_RECONF_INTRAPRD
Definition: ni_device_api.h:1743
ni_packet_buffer_free
ni_retcode_t ni_packet_buffer_free(ni_packet_t *p_packet)
Free packet buffer that was previously allocated with ni_packet_buffer_alloc.
Definition: ni_device_api.c:3843
_ni_session_context::blk_io_handle
ni_device_handle_t blk_io_handle
Definition: ni_device_api.h:1465
enc_recv_param::p_ctx
ni_demo_context_t * p_ctx
Definition: ni_encode_utils.h:49
_ni_demo_context::p_av1_seq_header
uint8_t * p_av1_seq_header[MAX_OUTPUT_FILES]
Definition: ni_generic_utils.h:136
_ni_encoder_cfg_params::roi_enable
int roi_enable
Definition: ni_device_api.h:2257
ni_encoder_frame_zerocopy_check
ni_retcode_t ni_encoder_frame_zerocopy_check(ni_session_context_t *p_enc_ctx, ni_xcoder_params_t *p_enc_params, int width, int height, const int linesize[], bool set_linesize)
Check if incoming frame is encoder zero copy compatible or not.
Definition: ni_device_api.c:2733
_ni_demo_context::end_all_threads
uint8_t end_all_threads
Definition: ni_generic_utils.h:111
_ni_demo_context::enc_sos_sent
int enc_sos_sent[MAX_OUTPUT_FILES]
Definition: ni_generic_utils.h:146
NI_MULTICORE_ENCODE_MIN_HEIGHT
#define NI_MULTICORE_ENCODE_MIN_HEIGHT
Definition: ni_device_api.h:132
NI_FRAME_AUX_DATA_SLICE_ARG
@ NI_FRAME_AUX_DATA_SLICE_ARG
Definition: ni_device_api.h:590
_ni_frame::sei_user_data_unreg_len
unsigned int sei_user_data_unreg_len
Definition: ni_device_api.h:2644
_ni_packet::av1_p_data
uint8_t * av1_p_data[MAX_AV1_ENCODER_GOP_NUM]
Definition: ni_device_api.h:2845
ni_copy_hw_descriptors
void ni_copy_hw_descriptors(uint8_t *p_dst[NI_MAX_NUM_DATA_POINTERS], uint8_t *p_src[NI_MAX_NUM_DATA_POINTERS])
Copy Descriptor data to Netint HW descriptor frame layout to be sent to encoder for encoding....
Definition: ni_util.c:3576
encoder_sequence_change
int encoder_sequence_change(ni_session_context_t *p_enc_ctx, ni_session_data_io_t *p_in_data, ni_session_data_io_t *p_out_data, int width, int height, ni_pix_fmt_t pix_fmt)
Definition: ni_encode_utils.c:1949
_ni_xcoder_params::roi_demo_mode
int roi_demo_mode
Definition: ni_device_api.h:2730
_ni_session_context::src_endian
int src_endian
Definition: ni_device_api.h:1493
_ni_packet::av1_buffer_size
uint32_t av1_buffer_size[MAX_AV1_ENCODER_GOP_NUM]
Definition: ni_device_api.h:2846
_ni_frame::sei_total_len
unsigned int sei_total_len
Definition: ni_device_api.h:2629
ni_reconfig_vbv_value
ni_retcode_t ni_reconfig_vbv_value(ni_session_context_t *p_ctx, int32_t vbvMaxRate, int32_t vbvBufferSize)
Reconfigure vbv buffer size and vbv max rate dynamically during encoding.
Definition: ni_device_api.c:10418
_ni_xcoder_params::video_full_range_flag
int video_full_range_flag
Definition: ni_device_api.h:2761
_ni_encoder_change_params_t::aspectRatioHeight
uint16_t aspectRatioHeight
Definition: ni_device_api.h:1024
_ni_encoder_change_params_t::enable_option
uint32_t enable_option
Definition: ni_device_api.h:984
XCODER_TEST_RECONF_VBV
@ XCODER_TEST_RECONF_VBV
Definition: ni_device_api.h:1756
_ni_encoder_cfg_params::aspectRatioWidth
int aspectRatioWidth
Definition: ni_device_api.h:2240
XCODER_TEST_INVALID_REF_FRAME
@ XCODER_TEST_INVALID_REF_FRAME
Definition: ni_device_api.h:1750
NI_TEST_RETCODE_NEXT_INPUT
#define NI_TEST_RETCODE_NEXT_INPUT
Definition: ni_generic_utils.h:54
_ni_packet::av1_data_len
uint32_t av1_data_len[MAX_AV1_ENCODER_GOP_NUM]
Definition: ni_device_api.h:2847
_ni_session_context::bit_depth_factor
int bit_depth_factor
Definition: ni_device_api.h:1494
XCODER_TEST_RECONF_RC_MIN_MAX_QP_API_REDUNDANT
@ XCODER_TEST_RECONF_RC_MIN_MAX_QP_API_REDUNDANT
Definition: ni_device_api.h:1764
_ni_encoder_change_params_t
This is a data structure for encoding parameters that have changed.
Definition: ni_device_api.h:982
NI_ALIGN
#define NI_ALIGN(x, a)
Definition: ni_generic_utils.h:57
scan_and_clean_hwdescriptors
int scan_and_clean_hwdescriptors(void)
Definition: ni_generic_utils.c:635
_ni_frame::force_pic_qp
uint16_t force_pic_qp
Definition: ni_device_api.h:2659
_ni_packet::av1_buffer_index
int av1_buffer_index
Definition: ni_device_api.h:2848
_ni_encoder_cfg_params::crf
int crf
Definition: ni_device_api.h:2269
_ni_session_context::hw_id
int hw_id
Definition: ni_device_api.h:1478
ni_copy_hw_yuv420p
void ni_copy_hw_yuv420p(uint8_t *p_dst[NI_MAX_NUM_DATA_POINTERS], uint8_t *p_src[NI_MAX_NUM_DATA_POINTERS], int frame_width, int frame_height, int factor, int is_semiplanar, int conf_win_right, int dst_stride[NI_MAX_NUM_DATA_POINTERS], int dst_height[NI_MAX_NUM_DATA_POINTERS], int src_stride[NI_MAX_NUM_DATA_POINTERS], int src_height[NI_MAX_NUM_DATA_POINTERS])
Copy YUV data to Netint HW YUV420p frame layout to be sent to encoder for encoding....
Definition: ni_util.c:2352
NI_FRAME_AUX_DATA_VBV_BUFFER_SIZE
@ NI_FRAME_AUX_DATA_VBV_BUFFER_SIZE
Definition: ni_device_api.h:586
ni_reconfig_crf2
ni_retcode_t ni_reconfig_crf2(ni_session_context_t *p_ctx, float crf)
Reconfigure crf float point value dynamically during encoding.
Definition: ni_device_api.c:10358
_ni_session_context::ori_width
int ori_width
Definition: ni_device_api.h:1562
_ni_xcoder_params::fps_number
uint32_t fps_number
Definition: ni_device_api.h:2718
NI_FRAME_AUX_DATA_VBV_MAX_RATE
@ NI_FRAME_AUX_DATA_VBV_MAX_RATE
Definition: ni_device_api.h:582
_ni_enc_quad_roi_custom_map::roiAbsQp_flag
uint8_t roiAbsQp_flag
Definition: ni_device_api.h:887
_ni_session_context::ddr_config
uint8_t ddr_config
Definition: ni_device_api.h:1626
enc_send_param::p_ctx
ni_demo_context_t * p_ctx
Definition: ni_encode_utils.h:39
_ni_test_frame_list
Definition: ni_generic_utils.h:102
NI_SET_CHANGE_PARAM_VUI_HRD_PARAM
@ NI_SET_CHANGE_PARAM_VUI_HRD_PARAM
Definition: ni_device_api.h:968
ni_retcode_t
ni_retcode_t
Definition: ni_defs.h:425
NI_FRAME_AUX_DATA_MAX_MIN_QP
@ NI_FRAME_AUX_DATA_MAX_MIN_QP
Definition: ni_device_api.h:570
_ni_vui_hrd::colorPrimaries
int32_t colorPrimaries
Definition: ni_device_api.h:661
SESSION_RUN_STATE_SEQ_CHANGE_DRAINING
@ SESSION_RUN_STATE_SEQ_CHANGE_DRAINING
Definition: ni_device_api.h:1178
ni_enc_prep_aux_data
void ni_enc_prep_aux_data(ni_session_context_t *p_enc_ctx, ni_frame_t *p_enc_frame, ni_frame_t *p_dec_frame, ni_codec_format_t codec_format, int should_send_sei_with_frame, uint8_t *mdcv_data, uint8_t *cll_data, uint8_t *cc_data, uint8_t *udu_data, uint8_t *hdrp_data)
Prepare auxiliary data that should be sent together with this frame to encoder based on the auxiliary...
Definition: ni_av_codec.c:858
SESSION_RUN_STATE_NORMAL
@ SESSION_RUN_STATE_NORMAL
Definition: ni_device_api.h:1177
ni_packet_buffer_alloc
ni_retcode_t ni_packet_buffer_alloc(ni_packet_t *p_packet, int packet_size)
Allocate memory for the packet buffer based on provided packet size.
Definition: ni_device_api.c:3713
frame_list_is_empty
bool frame_list_is_empty(ni_test_frame_list_t *list)
Definition: ni_generic_utils.c:175
ni_frame_new_aux_data
ni_aux_data_t * ni_frame_new_aux_data(ni_frame_t *frame, ni_aux_data_type_t type, int data_size)
Add a new auxiliary data to a frame.
Definition: ni_device_api.c:4005
_ni_encoder_change_params_t::colorPrimaries
uint8_t colorPrimaries
Definition: ni_device_api.h:1020
_ni_session_context::framerate
ni_framerate_t framerate
Definition: ni_device_api.h:1604
previous_time
struct timeval previous_time
Definition: ni_p2p_read_test.c:79
ni_log.h
Logging definitions.
XCODER_TEST_RECONF_OFF
@ XCODER_TEST_RECONF_OFF
Definition: ni_device_api.h:1741
_niFrameSurface1::ui16FrameIdx
uint16_t ui16FrameIdx
Definition: ni_device_api.h:2795
enc_send_param
Definition: ni_encode_utils.h:37
enc_send_param::p_enc_ctx
ni_session_context_t * p_enc_ctx
Definition: ni_encode_utils.h:40
_ni_long_term_ref
Definition: ni_device_api.h:683
_ni_demo_context
Definition: ni_generic_utils.h:109
NI_LOG_INFO
@ NI_LOG_INFO
Definition: ni_log.h:61
_ni_vui_hrd::colorDescPresent
int32_t colorDescPresent
Definition: ni_device_api.h:657
_ni_xcoder_params::source_height
int source_height
Definition: ni_device_api.h:2728
NI_MULTICORE_ENCODE_MIN_WIDTH
#define NI_MULTICORE_ENCODE_MIN_WIDTH
Definition: ni_device_api.h:131
NI_APP_ENC_FRAME_META_DATA_SIZE
#define NI_APP_ENC_FRAME_META_DATA_SIZE
Definition: ni_defs.h:308
_ni_encoder_change_params_t::colorSpace
uint8_t colorSpace
Definition: ni_device_api.h:1022
XCODER_TEST_RECONF_BR_API
@ XCODER_TEST_RECONF_BR_API
Definition: ni_device_api.h:1760
NI_LOG_ERROR
@ NI_LOG_ERROR
Definition: ni_log.h:60
_ni_session_context::src_bit_depth
int src_bit_depth
Definition: ni_device_api.h:1492
_ni_frame::ni_pict_type
ni_pic_type_t ni_pict_type
Definition: ni_device_api.h:2627
_ni_vui_hrd::colorTrc
int32_t colorTrc
Definition: ni_device_api.h:665
_ni_framerate::framerate_num
int32_t framerate_num
Definition: ni_device_api.h:698
NI_TEST_RETCODE_END_OF_STREAM
#define NI_TEST_RETCODE_END_OF_STREAM
Definition: ni_generic_utils.h:52
_ni_xcoder_params::cfg_enc_params
ni_encoder_cfg_params_t cfg_enc_params
Definition: ni_device_api.h:2765
NI_PIX_FMT_NV12
@ NI_PIX_FMT_NV12
Definition: ni_device_api.h:264
XCODER_TEST_RECONF_INTRAPRD_API
@ XCODER_TEST_RECONF_INTRAPRD_API
Definition: ni_device_api.h:1761
_ni_frame::data_len
uint32_t data_len[NI_MAX_NUM_DATA_POINTERS]
Definition: ni_device_api.h:2664
_ni_frame::extra_data_len
unsigned int extra_data_len
Definition: ni_device_api.h:2657
_ni_demo_context::enc_eos_sent
int enc_eos_sent[MAX_OUTPUT_FILES]
Definition: ni_generic_utils.h:147
_ni_demo_context::enc_resend
int enc_resend[MAX_OUTPUT_FILES]
Definition: ni_generic_utils.h:145
_ni_encoder_cfg_params::multicoreJointMode
int multicoreJointMode
Definition: ni_device_api.h:2289
ni_encoder_init_default_params
ni_retcode_t ni_encoder_init_default_params(ni_xcoder_params_t *p_param, int fps_num, int fps_denom, long bit_rate, int width, int height, ni_codec_format_t codec_format)
Initialize default encoder parameters.
Definition: ni_device_api.c:4147
_ni_encoder_change_params_t::colorDescPresent
uint8_t colorDescPresent
Definition: ni_device_api.h:1019
_ni_xcoder_params::reconf_hash
int reconf_hash[NI_BITRATE_RECONFIG_FILE_MAX_LINES][NI_BITRATE_RECONFIG_FILE_MAX_ENTRIES_PER_LINE]
Definition: ni_device_api.h:2773
NI_FRAME_AUX_DATA_FRAMERATE
@ NI_FRAME_AUX_DATA_FRAMERATE
Definition: ni_device_api.h:562
NI_TEST_RETCODE_EAGAIN
#define NI_TEST_RETCODE_EAGAIN
Definition: ni_generic_utils.h:53
_ni_test_frame_list::head
int head
Definition: ni_generic_utils.h:105
encoder_open_session
int encoder_open_session(ni_session_context_t *p_enc_ctx, int dst_codec_format, int iXcoderGUID, ni_xcoder_params_t *p_enc_params, int width, int height, ni_pix_fmt_t pix_fmt, bool check_zerocopy)
Encoder session open.
Definition: ni_encode_utils.c:1258
enc_send_param::output_total
int output_total
Definition: ni_encode_utils.h:43
encoder_receive
int encoder_receive(ni_demo_context_t *p_ctx, ni_session_context_t *enc_ctx_list, ni_session_data_io_t *in_frame, ni_session_data_io_t *pkt, int width, int height, int output_total, FILE **pfs_list)
Definition: ni_encode_utils.c:2181
NI_LOG_TRACE
@ NI_LOG_TRACE
Definition: ni_log.h:63
_ni_frame::end_of_stream
uint32_t end_of_stream
Definition: ni_device_api.h:2608
NI_NUM_PIXEL_ASPECT_RATIO
#define NI_NUM_PIXEL_ASPECT_RATIO
Definition: ni_av_codec.h:38
write_av1_ivf_trailer
int write_av1_ivf_trailer(ni_demo_context_t *p_ctx, ni_packet_t *p_out_pkt, uint32_t meta_size, FILE *p_file)
Definition: ni_encode_utils.c:1677
NI_TEST_RETCODE_FAILURE
#define NI_TEST_RETCODE_FAILURE
Definition: ni_generic_utils.h:50
_ni_xcoder_params::hwframes
int hwframes
Definition: ni_device_api.h:2774
_ni_session_context::device_handle
ni_device_handle_t device_handle
Definition: ni_device_api.h:1462
_ni_session_context::enc_change_params
ni_encoder_change_params_t * enc_change_params
Definition: ni_device_api.h:1592
_ni_session_context::p_session_config
void * p_session_config
Definition: ni_device_api.h:1473
NI_CODEC_FORMAT_AV1
@ NI_CODEC_FORMAT_AV1
Definition: ni_device_api.h:915
_ni_frame::sei_hdr_plus_len
unsigned int sei_hdr_plus_len
Definition: ni_device_api.h:2641
XCODER_TEST_RECONF_SLICE_ARG_API
@ XCODER_TEST_RECONF_SLICE_ARG_API
Definition: ni_device_api.h:1774
_ni_packet::av1_p_buffer
uint8_t * av1_p_buffer[MAX_AV1_ENCODER_GOP_NUM]
Definition: ni_device_api.h:2844
get_pixel_planar
ni_pixel_planar_format get_pixel_planar(ni_pix_fmt_t pix_fmt)
Definition: ni_generic_utils.c:147
_ni_packet::p_data
void * p_data
Definition: ni_device_api.h:2837
_ni_session_data_io
Definition: ni_device_api.h:2866
_ni_enc_quad_roi_custom_map::ipcm_flag
uint8_t ipcm_flag
Definition: ni_device_api.h:891
encoder_receive_thread
void * encoder_receive_thread(void *args)
Definition: ni_encode_utils.c:2368
_ni_enc_quad_roi_custom_map::field
struct _ni_enc_quad_roi_custom_map::@6 field
encoder_close_session
int encoder_close_session(ni_session_context_t *p_enc_ctx, ni_session_data_io_t *p_in_data, ni_session_data_io_t *p_out_data)
encoder session close
Definition: ni_encode_utils.c:1924
NI_PIX_FMT_YUV420P10LE
@ NI_PIX_FMT_YUV420P10LE
Definition: ni_device_api.h:263
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_session_context::sender_handle
ni_device_handle_t sender_handle
Definition: ni_device_api.h:1468
_ni_frame::vui_time_scale
uint32_t vui_time_scale
Definition: ni_device_api.h:2690
set_demo_roi_map
void set_demo_roi_map(ni_session_context_t *p_enc_ctx)
Set up hard coded demo ROI map.
Definition: ni_encode_utils.c:41
NI_TEST_RETCODE_SUCCESS
#define NI_TEST_RETCODE_SUCCESS
Definition: ni_generic_utils.h:51
_ni_demo_context::num_frames_sent
uint64_t num_frames_sent[MAX_OUTPUT_FILES]
Definition: ni_generic_utils.h:129
enc_recv_param::p_enc_ctx
ni_session_context_t * p_enc_ctx
Definition: ni_encode_utils.h:50
ni_reconfig_bitrate
ni_retcode_t ni_reconfig_bitrate(ni_session_context_t *p_ctx, int32_t bitrate)
Reconfigure bitrate dynamically during encoding.
Definition: ni_device_api.c:9869
ni_packet_buffer_free_av1
ni_retcode_t ni_packet_buffer_free_av1(ni_packet_t *p_packet)
Free packet buffer that was previously allocated with ni_packet_buffer_alloc for AV1 packets merge.
Definition: ni_device_api.c:3884
ni_usleep
void ni_usleep(int64_t usec)
Definition: ni_util.c:358
_ni_frame::force_key_frame
int force_key_frame
Definition: ni_device_api.h:2624
NI_GET_MAX_HWDESC_FRAME_INDEX
#define NI_GET_MAX_HWDESC_FRAME_INDEX(x)
Definition: ni_defs.h:278
NI_PIXEL_PLANAR_FORMAT_SEMIPLANAR
@ NI_PIXEL_PLANAR_FORMAT_SEMIPLANAR
Definition: ni_device_api.h:920
enc_recv_param::frame_list
ni_test_frame_list_t * frame_list
Definition: ni_encode_utils.h:55
_ni_xcoder_params::sar_num
int sar_num
Definition: ni_device_api.h:2759
XCODER_TEST_RECONF_RC_MIN_MAX_QP
@ XCODER_TEST_RECONF_RC_MIN_MAX_QP
Definition: ni_device_api.h:1747
_ni_test_frame_list::frames
ni_session_data_io_t frames[NI_MAX_BUFFERED_FRAME]
Definition: ni_generic_utils.h:104
_ni_packet
Definition: ni_device_api.h:2825
_ni_session_context::frame_num
uint64_t frame_num
Definition: ni_device_api.h:1531
write_av1_ivf_header
void write_av1_ivf_header(ni_demo_context_t *p_ctx, uint32_t width, uint32_t height, uint32_t frame_num, uint32_t frame_denom, FILE *p_file)
Definition: ni_encode_utils.c:1549
_ni_session_context::ori_luma_linesize
int ori_luma_linesize
Definition: ni_device_api.h:1680
_ni_session_context::roi_map
ni_enc_quad_roi_custom_map * roi_map
Definition: ni_device_api.h:1582
_ni_encoder_cfg_params::colorDescPresent
int colorDescPresent
Definition: ni_device_api.h:2311
_ni_frame::p_data
uint8_t * p_data[NI_MAX_NUM_DATA_POINTERS]
Definition: ni_device_api.h:2663
_ni_xcoder_params::sar_denom
int sar_denom
Definition: ni_device_api.h:2760
XCODER_TEST_RECONF_VBV_API
@ XCODER_TEST_RECONF_VBV_API
Definition: ni_device_api.h:1772
XCODER_TEST_FORCE_IDR_FRAME
@ XCODER_TEST_FORCE_IDR_FRAME
Definition: ni_device_api.h:1759
NI_TEST_RETCODE_SEQ_CHANGE_DONE
#define NI_TEST_RETCODE_SEQ_CHANGE_DONE
Definition: ni_generic_utils.h:55
_ni_frame::roi_len
unsigned int roi_len
Definition: ni_device_api.h:2653
NI_COL_TRC_UNSPECIFIED
@ NI_COL_TRC_UNSPECIFIED
Definition: ni_av_codec.h:125
encoder_open
int encoder_open(ni_session_context_t *enc_ctx_list, ni_xcoder_params_t *p_api_param_list, int output_total, char p_enc_conf_params[][2048], char p_enc_conf_gop[][2048], ni_frame_t *p_ni_frame, int width, int height, int fps_num, int fps_den, int bitrate, int codec_format, ni_pix_fmt_t pix_fmt, int aspect_ratio_idc, int xcoder_guid, niFrameSurface1_t *p_surface, int multi_thread, bool check_zerocopy)
Definition: ni_encode_utils.c:1999
NI_PARAM_AV1_ALIGN_WIDTH_HEIGHT
#define NI_PARAM_AV1_ALIGN_WIDTH_HEIGHT
Definition: ni_device_api.h:148
_ni_session_context::hw_action
int hw_action
Definition: ni_device_api.h:1609
_ni_session_context
Definition: ni_device_api.h:1408
_ni_rc_min_max_qp::minQpPB
int32_t minQpPB
Definition: ni_device_api.h:709
NI_PIX_FMT_P010LE
@ NI_PIX_FMT_P010LE
Definition: ni_device_api.h:265
ni_device_session_read
int ni_device_session_read(ni_session_context_t *p_ctx, ni_session_data_io_t *p_data, ni_device_type_t device_type)
Read data from the device If device_type is NI_DEVICE_TYPE_DECODER reads data packet from decoder If ...
Definition: ni_device_api.c:1769
_niFrameSurface1
Definition: ni_device_api.h:2793
NI_CODEC_HW_ENABLE
@ NI_CODEC_HW_ENABLE
Definition: ni_device_api.h:940
ni_frame_buffer_alloc_hwenc
ni_retcode_t ni_frame_buffer_alloc_hwenc(ni_frame_t *p_frame, int video_width, int video_height, int extra_len)
Allocate memory for the hwDescriptor buffer based on provided parameters taking into account pic size...
Definition: ni_device_api.c:8399
NI_MAX_NUM_DATA_POINTERS
#define NI_MAX_NUM_DATA_POINTERS
Definition: ni_defs.h:232
ni_device_session_restart
ni_retcode_t ni_device_session_restart(ni_session_context_t *p_ctx, int video_width, int video_height, ni_device_type_t device_type)
Send a restart command after flush command Only support Encoder now.
Definition: ni_device_api.c:12913
_ni_session_data_io::data
union _ni_session_data_io::@19 data
_ni_frame
Definition: ni_device_api.h:2601
enc_recv_param::output_height
int output_height
Definition: ni_encode_utils.h:52
NI_PIX_FMT_RGBA
@ NI_PIX_FMT_RGBA
Definition: ni_device_api.h:266
_ni_encoder_cfg_params::conf_win_bottom
int conf_win_bottom
Definition: ni_device_api.h:2298
_ni_frame::color_space
uint8_t color_space
Definition: ni_device_api.h:2684
ni_device_session_open
ni_retcode_t ni_device_session_open(ni_session_context_t *p_ctx, ni_device_type_t device_type)
Open a new device session depending on the device_type parameter If device_type is NI_DEVICE_TYPE_DEC...
Definition: ni_device_api.c:710
_ni_session_context::roi_len
uint32_t roi_len
Definition: ni_device_api.h:1496
XCODER_TEST_RECONF_VUI_HRD_API
@ XCODER_TEST_RECONF_VUI_HRD_API
Definition: ni_device_api.h:1762
_ni_encoder_cfg_params::colorSpace
int colorSpace
Definition: ni_device_api.h:2314
XCODER_TEST_RECONF_LONG_TERM_REF
@ XCODER_TEST_RECONF_LONG_TERM_REF
Definition: ni_device_api.h:1745
NI_PIX_FMT_ABGR
@ NI_PIX_FMT_ABGR
Definition: ni_device_api.h:269
_ni_frame::color_primaries
uint8_t color_primaries
Definition: ni_device_api.h:2682
_ni_demo_context::start_time
uint64_t start_time
Definition: ni_generic_utils.h:132
NI_PIX_FMT_ARGB
@ NI_PIX_FMT_ARGB
Definition: ni_device_api.h:268
ni_reconfig_max_frame_size
ni_retcode_t ni_reconfig_max_frame_size(ni_session_context_t *p_ctx, int32_t max_frame_size)
Reconfigure maxFrameSize dynamically during encoding.
Definition: ni_device_api.c:10176
_ni_xcoder_params
Definition: ni_device_api.h:2713
_ni_frame::sei_hdr_content_light_level_info_len
unsigned int sei_hdr_content_light_level_info_len
Definition: ni_device_api.h:2638
enc_recv_param::output_width
int output_width
Definition: ni_encode_utils.h:51
frame_list_drain
int frame_list_drain(ni_test_frame_list_t *list)
Definition: ni_generic_utils.c:219
_ni_frame::sei_hdr_mastering_display_color_vol_len
unsigned int sei_hdr_mastering_display_color_vol_len
Definition: ni_device_api.h:2636
NI_COL_PRI_UNSPECIFIED
@ NI_COL_PRI_UNSPECIFIED
Definition: ni_av_codec.h:99
_ni_frame::video_full_range_flag
int video_full_range_flag
Definition: ni_device_api.h:2685
XCODER_TEST_RECONF_MAX_FRAME_SIZE_RATIO_API
@ XCODER_TEST_RECONF_MAX_FRAME_SIZE_RATIO_API
Definition: ni_device_api.h:1773
_ni_session_context::ready_to_close
uint32_t ready_to_close
Definition: ni_device_api.h:1544
_ni_encoder_cfg_params::crop_width
int crop_width
Definition: ni_device_api.h:2395
ni_reconfig_max_frame_size_ratio
ni_retcode_t ni_reconfig_max_frame_size_ratio(ni_session_context_t *p_ctx, int32_t max_frame_size_ratio)
Reconfigure maxFrameSizeRatio dynamically during encoding.
Definition: ni_device_api.c:10468
ni_reconfig_vui
ni_retcode_t ni_reconfig_vui(ni_session_context_t *p_ctx, ni_vui_hrd_t *vui)
Reconfigure VUI HRD dynamically during encoding.
Definition: ni_device_api.c:9945
XCODER_TEST_RECONF_LTR_API
@ XCODER_TEST_RECONF_LTR_API
Definition: ni_device_api.h:1763
_ni_session_context::codec_format
uint32_t codec_format
Definition: ni_device_api.h:1486
_ni_rc_min_max_qp::maxQpPB
int32_t maxQpPB
Definition: ni_device_api.h:710
XCODER_TEST_RECONF_MAX_FRAME_SIZE_API
@ XCODER_TEST_RECONF_MAX_FRAME_SIZE_API
Definition: ni_device_api.h:1768
_ni_encoder_cfg_params::colorPrimaries
int colorPrimaries
Definition: ni_device_api.h:2312
_ni_demo_context::enc_total_bytes_sent
uint64_t enc_total_bytes_sent[MAX_OUTPUT_FILES]
Definition: ni_generic_utils.h:126
_ni_demo_context::av1_seq_header_len
uint32_t av1_seq_header_len[MAX_OUTPUT_FILES]
Definition: ni_generic_utils.h:137
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
XCODER_TEST_CRF_FLOAT_API
@ XCODER_TEST_CRF_FLOAT_API
Definition: ni_device_api.h:1771
XCODER_TEST_RECONF_MAX_FRAME_SIZE_RATIO
@ XCODER_TEST_RECONF_MAX_FRAME_SIZE_RATIO
Definition: ni_device_api.h:1757
ni_set_ltr_interval
ni_retcode_t ni_set_ltr_interval(ni_session_context_t *p_ctx, int32_t ltr_interval)
Set Long Term Reference interval.
Definition: ni_device_api.c:10054
_ni_encoder_cfg_params::crfFloat
float crfFloat
Definition: ni_device_api.h:2410
_ni_frame::sar_height
uint16_t sar_height
Definition: ni_device_api.h:2688
_ni_session_data_io::frame
ni_frame_t frame
Definition: ni_device_api.h:2870
_ni_session_context::ori_pix_fmt
int ori_pix_fmt
Definition: ni_device_api.h:1562
enc_send_param::output_width
int output_width
Definition: ni_encode_utils.h:41
NI_FRAME_AUX_DATA_LTR_INTERVAL
@ NI_FRAME_AUX_DATA_LTR_INTERVAL
Definition: ni_device_api.h:553
_ni_xcoder_params::color_space
int color_space
Definition: ni_device_api.h:2758
_ni_packet::av1_show_frame
int av1_show_frame
Definition: ni_device_api.h:2849
_ni_demo_context::reconfig_count
uint64_t reconfig_count
Definition: ni_generic_utils.h:131
ni_get_hw_yuv420p_dim
void ni_get_hw_yuv420p_dim(int width, int height, int factor, int is_semiplanar, int plane_stride[NI_MAX_NUM_DATA_POINTERS], int plane_height[NI_MAX_NUM_DATA_POINTERS])
Get dimension information of Netint HW YUV420p frame to be sent to encoder for encoding....
Definition: ni_util.c:2040
MAX_AV1_ENCODER_GOP_NUM
#define MAX_AV1_ENCODER_GOP_NUM
Definition: ni_defs.h:313
_ni_frame::video_height
uint32_t video_height
Definition: ni_device_api.h:2611
_ni_packet::p_buffer
void * p_buffer
Definition: ni_device_api.h:2841
ni_reconfig_min_max_qp
ni_retcode_t ni_reconfig_min_max_qp(ni_session_context_t *p_ctx, ni_rc_min_max_qp *p_min_max_qp)
Reconfigure min&max qp dynamically during encoding.
Definition: ni_device_api.c:10251
_ni_vui_hrd::colorSpace
int32_t colorSpace
Definition: ni_device_api.h:668
_ni_demo_context::num_packets_received
uint64_t num_packets_received[MAX_OUTPUT_FILES]
Definition: ni_generic_utils.h:130
_ni_encoder_cfg_params::colorTrc
int colorTrc
Definition: ni_device_api.h:2313
NI_FRAME_AUX_DATA_LONG_TERM_REF
@ NI_FRAME_AUX_DATA_LONG_TERM_REF
Definition: ni_device_api.h:548
NI_MAX_SEI_DATA
#define NI_MAX_SEI_DATA
Definition: ni_device_api.h:426
encoder_receive_data
int encoder_receive_data(ni_demo_context_t *p_ctx, ni_session_context_t *p_enc_ctx, ni_session_data_io_t *p_out_data, int output_video_width, int output_video_height, FILE *p_file, ni_session_data_io_t *p_in_data)
Receive output data from encoder.
Definition: ni_encode_utils.c:1721
NI_COL_SPC_UNSPECIFIED
@ NI_COL_SPC_UNSPECIFIED
Definition: ni_av_codec.h:161
write_av1_ivf_packet
void write_av1_ivf_packet(ni_demo_context_t *p_ctx, ni_packet_t *p_out_pkt, uint32_t meta_size, FILE *p_file)
Definition: ni_encode_utils.c:1598
NI_MAX_FRAME_SIZE
#define NI_MAX_FRAME_SIZE
Definition: ni_device_api.h:178
_ni_encoder_cfg_params::conf_win_right
int conf_win_right
Definition: ni_device_api.h:2300
_ni_session_context::session_run_state
ni_session_run_state_t session_run_state
Definition: ni_device_api.h:1547
_ni_long_term_ref::use_cur_src_as_long_term_pic
uint8_t use_cur_src_as_long_term_pic
Definition: ni_device_api.h:687
_niFrameSurface1::device_handle
int32_t device_handle
Definition: ni_device_api.h:2800
ni_device_session_write
int ni_device_session_write(ni_session_context_t *p_ctx, ni_session_data_io_t *p_data, ni_device_type_t device_type)
Sends data to the device If device_type is NI_DEVICE_TYPE_DECODER sends data packet to decoder If dev...
Definition: ni_device_api.c:1668
_ni_demo_context::read_framerate
int read_framerate
Definition: ni_generic_utils.h:120
_ni_demo_context::enc_eos_received
int enc_eos_received[MAX_OUTPUT_FILES]
Definition: ni_generic_utils.h:148
XCODER_TEST_INVALID_REF_FRAME_API
@ XCODER_TEST_INVALID_REF_FRAME_API
Definition: ni_device_api.h:1766
_ni_enc_quad_roi_custom_map
encoder AVC ROI custom map (1 MB = 8bits)
Definition: ni_device_api.h:883
_ni_session_context::meta_size
uint32_t meta_size
Params used in VFR mode Done///.
Definition: ni_device_api.h:1638
_ni_encoder_cfg_params::crop_height
int crop_height
Definition: ni_device_api.h:2396
_ni_encoder_cfg_params::aspectRatioHeight
int aspectRatioHeight
Definition: ni_device_api.h:2241
NI_MIN_HEIGHT
#define NI_MIN_HEIGHT
Definition: ni_device_api.h:126
XCODER_TEST_RECONF_BR
@ XCODER_TEST_RECONF_BR
Definition: ni_device_api.h:1742
_ni_encoder_cfg_params::planar
int planar
Definition: ni_device_api.h:2242
_ni_frame::start_of_stream
uint32_t start_of_stream
Definition: ni_device_api.h:2609
_ni_encoder_change_params_t::colorTrc
uint8_t colorTrc
Definition: ni_device_api.h:1021
_ni_packet::buffer_size
uint32_t buffer_size
Definition: ni_device_api.h:2842
_ni_vui_hrd::videoFullRange
int32_t videoFullRange
Definition: ni_device_api.h:679
_ni_vui_hrd::aspectRatioHeight
int32_t aspectRatioHeight
Definition: ni_device_api.h:675
_ni_frame::pixel_format
int pixel_format
Definition: ni_device_api.h:2672
enc_send_param::frame_list
ni_test_frame_list_t * frame_list
Definition: ni_encode_utils.h:44
enc_recv_param::output_total
int output_total
Definition: ni_encode_utils.h:54
_ni_demo_context::av1_output_obu
uint8_t av1_output_obu
Definition: ni_generic_utils.h:138
ni_util.h
Utility definitions.
_ni_encoder_cfg_params::videoFullRange
int videoFullRange
Definition: ni_device_api.h:2290
_ni_rc_min_max_qp::maxQpI
int32_t maxQpI
Definition: ni_device_api.h:707
_ni_encoder_cfg_params::lookAheadDepth
int lookAheadDepth
Definition: ni_device_api.h:2267
SESSION_RUN_STATE_SEQ_CHANGE_OPENING
@ SESSION_RUN_STATE_SEQ_CHANGE_OPENING
Definition: ni_device_api.h:1179
encoder_send_data2
int encoder_send_data2(ni_demo_context_t *p_ctx, ni_session_context_t *p_enc_ctx, ni_session_data_io_t *p_dec_out_data, ni_session_data_io_t *p_enc_in_data, int input_video_width, int input_video_height)
Definition: ni_encode_utils.c:876
NI_FRAME_AUX_DATA_CRF_FLOAT
@ NI_FRAME_AUX_DATA_CRF_FLOAT
Definition: ni_device_api.h:578
ni_aligned_free
#define ni_aligned_free(p_memptr)
Definition: ni_util.h:399
_ni_xcoder_params::low_delay_mode
int low_delay_mode
Definition: ni_device_api.h:2733
ni_device_session_flush
ni_retcode_t ni_device_session_flush(ni_session_context_t *p_ctx, ni_device_type_t device_type)
Send a flush command to the device If device_type is NI_DEVICE_TYPE_DECODER sends EOS command to deco...
Definition: ni_device_api.c:1512
_ni_session_context::pkt_num
uint64_t pkt_num
Definition: ni_device_api.h:1532
NI_CODEC_FORMAT_H264
@ NI_CODEC_FORMAT_H264
Definition: ni_device_api.h:911
_ni_frame::use_cur_src_as_long_term_pic
uint8_t use_cur_src_as_long_term_pic
Definition: ni_device_api.h:2621
NI_FRAME_AUX_DATA_INVALID_REF_FRAME
@ NI_FRAME_AUX_DATA_INVALID_REF_FRAME
Definition: ni_device_api.h:558
_ni_frame::video_width
uint32_t video_width
Definition: ni_device_api.h:2610
ni_should_send_sei_with_frame
int ni_should_send_sei_with_frame(ni_session_context_t *p_enc_ctx, ni_pic_type_t pic_type, ni_xcoder_params_t *p_param)
Whether SEI (HDR) should be sent together with this frame to encoder.
Definition: ni_av_codec.c:186
NI_FRAME_LITTLE_ENDIAN
#define NI_FRAME_LITTLE_ENDIAN
Definition: ni_device_api.h:108
_ni_xcoder_params::bitrate
int bitrate
Definition: ni_device_api.h:2729
enc_send_param::output_height
int output_height
Definition: ni_encode_utils.h:42
NI_MIN_WIDTH
#define NI_MIN_WIDTH
Definition: ni_device_api.h:124
_ni_session_context::pixel_format
int pixel_format
Definition: ni_device_api.h:1616
_ni_demo_context::enc_total_bytes_received
uint64_t enc_total_bytes_received[MAX_OUTPUT_FILES]
Definition: ni_generic_utils.h:127
_ni_aux_data
Definition: ni_device_api.h:618
NI_LOG_DEBUG
@ NI_LOG_DEBUG
Definition: ni_log.h:62
_ni_framerate
Definition: ni_device_api.h:695
_ni_vui_hrd::aspectRatioWidth
int32_t aspectRatioWidth
Definition: ni_device_api.h:672
_ni_encoder_change_params_t::aspectRatioWidth
uint16_t aspectRatioWidth
Definition: ni_device_api.h:1023
NI_FRAME_AUX_DATA_INTRAPRD
@ NI_FRAME_AUX_DATA_INTRAPRD
Definition: ni_device_api.h:539
_ni_demo_context::curr_enc_index
uint8_t curr_enc_index
Definition: ni_generic_utils.h:123
_ni_session_context::ori_bit_depth_factor
int ori_bit_depth_factor
Definition: ni_device_api.h:1562
_ni_session_context::ori_height
int ori_height
Definition: ni_device_api.h:1562
_ni_xcoder_params::color_transfer_characteristic
int color_transfer_characteristic
Definition: ni_device_api.h:2757
ni_device_session_close
ni_retcode_t ni_device_session_close(ni_session_context_t *p_ctx, int eos_recieved, ni_device_type_t device_type)
Close device session that was previously opened by calling ni_device_session_open() If device_type is...
Definition: ni_device_api.c:1379
encoder_send_data3
int encoder_send_data3(ni_demo_context_t *p_ctx, ni_session_context_t *p_enc_ctx, ni_session_data_io_t *p_in_data, int input_video_width, int input_video_height, int eos)
Send encoder input data, read from uploader instance hwframe.
Definition: ni_encode_utils.c:1179
_ni_enc_quad_roi_custom_map::qp_info
uint8_t qp_info
Definition: ni_device_api.h:889
_ni_rc_min_max_qp::maxDeltaQp
int32_t maxDeltaQp
Definition: ni_device_api.h:708
ni_set_ltr
ni_retcode_t ni_set_ltr(ni_session_context_t *p_ctx, ni_long_term_ref_t *ltr)
Set a frame's support of Long Term Reference frame during encoding.
Definition: ni_device_api.c:10026
ni_decoder_frame_buffer_free
ni_retcode_t ni_decoder_frame_buffer_free(ni_frame_t *p_frame)
Free decoder frame buffer that was previously allocated with ni_decoder_frame_buffer_alloc,...
Definition: ni_device_api.c:3644
_ni_session_context::roi_avg_qp
uint32_t roi_avg_qp
Definition: ni_device_api.h:1497
ni_reconfig_slice_arg
ni_retcode_t ni_reconfig_slice_arg(ni_session_context_t *p_ctx, int16_t sliceArg)
Reconfigure sliceArg dynamically during encoding.
Definition: ni_device_api.c:10541
_ni_session_context::async_mode
int async_mode
Definition: ni_device_api.h:1663
NI_FRAME_AUX_DATA_BITRATE
@ NI_FRAME_AUX_DATA_BITRATE
Definition: ni_device_api.h:535
_ni_encoder_change_params_t::videoFullRange
uint8_t videoFullRange
Definition: ni_device_api.h:1025
enc_recv_param
Definition: ni_encode_utils.h:47
NI_CODEC_FORMAT_H265
@ NI_CODEC_FORMAT_H265
Definition: ni_device_api.h:912
_ni_frame::use_long_term_ref
uint8_t use_long_term_ref
Definition: ni_device_api.h:2622
ni_frame_wipe_aux_data
void ni_frame_wipe_aux_data(ni_frame_t *frame)
Free and remove all auxiliary data from the frame.
Definition: ni_device_api.c:4115
_ni_packet::data_len
uint32_t data_len
Definition: ni_device_api.h:2838