FFmpeg
hevc.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Tim Walker <tdskywalker@gmail.com>
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "libavcodec/get_bits.h"
22 #include "libavcodec/golomb.h"
23 #include "libavcodec/hevc/hevc.h"
24 #include "libavutil/intreadwrite.h"
25 #include "libavutil/mem.h"
26 #include "avc.h"
27 #include "avio.h"
28 #include "avio_internal.h"
29 #include "hevc.h"
30 #include "nal.h"
31 
32 #define MAX_SPATIAL_SEGMENTATION 4096 // max. value of u(12) field
33 
34 enum {
41 };
42 
43 
44 #define FLAG_ARRAY_COMPLETENESS (1 << 0)
45 #define FLAG_IS_NALFF (1 << 1)
46 #define FLAG_IS_LHVC (1 << 2)
47 
48 typedef struct HVCCNALUnit {
49  uint8_t nuh_layer_id;
51  uint16_t nalUnitLength;
52  const uint8_t *nalUnit;
53 
54  // VPS
56 } HVCCNALUnit;
57 
58 typedef struct HVCCNALUnitArray {
60  uint8_t NAL_unit_type;
61  uint16_t numNalus;
64 
74  uint8_t parallelismType;
75  uint8_t chromaFormat;
78  uint16_t avgFrameRate;
83  uint8_t numOfArrays;
86 
87 typedef struct HVCCProfileTierLevel {
88  uint8_t profile_space;
89  uint8_t tier_flag;
90  uint8_t profile_idc;
93  uint8_t level_idc;
95 
98 {
99  /*
100  * The value of general_profile_space in all the parameter sets must be
101  * identical.
102  */
103  hvcc->general_profile_space = ptl->profile_space;
104 
105  /*
106  * The level indication general_level_idc must indicate a level of
107  * capability equal to or greater than the highest level indicated for the
108  * highest tier in all the parameter sets.
109  */
110  if (hvcc->general_tier_flag < ptl->tier_flag)
111  hvcc->general_level_idc = ptl->level_idc;
112  else
113  hvcc->general_level_idc = FFMAX(hvcc->general_level_idc, ptl->level_idc);
114 
115  /*
116  * The tier indication general_tier_flag must indicate a tier equal to or
117  * greater than the highest tier indicated in all the parameter sets.
118  */
119  hvcc->general_tier_flag = FFMAX(hvcc->general_tier_flag, ptl->tier_flag);
120 
121  /*
122  * The profile indication general_profile_idc must indicate a profile to
123  * which the stream associated with this configuration record conforms.
124  *
125  * If the sequence parameter sets are marked with different profiles, then
126  * the stream may need examination to determine which profile, if any, the
127  * entire stream conforms to. If the entire stream is not examined, or the
128  * examination reveals that there is no profile to which the entire stream
129  * conforms, then the entire stream must be split into two or more
130  * sub-streams with separate configuration records in which these rules can
131  * be met.
132  *
133  * Note: set the profile to the highest value for the sake of simplicity.
134  */
135  hvcc->general_profile_idc = FFMAX(hvcc->general_profile_idc, ptl->profile_idc);
136 
137  /*
138  * Each bit in general_profile_compatibility_flags may only be set if all
139  * the parameter sets set that bit.
140  */
141  hvcc->general_profile_compatibility_flags &= ptl->profile_compatibility_flags;
142 
143  /*
144  * Each bit in general_constraint_indicator_flags may only be set if all
145  * the parameter sets set that bit.
146  */
147  hvcc->general_constraint_indicator_flags &= ptl->constraint_indicator_flags;
148 }
149 
152  unsigned int max_sub_layers_minus1)
153 {
154  unsigned int i;
155  HVCCProfileTierLevel general_ptl;
156  uint8_t sub_layer_profile_present_flag[HEVC_MAX_SUB_LAYERS];
157  uint8_t sub_layer_level_present_flag[HEVC_MAX_SUB_LAYERS];
158 
159  general_ptl.profile_space = get_bits(gb, 2);
160  general_ptl.tier_flag = get_bits1(gb);
161  general_ptl.profile_idc = get_bits(gb, 5);
162  general_ptl.profile_compatibility_flags = get_bits_long(gb, 32);
163  general_ptl.constraint_indicator_flags = get_bits64(gb, 48);
164  general_ptl.level_idc = get_bits(gb, 8);
165  hvcc_update_ptl(hvcc, &general_ptl);
166 
167  for (i = 0; i < max_sub_layers_minus1; i++) {
168  sub_layer_profile_present_flag[i] = get_bits1(gb);
169  sub_layer_level_present_flag[i] = get_bits1(gb);
170  }
171 
172  if (max_sub_layers_minus1 > 0)
173  for (i = max_sub_layers_minus1; i < 8; i++)
174  skip_bits(gb, 2); // reserved_zero_2bits[i]
175 
176  for (i = 0; i < max_sub_layers_minus1; i++) {
177  if (sub_layer_profile_present_flag[i]) {
178  /*
179  * sub_layer_profile_space[i] u(2)
180  * sub_layer_tier_flag[i] u(1)
181  * sub_layer_profile_idc[i] u(5)
182  * sub_layer_profile_compatibility_flag[i][0..31] u(32)
183  * sub_layer_progressive_source_flag[i] u(1)
184  * sub_layer_interlaced_source_flag[i] u(1)
185  * sub_layer_non_packed_constraint_flag[i] u(1)
186  * sub_layer_frame_only_constraint_flag[i] u(1)
187  * sub_layer_reserved_zero_44bits[i] u(44)
188  */
189  skip_bits_long(gb, 32);
190  skip_bits_long(gb, 32);
191  skip_bits (gb, 24);
192  }
193 
194  if (sub_layer_level_present_flag[i])
195  skip_bits(gb, 8);
196  }
197 }
198 
200  unsigned int cpb_cnt_minus1,
201  uint8_t sub_pic_hrd_params_present_flag)
202 {
203  unsigned int i;
204 
205  for (i = 0; i <= cpb_cnt_minus1; i++) {
206  get_ue_golomb_long(gb); // bit_rate_value_minus1
207  get_ue_golomb_long(gb); // cpb_size_value_minus1
208 
209  if (sub_pic_hrd_params_present_flag) {
210  get_ue_golomb_long(gb); // cpb_size_du_value_minus1
211  get_ue_golomb_long(gb); // bit_rate_du_value_minus1
212  }
213 
214  skip_bits1(gb); // cbr_flag
215  }
216 }
217 
218 static int skip_hrd_parameters(GetBitContext *gb, uint8_t cprms_present_flag,
219  unsigned int max_sub_layers_minus1)
220 {
221  unsigned int i;
222  uint8_t sub_pic_hrd_params_present_flag = 0;
223  uint8_t nal_hrd_parameters_present_flag = 0;
224  uint8_t vcl_hrd_parameters_present_flag = 0;
225 
226  if (cprms_present_flag) {
227  nal_hrd_parameters_present_flag = get_bits1(gb);
228  vcl_hrd_parameters_present_flag = get_bits1(gb);
229 
230  if (nal_hrd_parameters_present_flag ||
231  vcl_hrd_parameters_present_flag) {
232  sub_pic_hrd_params_present_flag = get_bits1(gb);
233 
234  if (sub_pic_hrd_params_present_flag)
235  /*
236  * tick_divisor_minus2 u(8)
237  * du_cpb_removal_delay_increment_length_minus1 u(5)
238  * sub_pic_cpb_params_in_pic_timing_sei_flag u(1)
239  * dpb_output_delay_du_length_minus1 u(5)
240  */
241  skip_bits(gb, 19);
242 
243  /*
244  * bit_rate_scale u(4)
245  * cpb_size_scale u(4)
246  */
247  skip_bits(gb, 8);
248 
249  if (sub_pic_hrd_params_present_flag)
250  skip_bits(gb, 4); // cpb_size_du_scale
251 
252  /*
253  * initial_cpb_removal_delay_length_minus1 u(5)
254  * au_cpb_removal_delay_length_minus1 u(5)
255  * dpb_output_delay_length_minus1 u(5)
256  */
257  skip_bits(gb, 15);
258  }
259  }
260 
261  for (i = 0; i <= max_sub_layers_minus1; i++) {
262  unsigned int cpb_cnt_minus1 = 0;
263  uint8_t low_delay_hrd_flag = 0;
264  uint8_t fixed_pic_rate_within_cvs_flag = 0;
265  uint8_t fixed_pic_rate_general_flag = get_bits1(gb);
266 
267  if (!fixed_pic_rate_general_flag)
268  fixed_pic_rate_within_cvs_flag = get_bits1(gb);
269 
270  if (fixed_pic_rate_within_cvs_flag)
271  get_ue_golomb_long(gb); // elemental_duration_in_tc_minus1
272  else
273  low_delay_hrd_flag = get_bits1(gb);
274 
275  if (!low_delay_hrd_flag) {
276  cpb_cnt_minus1 = get_ue_golomb_long(gb);
277  if (cpb_cnt_minus1 > 31)
278  return AVERROR_INVALIDDATA;
279  }
280 
281  if (nal_hrd_parameters_present_flag)
282  skip_sub_layer_hrd_parameters(gb, cpb_cnt_minus1,
283  sub_pic_hrd_params_present_flag);
284 
285  if (vcl_hrd_parameters_present_flag)
286  skip_sub_layer_hrd_parameters(gb, cpb_cnt_minus1,
287  sub_pic_hrd_params_present_flag);
288  }
289 
290  return 0;
291 }
292 
294 {
295  skip_bits_long(gb, 32); // num_units_in_tick
296  skip_bits_long(gb, 32); // time_scale
297 
298  if (get_bits1(gb)) // poc_proportional_to_timing_flag
299  get_ue_golomb_long(gb); // num_ticks_poc_diff_one_minus1
300 }
301 
304  unsigned int max_sub_layers_minus1)
305 {
306  unsigned int min_spatial_segmentation_idc;
307 
308  if (get_bits1(gb)) // aspect_ratio_info_present_flag
309  if (get_bits(gb, 8) == 255) // aspect_ratio_idc
310  skip_bits_long(gb, 32); // sar_width u(16), sar_height u(16)
311 
312  if (get_bits1(gb)) // overscan_info_present_flag
313  skip_bits1(gb); // overscan_appropriate_flag
314 
315  if (get_bits1(gb)) { // video_signal_type_present_flag
316  skip_bits(gb, 4); // video_format u(3), video_full_range_flag u(1)
317 
318  if (get_bits1(gb)) // colour_description_present_flag
319  /*
320  * colour_primaries u(8)
321  * transfer_characteristics u(8)
322  * matrix_coeffs u(8)
323  */
324  skip_bits(gb, 24);
325  }
326 
327  if (get_bits1(gb)) { // chroma_loc_info_present_flag
328  get_ue_golomb_long(gb); // chroma_sample_loc_type_top_field
329  get_ue_golomb_long(gb); // chroma_sample_loc_type_bottom_field
330  }
331 
332  /*
333  * neutral_chroma_indication_flag u(1)
334  * field_seq_flag u(1)
335  * frame_field_info_present_flag u(1)
336  */
337  skip_bits(gb, 3);
338 
339  if (get_bits1(gb)) { // default_display_window_flag
340  get_ue_golomb_long(gb); // def_disp_win_left_offset
341  get_ue_golomb_long(gb); // def_disp_win_right_offset
342  get_ue_golomb_long(gb); // def_disp_win_top_offset
343  get_ue_golomb_long(gb); // def_disp_win_bottom_offset
344  }
345 
346  if (get_bits1(gb)) { // vui_timing_info_present_flag
347  skip_timing_info(gb);
348 
349  if (get_bits1(gb)) // vui_hrd_parameters_present_flag
350  skip_hrd_parameters(gb, 1, max_sub_layers_minus1);
351  }
352 
353  if (get_bits1(gb)) { // bitstream_restriction_flag
354  /*
355  * tiles_fixed_structure_flag u(1)
356  * motion_vectors_over_pic_boundaries_flag u(1)
357  * restricted_ref_pic_lists_flag u(1)
358  */
359  skip_bits(gb, 3);
360 
361  min_spatial_segmentation_idc = get_ue_golomb_long(gb);
362 
363  /*
364  * unsigned int(12) min_spatial_segmentation_idc;
365  *
366  * The min_spatial_segmentation_idc indication must indicate a level of
367  * spatial segmentation equal to or less than the lowest level of
368  * spatial segmentation indicated in all the parameter sets.
369  */
371  min_spatial_segmentation_idc);
372 
373  get_ue_golomb_long(gb); // max_bytes_per_pic_denom
374  get_ue_golomb_long(gb); // max_bits_per_min_cu_denom
375  get_ue_golomb_long(gb); // log2_max_mv_length_horizontal
376  get_ue_golomb_long(gb); // log2_max_mv_length_vertical
377  }
378 }
379 
381 {
382  get_ue_golomb_long(gb); // max_dec_pic_buffering_minus1
383  get_ue_golomb_long(gb); // max_num_reorder_pics
384  get_ue_golomb_long(gb); // max_latency_increase_plus1
385 }
386 
389 {
390  nal->parameter_set_id = get_bits(gb, 4);
391  /*
392  * vps_reserved_three_2bits u(2)
393  * vps_max_layers_minus1 u(6)
394  */
395  skip_bits(gb, 8);
396 
397  nal->vps_max_sub_layers_minus1 = get_bits(gb, 3);
398 
399  /*
400  * numTemporalLayers greater than 1 indicates that the stream to which this
401  * configuration record applies is temporally scalable and the contained
402  * number of temporal layers (also referred to as temporal sub-layer or
403  * sub-layer in ISO/IEC 23008-2) is equal to numTemporalLayers. Value 1
404  * indicates that the stream is not temporally scalable. Value 0 indicates
405  * that it is unknown whether the stream is temporally scalable.
406  */
408  nal->vps_max_sub_layers_minus1 + 1);
409 
410  /*
411  * vps_temporal_id_nesting_flag u(1)
412  * vps_reserved_0xffff_16bits u(16)
413  */
414  skip_bits(gb, 17);
415 
417 
418  /* nothing useful for hvcC past this point */
419  return 0;
420 }
421 
423 {
424  int i, j, k, num_coeffs;
425 
426  for (i = 0; i < 4; i++)
427  for (j = 0; j < (i == 3 ? 2 : 6); j++)
428  if (!get_bits1(gb)) // scaling_list_pred_mode_flag[i][j]
429  get_ue_golomb_long(gb); // scaling_list_pred_matrix_id_delta[i][j]
430  else {
431  num_coeffs = FFMIN(64, 1 << (4 + (i << 1)));
432 
433  if (i > 1)
434  get_se_golomb_long(gb); // scaling_list_dc_coef_minus8[i-2][j]
435 
436  for (k = 0; k < num_coeffs; k++)
437  get_se_golomb_long(gb); // scaling_list_delta_coef
438  }
439 }
440 
441 static int parse_rps(GetBitContext *gb, unsigned int rps_idx,
442  unsigned int num_rps,
443  unsigned int num_delta_pocs[HEVC_MAX_SHORT_TERM_REF_PIC_SETS])
444 {
445  unsigned int i;
446 
447  if (rps_idx && get_bits1(gb)) { // inter_ref_pic_set_prediction_flag
448  /* this should only happen for slice headers, and this isn't one */
449  if (rps_idx >= num_rps)
450  return AVERROR_INVALIDDATA;
451 
452  skip_bits1 (gb); // delta_rps_sign
453  get_ue_golomb_long(gb); // abs_delta_rps_minus1
454 
455  num_delta_pocs[rps_idx] = 0;
456 
457  /*
458  * From libavcodec/hevc_ps.c:
459  *
460  * if (is_slice_header) {
461  * //foo
462  * } else
463  * rps_ridx = &sps->st_rps[rps - sps->st_rps - 1];
464  *
465  * where:
466  * rps: &sps->st_rps[rps_idx]
467  * sps->st_rps: &sps->st_rps[0]
468  * is_slice_header: rps_idx == num_rps
469  *
470  * thus:
471  * if (num_rps != rps_idx)
472  * rps_ridx = &sps->st_rps[rps_idx - 1];
473  *
474  * NumDeltaPocs[RefRpsIdx]: num_delta_pocs[rps_idx - 1]
475  */
476  for (i = 0; i <= num_delta_pocs[rps_idx - 1]; i++) {
477  uint8_t use_delta_flag = 0;
478  uint8_t used_by_curr_pic_flag = get_bits1(gb);
479  if (!used_by_curr_pic_flag)
480  use_delta_flag = get_bits1(gb);
481 
482  if (used_by_curr_pic_flag || use_delta_flag)
483  num_delta_pocs[rps_idx]++;
484  }
485  } else {
486  unsigned int num_negative_pics = get_ue_golomb_long(gb);
487  unsigned int num_positive_pics = get_ue_golomb_long(gb);
488 
489  if ((num_positive_pics + (uint64_t)num_negative_pics) * 2 > get_bits_left(gb))
490  return AVERROR_INVALIDDATA;
491 
492  num_delta_pocs[rps_idx] = num_negative_pics + num_positive_pics;
493 
494  for (i = 0; i < num_negative_pics; i++) {
495  get_ue_golomb_long(gb); // delta_poc_s0_minus1[rps_idx]
496  skip_bits1 (gb); // used_by_curr_pic_s0_flag[rps_idx]
497  }
498 
499  for (i = 0; i < num_positive_pics; i++) {
500  get_ue_golomb_long(gb); // delta_poc_s1_minus1[rps_idx]
501  skip_bits1 (gb); // used_by_curr_pic_s1_flag[rps_idx]
502  }
503  }
504 
505  return 0;
506 }
507 
510 {
511  unsigned int i, sps_max_sub_layers_minus1, log2_max_pic_order_cnt_lsb_minus4;
512  unsigned int num_short_term_ref_pic_sets, num_delta_pocs[HEVC_MAX_SHORT_TERM_REF_PIC_SETS];
513  unsigned int sps_ext_or_max_sub_layers_minus1, multi_layer_ext_sps_flag;
514 
515  unsigned int sps_video_parameter_set_id = get_bits(gb, 4);
516 
517  if (nal->nuh_layer_id == 0) {
518  sps_ext_or_max_sub_layers_minus1 = 0;
519  sps_max_sub_layers_minus1 = get_bits(gb, 3);
520  } else {
521  sps_ext_or_max_sub_layers_minus1 = get_bits(gb, 3);
522  if (sps_ext_or_max_sub_layers_minus1 == 7) {
523  const HVCCNALUnitArray *array = &hvcc->arrays[VPS_INDEX];
524  const HVCCNALUnit *vps = NULL;
525 
526  for (i = 0; i < array->numNalus; i++)
527  if (sps_video_parameter_set_id == array->nal[i].parameter_set_id) {
528  vps = &array->nal[i];
529  break;
530  }
531  if (!vps)
532  return AVERROR_INVALIDDATA;
533 
534  sps_max_sub_layers_minus1 = vps->vps_max_sub_layers_minus1;
535  } else
536  sps_max_sub_layers_minus1 = sps_ext_or_max_sub_layers_minus1;
537  }
538  multi_layer_ext_sps_flag = nal->nuh_layer_id &&
539  sps_ext_or_max_sub_layers_minus1 == 7;
540 
541  /*
542  * numTemporalLayers greater than 1 indicates that the stream to which this
543  * configuration record applies is temporally scalable and the contained
544  * number of temporal layers (also referred to as temporal sub-layer or
545  * sub-layer in ISO/IEC 23008-2) is equal to numTemporalLayers. Value 1
546  * indicates that the stream is not temporally scalable. Value 0 indicates
547  * that it is unknown whether the stream is temporally scalable.
548  */
550  sps_max_sub_layers_minus1 + 1);
551 
552  if (!multi_layer_ext_sps_flag) {
553  hvcc->temporalIdNested = get_bits1(gb);
554  hvcc_parse_ptl(gb, hvcc, sps_max_sub_layers_minus1);
555  }
556 
558 
559  if (multi_layer_ext_sps_flag) {
560  if (get_bits1(gb)) // update_rep_format_flag
561  skip_bits(gb, 8); // sps_rep_format_idx
562  } else {
563  hvcc->chromaFormat = get_ue_golomb_long(gb);
564 
565  if (hvcc->chromaFormat == 3)
566  skip_bits1(gb); // separate_colour_plane_flag
567 
568  get_ue_golomb_long(gb); // pic_width_in_luma_samples
569  get_ue_golomb_long(gb); // pic_height_in_luma_samples
570 
571  if (get_bits1(gb)) { // conformance_window_flag
572  get_ue_golomb_long(gb); // conf_win_left_offset
573  get_ue_golomb_long(gb); // conf_win_right_offset
574  get_ue_golomb_long(gb); // conf_win_top_offset
575  get_ue_golomb_long(gb); // conf_win_bottom_offset
576  }
577 
580  }
581  log2_max_pic_order_cnt_lsb_minus4 = get_ue_golomb_long(gb);
582 
583  if (!multi_layer_ext_sps_flag) {
584  /* sps_sub_layer_ordering_info_present_flag */
585  i = get_bits1(gb) ? 0 : sps_max_sub_layers_minus1;
586  for (; i <= sps_max_sub_layers_minus1; i++)
588  }
589 
590  get_ue_golomb_long(gb); // log2_min_luma_coding_block_size_minus3
591  get_ue_golomb_long(gb); // log2_diff_max_min_luma_coding_block_size
592  get_ue_golomb_long(gb); // log2_min_transform_block_size_minus2
593  get_ue_golomb_long(gb); // log2_diff_max_min_transform_block_size
594  get_ue_golomb_long(gb); // max_transform_hierarchy_depth_inter
595  get_ue_golomb_long(gb); // max_transform_hierarchy_depth_intra
596 
597  if (get_bits1(gb)) { // scaling_list_enabled_flag
598  int sps_infer_scaling_list_flag = 0;
599  if (multi_layer_ext_sps_flag)
600  sps_infer_scaling_list_flag = get_bits1(gb);
601  if (sps_infer_scaling_list_flag)
602  skip_bits(gb, 6); // sps_scaling_list_ref_layer_id
603  else if (get_bits1(gb)) // sps_scaling_list_data_present_flag
605  }
606 
607  skip_bits1(gb); // amp_enabled_flag
608  skip_bits1(gb); // sample_adaptive_offset_enabled_flag
609 
610  if (get_bits1(gb)) { // pcm_enabled_flag
611  skip_bits (gb, 4); // pcm_sample_bit_depth_luma_minus1
612  skip_bits (gb, 4); // pcm_sample_bit_depth_chroma_minus1
613  get_ue_golomb_long(gb); // log2_min_pcm_luma_coding_block_size_minus3
614  get_ue_golomb_long(gb); // log2_diff_max_min_pcm_luma_coding_block_size
615  skip_bits1 (gb); // pcm_loop_filter_disabled_flag
616  }
617 
618  num_short_term_ref_pic_sets = get_ue_golomb_long(gb);
619  if (num_short_term_ref_pic_sets > HEVC_MAX_SHORT_TERM_REF_PIC_SETS)
620  return AVERROR_INVALIDDATA;
621 
622  for (i = 0; i < num_short_term_ref_pic_sets; i++) {
623  int ret = parse_rps(gb, i, num_short_term_ref_pic_sets, num_delta_pocs);
624  if (ret < 0)
625  return ret;
626  }
627 
628  if (get_bits1(gb)) { // long_term_ref_pics_present_flag
629  unsigned num_long_term_ref_pics_sps = get_ue_golomb_long(gb);
630  if (num_long_term_ref_pics_sps > 31U)
631  return AVERROR_INVALIDDATA;
632  for (i = 0; i < num_long_term_ref_pics_sps; i++) { // num_long_term_ref_pics_sps
633  int len = FFMIN(log2_max_pic_order_cnt_lsb_minus4 + 4, 16);
634  skip_bits (gb, len); // lt_ref_pic_poc_lsb_sps[i]
635  skip_bits1(gb); // used_by_curr_pic_lt_sps_flag[i]
636  }
637  }
638 
639  skip_bits1(gb); // sps_temporal_mvp_enabled_flag
640  skip_bits1(gb); // strong_intra_smoothing_enabled_flag
641 
642  if (get_bits1(gb)) // vui_parameters_present_flag
643  hvcc_parse_vui(gb, hvcc, sps_max_sub_layers_minus1);
644 
645  /* nothing useful for hvcC past this point */
646  return 0;
647 }
648 
651 {
652  uint8_t tiles_enabled_flag, entropy_coding_sync_enabled_flag;
653 
654  nal->parameter_set_id = get_ue_golomb_long(gb); // pps_pic_parameter_set_id
655  get_ue_golomb_long(gb); // pps_seq_parameter_set_id
656 
657  /*
658  * dependent_slice_segments_enabled_flag u(1)
659  * output_flag_present_flag u(1)
660  * num_extra_slice_header_bits u(3)
661  * sign_data_hiding_enabled_flag u(1)
662  * cabac_init_present_flag u(1)
663  */
664  skip_bits(gb, 7);
665 
666  get_ue_golomb_long(gb); // num_ref_idx_l0_default_active_minus1
667  get_ue_golomb_long(gb); // num_ref_idx_l1_default_active_minus1
668  get_se_golomb_long(gb); // init_qp_minus26
669 
670  /*
671  * constrained_intra_pred_flag u(1)
672  * transform_skip_enabled_flag u(1)
673  */
674  skip_bits(gb, 2);
675 
676  if (get_bits1(gb)) // cu_qp_delta_enabled_flag
677  get_ue_golomb_long(gb); // diff_cu_qp_delta_depth
678 
679  get_se_golomb_long(gb); // pps_cb_qp_offset
680  get_se_golomb_long(gb); // pps_cr_qp_offset
681 
682  /*
683  * pps_slice_chroma_qp_offsets_present_flag u(1)
684  * weighted_pred_flag u(1)
685  * weighted_bipred_flag u(1)
686  * transquant_bypass_enabled_flag u(1)
687  */
688  skip_bits(gb, 4);
689 
690  tiles_enabled_flag = get_bits1(gb);
691  entropy_coding_sync_enabled_flag = get_bits1(gb);
692 
693  if (entropy_coding_sync_enabled_flag && tiles_enabled_flag)
694  hvcc->parallelismType = 0; // mixed-type parallel decoding
695  else if (entropy_coding_sync_enabled_flag)
696  hvcc->parallelismType = 3; // wavefront-based parallel decoding
697  else if (tiles_enabled_flag)
698  hvcc->parallelismType = 2; // tile-based parallel decoding
699  else
700  hvcc->parallelismType = 1; // slice-based parallel decoding
701 
702  /* nothing useful for hvcC past this point */
703  return 0;
704 }
705 
706 static void nal_unit_parse_header(GetBitContext *gb, uint8_t *nal_type,
707  uint8_t *nuh_layer_id)
708 {
709  skip_bits1(gb); // forbidden_zero_bit
710 
711  *nal_type = get_bits(gb, 6);
712  *nuh_layer_id = get_bits(gb, 6);
713 
714  /*
715  * nuh_temporal_id_plus1 u(3)
716  */
717  skip_bits(gb, 3);
718 }
719 
720 static int hvcc_array_add_nal_unit(const uint8_t *nal_buf, uint32_t nal_size,
722 {
723  HVCCNALUnit *nal;
724  int ret;
725  uint16_t numNalus = array->numNalus;
726 
727  ret = av_reallocp_array(&array->nal, numNalus + 1, sizeof(*array->nal));
728  if (ret < 0)
729  return ret;
730 
731  nal = &array->nal[numNalus];
732  nal->nalUnit = nal_buf;
733  nal->nalUnitLength = nal_size;
734  array->numNalus++;
735 
736  return 0;
737 }
738 
739 static int hvcc_add_nal_unit(const uint8_t *nal_buf, uint32_t nal_size,
741  int flags, unsigned array_idx)
742 {
743  int ret = 0;
744  int is_nalff = !!(flags & FLAG_IS_NALFF);
745  int is_lhvc = !!(flags & FLAG_IS_LHVC);
746  int ps_array_completeness = !!(flags & FLAG_ARRAY_COMPLETENESS);
747  HVCCNALUnitArray *const array = &hvcc->arrays[array_idx];
748  HVCCNALUnit *nal;
749  GetBitContext gbc;
750  uint8_t nal_type, nuh_layer_id;
751  uint8_t *rbsp_buf;
752  uint32_t rbsp_size;
753 
754  rbsp_buf = ff_nal_unit_extract_rbsp(nal_buf, nal_size, &rbsp_size, 2);
755  if (!rbsp_buf) {
756  ret = AVERROR(ENOMEM);
757  goto end;
758  }
759 
760  ret = init_get_bits8(&gbc, rbsp_buf, rbsp_size);
761  if (ret < 0)
762  goto end;
763 
764  nal_unit_parse_header(&gbc, &nal_type, &nuh_layer_id);
765  if (!is_lhvc && nuh_layer_id > 0)
766  goto end;
767 
768  /*
769  * Note: only 'declarative' SEI messages are allowed in
770  * hvcC. Perhaps the SEI playload type should be checked
771  * and non-declarative SEI messages discarded?
772  */
773  ret = hvcc_array_add_nal_unit(nal_buf, nal_size, array);
774  if (ret < 0)
775  goto end;
776  if (array->numNalus == 1) {
777  hvcc->numOfArrays++;
778  array->NAL_unit_type = nal_type;
779 
780  /*
781  * When the sample entry name is ‘hvc1’, the default and mandatory value of
782  * array_completeness is 1 for arrays of all types of parameter sets, and 0
783  * for all other arrays. When the sample entry name is ‘hev1’, the default
784  * value of array_completeness is 0 for all arrays.
785  */
786  if (nal_type == HEVC_NAL_VPS || nal_type == HEVC_NAL_SPS ||
787  nal_type == HEVC_NAL_PPS)
788  array->array_completeness = ps_array_completeness;
789  }
790 
791  nal = &array->nal[array->numNalus-1];
792  nal->nuh_layer_id = nuh_layer_id;
793 
794  /* Don't parse parameter sets. We already have the needed information*/
795  if (is_nalff)
796  goto end;
797 
798  if (nal_type == HEVC_NAL_VPS)
799  ret = hvcc_parse_vps(&gbc, nal, hvcc);
800  else if (nal_type == HEVC_NAL_SPS)
801  ret = hvcc_parse_sps(&gbc, nal, hvcc);
802  else if (nal_type == HEVC_NAL_PPS)
803  ret = hvcc_parse_pps(&gbc, nal, hvcc);
804  if (ret < 0)
805  goto end;
806 
807 end:
808  av_free(rbsp_buf);
809  return ret;
810 }
811 
813 {
814  memset(hvcc, 0, sizeof(HEVCDecoderConfigurationRecord));
815  hvcc->configurationVersion = 1;
816  hvcc->lengthSizeMinusOne = 3; // 4 bytes
817 
818  /*
819  * The following fields have all their valid bits set by default,
820  * the ProfileTierLevel parsing code will unset them when needed.
821  */
822  hvcc->general_profile_compatibility_flags = 0xffffffff;
823  hvcc->general_constraint_indicator_flags = 0xffffffffffff;
824 
825  /*
826  * Initialize this field with an invalid value which can be used to detect
827  * whether we didn't see any VUI (in which case it should be reset to zero).
828  */
830 }
831 
833 {
834  for (unsigned i = 0; i < FF_ARRAY_ELEMS(hvcc->arrays); i++) {
835  HVCCNALUnitArray *const array = &hvcc->arrays[i];
836  array->numNalus = 0;
837  av_freep(&array->nal);
838  }
839 }
840 
842  int flags)
843 {
844  uint16_t numNalus[NB_ARRAYS] = { 0 };
845  int is_lhvc = !!(flags & FLAG_IS_LHVC);
846  int numOfArrays = 0;
847 
848  /*
849  * We only support writing HEVCDecoderConfigurationRecord version 1.
850  */
851  hvcc->configurationVersion = 1;
852 
853  /*
854  * If min_spatial_segmentation_idc is invalid, reset to 0 (unspecified).
855  */
858 
859  /*
860  * parallelismType indicates the type of parallelism that is used to meet
861  * the restrictions imposed by min_spatial_segmentation_idc when the value
862  * of min_spatial_segmentation_idc is greater than 0.
863  */
864  if (!hvcc->min_spatial_segmentation_idc)
865  hvcc->parallelismType = 0;
866 
867  /*
868  * It's unclear how to properly compute these fields, so
869  * let's always set them to values meaning 'unspecified'.
870  */
871  hvcc->avgFrameRate = 0;
872  /*
873  * lhvC doesn't store this field. It instead reserves the bits, setting them
874  * to '11'b.
875  */
876  hvcc->constantFrameRate = is_lhvc * 0x3;
877 
878  /*
879  * Skip all NALUs with nuh_layer_id == 0 if writing lhvC. We do it here and
880  * not before parsing them as some parameter sets with nuh_layer_id > 0
881  * may reference base layer parameters sets.
882  */
883  for (unsigned i = 0; i < FF_ARRAY_ELEMS(hvcc->arrays); i++) {
884  const HVCCNALUnitArray *const array = &hvcc->arrays[i];
885 
886  if (array->numNalus == 0)
887  continue;
888 
889  for (unsigned j = 0; j < array->numNalus; j++)
890  numNalus[i] += !is_lhvc || (array->nal[j].nuh_layer_id != 0);
891  numOfArrays += (numNalus[i] > 0);
892  }
893 
894  av_log(NULL, AV_LOG_TRACE, "%s\n", is_lhvc ? "lhvC" : "hvcC");
895  av_log(NULL, AV_LOG_TRACE, "configurationVersion: %"PRIu8"\n",
896  hvcc->configurationVersion);
897  if (!is_lhvc) {
898  av_log(NULL, AV_LOG_TRACE, "general_profile_space: %"PRIu8"\n",
899  hvcc->general_profile_space);
900  av_log(NULL, AV_LOG_TRACE, "general_tier_flag: %"PRIu8"\n",
901  hvcc->general_tier_flag);
902  av_log(NULL, AV_LOG_TRACE, "general_profile_idc: %"PRIu8"\n",
903  hvcc->general_profile_idc);
904  av_log(NULL, AV_LOG_TRACE, "general_profile_compatibility_flags: 0x%08"PRIx32"\n",
906  av_log(NULL, AV_LOG_TRACE, "general_constraint_indicator_flags: 0x%012"PRIx64"\n",
908  av_log(NULL, AV_LOG_TRACE, "general_level_idc: %"PRIu8"\n",
909  hvcc->general_level_idc);
910  }
911  av_log(NULL, AV_LOG_TRACE, "min_spatial_segmentation_idc: %"PRIu16"\n",
913  av_log(NULL, AV_LOG_TRACE, "parallelismType: %"PRIu8"\n",
914  hvcc->parallelismType);
915  if (!is_lhvc) {
916  av_log(NULL, AV_LOG_TRACE, "chromaFormat: %"PRIu8"\n",
917  hvcc->chromaFormat);
918  av_log(NULL, AV_LOG_TRACE, "bitDepthLumaMinus8: %"PRIu8"\n",
919  hvcc->bitDepthLumaMinus8);
920  av_log(NULL, AV_LOG_TRACE, "bitDepthChromaMinus8: %"PRIu8"\n",
921  hvcc->bitDepthChromaMinus8);
922  av_log(NULL, AV_LOG_TRACE, "avgFrameRate: %"PRIu16"\n",
923  hvcc->avgFrameRate);
924  av_log(NULL, AV_LOG_TRACE, "constantFrameRate: %"PRIu8"\n",
925  hvcc->constantFrameRate);
926  }
927  av_log(NULL, AV_LOG_TRACE, "numTemporalLayers: %"PRIu8"\n",
928  hvcc->numTemporalLayers);
929  av_log(NULL, AV_LOG_TRACE, "temporalIdNested: %"PRIu8"\n",
930  hvcc->temporalIdNested);
931  av_log(NULL, AV_LOG_TRACE, "lengthSizeMinusOne: %"PRIu8"\n",
932  hvcc->lengthSizeMinusOne);
933  av_log(NULL, AV_LOG_TRACE, "numOfArrays: %"PRIu8"\n",
934  numOfArrays);
935  for (unsigned i = 0, j = 0; i < FF_ARRAY_ELEMS(hvcc->arrays); i++) {
936  const HVCCNALUnitArray *const array = &hvcc->arrays[i];
937 
938  if (numNalus[i] == 0)
939  continue;
940 
941  av_log(NULL, AV_LOG_TRACE, "array_completeness[%u]: %"PRIu8"\n",
942  j, array->array_completeness);
943  av_log(NULL, AV_LOG_TRACE, "NAL_unit_type[%u]: %"PRIu8"\n",
944  j, array->NAL_unit_type);
945  av_log(NULL, AV_LOG_TRACE, "numNalus[%u]: %"PRIu16"\n",
946  j, numNalus[i]);
947  for (unsigned k = 0; k < array->numNalus; k++) {
948  if (is_lhvc && array->nal[k].nuh_layer_id == 0)
949  continue;
950 
952  "nalUnitLength[%u][%u]: %"PRIu16"\n",
953  j, k, array->nal[k].nalUnitLength);
954  }
955  j++;
956  }
957 
958  /*
959  * We need at least one of each: VPS, SPS and PPS.
960  */
961  if ((!numNalus[VPS_INDEX] || numNalus[VPS_INDEX] > HEVC_MAX_VPS_COUNT) && !is_lhvc)
962  return AVERROR_INVALIDDATA;
963  if (!numNalus[SPS_INDEX] || numNalus[SPS_INDEX] > HEVC_MAX_SPS_COUNT ||
964  !numNalus[PPS_INDEX] || numNalus[PPS_INDEX] > HEVC_MAX_PPS_COUNT)
965  return AVERROR_INVALIDDATA;
966 
967  /* unsigned int(8) configurationVersion = 1; */
968  avio_w8(pb, hvcc->configurationVersion);
969 
970  if (!is_lhvc) {
971  /*
972  * unsigned int(2) general_profile_space;
973  * unsigned int(1) general_tier_flag;
974  * unsigned int(5) general_profile_idc;
975  */
976  avio_w8(pb, hvcc->general_profile_space << 6 |
977  hvcc->general_tier_flag << 5 |
978  hvcc->general_profile_idc);
979 
980  /* unsigned int(32) general_profile_compatibility_flags; */
982 
983  /* unsigned int(48) general_constraint_indicator_flags; */
986 
987  /* unsigned int(8) general_level_idc; */
988  avio_w8(pb, hvcc->general_level_idc);
989  }
990 
991  /*
992  * bit(4) reserved = '1111'b;
993  * unsigned int(12) min_spatial_segmentation_idc;
994  */
995  avio_wb16(pb, hvcc->min_spatial_segmentation_idc | 0xf000);
996 
997  /*
998  * bit(6) reserved = '111111'b;
999  * unsigned int(2) parallelismType;
1000  */
1001  avio_w8(pb, hvcc->parallelismType | 0xfc);
1002 
1003  if (!is_lhvc) {
1004  /*
1005  * bit(6) reserved = '111111'b;
1006  * unsigned int(2) chromaFormat;
1007  */
1008  avio_w8(pb, hvcc->chromaFormat | 0xfc);
1009 
1010  /*
1011  * bit(5) reserved = '11111'b;
1012  * unsigned int(3) bitDepthLumaMinus8;
1013  */
1014  avio_w8(pb, hvcc->bitDepthLumaMinus8 | 0xf8);
1015 
1016  /*
1017  * bit(5) reserved = '11111'b;
1018  * unsigned int(3) bitDepthChromaMinus8;
1019  */
1020  avio_w8(pb, hvcc->bitDepthChromaMinus8 | 0xf8);
1021 
1022  /* bit(16) avgFrameRate; */
1023  avio_wb16(pb, hvcc->avgFrameRate);
1024  }
1025 
1026  /*
1027  * if (!is_lhvc)
1028  * bit(2) constantFrameRate;
1029  * else
1030  * bit(2) reserved = '11'b;
1031  * bit(3) numTemporalLayers;
1032  * bit(1) temporalIdNested;
1033  * unsigned int(2) lengthSizeMinusOne;
1034  */
1035  avio_w8(pb, hvcc->constantFrameRate << 6 |
1036  hvcc->numTemporalLayers << 3 |
1037  hvcc->temporalIdNested << 2 |
1038  hvcc->lengthSizeMinusOne);
1039 
1040  /* unsigned int(8) numOfArrays; */
1041  avio_w8(pb, numOfArrays);
1042 
1043  for (unsigned i = 0; i < FF_ARRAY_ELEMS(hvcc->arrays); i++) {
1044  const HVCCNALUnitArray *const array = &hvcc->arrays[i];
1045 
1046  if (!numNalus[i])
1047  continue;
1048  /*
1049  * bit(1) array_completeness;
1050  * unsigned int(1) reserved = 0;
1051  * unsigned int(6) NAL_unit_type;
1052  */
1053  avio_w8(pb, array->array_completeness << 7 |
1054  array->NAL_unit_type & 0x3f);
1055 
1056  /* unsigned int(16) numNalus; */
1057  avio_wb16(pb, numNalus[i]);
1058 
1059  for (unsigned j = 0; j < array->numNalus; j++) {
1060  HVCCNALUnit *nal = &array->nal[j];
1061 
1062  if (is_lhvc && nal->nuh_layer_id == 0)
1063  continue;
1064 
1065  /* unsigned int(16) nalUnitLength; */
1066  avio_wb16(pb, nal->nalUnitLength);
1067 
1068  /* bit(8*nalUnitLength) nalUnit; */
1069  avio_write(pb, nal->nalUnit, nal->nalUnitLength);
1070  }
1071  }
1072 
1073  return 0;
1074 }
1075 
1076 int ff_hevc_annexb2mp4(AVIOContext *pb, const uint8_t *buf_in,
1077  int size, int filter_ps, int *ps_count)
1078 {
1079  int num_ps = 0, ret = 0;
1080  uint8_t *buf, *end, *start = NULL;
1081 
1082  if (!filter_ps) {
1083  ret = ff_nal_parse_units(pb, buf_in, size);
1084  goto end;
1085  }
1086 
1087  ret = ff_nal_parse_units_buf(buf_in, &start, &size);
1088  if (ret < 0)
1089  goto end;
1090 
1091  ret = 0;
1092  buf = start;
1093  end = start + size;
1094 
1095  while (end - buf > 4) {
1096  uint32_t len = FFMIN(AV_RB32(buf), end - buf - 4);
1097  uint8_t type = (buf[4] >> 1) & 0x3f;
1098 
1099  buf += 4;
1100 
1101  switch (type) {
1102  case HEVC_NAL_VPS:
1103  case HEVC_NAL_SPS:
1104  case HEVC_NAL_PPS:
1105  num_ps++;
1106  break;
1107  default:
1108  ret += 4 + len;
1109  avio_wb32(pb, len);
1110  avio_write(pb, buf, len);
1111  break;
1112  }
1113 
1114  buf += len;
1115  }
1116 
1117 end:
1118  av_free(start);
1119  if (ps_count)
1120  *ps_count = num_ps;
1121  return ret;
1122 }
1123 
1124 int ff_hevc_annexb2mp4_buf(const uint8_t *buf_in, uint8_t **buf_out,
1125  int *size, int filter_ps, int *ps_count)
1126 {
1127  AVIOContext *pb;
1128  int ret;
1129 
1130  ret = avio_open_dyn_buf(&pb);
1131  if (ret < 0)
1132  return ret;
1133 
1134  ret = ff_hevc_annexb2mp4(pb, buf_in, *size, filter_ps, ps_count);
1135  if (ret < 0) {
1136  ffio_free_dyn_buf(&pb);
1137  return ret;
1138  }
1139 
1140  *size = avio_close_dyn_buf(pb, buf_out);
1141 
1142  return 0;
1143 }
1144 
1145 static int hvcc_parse_nal_unit(const uint8_t *buf, uint32_t len, int type,
1147  int flags)
1148 {
1149  for (unsigned i = 0; i < FF_ARRAY_ELEMS(hvcc->arrays); i++) {
1150  static const uint8_t array_idx_to_type[] =
1153 
1154  if (type == array_idx_to_type[i]) {
1155  int ret = hvcc_add_nal_unit(buf, len, hvcc, flags, i);
1156  if (ret < 0)
1157  return ret;
1158  break;
1159  }
1160  }
1161 
1162  return 0;
1163 }
1164 
1165 static int write_configuration_record(AVIOContext *pb, const uint8_t *data,
1166  int size, int flags)
1167 {
1169  uint8_t *buf, *end, *start = NULL;
1170  int ret;
1171 
1172  if (size < 6) {
1173  /* We can't write a valid hvcC from the provided data */
1174  return AVERROR_INVALIDDATA;
1175  } else if (*data == 1) {
1176  /* Data is already hvcC-formatted. Parse the arrays to skip any NALU
1177  with nuh_layer_id > 0 */
1178  GetBitContext gbc;
1179  int num_arrays;
1180 
1181  if (size < 23)
1182  return AVERROR_INVALIDDATA;
1183 
1184  ret = init_get_bits8(&gbc, data, size);
1185  if (ret < 0)
1186  return ret;
1187 
1188  hvcc_init(&hvcc);
1189  skip_bits(&gbc, 8); // hvcc.configurationVersion
1190  hvcc.general_profile_space = get_bits(&gbc, 2);
1191  hvcc.general_tier_flag = get_bits1(&gbc);
1192  hvcc.general_profile_idc = get_bits(&gbc, 5);
1195  hvcc.general_level_idc = get_bits(&gbc, 8);
1196  skip_bits(&gbc, 4); // reserved
1197  hvcc.min_spatial_segmentation_idc = get_bits(&gbc, 12);
1198  skip_bits(&gbc, 6); // reserved
1199  hvcc.parallelismType = get_bits(&gbc, 2);
1200  skip_bits(&gbc, 6); // reserved
1201  hvcc.chromaFormat = get_bits(&gbc, 2);
1202  skip_bits(&gbc, 5); // reserved
1203  hvcc.bitDepthLumaMinus8 = get_bits(&gbc, 3);
1204  skip_bits(&gbc, 5); // reserved
1205  hvcc.bitDepthChromaMinus8 = get_bits(&gbc, 3);
1206  hvcc.avgFrameRate = get_bits(&gbc, 16);
1207  hvcc.constantFrameRate = get_bits(&gbc, 2);
1208  hvcc.numTemporalLayers = get_bits(&gbc, 3);
1209  hvcc.temporalIdNested = get_bits1(&gbc);
1210  hvcc.lengthSizeMinusOne = get_bits(&gbc, 2);
1211 
1212  flags |= FLAG_IS_NALFF;
1213 
1214  num_arrays = get_bits(&gbc, 8);
1215  for (int i = 0; i < num_arrays; i++) {
1216  int type, num_nalus;
1217 
1218  skip_bits(&gbc, 2);
1219  type = get_bits(&gbc, 6);
1220  num_nalus = get_bits(&gbc, 16);
1221  for (int j = 0; j < num_nalus; j++) {
1222  int len = get_bits(&gbc, 16);
1223 
1224  if (len > (get_bits_left(&gbc) / 8))
1225  goto end;
1226 
1228  len, type, &hvcc, flags);
1229  if (ret < 0)
1230  goto end;
1231 
1232  skip_bits_long(&gbc, len * 8);
1233  }
1234  }
1235 
1236  ret = hvcc_write(pb, &hvcc, flags);
1237  goto end;
1238  } else if (!(AV_RB24(data) == 1 || AV_RB32(data) == 1)) {
1239  /* Not a valid Annex B start code prefix */
1240  return AVERROR_INVALIDDATA;
1241  }
1242 
1243  ret = ff_nal_parse_units_buf(data, &start, &size);
1244  if (ret < 0)
1245  return ret;
1246 
1247  hvcc_init(&hvcc);
1248 
1249  buf = start;
1250  end = start + size;
1251 
1252  while (end - buf > 4) {
1253  uint32_t len = FFMIN(AV_RB32(buf), end - buf - 4);
1254  uint8_t type = (buf[4] >> 1) & 0x3f;
1255 
1256  buf += 4;
1257 
1258  ret = hvcc_parse_nal_unit(buf, len, type, &hvcc, flags);
1259  if (ret < 0)
1260  goto end;
1261 
1262  buf += len;
1263  }
1264 
1265  ret = hvcc_write(pb, &hvcc, flags);
1266 
1267 end:
1268  hvcc_close(&hvcc);
1269  av_free(start);
1270  return ret;
1271 }
1272 
1273 int ff_isom_write_hvcc(AVIOContext *pb, const uint8_t *data,
1274  int size, int ps_array_completeness)
1275 {
1276  return write_configuration_record(pb, data, size,
1277  !!ps_array_completeness * FLAG_ARRAY_COMPLETENESS);
1278 }
1279 
1280 int ff_isom_write_lhvc(AVIOContext *pb, const uint8_t *data,
1281  int size, int ps_array_completeness)
1282 {
1283  return write_configuration_record(pb, data, size,
1284  (!!ps_array_completeness * FLAG_ARRAY_COMPLETENESS) | FLAG_IS_LHVC);
1285 }
HEVCDecoderConfigurationRecord::avgFrameRate
uint16_t avgFrameRate
Definition: hevc.c:78
hvcc_parse_vps
static int hvcc_parse_vps(GetBitContext *gb, HVCCNALUnit *nal, HEVCDecoderConfigurationRecord *hvcc)
Definition: hevc.c:387
skip_bits_long
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:278
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:695
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
get_se_golomb_long
static int get_se_golomb_long(GetBitContext *gb)
Definition: golomb.h:294
hvcc_parse_nal_unit
static int hvcc_parse_nal_unit(const uint8_t *buf, uint32_t len, int type, HEVCDecoderConfigurationRecord *hvcc, int flags)
Definition: hevc.c:1145
get_bits_long
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:421
get_bits_count
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:266
HEVCDecoderConfigurationRecord::temporalIdNested
uint8_t temporalIdNested
Definition: hevc.c:81
ff_isom_write_lhvc
int ff_isom_write_lhvc(AVIOContext *pb, const uint8_t *data, int size, int ps_array_completeness)
Writes L-HEVC extradata (parameter sets with nuh_layer_id > 0, as a LHEVCDecoderConfigurationRecord) ...
Definition: hevc.c:1280
hvcc_close
static void hvcc_close(HEVCDecoderConfigurationRecord *hvcc)
Definition: hevc.c:832
data
const char data[16]
Definition: mxf.c:149
HEVCDecoderConfigurationRecord::general_profile_idc
uint8_t general_profile_idc
Definition: hevc.c:69
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
VPS_INDEX
@ VPS_INDEX
Definition: hevc.c:35
HVCCProfileTierLevel::level_idc
uint8_t level_idc
Definition: hevc.c:93
hvcc_init
static void hvcc_init(HEVCDecoderConfigurationRecord *hvcc)
Definition: hevc.c:812
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:381
golomb.h
exp golomb vlc stuff
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:335
HVCCNALUnit::nuh_layer_id
uint8_t nuh_layer_id
Definition: hevc.c:49
GetBitContext
Definition: get_bits.h:108
skip_scaling_list_data
static void skip_scaling_list_data(GetBitContext *gb)
Definition: hevc.c:422
HEVC_NAL_SEI_SUFFIX
@ HEVC_NAL_SEI_SUFFIX
Definition: hevc.h:69
HEVCDecoderConfigurationRecord::numTemporalLayers
uint8_t numTemporalLayers
Definition: hevc.c:80
HVCCProfileTierLevel::profile_idc
uint8_t profile_idc
Definition: hevc.c:90
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
HVCCNALUnit::parameter_set_id
uint8_t parameter_set_id
Definition: hevc.c:50
parse_rps
static int parse_rps(GetBitContext *gb, unsigned int rps_idx, unsigned int num_rps, unsigned int num_delta_pocs[HEVC_MAX_SHORT_TERM_REF_PIC_SETS])
Definition: hevc.c:441
HEVC_NAL_SEI_PREFIX
@ HEVC_NAL_SEI_PREFIX
Definition: hevc.h:68
avio_close_dyn_buf
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1407
hvcc_add_nal_unit
static int hvcc_add_nal_unit(const uint8_t *nal_buf, uint32_t nal_size, HEVCDecoderConfigurationRecord *hvcc, int flags, unsigned array_idx)
Definition: hevc.c:739
AV_LOG_TRACE
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:235
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:545
hvcc_write
static int hvcc_write(AVIOContext *pb, HEVCDecoderConfigurationRecord *hvcc, int flags)
Definition: hevc.c:841
HEVCDecoderConfigurationRecord::chromaFormat
uint8_t chromaFormat
Definition: hevc.c:75
FLAG_ARRAY_COMPLETENESS
#define FLAG_ARRAY_COMPLETENESS
Definition: hevc.c:44
ptl
const H265RawProfileTierLevel * ptl
Definition: h265_levels.c:170
avio_open_dyn_buf
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1362
HEVCDecoderConfigurationRecord::general_constraint_indicator_flags
uint64_t general_constraint_indicator_flags
Definition: hevc.c:71
intreadwrite.h
NB_ARRAYS
@ NB_ARRAYS
Definition: hevc.c:40
hvcc_parse_vui
static void hvcc_parse_vui(GetBitContext *gb, HEVCDecoderConfigurationRecord *hvcc, unsigned int max_sub_layers_minus1)
Definition: hevc.c:302
HEVC_NAL_VPS
@ HEVC_NAL_VPS
Definition: hevc.h:61
ff_nal_parse_units
int ff_nal_parse_units(AVIOContext *pb, const uint8_t *buf_in, int size)
Definition: nal.c:110
HEVCDecoderConfigurationRecord::general_profile_compatibility_flags
uint32_t general_profile_compatibility_flags
Definition: hevc.c:70
hevc.h
get_bits.h
HVCCProfileTierLevel::tier_flag
uint8_t tier_flag
Definition: hevc.c:89
NULL
#define NULL
Definition: coverity.c:32
HEVC_MAX_SHORT_TERM_REF_PIC_SETS
@ HEVC_MAX_SHORT_TERM_REF_PIC_SETS
Definition: hevc.h:125
skip_hrd_parameters
static int skip_hrd_parameters(GetBitContext *gb, uint8_t cprms_present_flag, unsigned int max_sub_layers_minus1)
Definition: hevc.c:218
ff_nal_unit_extract_rbsp
uint8_t * ff_nal_unit_extract_rbsp(const uint8_t *src, uint32_t src_len, uint32_t *dst_len, int header_len)
Definition: nal.c:160
ff_hevc_annexb2mp4_buf
int ff_hevc_annexb2mp4_buf(const uint8_t *buf_in, uint8_t **buf_out, int *size, int filter_ps, int *ps_count)
Writes Annex B formatted HEVC NAL units to a data buffer.
Definition: hevc.c:1124
nal_unit_parse_header
static void nal_unit_parse_header(GetBitContext *gb, uint8_t *nal_type, uint8_t *nuh_layer_id)
Definition: hevc.c:706
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:388
hvcc_parse_pps
static int hvcc_parse_pps(GetBitContext *gb, HVCCNALUnit *nal, HEVCDecoderConfigurationRecord *hvcc)
Definition: hevc.c:649
HEVCDecoderConfigurationRecord::arrays
HVCCNALUnitArray arrays[NB_ARRAYS]
Definition: hevc.c:84
avc.h
PPS_INDEX
@ PPS_INDEX
Definition: hevc.c:37
avio_w8
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:179
HEVCDecoderConfigurationRecord::lengthSizeMinusOne
uint8_t lengthSizeMinusOne
Definition: hevc.c:82
vps
static int FUNC() vps(CodedBitstreamContext *ctx, RWContext *rw, H265RawVPS *current)
Definition: cbs_h265_syntax_template.c:423
HVCCNALUnit::nalUnit
const uint8_t * nalUnit
Definition: hevc.c:52
skip_timing_info
static void skip_timing_info(GetBitContext *gb)
Definition: hevc.c:293
HEVCDecoderConfigurationRecord::bitDepthLumaMinus8
uint8_t bitDepthLumaMinus8
Definition: hevc.c:76
HVCCProfileTierLevel::profile_space
uint8_t profile_space
Definition: hevc.c:88
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
ff_nal_parse_units_buf
int ff_nal_parse_units_buf(const uint8_t *buf_in, uint8_t **buf, int *size)
Definition: nal.c:130
HEVC_NAL_SPS
@ HEVC_NAL_SPS
Definition: hevc.h:62
size
int size
Definition: twinvq_data.h:10344
avio.h
AV_RB32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:96
HEVC_NAL_PPS
@ HEVC_NAL_PPS
Definition: hevc.h:63
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:201
avio_wb32
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:365
av_reallocp_array
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Allocate, reallocate an array through a pointer to a pointer.
Definition: mem.c:225
HEVCDecoderConfigurationRecord::constantFrameRate
uint8_t constantFrameRate
Definition: hevc.c:79
FLAG_IS_LHVC
#define FLAG_IS_LHVC
Definition: hevc.c:46
SEI_PREFIX_INDEX
@ SEI_PREFIX_INDEX
Definition: hevc.c:38
skip_bits1
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:413
HEVCDecoderConfigurationRecord::min_spatial_segmentation_idc
uint16_t min_spatial_segmentation_idc
Definition: hevc.c:73
HEVCDecoderConfigurationRecord::bitDepthChromaMinus8
uint8_t bitDepthChromaMinus8
Definition: hevc.c:77
get_bits64
static uint64_t get_bits64(GetBitContext *s, int n)
Read 0-64 bits.
Definition: get_bits.h:453
ff_isom_write_hvcc
int ff_isom_write_hvcc(AVIOContext *pb, const uint8_t *data, int size, int ps_array_completeness)
Writes HEVC extradata (parameter sets and declarative SEI NAL units with nuh_layer_id == 0,...
Definition: hevc.c:1273
HEVCDecoderConfigurationRecord::numOfArrays
uint8_t numOfArrays
Definition: hevc.c:83
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
avio_internal.h
HVCCProfileTierLevel::profile_compatibility_flags
uint32_t profile_compatibility_flags
Definition: hevc.c:91
HVCCNALUnitArray::numNalus
uint16_t numNalus
Definition: hevc.c:61
SPS_INDEX
@ SPS_INDEX
Definition: hevc.c:36
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
HEVC_MAX_PPS_COUNT
@ HEVC_MAX_PPS_COUNT
Definition: hevc.h:117
len
int len
Definition: vorbis_enc_data.h:426
hvcc_update_ptl
static void hvcc_update_ptl(HEVCDecoderConfigurationRecord *hvcc, HVCCProfileTierLevel *ptl)
Definition: hevc.c:96
nal.h
hvcc_array_add_nal_unit
static int hvcc_array_add_nal_unit(const uint8_t *nal_buf, uint32_t nal_size, HVCCNALUnitArray *array)
Definition: hevc.c:720
HEVCDecoderConfigurationRecord::general_tier_flag
uint8_t general_tier_flag
Definition: hevc.c:68
HEVCDecoderConfigurationRecord::general_level_idc
uint8_t general_level_idc
Definition: hevc.c:72
HVCCProfileTierLevel
Definition: hevc.c:87
HEVCDecoderConfigurationRecord::configurationVersion
uint8_t configurationVersion
Definition: hevc.c:66
array
static int array[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:111
ffio_free_dyn_buf
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
Definition: aviobuf.c:1435
ret
ret
Definition: filter_design.txt:187
write_configuration_record
static int write_configuration_record(AVIOContext *pb, const uint8_t *data, int size, int flags)
Definition: hevc.c:1165
skip_sub_layer_hrd_parameters
static void skip_sub_layer_hrd_parameters(GetBitContext *gb, unsigned int cpb_cnt_minus1, uint8_t sub_pic_hrd_params_present_flag)
Definition: hevc.c:199
U
#define U(x)
Definition: vpx_arith.h:37
HVCCNALUnit
Definition: hevc.c:48
HEVCDecoderConfigurationRecord::parallelismType
uint8_t parallelismType
Definition: hevc.c:74
FLAG_IS_NALFF
#define FLAG_IS_NALFF
Definition: hevc.c:45
HVCCNALUnit::nalUnitLength
uint16_t nalUnitLength
Definition: hevc.c:51
HVCCNALUnitArray::array_completeness
uint8_t array_completeness
Definition: hevc.c:59
hvcc_parse_sps
static int hvcc_parse_sps(GetBitContext *gb, HVCCNALUnit *nal, HEVCDecoderConfigurationRecord *hvcc)
Definition: hevc.c:508
mem.h
get_ue_golomb_long
static unsigned get_ue_golomb_long(GetBitContext *gb)
Read an unsigned Exp-Golomb code in the range 0 to UINT32_MAX-1.
Definition: golomb.h:104
HVCCProfileTierLevel::constraint_indicator_flags
uint64_t constraint_indicator_flags
Definition: hevc.c:92
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
HEVC_MAX_SPS_COUNT
@ HEVC_MAX_SPS_COUNT
Definition: hevc.h:115
HEVCDecoderConfigurationRecord
Definition: hevc.c:65
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
SEI_SUFFIX_INDEX
@ SEI_SUFFIX_INDEX
Definition: hevc.c:39
avio_wb16
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:443
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:482
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
HEVC_MAX_VPS_COUNT
@ HEVC_MAX_VPS_COUNT
Definition: hevc.h:113
AV_RB24
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_RB24
Definition: bytestream.h:97
MAX_SPATIAL_SEGMENTATION
#define MAX_SPATIAL_SEGMENTATION
Definition: hevc.c:32
HVCCNALUnitArray::nal
HVCCNALUnit * nal
Definition: hevc.c:62
HVCCNALUnitArray::NAL_unit_type
uint8_t NAL_unit_type
Definition: hevc.c:60
skip_sub_layer_ordering_info
static void skip_sub_layer_ordering_info(GetBitContext *gb)
Definition: hevc.c:380
ff_hevc_annexb2mp4
int ff_hevc_annexb2mp4(AVIOContext *pb, const uint8_t *buf_in, int size, int filter_ps, int *ps_count)
Writes Annex B formatted HEVC NAL units to the provided AVIOContext.
Definition: hevc.c:1076
HEVC_MAX_SUB_LAYERS
@ HEVC_MAX_SUB_LAYERS
Definition: hevc.h:105
HVCCNALUnit::vps_max_sub_layers_minus1
uint8_t vps_max_sub_layers_minus1
Definition: hevc.c:55
HEVCDecoderConfigurationRecord::general_profile_space
uint8_t general_profile_space
Definition: hevc.c:67
HVCCNALUnitArray
Definition: hevc.c:58
hvcc_parse_ptl
static void hvcc_parse_ptl(GetBitContext *gb, HEVCDecoderConfigurationRecord *hvcc, unsigned int max_sub_layers_minus1)
Definition: hevc.c:150