libxcoder  5.2.0
ni_decode_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 decode_utils.c
24  *
25  * \brief Video decoding utility functions shared by Libxcoder API examples
26  ******************************************************************************/
27 
28 #include "ni_generic_utils.h"
29 #include "ni_decode_utils.h"
30 #include "ni_log.h"
31 #include "ni_util.h"
32 #include "ni_defs.h"
33 
34 static const uint8_t default_scaling_list_inter[] = {
35  16, 16, 16, 16, 17, 18, 20, 24, 16, 16, 16, 17, 18, 20, 24, 25,
36  16, 16, 17, 18, 20, 24, 25, 28, 16, 17, 18, 20, 24, 25, 28, 33,
37  17, 18, 20, 24, 25, 28, 33, 41, 18, 20, 24, 25, 28, 33, 41, 54,
38  20, 24, 25, 28, 33, 41, 54, 71, 24, 25, 28, 33, 41, 54, 71, 91};
39 
40 static const uint8_t default_scaling_list_intra[] = {
41  16, 16, 16, 16, 17, 18, 21, 24, 16, 16, 16, 16, 17, 19, 22, 25,
42  16, 16, 17, 18, 20, 22, 25, 29, 16, 16, 18, 21, 24, 27, 31, 36,
43  17, 17, 20, 24, 30, 35, 41, 47, 18, 19, 22, 27, 35, 44, 54, 65,
44  21, 22, 25, 31, 41, 54, 70, 88, 24, 25, 29, 36, 47, 65, 88, 115};
45 
46 static const uint8_t default_scaling4[2][16] = {
47  {6, 13, 20, 28, 13, 20, 28, 32, 20, 28, 32, 37, 28, 32, 37, 42},
48  {10, 14, 20, 24, 14, 20, 24, 27, 20, 24, 27, 30, 24, 27, 30, 34}};
49 
50 static const uint8_t default_scaling8[2][64] = {
51  {6, 10, 13, 16, 18, 23, 25, 27, 10, 11, 16, 18, 23, 25, 27, 29,
52  13, 16, 18, 23, 25, 27, 29, 31, 16, 18, 23, 25, 27, 29, 31, 33,
53  18, 23, 25, 27, 29, 31, 33, 36, 23, 25, 27, 29, 31, 33, 36, 38,
54  25, 27, 29, 31, 33, 36, 38, 40, 27, 29, 31, 33, 36, 38, 40, 42},
55  {9, 13, 15, 17, 19, 21, 22, 24, 13, 13, 17, 19, 21, 22, 24, 25,
56  15, 17, 19, 21, 22, 24, 25, 27, 17, 19, 21, 22, 24, 25, 27, 28,
57  19, 21, 22, 24, 25, 27, 28, 30, 21, 22, 24, 25, 27, 28, 30, 32,
58  22, 24, 25, 27, 28, 30, 32, 33, 24, 25, 27, 28, 30, 32, 33, 35}};
59 
60 static const uint8_t ni_zigzag_direct[64] = {
61  0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5,
62  12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28,
63  35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51,
64  58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63};
65 
66 static const uint8_t ni_zigzag_scan[16 + 1] = {
67  0 + 0 * 4, 1 + 0 * 4, 0 + 1 * 4, 0 + 2 * 4, 1 + 1 * 4, 2 + 0 * 4,
68  3 + 0 * 4, 2 + 1 * 4, 1 + 2 * 4, 0 + 3 * 4, 1 + 3 * 4, 2 + 2 * 4,
69  3 + 1 * 4, 3 + 2 * 4, 2 + 3 * 4, 3 + 3 * 4,
70 };
71 
72 static const ni_rational_t vui_sar[] = {
73  {0, 1}, {1, 1}, {12, 11}, {10, 11}, {16, 11}, {40, 33},
74  {24, 11}, {20, 11}, {32, 11}, {80, 33}, {18, 11}, {15, 11},
75  {64, 33}, {160, 99}, {4, 3}, {3, 2}, {2, 1},
76 };
77 
78 static const uint8_t hevc_sub_width_c[] = {1, 2, 2, 1};
79 
80 static const uint8_t hevc_sub_height_c[] = {1, 2, 1, 1};
81 
82 static const uint8_t hevc_diag_scan4x4_x[16] = {
83  0, 0, 1, 0, 1, 2, 0, 1, 2, 3, 1, 2, 3, 2, 3, 3,
84 };
85 
86 static const uint8_t hevc_diag_scan4x4_y[16] = {
87  0, 1, 0, 2, 1, 0, 3, 2, 1, 0, 3, 2, 1, 3, 2, 3,
88 };
89 
90 static const uint8_t hevc_diag_scan8x8_x[64] = {
91  0, 0, 1, 0, 1, 2, 0, 1, 2, 3, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 5, 0,
92  1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7, 2,
93  3, 4, 5, 6, 7, 3, 4, 5, 6, 7, 4, 5, 6, 7, 5, 6, 7, 6, 7, 7,
94 };
95 
96 static const uint8_t hevc_diag_scan8x8_y[64] = {
97  0, 1, 0, 2, 1, 0, 3, 2, 1, 0, 4, 3, 2, 1, 0, 5, 4, 3, 2, 1, 0, 6,
98  5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 7,
99  6, 5, 4, 3, 2, 7, 6, 5, 4, 3, 7, 6, 5, 4, 7, 6, 5, 7, 6, 7,
100 };
101 
102 // find/copy next H.264 NAL unit (including start code) and its type;
103 // return NAL data size if found, 0 otherwise
104 uint64_t find_h264_next_nalu(ni_demo_context_t *p_ctx, uint8_t *p_dst, int *nal_type)
105 {
106  uint64_t data_size;
107  uint64_t i = p_ctx->curr_file_offset;
108 
109  if (i + 3 >= p_ctx->total_file_size)
110  {
111  ni_log(NI_LOG_DEBUG, "%s reaching end, curr_pos %llu, total input size %llu\n",
112  __func__, (unsigned long long)p_ctx->curr_file_offset, (unsigned long long)p_ctx->total_file_size);
113 
114  if (p_ctx->loops_left > 1)
115  {
116  p_ctx->loops_left--;
117  ni_log(NI_LOG_DEBUG, "input processed, %d loops left\n", p_ctx->loops_left);
118  reset_data_buf_pos(p_ctx);
119  i = p_ctx->curr_file_offset;
120  } else {
121  return 0;
122  }
123  }
124 
125  // search for start code 0x000001 or 0x00000001
126  while ((p_ctx->file_cache[i] != 0x00 || p_ctx->file_cache[i + 1] != 0x00 ||
127  p_ctx->file_cache[i + 2] != 0x01) &&
128  (p_ctx->file_cache[i] != 0x00 || p_ctx->file_cache[i + 1] != 0x00 ||
129  p_ctx->file_cache[i + 2] != 0x00 || p_ctx->file_cache[i + 3] != 0x01))
130  {
131  i++;
132  if (i + 3 > p_ctx->total_file_size)
133  {
134  return 0;
135  }
136  }
137 
138  // found start code, advance to NAL unit start depends on actual start code
139  if (p_ctx->file_cache[i] != 0x00 || p_ctx->file_cache[i + 1] != 0x00 ||
140  p_ctx->file_cache[i + 2] != 0x01)
141  {
142  i++;
143  }
144 
145  i += 3;
146 
147  // get the NAL type
148  *nal_type = (p_ctx->file_cache[i] & 0x1f);
149 
150  // advance to the end of NAL, or stream
151  while ((p_ctx->file_cache[i] != 0x00 || p_ctx->file_cache[i + 1] != 0x00 ||
152  p_ctx->file_cache[i + 2] != 0x00) &&
153  (p_ctx->file_cache[i] != 0x00 || p_ctx->file_cache[i + 1] != 0x00 ||
154  p_ctx->file_cache[i + 2] != 0x01))
155  {
156  i++;
157  // if reaching the stream end
158  if (i + 3 > p_ctx->total_file_size)
159  {
160  data_size = p_ctx->total_file_size - p_ctx->curr_file_offset;
161  memcpy(p_dst, &p_ctx->file_cache[p_ctx->curr_file_offset], data_size);
162  p_ctx->curr_file_offset = p_ctx->total_file_size;
163  return data_size;
164  }
165  }
166 
167  data_size = i - p_ctx->curr_file_offset;
168  memcpy(p_dst, &p_ctx->file_cache[p_ctx->curr_file_offset], data_size);
169  p_ctx->curr_file_offset = i;
170  return data_size;
171 }
172 
173 // HRD parsing: return 0 if parsing ok, -1 otherwise
175 {
176  int cpb_count, i;
177 
178  cpb_count = (int)ni_bs_reader_get_ue(br) + 1;
179  if (cpb_count > 32U)
180  {
181  ni_log(NI_LOG_ERROR, "parse_hrd invalid cpb_count %d\n", cpb_count);
182  return -1;
183  }
184 
185  ni_bs_reader_get_bits(br, 4); // bit_rate_scale
186  ni_bs_reader_get_bits(br, 4); // cpb_size_scale
187  for (i = 0; i < cpb_count; i++)
188  {
189  ni_bs_reader_get_ue(br); // bit_rate_value_minus1
190  ni_bs_reader_get_ue(br); // cpb_size_value_minus1
191  ni_bs_reader_get_bits(br, 1); // cbr_flag
192  }
194  (int)ni_bs_reader_get_bits(br, 5) + 1;
195  sps->cpb_removal_delay_length = (int)ni_bs_reader_get_bits(br, 5) + 1;
196  sps->dpb_output_delay_length = (int)ni_bs_reader_get_bits(br, 5) + 1;
198  sps->cpb_cnt = cpb_count;
199  return 0;
200 }
201 
202 // VUI parsing: return 0 if parsing ok, -1 otherwise
204 {
205  int ret = -1, aspect_ratio_info_present_flag;
206  unsigned int aspect_ratio_idc;
207 
208  aspect_ratio_info_present_flag = ni_bs_reader_get_bits(br, 1);
209  if (aspect_ratio_info_present_flag)
210  {
211  aspect_ratio_idc = ni_bs_reader_get_bits(br, 8);
212  if (EXTENDED_SAR == aspect_ratio_idc)
213  {
214  sps->sar.num = ni_bs_reader_get_bits(br, 16);
215  sps->sar.den = ni_bs_reader_get_bits(br, 16);
216  } else if (aspect_ratio_idc < NI_NUM_PIXEL_ASPECT_RATIO)
217  {
218  sps->sar = ni_h264_pixel_aspect_list[aspect_ratio_idc];
219  } else
220  {
221  ni_log(NI_LOG_ERROR, "parse_vui: illegal aspect ratio %u\n",
222  aspect_ratio_idc);
223  goto end;
224  }
225  } else
226  {
227  sps->sar.num = sps->sar.den = 0;
228  }
229 
230  if (ni_bs_reader_get_bits(br, 1)) // overscan_info_present_flag
231  {
232  ni_bs_reader_get_bits(br, 1); // overscan_appropriate_flag
233  }
236  {
237  ni_bs_reader_get_bits(br, 3); // video_format
238  sps->full_range =
239  ni_bs_reader_get_bits(br, 1); // video_full_range_flag
240 
243  {
245  sps->color_trc = ni_bs_reader_get_bits(br, 8);
246  sps->colorspace = ni_bs_reader_get_bits(br, 8);
249  {
251  }
252  if (sps->color_trc < NI_COL_TRC_RESERVED0 ||
253  sps->color_trc >= NI_COL_TRC_NB)
254  {
256  }
257  if (sps->colorspace < NI_COL_SPC_RGB ||
258  sps->colorspace >= NI_COL_SPC_NB)
259  {
261  }
262  }
263  }
264 
265  if (ni_bs_reader_get_bits(br, 1)) // chroma_location_info_present_flag
266  {
267  ni_bs_reader_get_ue(br); // chroma_sample_location_type_top_field
268  ni_bs_reader_get_ue(br); // chroma_sample_location_type_bottom_field
269  }
270 
272  if (sps->timing_info_present_flag)
273  {
274  unsigned num_units_in_tick = ni_bs_reader_get_bits(br, 32);
275  unsigned time_scale = ni_bs_reader_get_bits(br, 32);
276  if (!num_units_in_tick || !time_scale)
277  {
278  ni_log(NI_LOG_ERROR, "parse_vui: error num_units_in_tick/time_scale "
279  "(%u/%u)\n",
280  num_units_in_tick, time_scale);
281  sps->timing_info_present_flag = 0;
282  }
284  }
285 
287  if (sps->nal_hrd_parameters_present_flag && h264_parse_hrd(br, sps) < 0)
288  {
289  ni_log(NI_LOG_ERROR, "parse_vui: nal_hrd_parameters_present and error "
290  "parse_hrd !\n");
291  goto end;
292  }
293 
295  if (sps->vcl_hrd_parameters_present_flag && h264_parse_hrd(br, sps) < 0)
296  {
297  ni_log(NI_LOG_ERROR, "parse_vui: vcl_hrd_parameters_present and error "
298  "parse_hrd !\n");
299  goto end;
300  }
301 
304  {
305  ni_bs_reader_get_bits(br, 1); // low_delay_hrd_flag
306  }
307 
309 
312  {
314  1); // motion_vectors_over_pic_boundaries_flag
315  ni_bs_reader_get_ue(br); // max_bytes_per_pic_denom
316  ni_bs_reader_get_ue(br); // max_bits_per_mb_denom
317  ni_bs_reader_get_ue(br); // log2_max_mv_length_horizontal
318  ni_bs_reader_get_ue(br); // log2_max_mv_length_vertical
321 
322  if (sps->num_reorder_frames > 16U)
323  {
324  ni_log(NI_LOG_ERROR, "parse_vui: clip illegal num_reorder_frames %d !\n",
325  sps->num_reorder_frames);
326  sps->num_reorder_frames = 16;
327  goto end;
328  }
329  }
330 
331  // everything is fine
332  ret = 0;
333 
334 end:
335  return ret;
336 }
337 
338 int h264_parse_scaling_list(ni_bitstream_reader_t *br, uint8_t *factors, int size,
339  const uint8_t *jvt_list, const uint8_t *fallback_list)
340 {
341  int i, last = 8, next = 8;
342  const uint8_t *scan = (size == 16 ? ni_zigzag_scan : ni_zigzag_direct);
343 
344  // matrix not written, we use the predicted one */
345  if (!ni_bs_reader_get_bits(br, 1))
346  {
347  memcpy(factors, fallback_list, size * sizeof(uint8_t));
348  } else
349  {
350  for (i = 0; i < size; i++)
351  {
352  if (next)
353  {
354  int v = ni_bs_reader_get_se(br);
355  if (v < -128 || v > 127)
356  {
357  ni_log(NI_LOG_ERROR, "delta scale %d is invalid\n", v);
358  return -1;
359  }
360  next = (last + v) & 0xff;
361  }
362  if (!i && !next)
363  { // matrix not written, we use the preset one
364  memcpy(factors, jvt_list, size * sizeof(uint8_t));
365  break;
366  }
367  last = (factors[scan[i]] = next ? next : last);
368  }
369  }
370  return 0;
371 }
372 
373 // SPS seq scaling matrices parsing: return 0 if parsing ok, -1 otherwise
375  uint8_t (*scaling_matrix4)[16], uint8_t (*scaling_matrix8)[64])
376 {
377  int ret = 0;
378  const uint8_t *fallback[4] = {default_scaling4[0], default_scaling4[1],
379  default_scaling8[0], default_scaling8[1]};
380 
381  if (ni_bs_reader_get_bits(br, 1)) // scaling_matrix_present
382  {
383  // retrieve matrices
384  ret |= h264_parse_scaling_list(br, scaling_matrix4[0], 16,
385  default_scaling4[0], fallback[0]); // Intra, Y
386  ret |= h264_parse_scaling_list(br, scaling_matrix4[1], 16,
387  default_scaling4[0], scaling_matrix4[0]); // Intra, Cr
388  ret |= h264_parse_scaling_list(br, scaling_matrix4[2], 16,
389  default_scaling4[0], scaling_matrix4[1]); // Intra, Cb
390  ret |= h264_parse_scaling_list(br, scaling_matrix4[3], 16,
391  default_scaling4[1], fallback[1]); // Inter, Y
392  ret |= h264_parse_scaling_list(br, scaling_matrix4[4], 16,
393  default_scaling4[1], scaling_matrix4[3]); // Inter, Cr
394  ret |= h264_parse_scaling_list(br, scaling_matrix4[5], 16,
395  default_scaling4[1], scaling_matrix4[4]); // Inter, Cb
396  ret |= h264_parse_scaling_list(br, scaling_matrix8[0], 64,
397  default_scaling8[0], fallback[2]); // Intra, Y
398  ret |= h264_parse_scaling_list(br, scaling_matrix8[3], 64,
399  default_scaling8[1], fallback[3]); // Inter, Y
400  if (sps->chroma_format_idc == 3)
401  {
402  ret |= h264_parse_scaling_list(br, scaling_matrix8[1], 64,
403  default_scaling8[0], scaling_matrix8[0]); // Intra, Cr
404  ret |= h264_parse_scaling_list(br, scaling_matrix8[4], 64,
405  default_scaling8[1], scaling_matrix8[3]); // Inter, Cr
406  ret |= h264_parse_scaling_list(br, scaling_matrix8[2], 64,
407  default_scaling8[0], scaling_matrix8[1]); // Intra, Cb
408  ret |= h264_parse_scaling_list(br, scaling_matrix8[5], 64,
409  default_scaling8[1], scaling_matrix8[4]); // Inter, Cb
410  }
411  if (!ret)
412  {
413  ret = 1;
414  }
415  }
416  return ret;
417 }
418 
419 // SPS parsing: return 0 if parsing ok, -1 otherwise
420 int h264_parse_sps(uint8_t *buf, int size_bytes, ni_h264_sps_t *sps)
421 {
422  int ret = -1;
424  int profile_idc, level_idc, constraint_set_flags = 0;
425  uint32_t sps_id;
426  int i, log2_max_frame_num_minus4;
427 
428  ni_bitstream_reader_init(&br, buf, 8 * size_bytes);
429  // skip NAL header
430  ni_bs_reader_skip_bits(&br, 8);
431 
432  profile_idc = ni_bs_reader_get_bits(&br, 8);
433  // from constraint_set0_flag to constraint_set5_flag
434  constraint_set_flags |= ni_bs_reader_get_bits(&br, 1) << 0;
435  constraint_set_flags |= ni_bs_reader_get_bits(&br, 1) << 1;
436  constraint_set_flags |= ni_bs_reader_get_bits(&br, 1) << 2;
437  constraint_set_flags |= ni_bs_reader_get_bits(&br, 1) << 3;
438  constraint_set_flags |= ni_bs_reader_get_bits(&br, 1) << 4;
439  constraint_set_flags |= ni_bs_reader_get_bits(&br, 1) << 5;
440  ni_bs_reader_skip_bits(&br, 2); // reserved_zero_2bits
441  level_idc = ni_bs_reader_get_bits(&br, 8);
442  sps_id = ni_bs_reader_get_ue(&br);
443 
444  sps->sps_id = sps_id;
445  sps->profile_idc = profile_idc;
446  sps->constraint_set_flags = constraint_set_flags;
447  sps->level_idc = level_idc;
448  sps->full_range = -1;
449 
450  memset(sps->scaling_matrix4, 16, sizeof(sps->scaling_matrix4));
451  memset(sps->scaling_matrix8, 16, sizeof(sps->scaling_matrix8));
452  sps->scaling_matrix_present = 0;
453  sps->colorspace = 2; // NI_COL_SPC_UNSPECIFIED
454 
455  if (100 == profile_idc || 110 == profile_idc || 122 == profile_idc ||
456  244 == profile_idc || 44 == profile_idc || 83 == profile_idc ||
457  86 == profile_idc || 118 == profile_idc || 128 == profile_idc ||
458  138 == profile_idc || 139 == profile_idc || 134 == profile_idc ||
459  135 == profile_idc || 144 == profile_idc /* old High444 profile */)
460  {
462  if (sps->chroma_format_idc > 3U)
463  {
464  ni_log(NI_LOG_ERROR, "parse_sps error: chroma_format_idc > 3 !\n");
465  goto end;
466  } else if (3 == sps->chroma_format_idc)
467  {
470  {
471  ni_log(NI_LOG_ERROR, "parse_sps error: residual_color_transform not "
472  "supported !\n");
473  goto end;
474  }
475  }
476  sps->bit_depth_luma = (int)ni_bs_reader_get_ue(&br) + 8;
477  sps->bit_depth_chroma = (int)ni_bs_reader_get_ue(&br) + 8;
478  if (sps->bit_depth_luma != sps->bit_depth_chroma)
479  {
480  ni_log(NI_LOG_ERROR, "parse_sps error: different luma %d & chroma %d "
481  "bit depth !\n",
482  sps->bit_depth_luma, sps->bit_depth_chroma);
483  goto end;
484  }
485  if (sps->bit_depth_luma < 8 || sps->bit_depth_luma > 12 ||
486  sps->bit_depth_chroma < 8 || sps->bit_depth_chroma > 12)
487  {
488  ni_log(NI_LOG_ERROR, "parse_sps error: illegal luma/chroma bit depth "
489  "value (%d %d) !\n",
490  sps->bit_depth_luma, sps->bit_depth_chroma);
491  goto end;
492  }
493 
495  ret = h264_parse_scaling_matrices(&br, sps, sps->scaling_matrix4, sps->scaling_matrix8);
496  if (ret < 0)
497  {
498  ni_log(NI_LOG_ERROR, "parse_sps error scaling matrices parse failed !\n");
499  goto end;
500  }
501  sps->scaling_matrix_present |= ret;
502  } // profile_idc
503  else
504  {
505  sps->chroma_format_idc = 1;
506  sps->bit_depth_luma = 8;
507  sps->bit_depth_chroma = 8;
508  }
509 
510  log2_max_frame_num_minus4 = ni_bs_reader_get_ue(&br);
511  if (log2_max_frame_num_minus4 < MIN_LOG2_MAX_FRAME_NUM - 4 ||
512  log2_max_frame_num_minus4 > MAX_LOG2_MAX_FRAME_NUM - 4)
513  {
514  ni_log(NI_LOG_ERROR, "parse_sps error: log2_max_frame_num_minus4 %d out of "
515  "range (0-12)!\n",
516  log2_max_frame_num_minus4);
517  goto end;
518  }
519  sps->log2_max_frame_num = log2_max_frame_num_minus4 + 4;
520 
521  sps->poc_type = ni_bs_reader_get_ue(&br);
522  if (0 == sps->poc_type)
523  {
524  uint32_t v = ni_bs_reader_get_ue(&br);
525  if (v > 12)
526  {
527  ni_log(NI_LOG_ERROR, "parse_sps error: log2_max_poc_lsb %u out of range! "
528  "\n",
529  v);
530  goto end;
531  }
532  sps->log2_max_poc_lsb = (int)v + 4;
533  } else if (1 == sps->poc_type)
534  {
539  if ((unsigned)sps->poc_cycle_length >= 256)
540  {
541  ni_log(NI_LOG_ERROR, "parse_sps error: poc_cycle_length %d out of range! "
542  "\n",
543  sps->poc_cycle_length);
544  goto end;
545  }
546  for (i = 0; i < sps->poc_cycle_length; i++)
547  {
549  }
550  } else if (2 != sps->poc_type)
551  {
552  ni_log(NI_LOG_ERROR, "parse_sps error: illegal PIC type %d!\n",
553  sps->poc_type);
554  goto end;
555  }
558  sps->mb_width = (int)ni_bs_reader_get_ue(&br) + 1;
559  sps->mb_height = (int)ni_bs_reader_get_ue(&br) + 1;
560 
562  sps->mb_height *= 2 - sps->frame_mbs_only_flag;
563 
564  if (!sps->frame_mbs_only_flag)
565  {
566  sps->mb_aff = ni_bs_reader_get_bits(&br, 1);
567  } else
568  {
569  sps->mb_aff = 0;
570  }
571 
573 
574  sps->crop = ni_bs_reader_get_bits(&br, 1);
575  if (sps->crop)
576  {
577  unsigned int crop_left = ni_bs_reader_get_ue(&br);
578  unsigned int crop_right = ni_bs_reader_get_ue(&br);
579  unsigned int crop_top = ni_bs_reader_get_ue(&br);
580  unsigned int crop_bottom = ni_bs_reader_get_ue(&br);
581 
582  // no range checking
583  int vsub = (sps->chroma_format_idc == 1) ? 1 : 0;
584  int hsub =
585  (sps->chroma_format_idc == 1 || sps->chroma_format_idc == 2) ? 1 :
586  0;
587  int step_x = 1 << hsub;
588  int step_y = (2 - sps->frame_mbs_only_flag) << vsub;
589 
590  sps->crop_left = crop_left * step_x;
591  sps->crop_right = crop_right * step_x;
592  sps->crop_top = crop_top * step_y;
593  sps->crop_bottom = crop_bottom * step_y;
594  } else
595  {
596  sps->crop_left = sps->crop_right = sps->crop_top = sps->crop_bottom =
597  sps->crop = 0;
598  }
599 
600  // deduce real width/heigh
601  sps->width = (int)(16 * sps->mb_width - sps->crop_left - sps->crop_right);
602  sps->height = (int)(16 * sps->mb_height - sps->crop_top - sps->crop_bottom);
603 
606  {
607  int ret1 = h264_parse_vui(&br, sps);
608  if (ret1 < 0)
609  {
610  ni_log(NI_LOG_ERROR, "parse_sps error: parse_vui failed %d!\n", ret);
611  goto end;
612  }
613  }
614 
615  // everything is fine
616  ret = 0;
617 
618 end:
619 
620  return ret;
621 }
622 
623 int h264_parse_sei(uint8_t *buf, int size_bytes, ni_h264_sps_t *sps,
624  int *sei_type, int *is_interlaced)
625 {
627  *is_interlaced = 0;
628  int ret = -1, dummy;
629  int cpb_dpb_delays_present_flag = (sps->nal_hrd_parameters_present_flag ||
631  //pic_struct_present_flag
632 
633  ni_bitstream_reader_init(&br, buf, 8 * size_bytes);
634  // skip NAL header
635  ni_bs_reader_skip_bits(&br, 8);
636 
637  while (ni_bs_reader_get_bits_left(&br) > 16)
638  {
639  int next, size = 0;
640  unsigned type = 0, tmp;
641 
642  do
643  {
644  if (ni_bs_reader_get_bits_left(&br) < 8)
645  {
646  ni_log(NI_LOG_ERROR, "parse_sei type parse error !\n");
647  goto end;
648  }
649  tmp = ni_bs_reader_get_bits(&br, 8);
650  type += tmp;
651  } while (tmp == 0xFF);
652 
653  *sei_type = (int)type;
654  do
655  {
656  if (ni_bs_reader_get_bits_left(&br) < 8)
657  {
658  ni_log(NI_LOG_ERROR, "parse_sei type %u size parse error !\n", type);
659  goto end;
660  }
661  tmp = ni_bs_reader_get_bits(&br, 8);
662  size += (int)tmp;
663  } while (tmp == 0xFF);
664 
665  if (size > ni_bs_reader_get_bits_left(&br) / 8)
666  {
667  ni_log(NI_LOG_DEBUG, "parse_sei SEI type %u size %d truncated at %d\n",
668  type, size, ni_bs_reader_get_bits_left(&br));
669  goto end;
670  }
671  next = ni_bs_reader_bits_count(&br) + 8 * size;
672 
673  switch (type)
674  {
676  if (cpb_dpb_delays_present_flag)
677  {
680  }
681  if (sps->pic_struct_present_flag)
682  {
683  dummy = ni_bs_reader_get_bits(&br, 4);
684  if (dummy < NI_H264_SEI_PIC_STRUCT_FRAME ||
686  {
688  "parse_sei pic_timing SEI invalid pic_struct: "
689  "%d\n",
690  dummy);
691  goto end;
692  }
693  if (dummy > NI_H264_SEI_PIC_STRUCT_FRAME)
694  {
695  *is_interlaced = 1;
696  }
697  goto success;
698  }
699  break;
700  default:
701  // skip all other SEI types
702  ;
703  }
705  } // while in SEI
706 
707 success:
708  ret = 0;
709 
710 end:
711  return ret;
712 }
713 
714 // probe h.264 stream info; return 0 if stream can be decoded, -1 otherwise
716 {
717  int ret = -1;
718  uint8_t *buf = NULL;
719  uint8_t *p_buf;
720  uint32_t nal_size, ep3_removed = 0, vcl_nal_count = 0;
721  int nal_type = -1, sei_type = -1;
722  int sps_parsed = 0, is_interlaced = 0;
723 
724  if (NULL == (buf = calloc(1, NI_MAX_TX_SZ)))
725  {
726  ni_log(NI_LOG_ERROR, "Error probe_h264_stream_info: allocate stream buf\n");
727  goto end;
728  }
729 
730  reset_data_buf_pos(p_ctx);
731  // probe at most 100 VCL before stops
732  while ((!sps_parsed || !is_interlaced) && vcl_nal_count < 100 &&
733  (nal_size = find_h264_next_nalu(p_ctx, buf, &nal_type)) > 0)
734  {
735  ni_log(NI_LOG_DEBUG, "nal %d nal_size %d\n", nal_type, nal_size);
736  p_buf = buf;
737 
738  // skip the start code
739  while (!(p_buf[0] == 0x00 && p_buf[1] == 0x00 && p_buf[2] == 0x01) &&
740  nal_size > 3)
741  {
742  p_buf++;
743  nal_size--;
744  }
745  if (nal_size <= 3)
746  {
747  ni_log(NI_LOG_ERROR, "Error probe_h264_stream_info NAL has no header\n");
748  continue;
749  }
750 
751  p_buf += 3;
752  nal_size -= 3;
753 
754  ep3_removed = ni_remove_emulation_prevent_bytes(p_buf, nal_size);
755  nal_size -= ep3_removed;
756 
757  if (H264_NAL_SPS == nal_type && !sps_parsed)
758  {
759  if (vcl_nal_count > 0)
760  {
762  "Warning: %s has %d slice NAL units ahead of SPS!\n",
763  __func__, vcl_nal_count);
764  }
765 
766  if (h264_parse_sps(p_buf, nal_size, sps))
767  {
768  ni_log(NI_LOG_ERROR, "probe_h264_stream_info: parse_sps error\n");
769  break;
770  }
771  sps_parsed = 1;
772  } else if (H264_NAL_SEI == nal_type)
773  {
774  h264_parse_sei(p_buf, nal_size, sps, &sei_type, &is_interlaced);
775  } else if (H264_NAL_SLICE == nal_type || H264_NAL_IDR_SLICE == nal_type)
776  {
777  vcl_nal_count++;
778  }
779 
780  if (sps_parsed &&
781  (sps->pic_struct_present_flag ||
784  NI_H264_SEI_TYPE_PIC_TIMING == sei_type && is_interlaced)
785  {
787  "probe_h264_stream_info interlaced NOT supported!\n");
788  break;
789  }
790  } // while for each NAL unit
791 
792  reset_data_buf_pos(p_ctx);
793 
794  ni_log(NI_LOG_DEBUG, "VCL NAL parsed: %d, SPS parsed: %s, is interlaced: %s\n",
795  vcl_nal_count, sps_parsed ? "Yes" : "No",
796  is_interlaced ? "Yes" : "No");
797  if (sps_parsed && !is_interlaced)
798  {
799  ret = 0;
800  } else
801  {
802  ni_log(NI_LOG_ERROR, "Input is either interlaced, or unable to determine, "
803  "probing failed.\n");
804  }
805 
806  static const char csp[4][5] = {"Gray", "420", "422", "444"};
808  "H.264 stream probed %d VCL NAL units, sps:%u "
809  "profile:%d/%d poc %d ref:%d %dx%d [SAR: %d:%d] %s %s "
810  "%" PRId32 "/%" PRId32 " %d bits max_reord:%d max_dec_buf:"
811  "%d\n",
812  vcl_nal_count, sps->sps_id, sps->profile_idc, sps->level_idc,
813  sps->poc_type, sps->ref_frame_count, sps->width, sps->height,
814  /*sps->crop_left, sps->crop_right, sps->crop_top, sps->crop_bottom,*/
815  sps->sar.num, sps->sar.den,
816  sps->vui_parameters_present_flag ? "VUI" : "no-VUI",
817  csp[sps->chroma_format_idc],
819  sps->timing_info_present_flag ? sps->time_scale : 0,
820  sps->bit_depth_luma,
823 
824 end:
825  free(buf);
826  buf = NULL;
827  return ret;
828 }
829 
830 // parse H.264 slice header to get frame_num; return 0 if success, -1 otherwise
831 int parse_h264_slice_header(uint8_t *buf, int size_bytes, ni_h264_sps_t *sps,
832  int32_t *frame_num, unsigned int *first_mb_in_slice)
833 {
835  uint8_t *p_buf = buf;
836  unsigned int slice_type, pps_id;
837 
838  // skip the start code
839  while (!(p_buf[0] == 0x00 && p_buf[1] == 0x00 && p_buf[2] == 0x01) &&
840  size_bytes > 3)
841  {
842  p_buf++;
843  size_bytes--;
844  }
845  if (size_bytes <= 3)
846  {
847  ni_log(NI_LOG_ERROR, "Error parse_h264_slice_header slice has no header\n");
848  return -1;
849  }
850 
851  p_buf += 3;
852  size_bytes -= 3;
853 
854  ni_bitstream_reader_init(&br, p_buf, 8 * size_bytes);
855 
856  // skip NAL header
857  ni_bs_reader_skip_bits(&br, 8);
858 
859  *first_mb_in_slice = ni_bs_reader_get_ue(&br);
860  slice_type = ni_bs_reader_get_ue(&br);
861  if (slice_type > 9)
862  {
863  ni_log(NI_LOG_ERROR, "parse_h264_slice_header error: slice type %u too "
864  "large at %u\n",
865  slice_type, *first_mb_in_slice);
866  return -1;
867  }
868  pps_id = ni_bs_reader_get_ue(&br);
869  *frame_num = ni_bs_reader_get_bits(&br, sps->log2_max_frame_num);
870 
871  ni_log(NI_LOG_DEBUG, "parse_h264_slice_header slice type %u frame_num %d "
872  "pps_id %u size %d first_mb %u\n",
873  slice_type, *frame_num, pps_id, size_bytes,
874  *first_mb_in_slice);
875 
876  return 0;
877 }
878 
883 uint64_t find_h265_next_nalu(ni_demo_context_t *p_ctx, uint8_t *p_dst, int *nal_type)
884 {
885  uint64_t data_size;
886  uint64_t i = p_ctx->curr_file_offset;
887 
888  if (i + 3 >= p_ctx->total_file_size)
889  {
890  ni_log(NI_LOG_DEBUG, "%s reaching end, curr_pos %llu, total input size %llu\n",
891  __func__, (unsigned long long)p_ctx->curr_file_offset, (unsigned long long)p_ctx->total_file_size);
892 
893  if (p_ctx->loops_left > 1)
894  {
895  p_ctx->loops_left--;
896  ni_log(NI_LOG_DEBUG, "input processed, %d loops left\n", p_ctx->loops_left);
897  reset_data_buf_pos(p_ctx);
898  i = p_ctx->curr_file_offset;
899  } else {
900  return 0;
901  }
902  }
903 
904  // search for start code 0x000001 or 0x00000001
905  while ((p_ctx->file_cache[i] != 0x00 || p_ctx->file_cache[i + 1] != 0x00 ||
906  p_ctx->file_cache[i + 2] != 0x01) &&
907  (p_ctx->file_cache[i] != 0x00 || p_ctx->file_cache[i + 1] != 0x00 ||
908  p_ctx->file_cache[i + 2] != 0x00 || p_ctx->file_cache[i + 3] != 0x01))
909  {
910  i++;
911  if (i + 3 > p_ctx->total_file_size)
912  {
913  return 0;
914  }
915  }
916 
917  // found start code, advance to NAL unit start depends on actual start code
918  if (p_ctx->file_cache[i] != 0x00 || p_ctx->file_cache[i + 1] != 0x00 ||
919  p_ctx->file_cache[i + 2] != 0x01)
920  {
921  i++;
922  }
923 
924  i += 3;
925 
926  // get the NAL type
927  *nal_type = (p_ctx->file_cache[i] & 0x7E) >> 1;
928 
929  // advance to the end of NAL, or stream
930  while ((p_ctx->file_cache[i] != 0x00 || p_ctx->file_cache[i + 1] != 0x00 ||
931  p_ctx->file_cache[i + 2] != 0x00) &&
932  (p_ctx->file_cache[i] != 0x00 || p_ctx->file_cache[i + 1] != 0x00 ||
933  p_ctx->file_cache[i + 2] != 0x01))
934  {
935  i++;
936  // if reaching the stream end
937  if (i + 3 > p_ctx->total_file_size)
938  {
939  data_size = p_ctx->total_file_size - p_ctx->curr_file_offset;
940  memcpy(p_dst, &p_ctx->file_cache[p_ctx->curr_file_offset], data_size);
941  p_ctx->curr_file_offset = p_ctx->total_file_size;
942  return data_size;
943  }
944  }
945 
946  data_size = i - p_ctx->curr_file_offset;
947  memcpy(p_dst, &p_ctx->file_cache[p_ctx->curr_file_offset], data_size);
948  p_ctx->curr_file_offset = i;
949  return data_size;
950 }
951 
952 void h265_decode_sublayer_hrd(ni_bitstream_reader_t *br, unsigned int nb_cpb,
953  int subpic_params_present)
954 {
955  uint32_t i;
956 
957  for (i = 0; i < nb_cpb; i++)
958  {
959  ni_bs_reader_get_ue(br); // bit_rate_value_minus1
960  ni_bs_reader_get_ue(br); // cpb_size_value_minus1
961 
962  if (subpic_params_present)
963  {
964  ni_bs_reader_get_ue(br); // cpb_size_du_value_minus1
965  ni_bs_reader_get_ue(br); // bit_rate_du_value_minus1
966  }
967  ni_bs_reader_skip_bits(br, 1); // cbr_flag
968  }
969 }
970 
972 {
973  int i;
974 
975  if (ni_bs_reader_get_bits_left(br) < 2 + 1 + 5 + 32 + 4 + 43 + 1)
976  return -1;
977 
978  ptl->profile_space = ni_bs_reader_get_bits(br, 2);
979  ptl->tier_flag = ni_bs_reader_get_bits(br, 1);
980  ptl->profile_idc = ni_bs_reader_get_bits(br, 5);
981 
982  for (i = 0; i < 32; i++)
983  {
985 
986  if (ptl->profile_idc == 0 && i > 0 &&
988  ptl->profile_idc = i;
989  }
994 
995 #define check_profile_idc(idc) \
996  ptl->profile_idc == (idc) || ptl->profile_compatibility_flag[idc]
997 
1000  check_profile_idc(10))
1001  {
1011 
1012  if (check_profile_idc(5) || check_profile_idc(9) ||
1013  check_profile_idc(10))
1014  {
1016  ni_bs_reader_skip_bits(br, 33); // XXX_reserved_zero_33bits[0..32]
1017  } else
1018  {
1019  ni_bs_reader_skip_bits(br, 34); // XXX_reserved_zero_34bits[0..33]
1020  }
1021  } else if (check_profile_idc(2))
1022  {
1023  ni_bs_reader_skip_bits(br, 7);
1025  ni_bs_reader_skip_bits(br, 35); // XXX_reserved_zero_35bits[0..34]
1026  } else
1027  {
1028  ni_bs_reader_skip_bits(br, 43); // XXX_reserved_zero_43bits[0..42]
1029  }
1030 
1033  ptl->inbld_flag = ni_bs_reader_get_bits(br, 1);
1034  else
1035  ni_bs_reader_skip_bits(br, 1);
1036 #undef check_profile_idc
1037 
1038  return 0;
1039 }
1040 
1041 int h265_parse_ptl(ni_bitstream_reader_t *br, PTL *ptl, int max_num_sub_layers)
1042 {
1043  int i;
1044  if (h265_decode_profile_tier_level(br, &ptl->general_ptl) < 0 ||
1046  8 + (8 * 2 * (max_num_sub_layers - 1 > 0)))
1047  {
1048  ni_log(NI_LOG_ERROR, "PTL information too short\n");
1049  return -1;
1050  }
1051 
1053 
1054  for (i = 0; i < max_num_sub_layers - 1; i++)
1055  {
1058  }
1059 
1060  if (max_num_sub_layers - 1 > 0)
1061  for (i = max_num_sub_layers - 1; i < 8; i++)
1062  ni_bs_reader_skip_bits(br, 2); // reserved_zero_2bits[i]
1063  for (i = 0; i < max_num_sub_layers - 1; i++)
1064  {
1065  if (ptl->sub_layer_profile_present_flag[i] &&
1067  {
1068  ni_log(NI_LOG_ERROR, "PTL information for sublayer %i too short\n",
1069  i);
1070  return -1;
1071  }
1072  if (ptl->sub_layer_level_present_flag[i])
1073  {
1074  if (ni_bs_reader_get_bits_left(br) < 8)
1075  {
1077  "Not enough data for sublayer %i level_idc\n", i);
1078  return -1;
1079  } else
1081  }
1082  }
1083 
1084  return 0;
1085 }
1086 
1087 int h265_decode_hrd(ni_bitstream_reader_t *br, int common_inf_present, int max_sublayers)
1088 {
1089  int nal_params_present = 0, vcl_params_present = 0;
1090  int subpic_params_present = 0;
1091  int i;
1092 
1093  if (common_inf_present)
1094  {
1095  nal_params_present = ni_bs_reader_get_bits(br, 1);
1096  vcl_params_present = ni_bs_reader_get_bits(br, 1);
1097 
1098  if (nal_params_present || vcl_params_present)
1099  {
1100  subpic_params_present = ni_bs_reader_get_bits(br, 1);
1101 
1102  if (subpic_params_present)
1103  {
1104  ni_bs_reader_skip_bits(br, 8); // tick_divisor_minus2
1106  br, 5); // du_cpb_removal_delay_increment_length_minus1
1108  br, 1); // sub_pic_cpb_params_in_pic_timing_sei_flag
1110  br, 5); // dpb_output_delay_du_length_minus1
1111  }
1112 
1113  ni_bs_reader_skip_bits(br, 4); // bit_rate_scale
1114  ni_bs_reader_skip_bits(br, 4); // cpb_size_scale
1115 
1116  if (subpic_params_present)
1117  ni_bs_reader_skip_bits(br, 4); // cpb_size_du_scale
1118 
1120  br, 5); // initial_cpb_removal_delay_length_minus1
1122  5); // au_cpb_removal_delay_length_minus1
1123  ni_bs_reader_skip_bits(br, 5); // dpb_output_delay_length_minus1
1124  }
1125  }
1126 
1127  for (i = 0; i < max_sublayers; i++)
1128  {
1129  int low_delay = 0;
1130  unsigned int nb_cpb = 1;
1131  int fixed_rate = ni_bs_reader_get_bits(br, 1);
1132 
1133  if (!fixed_rate)
1134  fixed_rate = ni_bs_reader_get_bits(br, 1);
1135 
1136  if (fixed_rate)
1137  ni_bs_reader_get_ue(br); // elemental_duration_in_tc_minus1
1138  else
1139  low_delay = ni_bs_reader_get_bits(br, 1);
1140 
1141  if (!low_delay)
1142  {
1143  nb_cpb = ni_bs_reader_get_ue(br) + 1;
1144  if (nb_cpb < 1 || nb_cpb > 32)
1145  {
1146  ni_log(NI_LOG_ERROR, "nb_cpb %d invalid\n", nb_cpb);
1147  return -1;
1148  }
1149  }
1150 
1151  if (nal_params_present)
1152  h265_decode_sublayer_hrd(br, nb_cpb, subpic_params_present);
1153  if (vcl_params_present)
1154  h265_decode_sublayer_hrd(br, nb_cpb, subpic_params_present);
1155  }
1156  return 0;
1157 }
1158 
1160 {
1161  int matrixId;
1162 
1163  for (matrixId = 0; matrixId < 6; matrixId++)
1164  {
1165  // 4x4 default is 16
1166  memset(sl->sl[0][matrixId], 16, 16);
1167  sl->sl_dc[0][matrixId] = 16; // default for 16x16
1168  sl->sl_dc[1][matrixId] = 16; // default for 32x32
1169  }
1170  memcpy(sl->sl[1][0], default_scaling_list_intra, 64);
1171  memcpy(sl->sl[1][1], default_scaling_list_intra, 64);
1172  memcpy(sl->sl[1][2], default_scaling_list_intra, 64);
1173  memcpy(sl->sl[1][3], default_scaling_list_inter, 64);
1174  memcpy(sl->sl[1][4], default_scaling_list_inter, 64);
1175  memcpy(sl->sl[1][5], default_scaling_list_inter, 64);
1176  memcpy(sl->sl[2][0], default_scaling_list_intra, 64);
1177  memcpy(sl->sl[2][1], default_scaling_list_intra, 64);
1178  memcpy(sl->sl[2][2], default_scaling_list_intra, 64);
1179  memcpy(sl->sl[2][3], default_scaling_list_inter, 64);
1180  memcpy(sl->sl[2][4], default_scaling_list_inter, 64);
1181  memcpy(sl->sl[2][5], default_scaling_list_inter, 64);
1182  memcpy(sl->sl[3][0], default_scaling_list_intra, 64);
1183  memcpy(sl->sl[3][1], default_scaling_list_intra, 64);
1184  memcpy(sl->sl[3][2], default_scaling_list_intra, 64);
1185  memcpy(sl->sl[3][3], default_scaling_list_inter, 64);
1186  memcpy(sl->sl[3][4], default_scaling_list_inter, 64);
1187  memcpy(sl->sl[3][5], default_scaling_list_inter, 64);
1188 }
1189 
1191 {
1192  uint8_t scaling_list_pred_mode_flag;
1193  int32_t scaling_list_dc_coef[2][6];
1194  int size_id, matrix_id, pos;
1195  int i;
1196 
1197  for (size_id = 0; size_id < 4; size_id++)
1198  for (matrix_id = 0; matrix_id < 6;
1199  matrix_id += ((size_id == 3) ? 3 : 1))
1200  {
1201  scaling_list_pred_mode_flag = ni_bs_reader_get_bits(br, 1);
1202  if (!scaling_list_pred_mode_flag)
1203  {
1204  int delta = ni_bs_reader_get_ue(br);
1205  /* Only need to handle non-zero delta. Zero means default,
1206  * which should already be in the arrays. */
1207  if (delta)
1208  {
1209  // Copy from previous array.
1210  delta *= (size_id == 3) ? 3 : 1;
1211  if (matrix_id < delta)
1212  {
1214  "Invalid delta in scaling list data: %d.\n",
1215  delta);
1216  return -1;
1217  }
1218 
1219  memcpy(sl->sl[size_id][matrix_id],
1220  sl->sl[size_id][matrix_id - delta],
1221  size_id > 0 ? 64 : 16);
1222  if (size_id > 1)
1223  sl->sl_dc[size_id - 2][matrix_id] =
1224  sl->sl_dc[size_id - 2][matrix_id - delta];
1225  }
1226  } else
1227  {
1228  int32_t next_coef, coef_num;
1229  int32_t scaling_list_delta_coef;
1230 
1231  next_coef = 8;
1232  coef_num = 1 << (4 + (size_id << 1));
1233  if (coef_num >= 64)
1234  coef_num = 64;
1235  if (size_id > 1)
1236  {
1237  scaling_list_dc_coef[size_id - 2][matrix_id] =
1238  ni_bs_reader_get_se(br) + 8;
1239  next_coef = scaling_list_dc_coef[size_id - 2][matrix_id];
1240  sl->sl_dc[size_id - 2][matrix_id] = next_coef;
1241  }
1242  for (i = 0; i < coef_num; i++)
1243  {
1244  if (size_id == 0)
1245  pos =
1246  4 * hevc_diag_scan4x4_y[i] + hevc_diag_scan4x4_x[i];
1247  else
1248  pos =
1249  8 * hevc_diag_scan8x8_y[i] + hevc_diag_scan8x8_x[i];
1250 
1251  scaling_list_delta_coef = ni_bs_reader_get_se(br);
1252  next_coef =
1253  (next_coef + 256U + scaling_list_delta_coef) % 256;
1254  sl->sl[size_id][matrix_id][pos] = next_coef;
1255  }
1256  }
1257  }
1258 
1259  if (sps->chroma_format_idc == 3)
1260  {
1261  for (i = 0; i < 64; i++)
1262  {
1263  sl->sl[3][1][i] = sl->sl[2][1][i];
1264  sl->sl[3][2][i] = sl->sl[2][2][i];
1265  sl->sl[3][4][i] = sl->sl[2][4][i];
1266  sl->sl[3][5][i] = sl->sl[2][5][i];
1267  }
1268  sl->sl_dc[1][1] = sl->sl_dc[0][1];
1269  sl->sl_dc[1][2] = sl->sl_dc[0][2];
1270  sl->sl_dc[1][4] = sl->sl_dc[0][4];
1271  sl->sl_dc[1][5] = sl->sl_dc[0][5];
1272  }
1273 
1274  return 0;
1275 }
1276 
1278  const ni_h265_sps_t *sps, int is_slice_header)
1279 {
1280  uint8_t rps_predict = 0;
1281  int32_t delta_poc;
1282  int k0 = 0;
1283  int k1 = 0;
1284  int32_t k = 0;
1285  int i;
1286 
1287  if (rps != sps->st_rps && sps->nb_st_rps)
1288  rps_predict = ni_bs_reader_get_bits(br, 1);
1289 
1290  if (rps_predict)
1291  {
1292  const ShortTermRPS *rps_ridx;
1293  int32_t delta_rps;
1294  int32_t abs_delta_rps;
1295  uint8_t use_delta_flag = 0;
1296  uint8_t delta_rps_sign;
1297 
1298  if (is_slice_header)
1299  {
1300  unsigned int delta_idx = ni_bs_reader_get_ue(br) + 1;
1301  if (delta_idx > sps->nb_st_rps)
1302  {
1304  "Invalid value of delta_idx in slice header RPS: %d > "
1305  "%d.\n",
1306  delta_idx, sps->nb_st_rps);
1307  return -1;
1308  }
1309  rps_ridx = &sps->st_rps[sps->nb_st_rps - delta_idx];
1310  rps->rps_idx_num_delta_pocs = rps_ridx->num_delta_pocs;
1311  } else
1312  rps_ridx = &sps->st_rps[rps - sps->st_rps - 1];
1313 
1314  delta_rps_sign = ni_bs_reader_get_bits(br, 1);
1315  abs_delta_rps = (int)(ni_bs_reader_get_ue(br) + 1);
1316  if (abs_delta_rps < 1 || abs_delta_rps > 32768)
1317  {
1318  ni_log(NI_LOG_ERROR, "Invalid value of abs_delta_rps: %d\n",
1319  abs_delta_rps);
1320  return -1;
1321  }
1322  delta_rps = (1 - (delta_rps_sign << 1)) * abs_delta_rps;
1323  for (i = 0; i <= rps_ridx->num_delta_pocs; i++)
1324  {
1325  int used = rps->used[k] = ni_bs_reader_get_bits(br, 1);
1326 
1327  if (!used)
1328  use_delta_flag = ni_bs_reader_get_bits(br, 1);
1329 
1330  if (used || use_delta_flag)
1331  {
1332  if (i < rps_ridx->num_delta_pocs)
1333  delta_poc = delta_rps + rps_ridx->delta_poc[i];
1334  else
1335  delta_poc = delta_rps;
1336  rps->delta_poc[k] = delta_poc;
1337  if (delta_poc < 0)
1338  k0++;
1339  else
1340  k1++;
1341  k++;
1342  }
1343  }
1344 
1345  if (k >= (sizeof(rps->used) / sizeof(rps->used[0])))
1346  {
1347  ni_log(NI_LOG_ERROR, "Invalid num_delta_pocs: %d\n", k);
1348  return -1;
1349  }
1350 
1351  rps->num_delta_pocs = k;
1352  rps->num_negative_pics = k0;
1353  // sort in increasing order (smallest first)
1354  if (rps->num_delta_pocs != 0)
1355  {
1356  int used, tmp;
1357  for (i = 1; i < rps->num_delta_pocs; i++)
1358  {
1359  delta_poc = rps->delta_poc[i];
1360  used = rps->used[i];
1361  for (k = i - 1; k >= 0; k--)
1362  {
1363  tmp = rps->delta_poc[k];
1364  if (delta_poc < tmp)
1365  {
1366  rps->delta_poc[k + 1] = tmp;
1367  rps->used[k + 1] = rps->used[k];
1368  rps->delta_poc[k] = delta_poc;
1369  rps->used[k] = used;
1370  }
1371  }
1372  }
1373  }
1374  if ((rps->num_negative_pics >> 1) != 0)
1375  {
1376  int used;
1377  k = rps->num_negative_pics - 1;
1378  // flip the negative values to largest first
1379  for (i = 0; i < (int)(rps->num_negative_pics >> 1); i++)
1380  {
1381  delta_poc = rps->delta_poc[i];
1382  used = rps->used[i];
1383  rps->delta_poc[i] = rps->delta_poc[k];
1384  rps->used[i] = rps->used[k];
1385  rps->delta_poc[k] = delta_poc;
1386  rps->used[k] = used;
1387  k--;
1388  }
1389  }
1390  } else
1391  {
1392  int prev, nb_positive_pics;
1394  nb_positive_pics = ni_bs_reader_get_ue(br);
1395 
1396  if (rps->num_negative_pics >= HEVC_MAX_REFS ||
1397  nb_positive_pics >= HEVC_MAX_REFS)
1398  {
1399  ni_log(NI_LOG_ERROR, "Too many refs in a short term RPS.\n");
1400  return -1;
1401  }
1402 
1403  rps->num_delta_pocs = (int)(rps->num_negative_pics + nb_positive_pics);
1404  if (rps->num_delta_pocs)
1405  {
1406  prev = 0;
1407  for (i = 0; i < (int)rps->num_negative_pics; i++)
1408  {
1409  delta_poc = ni_bs_reader_get_ue(br) + 1;
1410  if (delta_poc < 1 || delta_poc > 32768)
1411  {
1412  ni_log(NI_LOG_ERROR, "Invalid value of delta_poc: %d\n",
1413  delta_poc);
1414  return -1;
1415  }
1416  prev -= delta_poc;
1417  rps->delta_poc[i] = prev;
1418  rps->used[i] = ni_bs_reader_get_bits(br, 1);
1419  }
1420  prev = 0;
1421  for (i = 0; i < nb_positive_pics; i++)
1422  {
1423  delta_poc = ni_bs_reader_get_ue(br) + 1;
1424  if (delta_poc < 1 || delta_poc > 32768)
1425  {
1426  ni_log(NI_LOG_ERROR, "Invalid value of delta_poc: %d\n",
1427  delta_poc);
1428  return -1;
1429  }
1430  prev += delta_poc;
1431  rps->delta_poc[rps->num_negative_pics + i] = prev;
1432  rps->used[rps->num_negative_pics + i] =
1433  ni_bs_reader_get_bits(br, 1);
1434  }
1435  }
1436  }
1437  return 0;
1438 }
1439 
1440 int h265_decode_vui(ni_bitstream_reader_t *br, int apply_defdispwin, ni_h265_sps_t *sps)
1441 {
1442  VUI backup_vui, *vui = &sps->vui;
1443  ni_bitstream_reader_t br_backup;
1444  int sar_present, alt = 0;
1445 
1446  sar_present = ni_bs_reader_get_bits(br, 1);
1447  if (sar_present)
1448  {
1449  uint8_t sar_idx = ni_bs_reader_get_bits(br, 8);
1450  if (sar_idx < (sizeof(vui_sar) / sizeof(vui_sar[0])))
1451  vui->sar = vui_sar[sar_idx];
1452  else if (sar_idx == 255)
1453  {
1454  vui->sar.num = ni_bs_reader_get_bits(br, 16);
1455  vui->sar.den = ni_bs_reader_get_bits(br, 16);
1456  } else
1457  {
1458  ni_log(NI_LOG_ERROR, "Unknown SAR Index: %u.\n", sar_idx);
1459  }
1460  }
1461 
1463  if (vui->overscan_info_present_flag)
1465 
1468  {
1469  vui->video_format = ni_bs_reader_get_bits(br, 3);
1472  if (vui->video_full_range_flag && sps->pix_fmt == NI_PIX_FMT_YUV420P)
1473  sps->pix_fmt = NI_PIX_FMT_YUV420P;
1475  {
1478  vui->matrix_coeffs = ni_bs_reader_get_bits(br, 8);
1479 
1480  if (vui->colour_primaries >= NI_COL_PRI_NB)
1481  {
1483  }
1485  {
1487  }
1488  if (vui->matrix_coeffs >= NI_COL_SPC_NB)
1489  {
1491  }
1492  if (vui->matrix_coeffs == NI_COL_SPC_RGB)
1493  {
1494  if (sps->pix_fmt)
1495  {
1497  "Invalid format, only support yuv420p\n");
1498  return -1;
1499  }
1500  }
1501  }
1502  }
1503 
1506  {
1509  }
1510 
1512  vui->field_seq_flag = ni_bs_reader_get_bits(br, 1);
1514 
1515  // Backup context in case an alternate header is detected
1516  memcpy(&br_backup, br, sizeof(br_backup));
1517  memcpy(&backup_vui, vui, sizeof(backup_vui));
1519 
1520  if (vui->default_display_window_flag)
1521  {
1522  int vert_mult = hevc_sub_height_c[sps->chroma_format_idc];
1523  int horiz_mult = hevc_sub_width_c[sps->chroma_format_idc];
1524  vui->def_disp_win.left_offset = ni_bs_reader_get_ue(br) * horiz_mult;
1525  vui->def_disp_win.right_offset = ni_bs_reader_get_ue(br) * horiz_mult;
1526  vui->def_disp_win.top_offset = ni_bs_reader_get_ue(br) * vert_mult;
1527  vui->def_disp_win.bottom_offset = ni_bs_reader_get_ue(br) * vert_mult;
1528 
1529  if (apply_defdispwin)
1530  {
1532  "discarding vui default display window, "
1533  "original values are l:%u r:%u t:%u b:%u\n",
1537 
1540  0;
1541  }
1542  }
1543 
1544 timing_info:
1546 
1548  {
1549  if (ni_bs_reader_get_bits_left(br) < 66 && !alt)
1550  {
1551  // The alternate syntax seem to have timing info located
1552  // at where def_disp_win is normally located
1554  "Strange VUI timing information, retrying...\n");
1555  memcpy(vui, &backup_vui, sizeof(backup_vui));
1556  memcpy(br, &br_backup, sizeof(br_backup));
1557  alt = 1;
1558  goto timing_info;
1559  }
1561  vui->vui_time_scale = ni_bs_reader_get_bits(br, 32);
1562  if (alt)
1563  {
1564  ni_log(NI_LOG_INFO, "Retry got %u/%ufps\n", vui->vui_time_scale,
1565  vui->vui_num_units_in_tick);
1566  }
1572  h265_decode_hrd(br, 1, sps->max_sub_layers);
1573  }
1574 
1576  if (vui->bitstream_restriction_flag)
1577  {
1578  if (ni_bs_reader_get_bits_left(br) < 8 && !alt)
1579  {
1581  "Strange VUI bitstream restriction information, retrying"
1582  " from timing information...\n");
1583  memcpy(vui, &backup_vui, sizeof(backup_vui));
1584  memcpy(br, &br_backup, sizeof(br_backup));
1585  alt = 1;
1586  goto timing_info;
1587  }
1590  ni_bs_reader_get_bits(br, 1);
1597  }
1598 
1599  if (ni_bs_reader_get_bits_left(br) < 1 && !alt)
1600  {
1602  "Overread in VUI, retrying from timing information...\n");
1603  memcpy(vui, &backup_vui, sizeof(backup_vui));
1604  memcpy(br, &br_backup, sizeof(br_backup));
1605  alt = 1;
1606  goto timing_info;
1607  }
1608  return 0;
1609 }
1610 
1611 int h265_parse_sps(ni_h265_sps_t *sps, uint8_t *buf, int size_bytes)
1612 {
1613  ni_h265_window_t *ow;
1614  int ret = 0;
1615  int log2_diff_max_min_transform_block_size;
1616  int bit_depth_chroma, start, vui_present, sublayer_ordering_info;
1617  int i;
1618 
1620  uint32_t sps_id;
1621  ni_bitstream_reader_init(&br, buf, 8 * size_bytes);
1622 
1623  ni_bs_reader_skip_bits(&br, 16); // skip NAL header
1624 
1625  sps->vps_id = ni_bs_reader_get_bits(&br, 4);
1626 
1627  sps->max_sub_layers = (int)ni_bs_reader_get_bits(&br, 3) + 1;
1629  {
1630  ni_log(NI_LOG_ERROR, "sps_max_sub_layers out of range: %d\n",
1631  sps->max_sub_layers);
1632  return -1;
1633  }
1634 
1636 
1637  if ((ret = h265_parse_ptl(&br, &sps->ptl, sps->max_sub_layers)) < 0)
1638  return ret;
1639 
1640  sps_id = ni_bs_reader_get_ue(&br);
1641  if (sps_id >= HEVC_MAX_SPS_COUNT)
1642  {
1643  ni_log(NI_LOG_ERROR, "SPS id out of range: %d\n", sps_id);
1644  return -1;
1645  }
1646 
1648  if (sps->chroma_format_idc > 3U)
1649  {
1650  ni_log(NI_LOG_ERROR, "chroma_format_idc %d is invalid\n",
1651  sps->chroma_format_idc);
1652  return -1;
1653  }
1654 
1655  if (sps->chroma_format_idc == 3)
1657 
1658  if (sps->separate_colour_plane_flag)
1659  sps->chroma_format_idc = 0;
1660 
1661  sps->width = (int)ni_bs_reader_get_ue(&br);
1662  sps->height = (int)ni_bs_reader_get_ue(&br);
1663 
1664  if (ni_bs_reader_get_bits(&br, 1))
1665  { // pic_conformance_flag
1666  int vert_mult = hevc_sub_height_c[sps->chroma_format_idc];
1667  int horiz_mult = hevc_sub_width_c[sps->chroma_format_idc];
1668  sps->pic_conf_win.left_offset = ni_bs_reader_get_ue(&br) * horiz_mult;
1669  sps->pic_conf_win.right_offset = ni_bs_reader_get_ue(&br) * horiz_mult;
1670  sps->pic_conf_win.top_offset = ni_bs_reader_get_ue(&br) * vert_mult;
1671  sps->pic_conf_win.bottom_offset = ni_bs_reader_get_ue(&br) * vert_mult;
1672 
1673  sps->output_window = sps->pic_conf_win;
1674  }
1675 
1676  sps->bit_depth = (int)(ni_bs_reader_get_ue(&br) + 8);
1677  bit_depth_chroma = (int)(ni_bs_reader_get_ue(&br) + 8);
1678  if (sps->chroma_format_idc && bit_depth_chroma != sps->bit_depth)
1679  {
1681  "Luma bit depth (%d) is different from chroma bit depth (%d), "
1682  "this is unsupported.\n",
1683  sps->bit_depth, bit_depth_chroma);
1684  return -1;
1685  }
1686  sps->bit_depth_chroma = bit_depth_chroma;
1687  if (((sps->bit_depth != 8) && (sps->bit_depth != 10)) ||
1688  (sps->chroma_format_idc != 1))
1689  {
1691  "only support 8bit/10bit yuv420p, bit_depth %d, "
1692  "chroma_format_idc %d\n",
1693  sps->bit_depth, sps->chroma_format_idc);
1694  return -1;
1695  }
1696  sps->pix_fmt = 0;
1697  sps->hshift[0] = sps->vshift[0] = 0;
1698  sps->hshift[2] = sps->hshift[1] = 1;
1699  sps->vshift[2] = sps->vshift[1] = 1;
1700  sps->pixel_shift = sps->bit_depth > 8;
1701 
1702  sps->log2_max_poc_lsb = ni_bs_reader_get_ue(&br) + 4;
1703  if (sps->log2_max_poc_lsb > 16)
1704  {
1706  "log2_max_pic_order_cnt_lsb_minus4 out range: %d\n",
1707  sps->log2_max_poc_lsb - 4);
1708  return -1;
1709  }
1710 
1711  sublayer_ordering_info = ni_bs_reader_get_bits(&br, 1);
1712  start = sublayer_ordering_info ? 0 : sps->max_sub_layers - 1;
1713  for (i = start; i < sps->max_sub_layers; i++)
1714  {
1716  (int)(ni_bs_reader_get_ue(&br) + 1);
1719  (int)(ni_bs_reader_get_ue(&br) - 1);
1720  if (sps->temporal_layer[i].num_reorder_pics >
1722  {
1723  ni_log(NI_LOG_ERROR, "sps_max_num_reorder_pics out of range: %d\n",
1726  sps->temporal_layer[i].num_reorder_pics + 1;
1727  }
1728  }
1729 
1730  if (!sublayer_ordering_info)
1731  {
1732  for (i = 0; i < start; i++)
1733  {
1737  sps->temporal_layer[start].num_reorder_pics;
1740  }
1741  }
1742 
1743  sps->log2_min_cb_size = ni_bs_reader_get_ue(&br) + 3;
1745  sps->log2_min_tb_size = ni_bs_reader_get_ue(&br) + 2;
1746  log2_diff_max_min_transform_block_size = ni_bs_reader_get_ue(&br);
1747  sps->log2_max_trafo_size =
1748  log2_diff_max_min_transform_block_size + sps->log2_min_tb_size;
1749 
1750  if (sps->log2_min_cb_size < 3 || sps->log2_min_cb_size > 30)
1751  {
1752  ni_log(NI_LOG_ERROR, "Invalid value %d for log2_min_cb_size",
1753  sps->log2_min_cb_size);
1754  return -1;
1755  }
1756 
1758  {
1760  "Invalid value %d for log2_diff_max_min_coding_block_size",
1762  return -1;
1763  }
1764 
1765  if (sps->log2_min_tb_size >= sps->log2_min_cb_size ||
1766  sps->log2_min_tb_size < 2)
1767  {
1768  ni_log(NI_LOG_ERROR, "Invalid value for log2_min_tb_size");
1769  return -1;
1770  }
1771 
1772  if (log2_diff_max_min_transform_block_size < 0 ||
1773  log2_diff_max_min_transform_block_size > 30)
1774  {
1776  "Invalid value %d for log2_diff_max_min_transform_block_size",
1777  log2_diff_max_min_transform_block_size);
1778  return -1;
1779  }
1780 
1783 
1785  if (sps->scaling_list_enable_flag)
1786  {
1788 
1789  if (ni_bs_reader_get_bits(&br, 1))
1790  {
1791  ret = h265_scaling_list_data(&br, &sps->scaling_list, sps);
1792  if (ret < 0)
1793  return ret;
1794  }
1795  }
1796 
1797  sps->amp_enabled_flag = ni_bs_reader_get_bits(&br, 1);
1798  sps->sao_enabled = ni_bs_reader_get_bits(&br, 1);
1799 
1800  sps->pcm_enabled_flag = ni_bs_reader_get_bits(&br, 1);
1801  if (sps->pcm_enabled_flag)
1802  {
1803  sps->pcm.bit_depth = ni_bs_reader_get_bits(&br, 4) + 1;
1804  sps->pcm.bit_depth_chroma = ni_bs_reader_get_bits(&br, 4) + 1;
1806  sps->pcm.log2_max_pcm_cb_size =
1808  if ((sps->pcm.bit_depth > sps->bit_depth) ||
1809  (sps->pcm.bit_depth_chroma > sps->bit_depth))
1810  {
1812  "PCM bit depth (%d, %d) is greater than normal bit depth "
1813  "(%d)\n",
1814  sps->pcm.bit_depth, sps->pcm.bit_depth_chroma,
1815  sps->bit_depth);
1816  return -1;
1817  }
1818 
1820  }
1821 
1822  sps->nb_st_rps = ni_bs_reader_get_ue(&br);
1824  {
1825  ni_log(NI_LOG_ERROR, "Too many short term RPS: %d.\n", sps->nb_st_rps);
1826  return -1;
1827  }
1828  for (i = 0; i < (int)sps->nb_st_rps; i++)
1829  {
1830  if ((ret = h265_decode_short_term_rps(&br, &sps->st_rps[i], sps, 0)) <
1831  0)
1832  return ret;
1833  }
1834 
1837  {
1840  {
1841  ni_log(NI_LOG_ERROR, "Too many long term ref pics: %d.\n",
1843  return -1;
1844  }
1845  for (i = 0; i < sps->num_long_term_ref_pics_sps; i++)
1846  {
1847  sps->lt_ref_pic_poc_lsb_sps[i] =
1848  ni_bs_reader_get_bits(&br, (int)sps->log2_max_poc_lsb);
1850  ni_bs_reader_get_bits(&br, 1);
1851  }
1852  }
1853 
1856  sps->vui.sar = (ni_rational_t){0, 1};
1857  vui_present = ni_bs_reader_get_bits(&br, 1);
1858  if (vui_present)
1859  h265_decode_vui(&br, 0, sps);
1860 
1861  if (ni_bs_reader_get_bits(&br, 1))
1862  { // sps_extension_flag
1865  &br, 7); //sps_extension_7bits = ni_bs_reader_get_bits(br, 7);
1866  if (sps->sps_range_extension_flag)
1867  {
1869  ni_bs_reader_get_bits(&br, 1);
1871  ni_bs_reader_get_bits(&br, 1);
1873 
1875 
1877  ni_bs_reader_get_bits(&br, 1);
1879  ni_log(
1880  NI_LOG_INFO,
1881  "extended_precision_processing_flag not yet implemented\n");
1882 
1885  ni_bs_reader_get_bits(&br, 1);
1888  "high_precision_offsets_enabled_flag not yet "
1889  "implemented\n");
1890 
1892  ni_bs_reader_get_bits(&br, 1);
1893 
1895  ni_bs_reader_get_bits(&br, 1);
1898  "cabac_bypass_alignment_enabled_flag not yet "
1899  "implemented\n");
1900  }
1901  }
1902 
1903  ow = &sps->output_window;
1904  if (ow->left_offset >= INT32_MAX - ow->right_offset ||
1905  ow->top_offset >= INT32_MAX - ow->bottom_offset ||
1906  ow->left_offset + ow->right_offset >= (uint32_t)sps->width ||
1907  ow->top_offset + ow->bottom_offset >= (uint32_t)sps->height)
1908  {
1909  ni_log(NI_LOG_INFO, "Invalid cropping offsets: %u/%u/%u/%u\n",
1910  ow->left_offset, ow->right_offset, ow->top_offset,
1911  ow->bottom_offset);
1912  ni_log(NI_LOG_INFO, "Displaying the whole video surface.\n");
1913  memset(ow, 0, sizeof(*ow));
1914  memset(&sps->pic_conf_win, 0, sizeof(sps->pic_conf_win));
1915  }
1916 
1917  // Inferred parameters
1918  sps->log2_ctb_size =
1920  sps->log2_min_pu_size = sps->log2_min_cb_size - 1;
1921 
1923  {
1924  ni_log(NI_LOG_ERROR, "CTB size out of range: 2^%d\n",
1925  sps->log2_ctb_size);
1926  return -1;
1927  }
1928  if (sps->log2_ctb_size < 4)
1929  {
1930  ni_log(
1931  NI_LOG_ERROR,
1932  "log2_ctb_size %d differs from the bounds of any known profile\n",
1933  sps->log2_ctb_size);
1934  return -1;
1935  }
1936 
1937  sps->ctb_width =
1938  (sps->width + (1 << sps->log2_ctb_size) - 1) >> sps->log2_ctb_size;
1939  sps->ctb_height =
1940  (sps->height + (1 << sps->log2_ctb_size) - 1) >> sps->log2_ctb_size;
1941  sps->ctb_size = sps->ctb_width * sps->ctb_height;
1942 
1943  sps->min_cb_width = sps->width >> sps->log2_min_cb_size;
1944  sps->min_cb_height = sps->height >> sps->log2_min_cb_size;
1945  sps->min_tb_width = sps->width >> sps->log2_min_tb_size;
1946  sps->min_tb_height = sps->height >> sps->log2_min_tb_size;
1947  sps->min_pu_width = sps->width >> sps->log2_min_pu_size;
1948  sps->min_pu_height = sps->height >> sps->log2_min_pu_size;
1949  sps->tb_mask = (1 << (sps->log2_ctb_size - sps->log2_min_tb_size)) - 1;
1950 
1951  sps->qp_bd_offset = 6 * (sps->bit_depth - 8);
1952 
1953  if ((sps->width & ((1U << sps->log2_min_cb_size) - 1)) ||
1954  (sps->height & ((1U << sps->log2_min_cb_size) - 1)))
1955  {
1956  ni_log(NI_LOG_ERROR, "Invalid coded frame dimensions.\n");
1957  return -1;
1958  }
1959 
1961  (int)(sps->log2_ctb_size - sps->log2_min_tb_size))
1962  {
1964  "max_transform_hierarchy_depth_inter out of range: %d\n",
1966  return -1;
1967  }
1969  (int)(sps->log2_ctb_size - sps->log2_min_tb_size))
1970  {
1972  "max_transform_hierarchy_depth_intra out of range: %d\n",
1974  return -1;
1975  }
1976  if ((sps->log2_max_trafo_size > sps->log2_ctb_size) &&
1977  (sps->log2_max_trafo_size > 5))
1978  {
1979  ni_log(NI_LOG_ERROR, "max transform block size out of range: %d\n",
1980  sps->log2_max_trafo_size);
1981  return -1;
1982  }
1983  if (ni_bs_reader_get_bits_left(&br) < 0)
1984  {
1985  ni_log(NI_LOG_ERROR, "Overread SPS by %d bits\n",
1987  return -1;
1988  }
1989 
1990  return 0;
1991 }
1992 
1993 // probe h.265 stream info; return 0 if stream can be decoded, -1 otherwise
1995 {
1996  int ret = -1;
1997  uint8_t *buf = NULL;
1998  uint8_t *p_buf;
1999  uint32_t nal_size, ep3_removed = 0, vcl_nal_count = 0;
2000  int nal_type = -1;
2001  int sps_parsed = 0;
2002 
2003  if (NULL == (buf = calloc(1, NI_MAX_TX_SZ)))
2004  {
2006  "Error probe_h265_stream_info: allocate stream buf failed\n");
2007  goto end;
2008  }
2009 
2010  reset_data_buf_pos(p_ctx);
2011  // probe at most 100 VCL before stops
2012  while ((!sps_parsed) && vcl_nal_count < 100 &&
2013  (nal_size = find_h265_next_nalu(p_ctx, buf, &nal_type)) > 0)
2014  {
2015  p_buf = buf;
2016 
2017  // skip the start code
2018  while (!(p_buf[0] == 0x00 && p_buf[1] == 0x00 && p_buf[2] == 0x01) &&
2019  (nal_size > 3))
2020  {
2021  p_buf++;
2022  nal_size--;
2023  }
2024  if (nal_size <= 3)
2025  {
2027  "Error probe_h265_stream_info NAL has no header\n");
2028  continue;
2029  }
2030 
2031  p_buf += 3; // skip start code
2032  nal_size -= 3;
2033  ep3_removed = ni_remove_emulation_prevent_bytes(p_buf, nal_size);
2034  nal_size -= ep3_removed;
2035  ni_log(NI_LOG_DEBUG, "nal %d nal_size %d\n", nal_type, nal_size);
2036 
2037  if (HEVC_NAL_SPS == nal_type && !sps_parsed)
2038  {
2039  if (vcl_nal_count > 0)
2040  {
2042  "Warning: %s has %d slice NAL units ahead of SPS!\n",
2043  __func__, vcl_nal_count);
2044  }
2045 
2046  if (h265_parse_sps(sps, p_buf, nal_size))
2047  {
2049  "probe_h265_stream_info: parse_sps error\n");
2050  break;
2051  }
2052  sps_parsed = 1;
2053  } else if (nal_type < 32)
2054  {
2055  vcl_nal_count++;
2056  }
2057  }
2058 
2059  reset_data_buf_pos(p_ctx);
2060  if (sps_parsed)
2061  {
2062  ret = 0;
2063  } else
2064  {
2065  ni_log(NI_LOG_ERROR, "probing failed.\n");
2066  }
2067 
2068 end:
2069  free(buf);
2070  buf = NULL;
2071  return ret;
2072 }
2073 
2074 uint64_t find_vp9_next_packet(ni_demo_context_t *p_ctx, uint8_t *p_dst, ni_vp9_header_info_t *vp9_info)
2075 {
2076  uint64_t data_size;
2077  uint64_t i = p_ctx->curr_file_offset ? p_ctx->curr_file_offset : vp9_info->header_length;
2078  if (i + 12 >= p_ctx->total_file_size)
2079  {
2080  ni_log(NI_LOG_DEBUG, "%s reaching end, curr_pos %llu, total input size %llu\n",
2081  __func__, (unsigned long long)p_ctx->curr_file_offset, (unsigned long long)p_ctx->total_file_size);
2082 
2083  if (p_ctx->loops_left > 1)
2084  {
2085  p_ctx->loops_left--;
2086  ni_log(NI_LOG_DEBUG, "input processed, %d loops left\n", p_ctx->loops_left);
2087  reset_data_buf_pos(p_ctx);
2088  i = vp9_info->header_length;
2089  } else {
2090  return 0;
2091  }
2092  }
2098  data_size =
2099  ((p_ctx->file_cache[i]) + (p_ctx->file_cache[i + 1] << 8) +
2100  (p_ctx->file_cache[i + 2] << 16) + (p_ctx->file_cache[i + 3] << 24));
2101  ni_log(NI_LOG_DEBUG, "vp9 packet data_size %u\n", data_size);
2102  i += 12;
2103 
2104  if (i + data_size > p_ctx->total_file_size)
2105  {
2106  data_size = p_ctx->total_file_size - i;
2107  memcpy(p_dst, &p_ctx->file_cache[i], data_size);
2108  p_ctx->curr_file_offset = p_ctx->total_file_size;
2109  return data_size;
2110  }
2111 
2112  memcpy(p_dst, &p_ctx->file_cache[i], data_size);
2113  p_ctx->curr_file_offset = i + data_size; // point to the start of data packet
2114  return data_size;
2115 }
2116 
2117 int vp9_parse_header(ni_vp9_header_info_t *vp9_info, uint8_t *buf, int size_bytes)
2118 {
2120  ni_bitstream_reader_init(&br, buf, 8 * size_bytes);
2121 
2122  ni_bs_reader_skip_bits(&br, 32); // skip signature
2123  ni_bs_reader_skip_bits(&br, 16); // skip version
2124 
2125  vp9_info->header_length = ni_bs_reader_get_bits(&br, 8);
2126  vp9_info->header_length |= ni_bs_reader_get_bits(&br, 8) << 8;
2127 
2128  ni_bs_reader_skip_bits(&br, 32); // skip codec fucc
2129 
2130  vp9_info->width = ni_bs_reader_get_bits(&br, 8);
2131  vp9_info->width |= ni_bs_reader_get_bits(&br, 8) << 8;
2132 
2133  vp9_info->height = ni_bs_reader_get_bits(&br, 8);
2134  vp9_info->height |= ni_bs_reader_get_bits(&br, 8) << 8;
2135 
2136  vp9_info->timebase.den = ni_bs_reader_get_bits(&br, 8);
2137  vp9_info->timebase.den |= ni_bs_reader_get_bits(&br, 8) << 8;
2138  vp9_info->timebase.den |= ni_bs_reader_get_bits(&br, 8) << 16;
2139  vp9_info->timebase.den |= ni_bs_reader_get_bits(&br, 8) << 24;
2140 
2141  vp9_info->timebase.num = ni_bs_reader_get_bits(&br, 8);
2142  vp9_info->timebase.num |= ni_bs_reader_get_bits(&br, 8) << 8;
2143  vp9_info->timebase.num |= ni_bs_reader_get_bits(&br, 8) << 16;
2144  vp9_info->timebase.num |= ni_bs_reader_get_bits(&br, 8) << 24;
2145 
2146  vp9_info->total_frames = ni_bs_reader_get_bits(&br, 8);
2147  vp9_info->total_frames |= ni_bs_reader_get_bits(&br, 8) << 8;
2148  vp9_info->total_frames |= ni_bs_reader_get_bits(&br, 8) << 16;
2149  vp9_info->total_frames |= ni_bs_reader_get_bits(&br, 8) << 24;
2150 
2151  if (vp9_info->header_length != 32)
2152  {
2153  ni_log(NI_LOG_ERROR, "Parse faled: header_length %d != 32\n",
2154  vp9_info->header_length);
2155  return -1;
2156  }
2157  ni_bs_reader_skip_bits(&br, 32); // unused bytes
2158  // here we skip frame header(12 bytes) to get profile
2159  ni_bs_reader_skip_bits(&br, 8 * 12);
2160  if (ni_bs_reader_get_bits(&br, 2) != 0x2) // frame marker
2161  {
2162  ni_log(NI_LOG_ERROR, "Invalid frame marker\n");
2163  return -1;
2164  }
2165  int profile = 0;
2166  profile = ni_bs_reader_get_bits(&br, 1);
2167  profile |= ni_bs_reader_get_bits(&br, 1) << 1;
2168  if ((profile != 0) && (profile != 2))
2169  {
2170  ni_log(
2171  NI_LOG_ERROR,
2172  "Only support profile0(yuv420,8bit) and profile2(yuv420, 10bit)\n");
2173  return -1;
2174  }
2175  vp9_info->profile = profile;
2176  return 0;
2177 }
2178 
2179 // probe vp9 stream info; return 0 if stream can be decoded, -1 otherwise
2181 {
2182  int ret = -1;
2183  uint8_t *buf = NULL;
2184 
2185  if (NULL == (buf = calloc(1, 64)))
2186  {
2188  "Error probe_vp9_stream_info: allocate stream buf failed\n");
2189  goto end;
2190  }
2191 
2192  reset_data_buf_pos(p_ctx);
2193  uint32_t size_bytes = 64;
2194  if (32 + 12 + 1 >= p_ctx->total_file_size)
2195  {
2196  ni_log(NI_LOG_ERROR, "No frame data probed!\n");
2197  goto end;
2198  } else
2199  {
2200  if (size_bytes > p_ctx->total_file_size)
2201  size_bytes = p_ctx->total_file_size;
2202  memcpy(buf, &p_ctx->file_cache[p_ctx->curr_file_offset], size_bytes);
2203  }
2204 
2205  ret = vp9_parse_header(vp9_info, buf, size_bytes);
2206  if (ret)
2207  {
2208  ni_log(NI_LOG_ERROR, "Failed to parse vp9 header info\n");
2209  goto end;
2210  }
2211  reset_data_buf_pos(p_ctx);
2212  // packets data starts after ivf file header
2213  p_ctx->curr_file_offset += vp9_info->header_length;
2214 
2215 end:
2216  free(buf);
2217  buf = NULL;
2218  return ret;
2219 }
2220 
2221 /*!*****************************************************************************
2222  * \brief Send decoder input data
2223  *
2224  * \param
2225  *
2226  * \return
2227  ******************************************************************************/
2229  ni_session_data_io_t *p_in_data,
2230  int input_video_width, int input_video_height,
2231  void *stream_info)
2232 {
2233  static uint8_t tmp_buf[NI_MAX_TX_SZ];
2234  uint8_t *tmp_buf_ptr = tmp_buf;
2235  int packet_size;
2236  uint32_t frame_pkt_size = 0, nal_size;
2237  int nal_type = -1;
2238  int tx_size = 0;
2239  uint32_t send_size = 0;
2240  int new_packet = 0;
2241  int32_t frame_num = -1, curr_frame_num;
2242  unsigned int first_mb_in_slice = 0;
2243  ni_packet_t *p_in_pkt = &(p_in_data->data.packet);
2244 
2245  ni_log(NI_LOG_DEBUG, "===> decoder_send_data <===\n");
2246 
2247  if (p_ctx->dec_eos_sent)
2248  {
2249  ni_log(NI_LOG_DEBUG, "decoder_send_data: ALL data (incl. eos) sent "
2250  "already!\n");
2251  return NI_TEST_RETCODE_SUCCESS;
2252  }
2253 
2254  if (0 == p_in_pkt->data_len)
2255  {
2256  memset(p_in_pkt, 0, sizeof(ni_packet_t));
2257 
2258  if (NI_CODEC_FORMAT_H264 == p_dec_ctx->codec_format)
2259  {
2260  ni_h264_sps_t *sps;
2261  sps = (ni_h264_sps_t *)stream_info;
2262  // send whole encoded packet which ends with a slice NAL
2263  while ((nal_size = find_h264_next_nalu(p_ctx, tmp_buf_ptr, &nal_type)) > 0)
2264  {
2265  frame_pkt_size += nal_size;
2266  tmp_buf_ptr += nal_size;
2267  ni_log(NI_LOG_DEBUG, "%s nal %d nal_size %d\n", __func__,
2268  nal_type, nal_size);
2269 
2270  if (H264_NAL_SLICE == nal_type ||
2271  H264_NAL_IDR_SLICE == nal_type)
2272  {
2273  if (!parse_h264_slice_header(tmp_buf_ptr - nal_size,
2274  nal_size, sps, &curr_frame_num,
2275  &first_mb_in_slice))
2276  {
2277  if (-1 == frame_num)
2278  {
2279  // first slice, continue to check
2280  frame_num = curr_frame_num;
2281  } else if (curr_frame_num != frame_num ||
2282  0 == first_mb_in_slice)
2283  {
2284  // this slice has diff. frame_num or first_mb_in_slice addr is
2285  // 0: not the same frame and return
2286  rewind_data_buf_pos_by(p_ctx, nal_size);
2287  frame_pkt_size -= nal_size;
2288  break;
2289  }
2290  // this slice is in the same frame, so continue to check and see
2291  // if there is more
2292  } else
2293  {
2295  "decoder_send_data: parse_slice_header error "
2296  "NAL type %d size %u, continue\n",
2297  nal_type, nal_size);
2298  }
2299  } else if (-1 != frame_num)
2300  {
2301  // already got a slice and this is non-slice NAL: return
2302  rewind_data_buf_pos_by(p_ctx, nal_size);
2303  frame_pkt_size -= nal_size;
2304  break;
2305  }
2306  // otherwise continue until a slice is found
2307  } // while there is still NAL
2308  } else if (NI_CODEC_FORMAT_H265 == p_dec_ctx->codec_format)
2309  {
2310  while ((nal_size = find_h265_next_nalu(p_ctx, tmp_buf_ptr, &nal_type)) > 0)
2311  {
2312  frame_pkt_size += nal_size;
2313  tmp_buf_ptr += nal_size;
2314  ni_log(NI_LOG_DEBUG, "%s nal_type %d nal_size %d\n", __func__,
2315  nal_type, nal_size);
2316 
2317  if (nal_type >= 0 && nal_type <= 23) // vcl units
2318  {
2319  ni_log(NI_LOG_DEBUG, "%s send vcl_nal %d nal_size %d\n",
2320  __func__, nal_type, nal_size);
2321  break;
2322  }
2323  }
2324  } else if (NI_CODEC_FORMAT_VP9 == p_dec_ctx->codec_format)
2325  {
2326  while ((packet_size = find_vp9_next_packet(p_ctx, tmp_buf_ptr, stream_info)) > 0)
2327  {
2328  frame_pkt_size += packet_size;
2329  ni_log(NI_LOG_DEBUG, "%s vp9 packet_size %d\n", __func__,
2330  packet_size);
2331  break;
2332  }
2333  } else {
2334  ni_log(NI_LOG_ERROR, "Error: Unsupported codec format %u", p_dec_ctx->codec_format);
2335  return NI_TEST_RETCODE_FAILURE;
2336  }
2337  ni_log(NI_LOG_DEBUG, "decoder_send_data * frame_pkt_size %d\n",
2338  frame_pkt_size);
2339 
2340  p_in_pkt->p_data = NULL;
2341  send_size = frame_pkt_size + p_dec_ctx->prev_size;
2342  if (send_size > 0)
2343  {
2344  ni_packet_buffer_alloc(p_in_pkt, (int)send_size);
2345  }
2346  p_in_pkt->data_len = send_size;
2347  new_packet = 1;
2348  } else
2349  {
2350  send_size = p_in_pkt->data_len;
2351  }
2352 
2353  p_in_pkt->start_of_stream = 0;
2354  if (!p_ctx->dec_sos_sent)
2355  {
2356  p_in_pkt->start_of_stream = 1;
2357  p_ctx->dec_sos_sent = 1;
2358  }
2359  p_in_pkt->end_of_stream = 0;
2360  p_in_pkt->video_width = input_video_width;
2361  p_in_pkt->video_height = input_video_height;
2362 
2363  if (send_size == 0)
2364  {
2365  if (p_ctx->curr_file_offset)
2366  {
2367  p_in_pkt->end_of_stream = 1;
2368  ni_log(NI_LOG_ERROR, "Sending eos\n");
2369  }
2370  } else
2371  {
2372  if (new_packet)
2373  {
2374  ni_packet_copy(p_in_pkt->p_data, tmp_buf, frame_pkt_size,
2375  p_dec_ctx->p_leftover, &p_dec_ctx->prev_size);
2376  }
2377  }
2378 
2379  tx_size =
2380  ni_device_session_write(p_dec_ctx, p_in_data, NI_DEVICE_TYPE_DECODER);
2381 
2382  if (tx_size < 0)
2383  {
2384  // Error
2385  ni_log(NI_LOG_ERROR, "Error: sending data error. rc:%d\n", tx_size);
2386  return NI_TEST_RETCODE_FAILURE;
2387  } else if (tx_size == 0 && !p_dec_ctx->ready_to_close)
2388  {
2389  ni_log(NI_LOG_DEBUG, "0 byte sent this time, return EAGAIN to retry.\n");
2390  return NI_TEST_RETCODE_EAGAIN;
2391  }
2392 
2393  p_ctx->dec_total_bytes_sent += tx_size;
2394 
2395  if (p_dec_ctx->ready_to_close)
2396  {
2397  p_ctx->dec_eos_sent = 1;
2398  }
2399 
2400  if (tx_size > 0)
2401  {
2402  ni_log(NI_LOG_DEBUG, "decoder_send_data: reset packet_buffer.\n");
2403  ni_packet_buffer_free(p_in_pkt);
2404  }
2405 
2406  return NI_TEST_RETCODE_SUCCESS;
2407 }
2408 
2409 /*!*****************************************************************************
2410  * \brief Receive decoded output data from decoder
2411  *
2412  * \param
2413  *
2414  * \return 0: got YUV frame; 1: end-of-stream; 2: got nothing
2415  ******s************************************************************************/
2417  ni_session_data_io_t *p_out_data,
2418  int output_video_width, int output_video_height,
2419  FILE *p_file, int write_to_file,
2420  int * p_rx_size)
2421 {
2422  int rc = NI_RETCODE_FAILURE;
2423  int rx_size = 0;
2424  bool b_is_hwframe = p_dec_ctx->hw_action;
2425  ni_frame_t *p_out_frame = &(p_out_data->data.frame);
2426  ni_session_data_io_t hwdl_session_data = {0};
2427  int width, height;
2428  // In decoder session read function it will allocate the actual YUV
2429  // transfer size for the very first read. And the pixel format of session
2430  // context would be set as well. So it does not matter with the planar
2431  // format for the first call of this function.
2432  int is_planar = get_pixel_planar(p_dec_ctx->pixel_format) == NI_PIXEL_PLANAR_FORMAT_PLANAR;
2433 
2435  "===> decoder_receive_data hwframe %d pixel_format %d <===\n",
2436  b_is_hwframe, p_dec_ctx->pixel_format);
2437 
2438  if (p_ctx->dec_eos_received)
2439  {
2440  ni_log(NI_LOG_DEBUG, "decoder_receive_data eos received already, Done!\n");
2442  goto end;
2443  }
2444 
2445  // prepare memory buffer for receiving decoded frame
2446  width = p_dec_ctx->actual_video_width > 0 ?
2447  (int)(p_dec_ctx->actual_video_width) :
2448  output_video_width;
2449  height = p_dec_ctx->active_video_height > 0 ?
2450  (int)(p_dec_ctx->active_video_height) :
2451  output_video_height;
2452 
2453  // allocate memory only after resolution is known (for buffer pool set up)
2454  int alloc_mem = (p_dec_ctx->active_video_width > 0 &&
2455  p_dec_ctx->active_video_height > 0 ?
2456  1 :
2457  0);
2458  if (!b_is_hwframe)
2459  {
2461  p_dec_ctx->dec_fme_buf_pool, &(p_out_data->data.frame), alloc_mem,
2462  width, height, p_dec_ctx->codec_format == NI_CODEC_FORMAT_H264,
2463  p_dec_ctx->bit_depth_factor, is_planar);
2464  if (NI_RETCODE_SUCCESS != rc)
2465  {
2467  goto end;
2468  }
2469  rx_size = ni_device_session_read(p_dec_ctx, p_out_data,
2471  } else
2472  {
2473  rc = ni_frame_buffer_alloc(
2474  &(p_out_data->data.frame), width, height,
2475  p_dec_ctx->codec_format == NI_CODEC_FORMAT_H264, 1,
2476  p_dec_ctx->bit_depth_factor,
2477  3 /*3 is max supported hwframe output count per frame*/, is_planar);
2478  if (NI_RETCODE_SUCCESS != rc)
2479  {
2481  goto end;
2482  }
2483  rx_size = ni_device_session_read_hwdesc(p_dec_ctx, p_out_data,
2485  }
2486 
2487  // the actual pix_fmt is known and updated in ctx only after the first
2488  // frame is decoded, so check/update it here again to be used below
2489  is_planar = get_pixel_planar(p_dec_ctx->pixel_format) ==
2491 
2492  if (rx_size < 0)
2493  {
2494  ni_log(NI_LOG_ERROR, "Error: receiving data error. rc:%d\n", rx_size);
2495  if (!b_is_hwframe)
2496  {
2497  ni_decoder_frame_buffer_free(p_out_frame);
2498  } else
2499  {
2500  ni_frame_buffer_free(p_out_frame);
2501  }
2503  goto end;
2504  } else if (rx_size > 0)
2505  {
2506  p_ctx->num_frames_received++;
2507  ni_log(NI_LOG_DEBUG, "Got frame # %" PRIu64 " bytes %d\n",
2508  p_dec_ctx->frame_num, rx_size);
2509 
2510  ni_dec_retrieve_aux_data(p_out_frame);
2511 
2512  if (p_file && write_to_file) {
2513  if (p_dec_ctx->hw_action == NI_CODEC_HW_ENABLE)
2514  {
2515  rc = hwdl_frame(p_dec_ctx, &hwdl_session_data, p_out_frame, p_dec_ctx->pixel_format);
2516  if (rc <= 0) {
2518  goto end;
2519  }
2520  rc = write_rawvideo_data(p_file, p_dec_ctx->active_video_width, p_dec_ctx->active_video_height,
2521  output_video_width, output_video_height, p_dec_ctx->pixel_format,
2522  &hwdl_session_data.data.frame);
2523  ni_frame_buffer_free(&hwdl_session_data.data.frame);
2524  } else
2525  {
2526  rc = write_rawvideo_data(p_file, p_dec_ctx->active_video_width, p_dec_ctx->active_video_height,
2527  output_video_width, output_video_height, p_dec_ctx->pixel_format, p_out_frame);
2528  }
2529 
2530  if (rc < 0) {
2531  goto end;
2532  }
2533  }
2534  } else // rx_size == 0 means no decoded frame is available now
2535  {
2536  ni_log(NI_LOG_DEBUG, "No data received from decoder, return EAGAIN and retry\n");
2537  if (!p_out_frame->end_of_stream)
2538  {
2539  if (!b_is_hwframe)
2540  {
2541  ni_decoder_frame_buffer_free(p_out_frame);
2542  } else
2543  {
2544  ni_frame_buffer_free(p_out_frame);
2545  }
2546  }
2548  }
2549 
2550  p_ctx->dec_total_bytes_received += rx_size;
2551  *p_rx_size = rx_size;
2552 
2553  if (p_out_frame->end_of_stream)
2554  {
2555  ni_log(NI_LOG_INFO, "Decoder Receiving done.\n");
2556  p_ctx->dec_eos_received = 1;
2558  }
2559 
2560  ni_log(NI_LOG_DEBUG, "decoder_receive_data: success\n");
2561 
2562 end:
2563  ni_log(NI_LOG_DEBUG, "decoder_receive_data: rc %d rx_size %d\n", rc, rx_size);
2564 
2565  return rc;
2566 }
2567 
2568 /*!*****************************************************************************
2569  * \brief decoder session open
2570  *
2571  * \param
2572  *
2573  * \return 0 if successful, < 0 otherwise
2574  ******************************************************************************/
2575 int decoder_open_session(ni_session_context_t *p_dec_ctx, int iXcoderGUID,
2576  ni_xcoder_params_t *p_dec_params)
2577 {
2578  int ret = 0;
2579 
2580  // default is little_endian
2581  p_dec_ctx->src_endian = NI_FRAME_LITTLE_ENDIAN;
2582  p_dec_ctx->p_session_config = p_dec_params;
2583  p_dec_ctx->session_id = NI_INVALID_SESSION_ID;
2584 
2585  // assign the card GUID in the encoder context and let session open
2586  // take care of the rest
2587  p_dec_ctx->device_handle = NI_INVALID_DEVICE_HANDLE;
2588  p_dec_ctx->blk_io_handle = NI_INVALID_DEVICE_HANDLE;
2589  p_dec_ctx->hw_id = iXcoderGUID;
2590 
2591  if (p_dec_params->dec_input_params.hwframes)
2592  {
2593  p_dec_ctx->hw_action = NI_CODEC_HW_ENABLE;
2594  } else
2595  {
2596  p_dec_ctx->hw_action = NI_CODEC_HW_NONE;
2597  }
2598 
2600 
2601  if (ret != NI_RETCODE_SUCCESS)
2602  {
2603  ni_log(NI_LOG_ERROR, "Error: ni_decoder_session_open() failure!\n");
2604  return -1;
2605  } else
2606  {
2607  ni_log(NI_LOG_INFO, "Decoder device %d session open successful.\n", iXcoderGUID);
2608  return 0;
2609  }
2610 }
2611 
2613 {
2614  uint64_t current_time;
2615 
2617 
2618  ni_log(NI_LOG_ERROR, "Decoder Closing, Got: Frames=%u FPS=%.2f Total bytes %llu\n",
2619  p_ctx->num_frames_received,
2620  (float)p_ctx->num_frames_received / (float)(current_time - p_ctx->start_time) * (float)1000000000,
2621  p_ctx->dec_total_bytes_received);
2622 
2624 }
2625 
2626 void *decoder_send_thread(void *args)
2627 {
2628  dec_send_param_t *p_dec_send_param = args;
2629  ni_demo_context_t *p_ctx = p_dec_send_param->p_ctx;
2630  ni_session_context_t *p_dec_ctx = p_dec_send_param->p_dec_ctx;
2631  ni_test_frame_list_t *frame_list = p_dec_send_param->frame_list;
2632  ni_session_data_io_t in_pkt = {0};
2633  int retval = 0;
2634 
2635  ni_log(NI_LOG_INFO, "decoder_send_thread start: decoder_low_delay %d\n",
2636  p_dec_ctx->decoder_low_delay);
2637  while (1)
2638  {
2639  // Do not send packet to decoder if the output frame list is full.
2640  // Try to consume one decoded frame before sending new packets
2641  while (frame_list_is_full(frame_list) && !p_ctx->end_all_threads)
2642  {
2643  ni_usleep(100);
2644  }
2645 
2646  if (p_ctx->end_all_threads)
2647  {
2648  break;
2649  }
2650 
2651  retval = decoder_send_data(p_ctx, p_dec_ctx, &in_pkt, p_dec_send_param->input_width,
2652  p_dec_send_param->input_height, p_dec_send_param->p_stream_info);
2653  if (retval < 0) // Error
2654  {
2655  ni_log(NI_LOG_ERROR, "Error: decoder send packet failed\n");
2656  break;
2657  } else if (p_dec_send_param->p_ctx->dec_eos_sent) //eos
2658  {
2659  ni_log(NI_LOG_INFO, "decoder_send_thread reached eos\n");
2660  break;
2661  } else if (retval == NI_TEST_RETCODE_EAGAIN) {
2662  ni_usleep(100);
2663  }
2664  }
2665 
2667 
2668  // Broadcast all codec threads to quit on exception such as NVMe IO.
2669  if (retval < 0)
2670  {
2671  p_ctx->end_all_threads = 1;
2672  }
2673 
2674  ni_log(NI_LOG_TRACE, "decoder_send_thread exit\n");
2675  return (void *)(long)retval;
2676 }
2677 
2678 void *decoder_receive_thread(void *args)
2679 {
2680  dec_recv_param_t *p_dec_recv_param = args;
2681  ni_demo_context_t *p_ctx = p_dec_recv_param->p_ctx;
2682  ni_session_context_t *p_dec_ctx = p_dec_recv_param->p_dec_ctx;
2683  ni_test_frame_list_t *frame_list = p_dec_recv_param->frame_list;
2684  ni_scale_params_t *scale_params = p_dec_recv_param->scale_params;
2685  ni_drawbox_params_t *drawbox_params = p_dec_recv_param->drawbox_params;
2686  ni_session_data_io_t filter_out_frame = {0};
2687  ni_session_data_io_t *p_out_frame = NULL;
2688  ni_frame_t *p_ni_frame = NULL;
2689  niFrameSurface1_t *p_hwframe;
2690  int retval = 0;
2691  int rx_size = 0;
2692  uint64_t current_time, previous_time = p_ctx->start_time;
2693 
2694  ni_log(NI_LOG_INFO, "decoder_receive_thread start\n");
2695 
2696  for (;;)
2697  {
2698  while (frame_list_is_full(frame_list) && !p_ctx->end_all_threads)
2699  {
2700  ni_usleep(100);
2701  }
2702 
2703  if (p_ctx->end_all_threads)
2704  {
2705  break;
2706  }
2707 
2708  p_out_frame = &frame_list->frames[frame_list->tail];
2709  p_ni_frame = &p_out_frame->data.frame;
2710  retval = decoder_receive_data(
2711  p_ctx, p_dec_ctx, p_out_frame, p_dec_recv_param->input_width,
2712  p_dec_recv_param->input_height, NULL, 0 /* no save to file */, &rx_size);
2713  if (retval < 0) // Error
2714  {
2715  if (!p_dec_ctx->hw_action)
2716  {
2717  ni_decoder_frame_buffer_free(p_ni_frame);
2718  } else
2719  {
2720  ni_frame_buffer_free(p_ni_frame);
2721  }
2722  ni_log(
2723  NI_LOG_ERROR,
2724  "Error: decoder_receive_thread break in transcode mode!\n");
2725  break;
2726  } else if (p_ni_frame->end_of_stream)
2727  {
2728  frame_list_enqueue(frame_list);
2729  ni_log(NI_LOG_INFO, "decoder_receive_thread reach eos\n");
2730  retval = 0;
2731  break;
2732  } else if (retval == NI_TEST_RETCODE_EAGAIN)
2733  {
2734  if (!p_dec_ctx->hw_action)
2735  {
2736  ni_decoder_frame_buffer_free(p_ni_frame);
2737  } else
2738  {
2739  ni_frame_buffer_free(p_ni_frame);
2740  }
2741  ni_usleep(100);
2742  } else
2743  {
2744  if (scale_params->enabled)
2745  {
2746  p_hwframe = (niFrameSurface1_t *)p_ni_frame->p_data[3];
2747  ni_hw_frame_ref(p_hwframe);
2748  scale_filter(p_dec_recv_param->p_sca_ctx, p_ni_frame, &filter_out_frame, p_dec_recv_param->xcoderGUID,
2749  scale_params->width, scale_params->height, ni_to_gc620_pix_fmt(p_dec_ctx->pixel_format),
2750  scale_params->format);
2751  ni_hw_frame_unref(p_hwframe->ui16FrameIdx);
2752  ni_frame_buffer_free(p_ni_frame);
2753  memcpy(p_out_frame, &filter_out_frame, sizeof(ni_session_data_io_t));
2754  memset(&filter_out_frame, 0, sizeof(ni_session_data_io_t));
2755  }
2756  else if (drawbox_params->enabled)
2757  {
2758  p_hwframe = (niFrameSurface1_t *)p_ni_frame->p_data[3];
2759  ni_hw_frame_ref(p_hwframe);
2760  drawbox_filter(p_dec_recv_param->p_crop_ctx, p_dec_recv_param->p_pad_ctx, p_dec_recv_param->p_ovly_ctx,
2761  p_dec_recv_param->p_fmt_ctx, p_ni_frame, &filter_out_frame, drawbox_params,
2762  p_dec_recv_param->xcoderGUID, ni_to_gc620_pix_fmt(p_dec_ctx->pixel_format), GC620_I420);
2763  ni_hw_frame_unref(p_hwframe->ui16FrameIdx);
2764  ni_frame_buffer_free(p_ni_frame);
2765  memcpy(p_out_frame, &filter_out_frame, sizeof(ni_session_data_io_t));
2766  memset(&filter_out_frame, 0, sizeof(ni_session_data_io_t));
2767  }
2768  if (p_dec_ctx->hw_action)
2769  {
2770  uint16_t current_hwframe_index = ((niFrameSurface1_t *)p_ni_frame->p_data[3])->ui16FrameIdx;
2771  ni_log(NI_LOG_DEBUG, "decoder recv:%d, tail:%d\n", current_hwframe_index, frame_list->tail);
2772  }
2773  frame_list_enqueue(frame_list);
2774  }
2775 
2777  if (current_time - previous_time >= (uint64_t)1000000000) {
2778  ni_log(NI_LOG_INFO, "Decoder stats: received %u frames, fps %.2f, total bytes %u\n",
2779  p_ctx->num_frames_received,
2780  (float)p_ctx->num_frames_received / (float)(current_time - p_ctx->start_time) * (float)1000000000,
2781  p_ctx->dec_total_bytes_received);
2783  }
2784  }
2785 
2786  ni_frame_buffer_free(&filter_out_frame.data.frame);
2787 
2788  // Broadcast all codec threads to quit on exception such as NVMe IO.
2789  if (retval < 0)
2790  {
2791  p_ctx->end_all_threads = 1;
2792  }
2793 
2794  ni_log(NI_LOG_TRACE, "decoder_receive_thread exit\n");
2795  return (void *)(long)retval;
2796 }
_ni_h265_sps_t::min_pu_width
int min_pu_width
Definition: ni_decode_utils.h:326
_ni_scale_params::height
int height
Definition: ni_filter_utils.h:37
PTLCommon::progressive_source_flag
uint8_t progressive_source_flag
Definition: ni_decode_utils.h:194
_ni_h265_sps_t::pixel_shift
int pixel_shift
Definition: ni_decode_utils.h:252
_ni_h265_sps_t::st_rps
ShortTermRPS st_rps[HEVC_MAX_SHORT_TERM_REF_PIC_SETS]
Definition: ni_decode_utils.h:274
ni_decode_utils.h
PTL::sub_layer_profile_present_flag
uint8_t sub_layer_profile_present_flag[HEVC_MAX_SUB_LAYERS]
Definition: ni_decode_utils.h:217
dec_recv_param::frame_list
ni_test_frame_list_t * frame_list
Definition: ni_decode_utils.h:62
_ni_h265_sps_t::temporal_id_nesting_flag
uint8_t temporal_id_nesting_flag
Definition: ni_decode_utils.h:265
NI_MAX_TX_SZ
#define NI_MAX_TX_SZ
Definition: ni_defs.h:249
write_rawvideo_data
int write_rawvideo_data(FILE *p_file, int input_aligned_width, int input_aligned_height, int output_width, int output_height, int format, ni_frame_t *p_out_frame)
Write hwdl data to files.
Definition: ni_generic_utils.c:510
frame_list_is_full
bool frame_list_is_full(ni_test_frame_list_t *list)
Definition: ni_generic_utils.c:180
dec_recv_param::scale_params
ni_scale_params_t * scale_params
Definition: ni_decode_utils.h:63
H264_NAL_SPS
@ H264_NAL_SPS
Definition: ni_decode_utils.h:362
h265_decode_hrd
int h265_decode_hrd(ni_bitstream_reader_t *br, int common_inf_present, int max_sublayers)
Definition: ni_decode_utils.c:1087
VUI::vui_poc_proportional_to_timing_flag
int vui_poc_proportional_to_timing_flag
Definition: ni_decode_utils.h:173
dec_recv_param::p_sca_ctx
ni_session_context_t * p_sca_ctx
Definition: ni_decode_utils.h:54
_ni_session_context::decoder_low_delay
int decoder_low_delay
Definition: ni_device_api.h:1594
_ni_demo_context::loops_left
uint64_t loops_left
Definition: ni_generic_utils.h:117
reset_data_buf_pos
void reset_data_buf_pos(ni_demo_context_t *p_ctx)
Definition: ni_generic_utils.c:316
_ni_h265_sps_t::transform_skip_rotation_enabled_flag
int transform_skip_rotation_enabled_flag
Definition: ni_decode_utils.h:306
_ni_h264_sps_t::color_trc
ni_color_transfer_characteristic_t color_trc
Definition: ni_decode_utils.h:107
_ni_h265_sps_t::bit_depth_chroma
int bit_depth_chroma
Definition: ni_decode_utils.h:251
ni_rational_t
struct _ni_rational ni_rational_t
VUI::vui_hrd_parameters_present_flag
int vui_hrd_parameters_present_flag
Definition: ni_decode_utils.h:175
VUI::colour_primaries
uint8_t colour_primaries
Definition: ni_decode_utils.h:155
_ni_h264_sps_t::time_offset_length
int time_offset_length
Definition: ni_decode_utils.h:123
_ni_h264_sps_t::log2_max_poc_lsb
int log2_max_poc_lsb
log2_max_pic_order_cnt_lsb_minus4
Definition: ni_decode_utils.h:82
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
H264_NAL_IDR_SLICE
@ H264_NAL_IDR_SLICE
Definition: ni_decode_utils.h:360
PTLCommon::profile_idc
uint8_t profile_idc
Definition: ni_decode_utils.h:192
VUI::motion_vectors_over_pic_boundaries_flag
int motion_vectors_over_pic_boundaries_flag
Definition: ni_decode_utils.h:179
ni_hw_frame_ref
void ni_hw_frame_ref(const niFrameSurface1_t *p_surface)
Definition: ni_generic_utils.c:658
_ni_vp9_header_info::den
uint32_t den
Definition: ni_decode_utils.h:347
HEVC_MAX_SPS_COUNT
#define HEVC_MAX_SPS_COUNT
Definition: ni_generic_utils.h:72
PTLCommon::interlaced_source_flag
uint8_t interlaced_source_flag
Definition: ni_decode_utils.h:195
_ni_h265_sps_t::min_cb_width
int min_cb_width
Definition: ni_decode_utils.h:322
_ni_h264_sps_t::offset_for_ref_frame
short offset_for_ref_frame[256]
Definition: ni_decode_utils.h:113
dec_send_param::input_width
int input_width
Definition: ni_decode_utils.h:44
VUI::chroma_loc_info_present_flag
int chroma_loc_info_present_flag
Definition: ni_decode_utils.h:159
_ni_h264_sps_t::vcl_hrd_parameters_present_flag
int vcl_hrd_parameters_present_flag
Definition: ni_decode_utils.h:121
PTLCommon::max_420chroma_constraint_flag
uint8_t max_420chroma_constraint_flag
Definition: ni_decode_utils.h:202
VUI::chroma_sample_loc_type_top_field
int chroma_sample_loc_type_top_field
Definition: ni_decode_utils.h:160
_ni_h264_sps_t::crop_bottom
unsigned int crop_bottom
frame_cropping_rect_bottom_offset
Definition: ni_decode_utils.h:100
NI_DEVICE_TYPE_DECODER
@ NI_DEVICE_TYPE_DECODER
Definition: ni_defs.h:346
_ni_h264_sps_t::cpb_removal_delay_length
int cpb_removal_delay_length
cpb_removal_delay_length_minus1 + 1
Definition: ni_decode_utils.h:126
probe_h264_stream_info
int probe_h264_stream_info(ni_demo_context_t *p_ctx, ni_h264_sps_t *sps)
Definition: ni_decode_utils.c:715
_ni_h264_sps_t::offset_for_top_to_bottom_field
int offset_for_top_to_bottom_field
Definition: ni_decode_utils.h:85
_ni_session_context::session_id
uint32_t session_id
Definition: ni_device_api.h:1480
_ni_h264_sps_t::vui_parameters_present_flag
int vui_parameters_present_flag
Definition: ni_decode_utils.h:101
VUI::video_signal_type_present_flag
int video_signal_type_present_flag
Definition: ni_decode_utils.h:151
ni_hw_frame_unref
void ni_hw_frame_unref(uint16_t hwframe_index)
Definition: ni_generic_utils.c:679
_ni_packet::start_of_stream
uint32_t start_of_stream
Definition: ni_device_api.h:2832
vp9_parse_header
int vp9_parse_header(ni_vp9_header_info_t *vp9_info, uint8_t *buf, int size_bytes)
Definition: ni_decode_utils.c:2117
ni_generic_utils.h
_ni_demo_context::total_file_size
uint64_t total_file_size
Definition: ni_generic_utils.h:116
VUI::log2_max_mv_length_vertical
int log2_max_mv_length_vertical
Definition: ni_decode_utils.h:185
NI_CODEC_HW_NONE
@ NI_CODEC_HW_NONE
Definition: ni_device_api.h:939
PTLCommon::profile_space
uint8_t profile_space
Definition: ni_decode_utils.h:190
NI_PIX_FMT_YUV420P
@ NI_PIX_FMT_YUV420P
Definition: ni_device_api.h:262
_ni_h265_sps_t::ctb_height
int ctb_height
Definition: ni_decode_utils.h:320
_ni_packet::end_of_stream
uint32_t end_of_stream
Definition: ni_device_api.h:2831
ni_frame_buffer_alloc
ni_retcode_t ni_frame_buffer_alloc(ni_frame_t *p_frame, int video_width, int video_height, int alignment, int metadata_flag, int factor, int hw_frame_count, int is_planar)
Allocate preliminary memory for the frame buffer based on provided parameters. Applicable to YUV420 P...
Definition: ni_device_api.c:2123
VUI::chroma_sample_loc_type_bottom_field
int chroma_sample_loc_type_bottom_field
Definition: ni_decode_utils.h:161
_ni_session_context::p_leftover
uint8_t * p_leftover
Definition: ni_device_api.h:1506
_ni_session_data_io::packet
ni_packet_t packet
Definition: ni_device_api.h:2871
_ni_h265_sps_t::intra_smoothing_disabled_flag
int intra_smoothing_disabled_flag
Definition: ni_decode_utils.h:311
NI_RETCODE_SUCCESS
@ NI_RETCODE_SUCCESS
Definition: ni_defs.h:427
_ni_session_context::active_video_width
uint32_t active_video_width
Definition: ni_device_api.h:1549
_ni_h265_sps_t::long_term_ref_pics_present_flag
uint8_t long_term_ref_pics_present_flag
Definition: ni_decode_utils.h:279
HEVC_MAX_SUB_LAYERS
#define HEVC_MAX_SUB_LAYERS
Definition: ni_generic_utils.h:69
dec_recv_param::xcoderGUID
int xcoderGUID
Definition: ni_decode_utils.h:59
PTLCommon::max_422chroma_constraint_flag
uint8_t max_422chroma_constraint_flag
Definition: ni_decode_utils.h:201
h265_decode_sublayer_hrd
void h265_decode_sublayer_hrd(ni_bitstream_reader_t *br, unsigned int nb_cpb, int subpic_params_present)
Definition: ni_decode_utils.c:952
NI_INVALID_SESSION_ID
#define NI_INVALID_SESSION_ID
Definition: ni_device_api.h:111
_ni_h264_sps_t::log2_max_frame_num
int log2_max_frame_num
log2_max_frame_num_minus4 + 4
Definition: ni_decode_utils.h:80
_ni_h265_sps_t::min_cb_height
int min_cb_height
Definition: ni_decode_utils.h:323
_ni_session_context::actual_video_width
uint32_t actual_video_width
Definition: ni_device_api.h:1553
ni_gettime_ns
uint64_t ni_gettime_ns(void)
Definition: ni_util.c:1998
HEVC_NAL_SPS
@ HEVC_NAL_SPS
Definition: ni_decode_utils.h:391
_ni_h265_sps_t::max_latency_increase
int max_latency_increase
Definition: ni_decode_utils.h:263
_ni_h265_window_t::bottom_offset
unsigned int bottom_offset
Definition: ni_decode_utils.h:141
PTLCommon::inbld_flag
uint8_t inbld_flag
Definition: ni_decode_utils.h:208
probe_h265_stream_info
int probe_h265_stream_info(ni_demo_context_t *p_ctx, ni_h265_sps_t *sps)
Definition: ni_decode_utils.c:1994
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
PTL::general_ptl
PTLCommon general_ptl
Definition: ni_decode_utils.h:214
_ni_demo_context::dec_eos_received
int dec_eos_received
Definition: ni_generic_utils.h:144
_ni_h265_window_t::left_offset
unsigned int left_offset
Definition: ni_decode_utils.h:138
_ni_h264_sps_t::gaps_in_frame_num_allowed_flag
int gaps_in_frame_num_allowed_flag
Definition: ni_decode_utils.h:88
_ni_demo_context::end_all_threads
uint8_t end_all_threads
Definition: ni_generic_utils.h:111
h264_parse_hrd
int h264_parse_hrd(ni_bitstream_reader_t *br, ni_h264_sps_t *sps)
Definition: ni_decode_utils.c:174
_ni_h265_sps_t::loop_filter_disable_flag
uint8_t loop_filter_disable_flag
Definition: ni_decode_utils.h:290
NI_H264_SEI_PIC_STRUCT_FRAME
@ NI_H264_SEI_PIC_STRUCT_FRAME
frame
Definition: ni_av_codec.h:73
dec_send_param::frame_list
ni_test_frame_list_t * frame_list
Definition: ni_decode_utils.h:47
_ni_session_context::src_endian
int src_endian
Definition: ni_device_api.h:1493
VUI::default_display_window_flag
int default_display_window_flag
Definition: ni_decode_utils.h:167
VUI::frame_field_info_present_flag
int frame_field_info_present_flag
Definition: ni_decode_utils.h:165
decoder_receive_thread
void * decoder_receive_thread(void *args)
Definition: ni_decode_utils.c:2678
_ni_demo_context::curr_file_offset
uint64_t curr_file_offset
Definition: ni_generic_utils.h:115
EXTENDED_SAR
#define EXTENDED_SAR
Definition: ni_generic_utils.h:65
_ni_h265_sps_t::min_pu_height
int min_pu_height
Definition: ni_decode_utils.h:327
drawbox_filter
int drawbox_filter(ni_session_context_t *p_crop_ctx, ni_session_context_t *p_pad_ctx, ni_session_context_t *p_overlay_ctx, ni_session_context_t *p_fmt_ctx, ni_frame_t *p_frame_in, ni_session_data_io_t *p_data_out, ni_drawbox_params_t *p_box_params, int iXcoderGUID, int input_format, int output_format)
Use crop->pad->overlay to simulate a drawbox filter.
Definition: ni_filter_utils.c:375
NI_COL_SPC_NB
@ NI_COL_SPC_NB
Definition: ni_av_codec.h:181
_ni_h265_sps_t::implicit_rdpcm_enabled_flag
int implicit_rdpcm_enabled_flag
Definition: ni_decode_utils.h:308
_ni_h265_sps_t::log2_diff_max_min_coding_block_size
unsigned int log2_diff_max_min_coding_block_size
Definition: ni_decode_utils.h:296
_ni_h264_sps_t::crop_top
unsigned int crop_top
frame_cropping_rect_top_offset
Definition: ni_decode_utils.h:99
_ni_h264_sps_t::scaling_matrix8
uint8_t scaling_matrix8[6][64]
Definition: ni_decode_utils.h:119
VUI::overscan_appropriate_flag
int overscan_appropriate_flag
Definition: ni_decode_utils.h:149
_ni_session_context::bit_depth_factor
int bit_depth_factor
Definition: ni_device_api.h:1494
_ni_h264_sps_t::fixed_frame_rate_flag
int fixed_frame_rate_flag
Definition: ni_decode_utils.h:112
_ni_h264_sps_t::colour_description_present_flag
int colour_description_present_flag
Definition: ni_decode_utils.h:105
_ni_h265_sps_t::sps_strong_intra_smoothing_enable_flag
uint8_t sps_strong_intra_smoothing_enable_flag
Definition: ni_decode_utils.h:293
_ni_demo_context::dec_eos_sent
int dec_eos_sent
Definition: ni_generic_utils.h:143
_ni_h265_sps_t::amp_enabled_flag
uint8_t amp_enabled_flag
Definition: ni_decode_utils.h:276
ni_bs_reader_get_bits
uint32_t ni_bs_reader_get_bits(ni_bitstream_reader_t *br, int n)
read bits (up to 32) from the bitstream reader, after reader init
Definition: ni_bitstream.c:471
dec_send_param::p_stream_info
void * p_stream_info
Definition: ni_decode_utils.h:46
VUI::video_format
int video_format
Definition: ni_decode_utils.h:152
PTL::sub_layer_level_present_flag
uint8_t sub_layer_level_present_flag[HEVC_MAX_SUB_LAYERS]
Definition: ni_decode_utils.h:218
_ni_h265_sps_t::width
int width
Definition: ni_decode_utils.h:317
_ni_session_context::hw_id
int hw_id
Definition: ni_device_api.h:1478
_ni_h265_sps_t::pix_fmt
int pix_fmt
Definition: ni_decode_utils.h:253
_ni_h265_sps_t::tb_mask
int tb_mask
Definition: ni_decode_utils.h:328
h264_parse_vui
int h264_parse_vui(ni_bitstream_reader_t *br, ni_h264_sps_t *sps)
Definition: ni_decode_utils.c:203
dec_recv_param::p_fmt_ctx
ni_session_context_t * p_fmt_ctx
Definition: ni_decode_utils.h:58
dec_recv_param::input_width
int input_width
Definition: ni_decode_utils.h:60
_ni_test_frame_list
Definition: ni_generic_utils.h:102
_ni_h265_sps_t::max_dec_pic_buffering
int max_dec_pic_buffering
Definition: ni_decode_utils.h:261
decoder_receive_data
int decoder_receive_data(ni_demo_context_t *p_ctx, ni_session_context_t *p_dec_ctx, ni_session_data_io_t *p_out_data, int output_video_width, int output_video_height, FILE *p_file, int write_to_file, int *p_rx_size)
Receive decoded output data from decoder.
Definition: ni_decode_utils.c:2416
_ni_h264_sps_t::crop
int crop
frame_cropping_flag
Definition: ni_decode_utils.h:95
_ni_h264_sps_t::initial_cpb_removal_delay_length
int initial_cpb_removal_delay_length
initial_cpb_removal_delay_length_minus1 + 1
Definition: ni_decode_utils.h:125
ni_packet_buffer_alloc
ni_retcode_t ni_packet_buffer_alloc(ni_packet_t *p_packet, int packet_size)
Allocate memory for the packet buffer based on provided packet size.
Definition: ni_device_api.c:3713
_ni_h264_sps_t::dpb_output_delay_length
int dpb_output_delay_length
dpb_output_delay_length_minus1 + 1
Definition: ni_decode_utils.h:127
ShortTermRPS::delta_poc
int32_t delta_poc[32]
Definition: ni_decode_utils.h:234
h264_parse_sps
int h264_parse_sps(uint8_t *buf, int size_bytes, ni_h264_sps_t *sps)
Definition: ni_decode_utils.c:420
find_vp9_next_packet
uint64_t find_vp9_next_packet(ni_demo_context_t *p_ctx, uint8_t *p_dst, ni_vp9_header_info_t *vp9_info)
Definition: ni_decode_utils.c:2074
h265_parse_sps
int h265_parse_sps(ni_h265_sps_t *sps, uint8_t *buf, int size_bytes)
Definition: ni_decode_utils.c:1611
probe_vp9_stream_info
int probe_vp9_stream_info(ni_demo_context_t *p_ctx, ni_vp9_header_info_t *vp9_info)
Definition: ni_decode_utils.c:2180
VUI::video_full_range_flag
int video_full_range_flag
Definition: ni_decode_utils.h:153
_ni_h265_sps_t::max_transform_hierarchy_depth_intra
int max_transform_hierarchy_depth_intra
Definition: ni_decode_utils.h:303
ni_log.h
Logging definitions.
_ni_scale_params::format
int format
Definition: ni_filter_utils.h:38
frame_list_enqueue
int frame_list_enqueue(ni_test_frame_list_t *list)
Definition: ni_generic_utils.c:209
_niFrameSurface1::ui16FrameIdx
uint16_t ui16FrameIdx
Definition: ni_device_api.h:2795
NI_H264_SEI_PIC_STRUCT_FRAME_TRIPLING
@ NI_H264_SEI_PIC_STRUCT_FRAME_TRIPLING
frame tripling
Definition: ni_av_codec.h:89
_ni_rational
Definition: ni_device_api.h:598
_ni_demo_context
Definition: ni_generic_utils.h:109
_ni_vp9_header_info
Definition: ni_decode_utils.h:339
NI_LOG_INFO
@ NI_LOG_INFO
Definition: ni_log.h:61
HEVC_MAX_LOG2_CTB_SIZE
#define HEVC_MAX_LOG2_CTB_SIZE
Definition: ni_generic_utils.h:74
decoder_stat_report_and_close
void decoder_stat_report_and_close(ni_demo_context_t *p_ctx, ni_session_context_t *p_dec_ctx)
Definition: ni_decode_utils.c:2612
VUI::vui_timing_info_present_flag
int vui_timing_info_present_flag
Definition: ni_decode_utils.h:170
ni_bs_reader_get_ue
uint32_t ni_bs_reader_get_ue(ni_bitstream_reader_t *br)
read an unsigned Exp-Golomb code ue(v)
Definition: ni_bitstream.c:522
ni_decoder_frame_buffer_alloc
ni_retcode_t ni_decoder_frame_buffer_alloc(ni_buf_pool_t *p_pool, ni_frame_t *p_frame, int alloc_mem, int video_width, int video_height, int alignment, int factor, int is_planar)
Allocate memory for decoder frame buffer based on provided parameters; the memory is retrieved from a...
Definition: ni_device_api.c:2586
NI_LOG_ERROR
@ NI_LOG_ERROR
Definition: ni_log.h:60
ni_to_gc620_pix_fmt
int ni_to_gc620_pix_fmt(ni_pix_fmt_t pix_fmt)
Definition: ni_generic_utils.c:117
_ni_h264_sps_t::direct_8x8_inference_flag
int direct_8x8_inference_flag
Definition: ni_decode_utils.h:94
ScalingList::sl
uint8_t sl[4][6][64]
Definition: ni_decode_utils.h:225
_ni_h264_sps_t::full_range
int full_range
Definition: ni_decode_utils.h:104
_ni_h265_sps_t::min_tb_height
int min_tb_height
Definition: ni_decode_utils.h:325
NI_TEST_RETCODE_END_OF_STREAM
#define NI_TEST_RETCODE_END_OF_STREAM
Definition: ni_generic_utils.h:52
_ni_vp9_header_info::header_length
uint16_t header_length
Definition: ni_decode_utils.h:342
_ni_h265_sps_t::cabac_bypass_alignment_enabled_flag
int cabac_bypass_alignment_enabled_flag
coded frame dimension in various units
Definition: ni_decode_utils.h:314
_ni_vp9_header_info::timebase
struct _ni_vp9_header_info::@3 timebase
ni_defs.h
Common NETINT definitions used by all modules.
VUI::vui_num_units_in_tick
uint32_t vui_num_units_in_tick
Definition: ni_decode_utils.h:171
NI_COL_TRC_NB
@ NI_COL_TRC_NB
Definition: ni_av_codec.h:150
_ni_test_frame_list::tail
int tail
Definition: ni_generic_utils.h:106
_ni_demo_context::num_frames_received
uint64_t num_frames_received
Definition: ni_generic_utils.h:128
_ni_h264_sps_t
Definition: ni_decode_utils.h:70
_ni_h264_sps_t::time_scale
uint32_t time_scale
Definition: ni_decode_utils.h:111
_ni_h265_sps_t::high_precision_offsets_enabled_flag
int high_precision_offsets_enabled_flag
Definition: ni_decode_utils.h:312
_ni_decoder_input_params_t::hwframes
int hwframes
Definition: ni_device_api.h:2499
HEVC_MAX_REFS
#define HEVC_MAX_REFS
Definition: ni_generic_utils.h:73
VUI::min_spatial_segmentation_idc
int min_spatial_segmentation_idc
Definition: ni_decode_utils.h:181
MAX_LOG2_MAX_FRAME_NUM
#define MAX_LOG2_MAX_FRAME_NUM
Definition: ni_generic_utils.h:63
_ni_vp9_header_info::num
uint32_t num
Definition: ni_decode_utils.h:348
ShortTermRPS::rps_idx_num_delta_pocs
int rps_idx_num_delta_pocs
Definition: ni_decode_utils.h:233
h265_decode_profile_tier_level
int h265_decode_profile_tier_level(ni_bitstream_reader_t *br, PTLCommon *ptl)
Definition: ni_decode_utils.c:971
NI_TEST_RETCODE_EAGAIN
#define NI_TEST_RETCODE_EAGAIN
Definition: ni_generic_utils.h:53
_ni_h264_sps_t::mb_height
int mb_height
Definition: ni_decode_utils.h:91
decoder_open_session
int decoder_open_session(ni_session_context_t *p_dec_ctx, int iXcoderGUID, ni_xcoder_params_t *p_dec_params)
decoder session open
Definition: ni_decode_utils.c:2575
_ni_demo_context::dec_sos_sent
int dec_sos_sent
Definition: ni_generic_utils.h:142
NI_LOG_TRACE
@ NI_LOG_TRACE
Definition: ni_log.h:63
_ni_h265_sps_t::height
int height
Definition: ni_decode_utils.h:318
previous_time
struct timeval previous_time
Definition: ni_p2p_test.c: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
dec_recv_param::p_ovly_ctx
ni_session_context_t * p_ovly_ctx
Definition: ni_decode_utils.h:57
current_time
struct timeval current_time
Definition: ni_p2p_test.c:64
NI_TEST_RETCODE_FAILURE
#define NI_TEST_RETCODE_FAILURE
Definition: ni_generic_utils.h:50
_ni_session_context::prev_size
int prev_size
Definition: ni_device_api.h:1507
_ni_session_context::device_handle
ni_device_handle_t device_handle
Definition: ni_device_api.h:1462
h264_parse_sei
int h264_parse_sei(uint8_t *buf, int size_bytes, ni_h264_sps_t *sps, int *sei_type, int *is_interlaced)
Definition: ni_decode_utils.c:623
MIN_LOG2_MAX_FRAME_NUM
#define MIN_LOG2_MAX_FRAME_NUM
Definition: ni_generic_utils.h:64
_ni_scale_params::width
int width
Definition: ni_filter_utils.h:36
_ni_session_context::p_session_config
void * p_session_config
Definition: ni_device_api.h:1473
_ni_h265_sps_t::pic_conf_win
ni_h265_window_t pic_conf_win
Definition: ni_decode_utils.h:248
_ni_h265_sps_t::log2_min_pcm_cb_size
unsigned int log2_min_pcm_cb_size
Definition: ni_decode_utils.h:288
VUI::colour_description_present_flag
int colour_description_present_flag
Definition: ni_decode_utils.h:154
get_pixel_planar
ni_pixel_planar_format get_pixel_planar(ni_pix_fmt_t pix_fmt)
Definition: ni_generic_utils.c:147
_ni_session_context::dec_fme_buf_pool
ni_buf_pool_t * dec_fme_buf_pool
Definition: ni_device_api.h:1559
_ni_h265_sps_t::num_reorder_pics
int num_reorder_pics
Definition: ni_decode_utils.h:262
_ni_h264_sps_t::bit_depth_chroma
int bit_depth_chroma
bit_depth_chroma_minus8 + 8
Definition: ni_decode_utils.h:129
_ni_h265_sps_t::output_window
ni_h265_window_t output_window
Definition: ni_decode_utils.h:247
PTLCommon::tier_flag
uint8_t tier_flag
Definition: ni_decode_utils.h:191
_ni_h265_window_t::right_offset
unsigned int right_offset
Definition: ni_decode_utils.h:139
VUI::field_seq_flag
int field_seq_flag
Definition: ni_decode_utils.h:164
_ni_h265_sps_t::min_tb_width
int min_tb_width
Definition: ni_decode_utils.h:324
h264_parse_scaling_matrices
int h264_parse_scaling_matrices(ni_bitstream_reader_t *br, const ni_h264_sps_t *sps, uint8_t(*scaling_matrix4)[16], uint8_t(*scaling_matrix8)[64])
Definition: ni_decode_utils.c:374
_ni_packet::p_data
void * p_data
Definition: ni_device_api.h:2837
_ni_session_data_io
Definition: ni_device_api.h:2866
_ni_h265_sps_t
Definition: ni_decode_utils.h:241
dec_recv_param::drawbox_params
ni_drawbox_params_t * drawbox_params
Definition: ni_decode_utils.h:64
_ni_h264_sps_t::num_units_in_tick
uint32_t num_units_in_tick
Definition: ni_decode_utils.h:110
PTLCommon::one_picture_only_constraint_flag
uint8_t one_picture_only_constraint_flag
Definition: ni_decode_utils.h:205
_ni_rational::num
int num
Definition: ni_device_api.h:600
ni_bs_reader_bits_count
int ni_bs_reader_bits_count(ni_bitstream_reader_t *br)
return the number of bits already parsed in stream
Definition: ni_bitstream.c:338
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
PTLCommon::frame_only_constraint_flag
uint8_t frame_only_constraint_flag
Definition: ni_decode_utils.h:197
_ni_h265_sps_t::bit_depth
int bit_depth
Definition: ni_decode_utils.h:250
VUI::max_bits_per_min_cu_denom
int max_bits_per_min_cu_denom
Definition: ni_decode_utils.h:183
NI_TEST_RETCODE_SUCCESS
#define NI_TEST_RETCODE_SUCCESS
Definition: ni_generic_utils.h:51
_ni_h264_sps_t::width
int width
Definition: ni_decode_utils.h:72
_ni_h264_sps_t::scaling_matrix_present
int scaling_matrix_present
Definition: ni_decode_utils.h:117
decoder_send_thread
void * decoder_send_thread(void *args)
Definition: ni_decode_utils.c:2626
ShortTermRPS
Definition: ni_decode_utils.h:229
NI_COL_PRI_NB
@ NI_COL_PRI_NB
Not part of ABI.
Definition: ni_av_codec.h:116
PTLCommon::max_monochrome_constraint_flag
uint8_t max_monochrome_constraint_flag
Definition: ni_decode_utils.h:203
VUI::matrix_coeffs
uint8_t matrix_coeffs
Definition: ni_decode_utils.h:157
ni_usleep
void ni_usleep(int64_t usec)
Definition: ni_util.c:358
_ni_h264_sps_t::bit_depth_luma
int bit_depth_luma
bit_depth_luma_minus8 + 8
Definition: ni_decode_utils.h:128
GC620_I420
#define GC620_I420
Definition: ni_device_api.h:193
dec_send_param
Definition: ni_decode_utils.h:40
VUI::restricted_ref_pic_lists_flag
int restricted_ref_pic_lists_flag
Definition: ni_decode_utils.h:180
ni_packet_copy
int ni_packet_copy(void *p_destination, const void *const p_source, int cur_size, void *p_leftover, int *p_prev_size)
Copy video packet accounting for alignment.
Definition: ni_device_api.c:3936
_ni_h265_sps_t::sao_enabled
uint8_t sao_enabled
Definition: ni_decode_utils.h:277
_ni_vp9_header_info::profile
int profile
Definition: ni_decode_utils.h:341
ShortTermRPS::used
uint8_t used[32]
Definition: ni_decode_utils.h:235
ni_dec_retrieve_aux_data
void ni_dec_retrieve_aux_data(ni_frame_t *frame)
Retrieve auxiliary data (close caption, various SEI) associated with this frame that is returned by d...
Definition: ni_av_codec.c:237
decoder_send_data
int decoder_send_data(ni_demo_context_t *p_ctx, ni_session_context_t *p_dec_ctx, ni_session_data_io_t *p_in_data, int input_video_width, int input_video_height, void *stream_info)
Send decoder input data.
Definition: ni_decode_utils.c:2228
_ni_packet::video_height
uint32_t video_height
Definition: ni_device_api.h:2834
_ni_h264_sps_t::bitstream_restriction_flag
int bitstream_restriction_flag
Definition: ni_decode_utils.h:114
_ni_h264_sps_t::residual_color_transform_flag
int residual_color_transform_flag
residual_colour_transform_flag
Definition: ni_decode_utils.h:130
VUI::def_disp_win
ni_h265_window_t def_disp_win
Definition: ni_decode_utils.h:168
dec_recv_param::input_height
int input_height
Definition: ni_decode_utils.h:61
PTLCommon::max_12bit_constraint_flag
uint8_t max_12bit_constraint_flag
Definition: ni_decode_utils.h:198
_ni_test_frame_list::frames
ni_session_data_io_t frames[NI_MAX_BUFFERED_FRAME]
Definition: ni_generic_utils.h:104
_ni_h265_sps_t::log2_max_pcm_cb_size
unsigned int log2_max_pcm_cb_size
Definition: ni_decode_utils.h:289
_ni_h265_window_t::top_offset
unsigned int top_offset
Definition: ni_decode_utils.h:140
_ni_h265_sps_t::lt_ref_pic_poc_lsb_sps
uint16_t lt_ref_pic_poc_lsb_sps[HEVC_MAX_LONG_TERM_REF_PICS]
Definition: ni_decode_utils.h:280
_ni_packet
Definition: ni_device_api.h:2825
ScalingList::sl_dc
uint8_t sl_dc[2][6]
Definition: ni_decode_utils.h:226
_ni_h265_sps_t::transform_skip_context_enabled_flag
int transform_skip_context_enabled_flag
Definition: ni_decode_utils.h:307
_ni_session_context::frame_num
uint64_t frame_num
Definition: ni_device_api.h:1531
h264_parse_scaling_list
int h264_parse_scaling_list(ni_bitstream_reader_t *br, uint8_t *factors, int size, const uint8_t *jvt_list, const uint8_t *fallback_list)
Definition: ni_decode_utils.c:338
dec_send_param::input_height
int input_height
Definition: ni_decode_utils.h:45
_ni_h265_sps_t::separate_colour_plane_flag
uint8_t separate_colour_plane_flag
Definition: ni_decode_utils.h:245
_ni_h265_sps_t::extended_precision_processing_flag
int extended_precision_processing_flag
Definition: ni_decode_utils.h:310
_ni_h265_sps_t::log2_min_tb_size
unsigned int log2_min_tb_size
Definition: ni_decode_utils.h:297
_ni_h265_sps_t::max_sub_layers
int max_sub_layers
Definition: ni_decode_utils.h:258
_ni_frame::p_data
uint8_t * p_data[NI_MAX_NUM_DATA_POINTERS]
Definition: ni_device_api.h:2663
_ni_h264_sps_t::timing_info_present_flag
int timing_info_present_flag
Definition: ni_decode_utils.h:109
NI_CODEC_FORMAT_VP9
@ NI_CODEC_FORMAT_VP9
Definition: ni_device_api.h:913
_ni_vp9_header_info::height
uint16_t height
Definition: ni_decode_utils.h:344
H264_NAL_SEI
@ H264_NAL_SEI
Definition: ni_decode_utils.h:361
h265_parse_ptl
int h265_parse_ptl(ni_bitstream_reader_t *br, PTL *ptl, int max_num_sub_layers)
Definition: ni_decode_utils.c:1041
_ni_h264_sps_t::color_primaries
ni_color_primaries_t color_primaries
Definition: ni_decode_utils.h:106
_ni_vp9_header_info::width
uint16_t width
Definition: ni_decode_utils.h:343
NI_COL_TRC_UNSPECIFIED
@ NI_COL_TRC_UNSPECIFIED
Definition: ni_av_codec.h:125
PTLCommon::max_14bit_constraint_flag
uint8_t max_14bit_constraint_flag
Definition: ni_decode_utils.h:207
VUI::vui_num_ticks_poc_diff_one_minus1
int vui_num_ticks_poc_diff_one_minus1
Definition: ni_decode_utils.h:174
_ni_session_context::hw_action
int hw_action
Definition: ni_device_api.h:1609
_ni_scale_params
Definition: ni_filter_utils.h:33
_ni_session_context
Definition: ni_device_api.h:1408
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
VUI::max_bytes_per_pic_denom
int max_bytes_per_pic_denom
Definition: ni_decode_utils.h:182
_ni_h264_sps_t::transform_bypass
int transform_bypass
qpprime_y_zero_transform_bypass_flag
Definition: ni_decode_utils.h:79
_ni_session_data_io::data
union _ni_session_data_io::@19 data
_ni_frame
Definition: ni_device_api.h:2601
VUI::sar
ni_rational_t sar
Definition: ni_decode_utils.h:146
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
find_h264_next_nalu
uint64_t find_h264_next_nalu(ni_demo_context_t *p_ctx, uint8_t *p_dst, int *nal_type)
Definition: ni_decode_utils.c:104
_ni_h265_sps_t::pcm_enabled_flag
int pcm_enabled_flag
Definition: ni_decode_utils.h:256
_ni_h264_sps_t::crop_left
unsigned int crop_left
frame_cropping_rect_left_offset
Definition: ni_decode_utils.h:97
_ni_h265_sps_t::vps_id
unsigned vps_id
Definition: ni_decode_utils.h:243
hwdl_frame
int hwdl_frame(ni_session_context_t *p_ctx, ni_session_data_io_t *p_session_data, ni_frame_t *p_src_frame, int output_format)
Download hw frames by HwDesc.
Definition: ni_generic_utils.c:709
_ni_demo_context::start_time
uint64_t start_time
Definition: ni_generic_utils.h:132
_ni_xcoder_params
Definition: ni_device_api.h:2713
VUI::tiles_fixed_structure_flag
int tiles_fixed_structure_flag
Definition: ni_decode_utils.h:178
_ni_h264_sps_t::sps_id
unsigned int sps_id
Definition: ni_decode_utils.h:75
PTLCommon::max_10bit_constraint_flag
uint8_t max_10bit_constraint_flag
Definition: ni_decode_utils.h:199
VUI::log2_max_mv_length_horizontal
int log2_max_mv_length_horizontal
Definition: ni_decode_utils.h:184
PTL
Definition: ni_decode_utils.h:212
NI_COL_PRI_UNSPECIFIED
@ NI_COL_PRI_UNSPECIFIED
Definition: ni_av_codec.h:99
_ni_h265_sps_t::explicit_rdpcm_enabled_flag
int explicit_rdpcm_enabled_flag
Definition: ni_decode_utils.h:309
_ni_session_context::ready_to_close
uint32_t ready_to_close
Definition: ni_device_api.h:1544
_ni_demo_context::file_cache
uint8_t * file_cache
Definition: ni_generic_utils.h:114
_ni_demo_context::dec_total_bytes_sent
uint64_t dec_total_bytes_sent
Definition: ni_generic_utils.h:124
dec_send_param::p_ctx
ni_demo_context_t * p_ctx
Definition: ni_decode_utils.h:42
_ni_h265_sps_t::qp_bd_offset
int qp_bd_offset
Definition: ni_decode_utils.h:333
_ni_session_context::codec_format
uint32_t codec_format
Definition: ni_device_api.h:1486
_ni_h264_sps_t::profile_idc
int profile_idc
Definition: ni_decode_utils.h:76
PTL::sub_layer_ptl
PTLCommon sub_layer_ptl[HEVC_MAX_SUB_LAYERS]
Definition: ni_decode_utils.h:215
scale_filter
int scale_filter(ni_session_context_t *p_ctx, ni_frame_t *p_frame_in, ni_session_data_io_t *p_data_out, int iXcoderGUID, int scale_width, int scale_height, int in_format, int out_format)
Do a scale and/or format-change operation.
Definition: ni_filter_utils.c:513
PTLCommon::non_packed_constraint_flag
uint8_t non_packed_constraint_flag
Definition: ni_decode_utils.h:196
_ni_demo_context::dec_total_bytes_received
uint64_t dec_total_bytes_received
Definition: ni_generic_utils.h:125
VUI::overscan_info_present_flag
int overscan_info_present_flag
Definition: ni_decode_utils.h:148
_ni_h264_sps_t::mb_aff
int mb_aff
mb_adaptive_frame_field_flag
Definition: ni_decode_utils.h:93
_ni_h265_sps_t::hshift
int hshift[3]
Definition: ni_decode_utils.h:330
NI_COL_SPC_RGB
@ NI_COL_SPC_RGB
Definition: ni_av_codec.h:157
_ni_session_data_io::frame
ni_frame_t frame
Definition: ni_device_api.h:2870
_ni_h265_sps_t::log2_min_cb_size
unsigned int log2_min_cb_size
Definition: ni_decode_utils.h:295
_ni_h265_sps_t::log2_min_pu_size
unsigned int log2_min_pu_size
Definition: ni_decode_utils.h:300
_ni_h265_sps_t::sps_range_extension_flag
int sps_range_extension_flag
Definition: ni_decode_utils.h:305
PTLCommon
Definition: ni_decode_utils.h:188
check_profile_idc
#define check_profile_idc(idc)
parse_h264_slice_header
int parse_h264_slice_header(uint8_t *buf, int size_bytes, ni_h264_sps_t *sps, int32_t *frame_num, unsigned int *first_mb_in_slice)
Definition: ni_decode_utils.c:831
VUI::bitstream_restriction_flag
int bitstream_restriction_flag
Definition: ni_decode_utils.h:177
_ni_h264_sps_t::scaling_matrix4
uint8_t scaling_matrix4[6][16]
Definition: ni_decode_utils.h:118
_ni_h265_sps_t::log2_ctb_size
unsigned int log2_ctb_size
Definition: ni_decode_utils.h:299
_ni_h264_sps_t::delta_pic_order_always_zero_flag
int delta_pic_order_always_zero_flag
Definition: ni_decode_utils.h:83
HEVC_MAX_SHORT_TERM_REF_PIC_SETS
#define HEVC_MAX_SHORT_TERM_REF_PIC_SETS
Definition: ni_generic_utils.h:70
NI_H264_SEI_TYPE_PIC_TIMING
@ NI_H264_SEI_TYPE_PIC_TIMING
Definition: ni_av_codec.h:54
h265_decode_short_term_rps
int h265_decode_short_term_rps(ni_bitstream_reader_t *br, ShortTermRPS *rps, const ni_h265_sps_t *sps, int is_slice_header)
Definition: ni_decode_utils.c:1277
_ni_h265_sps_t::num_long_term_ref_pics_sps
uint8_t num_long_term_ref_pics_sps
Definition: ni_decode_utils.h:282
_ni_h265_sps_t::ctb_width
int ctb_width
Definition: ni_decode_utils.h:319
_ni_session_context::active_video_height
uint32_t active_video_height
Definition: ni_device_api.h:1551
NI_COL_TRC_RESERVED0
@ NI_COL_TRC_RESERVED0
Definition: ni_av_codec.h:123
NI_COL_SPC_UNSPECIFIED
@ NI_COL_SPC_UNSPECIFIED
Definition: ni_av_codec.h:161
VUI::neutra_chroma_indication_flag
int neutra_chroma_indication_flag
Definition: ni_decode_utils.h:162
_ni_h264_sps_t::nal_hrd_parameters_present_flag
int nal_hrd_parameters_present_flag
Definition: ni_decode_utils.h:120
ShortTermRPS::num_negative_pics
unsigned int num_negative_pics
Definition: ni_decode_utils.h:231
_ni_scale_params::enabled
int enabled
Definition: ni_filter_utils.h:35
ScalingList
Definition: ni_decode_utils.h:221
_ni_drawbox_params::enabled
int enabled
Definition: ni_filter_utils.h:43
NI_COL_PRI_RESERVED0
@ NI_COL_PRI_RESERVED0
Definition: ni_av_codec.h:96
ni_bitstream_reader_init
void ni_bitstream_reader_init(ni_bitstream_reader_t *br, const uint8_t *data, int bit_size)
init a bitstream reader Note: bitstream_reader takes reading ownership of the data
Definition: ni_bitstream.c:317
_ni_h265_sps_t::vshift
int vshift[3]
Definition: ni_decode_utils.h:331
h265_decode_vui
int h265_decode_vui(ni_bitstream_reader_t *br, int apply_defdispwin, ni_h265_sps_t *sps)
Definition: ni_decode_utils.c:1440
ni_bs_reader_get_se
int32_t ni_bs_reader_get_se(ni_bitstream_reader_t *br)
read a signed Exp-Golomb code se(v)
Definition: ni_bitstream.c:546
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_h265_sps_t::used_by_curr_pic_lt_sps_flag
uint8_t used_by_curr_pic_lt_sps_flag[HEVC_MAX_LONG_TERM_REF_PICS]
Definition: ni_decode_utils.h:281
find_h265_next_nalu
uint64_t find_h265_next_nalu(ni_demo_context_t *p_ctx, uint8_t *p_dst, int *nal_type)
Definition: ni_decode_utils.c:883
_ni_h264_sps_t::level_idc
int level_idc
Definition: ni_decode_utils.h:77
_ni_h264_sps_t::video_signal_type_present_flag
int video_signal_type_present_flag
Definition: ni_decode_utils.h:103
_ni_vp9_header_info::total_frames
uint32_t total_frames
Definition: ni_decode_utils.h:350
PTLCommon::level_idc
uint8_t level_idc
Definition: ni_decode_utils.h:209
NI_RETCODE_FAILURE
@ NI_RETCODE_FAILURE
Definition: ni_defs.h:428
_ni_h264_sps_t::poc_type
int poc_type
pic_order_cnt_type
Definition: ni_decode_utils.h:81
dec_recv_param::p_ctx
ni_demo_context_t * p_ctx
Definition: ni_decode_utils.h:52
_ni_rational::den
int den
Definition: ni_device_api.h:601
_ni_h264_sps_t::offset_for_non_ref_pic
int offset_for_non_ref_pic
Definition: ni_decode_utils.h:84
_ni_h265_sps_t::persistent_rice_adaptation_enabled_flag
int persistent_rice_adaptation_enabled_flag
Definition: ni_decode_utils.h:313
PTLCommon::intra_constraint_flag
uint8_t intra_constraint_flag
Definition: ni_decode_utils.h:204
_ni_h265_sps_t::ctb_size
int ctb_size
Definition: ni_decode_utils.h:321
_ni_h265_sps_t::nb_st_rps
unsigned int nb_st_rps
Definition: ni_decode_utils.h:273
_ni_h265_sps_t::pcm
struct _ni_h265_sps_t::@2 pcm
ni_util.h
Utility definitions.
h265_set_default_scaling_list_data
void h265_set_default_scaling_list_data(ScalingList *sl)
Definition: ni_decode_utils.c:1159
_ni_h265_sps_t::log2_max_poc_lsb
unsigned int log2_max_poc_lsb
Definition: ni_decode_utils.h:255
dec_recv_param::p_pad_ctx
ni_session_context_t * p_pad_ctx
Definition: ni_decode_utils.h:56
_ni_h264_sps_t::crop_right
unsigned int crop_right
frame_cropping_rect_right_offset
Definition: ni_decode_utils.h:98
ni_device_session_read_hwdesc
int ni_device_session_read_hwdesc(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 hwdesc from decoder If ...
Definition: ni_device_api.c:8043
ni_remove_emulation_prevent_bytes
int ni_remove_emulation_prevent_bytes(uint8_t *buf, int size)
Remove emulation prevention byte(s) as needed from the data buffer.
Definition: ni_util.c:2644
_ni_h265_sps_t::log2_max_trafo_size
unsigned int log2_max_trafo_size
Definition: ni_decode_utils.h:298
dec_recv_param::p_dec_ctx
ni_session_context_t * p_dec_ctx
Definition: ni_decode_utils.h:53
_ni_h264_sps_t::chroma_format_idc
int chroma_format_idc
Definition: ni_decode_utils.h:78
_ni_h265_sps_t::chroma_format_idc
int chroma_format_idc
Definition: ni_decode_utils.h:244
rewind_data_buf_pos_by
void rewind_data_buf_pos_by(ni_demo_context_t *p_ctx, uint64_t nb_bytes)
Definition: ni_generic_utils.c:322
dec_send_param::p_dec_ctx
ni_session_context_t * p_dec_ctx
Definition: ni_decode_utils.h:43
dec_recv_param::p_crop_ctx
ni_session_context_t * p_crop_ctx
Definition: ni_decode_utils.h:55
h265_scaling_list_data
int h265_scaling_list_data(ni_bitstream_reader_t *br, ScalingList *sl, ni_h265_sps_t *sps)
Definition: ni_decode_utils.c:1190
_ni_h264_sps_t::max_dec_frame_buffering
unsigned int max_dec_frame_buffering
Definition: ni_decode_utils.h:116
_ni_h264_sps_t::frame_mbs_only_flag
int frame_mbs_only_flag
Definition: ni_decode_utils.h:92
_ni_h264_sps_t::cpb_cnt
int cpb_cnt
See H.264 E.1.2.
Definition: ni_decode_utils.h:124
dec_recv_param
Definition: ni_decode_utils.h:50
VUI::vui_time_scale
uint32_t vui_time_scale
Definition: ni_decode_utils.h:172
_ni_packet::video_width
uint32_t video_width
Definition: ni_device_api.h:2833
ni_bs_reader_skip_bits
void ni_bs_reader_skip_bits(ni_bitstream_reader_t *br, int n)
skip a number of bits ahead in the bitstream reader
Definition: ni_bitstream.c:361
_ni_bitstream_reader_t
Definition: ni_bitstream.h:119
NI_CODEC_FORMAT_H264
@ NI_CODEC_FORMAT_H264
Definition: ni_device_api.h:911
_ni_h264_sps_t::height
int height
Definition: ni_decode_utils.h:73
_ni_h264_sps_t::pic_struct_present_flag
int pic_struct_present_flag
Definition: ni_decode_utils.h:122
NI_FRAME_LITTLE_ENDIAN
#define NI_FRAME_LITTLE_ENDIAN
Definition: ni_device_api.h:108
_ni_h265_sps_t::ptl
PTL ptl
Definition: ni_decode_utils.h:268
H264_NAL_SLICE
@ H264_NAL_SLICE
Definition: ni_decode_utils.h:356
ni_bs_reader_get_bits_left
int ni_bs_reader_get_bits_left(ni_bitstream_reader_t *br)
return the number of bits left to parse in stream
Definition: ni_bitstream.c:349
ShortTermRPS::num_delta_pocs
int num_delta_pocs
Definition: ni_decode_utils.h:232
_ni_h265_sps_t::scaling_list
ScalingList scaling_list
Definition: ni_decode_utils.h:271
_ni_h264_sps_t::sar
ni_rational_t sar
Definition: ni_decode_utils.h:102
_ni_session_context::pixel_format
int pixel_format
Definition: ni_device_api.h:1616
_ni_h264_sps_t::mb_width
int mb_width
(pic_height_in_map_units_minus1 + 1) * (2 - frame_mbs_only_flag)
Definition: ni_decode_utils.h:89
_ni_h265_sps_t::vui
VUI vui
Definition: ni_decode_utils.h:267
NI_LOG_DEBUG
@ NI_LOG_DEBUG
Definition: ni_log.h:62
VUI
Definition: ni_decode_utils.h:144
_ni_xcoder_params::dec_input_params
ni_decoder_input_params_t dec_input_params
Definition: ni_device_api.h:2766
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
PTLCommon::profile_compatibility_flag
uint8_t profile_compatibility_flag[32]
Definition: ni_decode_utils.h:193
NI_PIXEL_PLANAR_FORMAT_PLANAR
@ NI_PIXEL_PLANAR_FORMAT_PLANAR
Definition: ni_device_api.h:921
_ni_h265_sps_t::sps_temporal_mvp_enabled_flag
uint8_t sps_temporal_mvp_enabled_flag
Definition: ni_decode_utils.h:292
PTLCommon::lower_bit_rate_constraint_flag
uint8_t lower_bit_rate_constraint_flag
Definition: ni_decode_utils.h:206
_ni_h264_sps_t::ref_frame_count
int ref_frame_count
num_ref_frames
Definition: ni_decode_utils.h:87
_ni_drawbox_params
Definition: ni_filter_utils.h:41
_ni_h264_sps_t::colorspace
ni_color_space_t colorspace
Definition: ni_decode_utils.h:108
_ni_h264_sps_t::poc_cycle_length
int poc_cycle_length
num_ref_frames_in_pic_order_cnt_cycle
Definition: ni_decode_utils.h:86
_ni_h265_sps_t::scaling_list_enable_flag
uint8_t scaling_list_enable_flag
Definition: ni_decode_utils.h:270
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
PTLCommon::max_8bit_constraint_flag
uint8_t max_8bit_constraint_flag
Definition: ni_decode_utils.h:200
_ni_h265_sps_t::temporal_layer
struct _ni_h265_sps_t::@1 temporal_layer[HEVC_MAX_SUB_LAYERS]
_ni_h264_sps_t::constraint_set_flags
int constraint_set_flags
constraint_set[0-3]_flag
Definition: ni_decode_utils.h:131
VUI::transfer_characteristic
uint8_t transfer_characteristic
Definition: ni_decode_utils.h:156
_ni_h265_sps_t::max_transform_hierarchy_depth_inter
int max_transform_hierarchy_depth_inter
Definition: ni_decode_utils.h:302
NI_CODEC_FORMAT_H265
@ NI_CODEC_FORMAT_H265
Definition: ni_device_api.h:912
_ni_h265_window_t
Definition: ni_decode_utils.h:136
_ni_h264_sps_t::num_reorder_frames
int num_reorder_frames
Definition: ni_decode_utils.h:115
HEVC_MAX_LONG_TERM_REF_PICS
#define HEVC_MAX_LONG_TERM_REF_PICS
Definition: ni_generic_utils.h:71
_ni_packet::data_len
uint32_t data_len
Definition: ni_device_api.h:2838