使用 FFmpeg 分析编码的数据信息

最近在解决HLS的EXT-X-STREAM-INF信息中显示CODECS信息的时候遇到了解析HEVC的NALUnit的问题,经过与赵军聊过后,发现使用ffmpeg的bitstream filter的trace_headers参数可以很有效率的解决分析NALU的问题,很方便,所以大概总结一下分享给大家。

首先我们了解一下 trace_headers 的基础信息,看一下官方文档的说明:


Log trace output containing all syntax elements in 
the coded stream headers (everything above the level of 
individual coded blocks). This can be useful for debugging 
low-level stream issues.

Supports AV1, H.264, H.265, (M)JPEG, MPEG-2 and VP9,
but depending on the build only a subset of these may be
available.

翻译一下内容:

日志跟踪输出,其中包含编码流标头中的所有语法元素
(高于单个编码块级别的所有元素)。
这对于调试低级流问题很有用。

支持AV1,H.264,H.265,(M)JPEG,MPEG-2和VP9,
但根据内部版本,可能只有其中一部分可用。

大概意思已经很明显了,虽然翻译得不是很好,但是应该足够获得需要的信息了,那么接下来可以尝试着确认一下:

命令行如下:

./ffmpeg -i input.mp4 -c copy -bsf:v trace_headers -y output.mp4

这是一个只有 2 帧的 hevc 的 input.mp4 视频文件,然后通过 codec copy 的方式将编码数据存入 output.mp4 中,输出的信息如下:


ffmpeg version N-99816-g3da35b7cc7 Copyright (c) 2000-2020 the FFmpeg developers
  built with Apple LLVM version 10.0.0 (clang-1000.11.45.5)
  configuration: --enable-fontconfig --enable-gpl --enable-libass --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libspeex --enable-libx264 --enable-libx265 --enable-libfdk-aac --enable-version3 --enable-nonfree --enable-videotoolbox --enable-libxml2 --samples=fate-suite/ --enable-libopencv --enable-libopus --enable-opengl --enable-libzmq --disable-optimizations --disable-stripping
  libavutil      56. 60.100 / 56. 60.100
  libavcodec     58.112.100 / 58.112.100
  libavformat    58. 64.100 / 58. 64.100
  libavdevice    58. 11.102 / 58. 11.102
  libavfilter     7. 88.100 /  7. 88.100
  libswscale      5.  8.100 /  5.  8.100
  libswresample   3.  8.100 /  3.  8.100
  libpostproc    55.  8.100 / 55.  8.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'input.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2mp41
    encoder         : Lavf58.35.100
  Duration: 00:00:00.08, start: 0.000000, bitrate: 295 kb/s
    Stream #0:0(und): Video: hevc (Main) (hev1 / 0x31766568), yuv420p(tv, progressive), 176x144 [SAR 1:1 DAR 11:9], 6 kb/s, 25 fps, 25 tbr, 12800 tbn, 25 tbc (default)
    Metadata:
      handler_name    : VideoHandler
[trace_headers @ 0x7fb8d9603a00] Extradata
[trace_headers @ 0x7fb8d9603a00] Video Parameter Set
[trace_headers @ 0x7fb8d9603a00] 0           forbidden_zero_bit                                          0 = 0
[trace_headers @ 0x7fb8d9603a00] 1           nal_unit_type                                          100000 = 32
[trace_headers @ 0x7fb8d9603a00] 7           nuh_layer_id                                           000000 = 0
[trace_headers @ 0x7fb8d9603a00] 13          nuh_temporal_id_plus1                                     001 = 1
[trace_headers @ 0x7fb8d9603a00] 16          vps_video_parameter_set_id                               0000 = 0
[trace_headers @ 0x7fb8d9603a00] 20          vps_base_layer_internal_flag                                1 = 1
[trace_headers @ 0x7fb8d9603a00] 21          vps_base_layer_available_flag                               1 = 1
[trace_headers @ 0x7fb8d9603a00] 22          vps_max_layers_minus1                                  000000 = 0
[trace_headers @ 0x7fb8d9603a00] 28          vps_max_sub_layers_minus1                                 000 = 0
[trace_headers @ 0x7fb8d9603a00] 31          vps_temporal_id_nesting_flag                                1 = 1
[trace_headers @ 0x7fb8d9603a00] 32          vps_reserved_0xffff_16bits                   1111111111111111 = 65535
[trace_headers @ 0x7fb8d9603a00] 48          general_profile_space                                      00 = 0
[trace_headers @ 0x7fb8d9603a00] 50          general_tier_flag                                           0 = 0
[trace_headers @ 0x7fb8d9603a00] 51          general_profile_idc                                     00001 = 1
[trace_headers @ 0x7fb8d9603a00] 56          general_profile_compatibility_flag[0]                       0 = 0
[trace_headers @ 0x7fb8d9603a00] 57          general_profile_compatibility_flag[1]                       1 = 1
[trace_headers @ 0x7fb8d9603a00] 58          general_profile_compatibility_flag[2]                       1 = 1
[trace_headers @ 0x7fb8d9603a00] 59          general_profile_compatibility_flag[3]                       0 = 0
为了节省篇幅干掉了好多内容,自己脑补吧。
[trace_headers @ 0x7fb8d9603a00] 84          general_profile_compatibility_flag[28]                      0 = 0
[trace_headers @ 0x7fb8d9603a00] 85          general_profile_compatibility_flag[29]                      0 = 0
[trace_headers @ 0x7fb8d9603a00] 86          general_profile_compatibility_flag[30]                      0 = 0
[trace_headers @ 0x7fb8d9603a00] 87          general_profile_compatibility_flag[31]                      0 = 0
[trace_headers @ 0x7fb8d9603a00] 88          general_progressive_source_flag                             1 = 1
[trace_headers @ 0x7fb8d9603a00] 89          general_interlaced_source_flag                              0 = 0
[trace_headers @ 0x7fb8d9603a00] 90          general_non_packed_constraint_flag                          0 = 0
[trace_headers @ 0x7fb8d9603a00] 91          general_frame_only_constraint_flag                          1 = 1
[trace_headers @ 0x7fb8d9603a00] 92          general_reserved_zero_7bits                           0000000 = 0
[trace_headers @ 0x7fb8d9603a00] 99          general_one_picture_only_constraint_flag                    0 = 0
[trace_headers @ 0x7fb8d9603a00] 100         general_reserved_zero_35bits         000000000000000000000000 = 0
[trace_headers @ 0x7fb8d9603a00] 124         general_reserved_zero_35bits                      00000000000 = 0
[trace_headers @ 0x7fb8d9603a00] 135         general_inbld_flag                                          0 = 0
[trace_headers @ 0x7fb8d9603a00] 136         general_level_idc                                    00111100 = 60
[trace_headers @ 0x7fb8d9603a00] 144         vps_sub_layer_ordering_info_present_flag                    1 = 1
[trace_headers @ 0x7fb8d9603a00] 145         vps_max_dec_pic_buffering_minus1[0]                     00101 = 4
[trace_headers @ 0x7fb8d9603a00] 150         vps_max_num_reorder_pics[0]                               011 = 2
[trace_headers @ 0x7fb8d9603a00] 153         vps_max_latency_increase_plus1[0]                       00110 = 5
[trace_headers @ 0x7fb8d9603a00] 158         vps_max_layer_id                                       000000 = 0
[trace_headers @ 0x7fb8d9603a00] 164         vps_num_layer_sets_minus1                                   1 = 0
[trace_headers @ 0x7fb8d9603a00] 165         vps_timing_info_present_flag                                0 = 0
[trace_headers @ 0x7fb8d9603a00] 166         vps_extension_flag                                          0 = 0
[trace_headers @ 0x7fb8d9603a00] 167         rbsp_stop_one_bit                                           1 = 1
[trace_headers @ 0x7fb8d9603a00] Sequence Parameter Set
[trace_headers @ 0x7fb8d9603a00] 0           forbidden_zero_bit                                          0 = 0
[trace_headers @ 0x7fb8d9603a00] 1           nal_unit_type                                          100001 = 33
[trace_headers @ 0x7fb8d9603a00] 7           nuh_layer_id                                           000000 = 0
[trace_headers @ 0x7fb8d9603a00] 13          nuh_temporal_id_plus1                                     001 = 1
[trace_headers @ 0x7fb8d9603a00] 16          sps_video_parameter_set_id                               0000 = 0
[trace_headers @ 0x7fb8d9603a00] 20          sps_max_sub_layers_minus1                                 000 = 0
[trace_headers @ 0x7fb8d9603a00] 23          sps_temporal_id_nesting_flag                                1 = 1
[trace_headers @ 0x7fb8d9603a00] 24          general_profile_space                                      00 = 0
[trace_headers @ 0x7fb8d9603a00] 26          general_tier_flag                                           0 = 0
[trace_headers @ 0x7fb8d9603a00] 27          general_profile_idc                                     00001 = 1
[trace_headers @ 0x7fb8d9603a00] 32          general_profile_compatibility_flag[0]                       0 = 0
[trace_headers @ 0x7fb8d9603a00] 33          general_profile_compatibility_flag[1]                       1 = 1
[trace_headers @ 0x7fb8d9603a00] 34          general_profile_compatibility_flag[2]                       1 = 1
[trace_headers @ 0x7fb8d9603a00] 35          general_profile_compatibility_flag[3]                       0 = 0
为了节省篇幅,干掉了好多,自己脑补吧。
[trace_headers @ 0x7fb8d9603a00] 57          general_profile_compatibility_flag[25]                      0 = 0
[trace_headers @ 0x7fb8d9603a00] 58          general_profile_compatibility_flag[26]                      0 = 0
[trace_headers @ 0x7fb8d9603a00] 59          general_profile_compatibility_flag[27]                      0 = 0
[trace_headers @ 0x7fb8d9603a00] 60          general_profile_compatibility_flag[28]                      0 = 0
[trace_headers @ 0x7fb8d9603a00] 61          general_profile_compatibility_flag[29]                      0 = 0
[trace_headers @ 0x7fb8d9603a00] 62          general_profile_compatibility_flag[30]                      0 = 0
[trace_headers @ 0x7fb8d9603a00] 63          general_profile_compatibility_flag[31]                      0 = 0
[trace_headers @ 0x7fb8d9603a00] 64          general_progressive_source_flag                             1 = 1
[trace_headers @ 0x7fb8d9603a00] 65          general_interlaced_source_flag                              0 = 0
[trace_headers @ 0x7fb8d9603a00] 66          general_non_packed_constraint_flag                          0 = 0
[trace_headers @ 0x7fb8d9603a00] 67          general_frame_only_constraint_flag                          1 = 1
[trace_headers @ 0x7fb8d9603a00] 68          general_reserved_zero_7bits                           0000000 = 0
[trace_headers @ 0x7fb8d9603a00] 75          general_one_picture_only_constraint_flag                    0 = 0
[trace_headers @ 0x7fb8d9603a00] 76          general_reserved_zero_35bits         000000000000000000000000 = 0
[trace_headers @ 0x7fb8d9603a00] 100         general_reserved_zero_35bits                      00000000000 = 0
[trace_headers @ 0x7fb8d9603a00] 111         general_inbld_flag                                          0 = 0
[trace_headers @ 0x7fb8d9603a00] 112         general_level_idc                                    00111100 = 60
[trace_headers @ 0x7fb8d9603a00] 120         sps_seq_parameter_set_id                                    1 = 0
[trace_headers @ 0x7fb8d9603a00] 121         chroma_format_idc                                         010 = 1
[trace_headers @ 0x7fb8d9603a00] 124         pic_width_in_luma_samples                     000000010110001 = 176
[trace_headers @ 0x7fb8d9603a00] 139         pic_height_in_luma_samples                    000000010010001 = 144
[trace_headers @ 0x7fb8d9603a00] 154         conformance_window_flag                                     0 = 0
[trace_headers @ 0x7fb8d9603a00] 155         bit_depth_luma_minus8                                       1 = 0
[trace_headers @ 0x7fb8d9603a00] 156         bit_depth_chroma_minus8                                     1 = 0
[trace_headers @ 0x7fb8d9603a00] 157         log2_max_pic_order_cnt_lsb_minus4                       00101 = 4
[trace_headers @ 0x7fb8d9603a00] 162         sps_sub_layer_ordering_info_present_flag                    1 = 1
[trace_headers @ 0x7fb8d9603a00] 163         sps_max_dec_pic_buffering_minus1[0]                     00101 = 4
[trace_headers @ 0x7fb8d9603a00] 168         sps_max_num_reorder_pics[0]                               011 = 2
[trace_headers @ 0x7fb8d9603a00] 171         sps_max_latency_increase_plus1[0]                       00110 = 5
[trace_headers @ 0x7fb8d9603a00] 176         log2_min_luma_coding_block_size_minus3                      1 = 0
[trace_headers @ 0x7fb8d9603a00] 177         log2_diff_max_min_luma_coding_block_size                00100 = 3
[trace_headers @ 0x7fb8d9603a00] 182         log2_min_luma_transform_block_size_minus2                   1 = 0
[trace_headers @ 0x7fb8d9603a00] 183         log2_diff_max_min_luma_transform_block_size             00100 = 3
[trace_headers @ 0x7fb8d9603a00] 188         max_transform_hierarchy_depth_inter                         1 = 0
[trace_headers @ 0x7fb8d9603a00] 189         max_transform_hierarchy_depth_intra                         1 = 0
[trace_headers @ 0x7fb8d9603a00] 190         scaling_list_enabled_flag                                   0 = 0
[trace_headers @ 0x7fb8d9603a00] 191         amp_enabled_flag                                            0 = 0
[trace_headers @ 0x7fb8d9603a00] 192         sample_adaptive_offset_enabled_flag                         1 = 1
[trace_headers @ 0x7fb8d9603a00] 193         pcm_enabled_flag                                            0 = 0
[trace_headers @ 0x7fb8d9603a00] 194         num_short_term_ref_pic_sets                                 1 = 0
[trace_headers @ 0x7fb8d9603a00] 195         long_term_ref_pics_present_flag                             0 = 0
[trace_headers @ 0x7fb8d9603a00] 196         sps_temporal_mvp_enabled_flag                               1 = 1
[trace_headers @ 0x7fb8d9603a00] 197         strong_intra_smoothing_enabled_flag                         1 = 1
[trace_headers @ 0x7fb8d9603a00] 198         vui_parameters_present_flag                                 1 = 1
[trace_headers @ 0x7fb8d9603a00] 199         aspect_ratio_info_present_flag                              1 = 1
[trace_headers @ 0x7fb8d9603a00] 200         aspect_ratio_idc                                     00000001 = 1
[trace_headers @ 0x7fb8d9603a00] 208         overscan_info_present_flag                                  0 = 0
[trace_headers @ 0x7fb8d9603a00] 209         video_signal_type_present_flag                              1 = 1
[trace_headers @ 0x7fb8d9603a00] 210         video_format                                              101 = 5
[trace_headers @ 0x7fb8d9603a00] 213         video_full_range_flag                                       0 = 0
[trace_headers @ 0x7fb8d9603a00] 214         colour_description_present_flag                             0 = 0
[trace_headers @ 0x7fb8d9603a00] 215         chroma_loc_info_present_flag                                0 = 0
[trace_headers @ 0x7fb8d9603a00] 216         neutral_chroma_indication_flag                              0 = 0
[trace_headers @ 0x7fb8d9603a00] 217         field_seq_flag                                              0 = 0
[trace_headers @ 0x7fb8d9603a00] 218         frame_field_info_present_flag                               0 = 0
[trace_headers @ 0x7fb8d9603a00] 219         default_display_window_flag                                 0 = 0
[trace_headers @ 0x7fb8d9603a00] 220         vui_timing_info_present_flag                                1 = 1
[trace_headers @ 0x7fb8d9603a00] 221         vui_num_units_in_tick        00000000000000000000000000000001 = 1
[trace_headers @ 0x7fb8d9603a00] 253         vui_time_scale               00000000000000000000000000011001 = 25
[trace_headers @ 0x7fb8d9603a00] 285         vui_poc_proportional_to_timing_flag                         0 = 0
[trace_headers @ 0x7fb8d9603a00] 286         vui_hrd_parameters_present_flag                             0 = 0
[trace_headers @ 0x7fb8d9603a00] 287         bitstream_restriction_flag                                  0 = 0
[trace_headers @ 0x7fb8d9603a00] 288         sps_extension_present_flag                                  0 = 0
[trace_headers @ 0x7fb8d9603a00] 289         rbsp_stop_one_bit                                           1 = 1
[trace_headers @ 0x7fb8d9603a00] 290         rbsp_alignment_zero_bit                                     0 = 0
[trace_headers @ 0x7fb8d9603a00] 291         rbsp_alignment_zero_bit                                     0 = 0
[trace_headers @ 0x7fb8d9603a00] 292         rbsp_alignment_zero_bit                                     0 = 0
[trace_headers @ 0x7fb8d9603a00] 293         rbsp_alignment_zero_bit                                     0 = 0
[trace_headers @ 0x7fb8d9603a00] 294         rbsp_alignment_zero_bit                                     0 = 0
[trace_headers @ 0x7fb8d9603a00] 295         rbsp_alignment_zero_bit                                     0 = 0
[trace_headers @ 0x7fb8d9603a00] Picture Parameter Set
[trace_headers @ 0x7fb8d9603a00] 0           forbidden_zero_bit                                          0 = 0
[trace_headers @ 0x7fb8d9603a00] 1           nal_unit_type                                          100010 = 34
[trace_headers @ 0x7fb8d9603a00] 7           nuh_layer_id                                           000000 = 0
[trace_headers @ 0x7fb8d9603a00] 13          nuh_temporal_id_plus1                                     001 = 1
[trace_headers @ 0x7fb8d9603a00] 16          pps_pic_parameter_set_id                                    1 = 0
[trace_headers @ 0x7fb8d9603a00] 17          pps_seq_parameter_set_id                                    1 = 0
[trace_headers @ 0x7fb8d9603a00] 18          dependent_slice_segments_enabled_flag                       0 = 0
[trace_headers @ 0x7fb8d9603a00] 19          output_flag_present_flag                                    0 = 0
[trace_headers @ 0x7fb8d9603a00] 20          num_extra_slice_header_bits                               000 = 0
[trace_headers @ 0x7fb8d9603a00] 23          sign_data_hiding_enabled_flag                               1 = 1
[trace_headers @ 0x7fb8d9603a00] 24          cabac_init_present_flag                                     0 = 0
[trace_headers @ 0x7fb8d9603a00] 25          num_ref_idx_l0_default_active_minus1                        1 = 0
[trace_headers @ 0x7fb8d9603a00] 26          num_ref_idx_l1_default_active_minus1                        1 = 0
[trace_headers @ 0x7fb8d9603a00] 27          init_qp_minus26                                             1 = 0
[trace_headers @ 0x7fb8d9603a00] 28          constrained_intra_pred_flag                                 0 = 0
[trace_headers @ 0x7fb8d9603a00] 29          transform_skip_enabled_flag                                 0 = 0
[trace_headers @ 0x7fb8d9603a00] 30          cu_qp_delta_enabled_flag                                    1 = 1
[trace_headers @ 0x7fb8d9603a00] 31          diff_cu_qp_delta_depth                                    010 = 1
[trace_headers @ 0x7fb8d9603a00] 34          pps_cb_qp_offset                                            1 = 0
[trace_headers @ 0x7fb8d9603a00] 35          pps_cr_qp_offset                                            1 = 0
[trace_headers @ 0x7fb8d9603a00] 36          pps_slice_chroma_qp_offsets_present_flag                    0 = 0
[trace_headers @ 0x7fb8d9603a00] 37          weighted_pred_flag                                          1 = 1
[trace_headers @ 0x7fb8d9603a00] 38          weighted_bipred_flag                                        0 = 0
[trace_headers @ 0x7fb8d9603a00] 39          transquant_bypass_enabled_flag                              0 = 0
[trace_headers @ 0x7fb8d9603a00] 40          tiles_enabled_flag                                          0 = 0
[trace_headers @ 0x7fb8d9603a00] 41          entropy_coding_sync_enabled_flag                            1 = 1
[trace_headers @ 0x7fb8d9603a00] 42          pps_loop_filter_across_slices_enabled_flag                  1 = 1
[trace_headers @ 0x7fb8d9603a00] 43          deblocking_filter_control_present_flag                      0 = 0
[trace_headers @ 0x7fb8d9603a00] 44          pps_scaling_list_data_present_flag                          0 = 0
[trace_headers @ 0x7fb8d9603a00] 45          lists_modification_present_flag                             0 = 0
[trace_headers @ 0x7fb8d9603a00] 46          log2_parallel_merge_level_minus2                            1 = 0
[trace_headers @ 0x7fb8d9603a00] 47          slice_segment_header_extension_present_flag                 0 = 0
[trace_headers @ 0x7fb8d9603a00] 48          pps_extension_present_flag                                  0 = 0
[trace_headers @ 0x7fb8d9603a00] 49          rbsp_stop_one_bit                                           1 = 1
[trace_headers @ 0x7fb8d9603a00] 50          rbsp_alignment_zero_bit                                     0 = 0
[trace_headers @ 0x7fb8d9603a00] 51          rbsp_alignment_zero_bit                                     0 = 0
[trace_headers @ 0x7fb8d9603a00] 52          rbsp_alignment_zero_bit                                     0 = 0
[trace_headers @ 0x7fb8d9603a00] 53          rbsp_alignment_zero_bit                                     0 = 0
[trace_headers @ 0x7fb8d9603a00] 54          rbsp_alignment_zero_bit                                     0 = 0
[trace_headers @ 0x7fb8d9603a00] 55          rbsp_alignment_zero_bit                                     0 = 0
[trace_headers @ 0x7fb8d9603a00] Prefix Supplemental Enhancement Information
[trace_headers @ 0x7fb8d9603a00] 0           forbidden_zero_bit                                          0 = 0
[trace_headers @ 0x7fb8d9603a00] 1           nal_unit_type                                          100111 = 39
[trace_headers @ 0x7fb8d9603a00] 7           nuh_layer_id                                           000000 = 0
[trace_headers @ 0x7fb8d9603a00] 13          nuh_temporal_id_plus1                                     001 = 1
[trace_headers @ 0x7fb8d9603a00] 16          last_payload_type_byte                               00000101 = 5
[trace_headers @ 0x7fb8d9603a00] 24          ff_byte                                              11111111 = 255
[trace_headers @ 0x7fb8d9603a00] 32          ff_byte                                              11111111 = 255
[trace_headers @ 0x7fb8d9603a00] 40          ff_byte                                              11111111 = 255
[trace_headers @ 0x7fb8d9603a00] 48          ff_byte                                              11111111 = 255
[trace_headers @ 0x7fb8d9603a00] 56          ff_byte                                              11111111 = 255
[trace_headers @ 0x7fb8d9603a00] 64          ff_byte                                              11111111 = 255
[trace_headers @ 0x7fb8d9603a00] 72          ff_byte                                              11111111 = 255
[trace_headers @ 0x7fb8d9603a00] 80          last_payload_size_byte                               10101001 = 169
[trace_headers @ 0x7fb8d9603a00] User Data Unregistered
[trace_headers @ 0x7fb8d9603a00] 88          uuid_iso_iec_11578[0]                                00101100 = 44
[trace_headers @ 0x7fb8d9603a00] 96          uuid_iso_iec_11578[1]                                10100010 = 162
[trace_headers @ 0x7fb8d9603a00] 104         uuid_iso_iec_11578[2]                                11011110 = 222
。。。。。。 为了节省篇幅,好多东西干掉了,自己脑补吧。
[trace_headers @ 0x7fb8d9603a00] 15724       rbsp_alignment_zero_bit                                     0 = 0
[trace_headers @ 0x7fb8d9603a00] 15725       rbsp_alignment_zero_bit                                     0 = 0
[trace_headers @ 0x7fb8d9603a00] 15726       rbsp_alignment_zero_bit                                     0 = 0
[trace_headers @ 0x7fb8d9603a00] 15727       rbsp_alignment_zero_bit                                     0 = 0
Output #0, mp4, to 'output.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2mp41
    encoder         : Lavf58.64.100
    Stream #0:0(und): Video: hevc (Main) (hev1 / 0x31766568), yuv420p(tv, progressive), 176x144 [SAR 1:1 DAR 11:9], q=2-31, 6 kb/s, 25 fps, 25 tbr, 12800 tbn, 12800 tbc (default)
    Metadata:
      handler_name    : VideoHandler
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
Press [q] to stop, [?] for help
[trace_headers @ 0x7fb8d9603a00] Packet: 38 bytes, key frame, pts 0, dts 0, duration 512.
[trace_headers @ 0x7fb8d9603a00] Slice Segment Header
[trace_headers @ 0x7fb8d9603a00] 0           forbidden_zero_bit                                          0 = 0
[trace_headers @ 0x7fb8d9603a00] 1           nal_unit_type                                          010100 = 20
[trace_headers @ 0x7fb8d9603a00] 7           nuh_layer_id                                           000000 = 0
[trace_headers @ 0x7fb8d9603a00] 13          nuh_temporal_id_plus1                                     001 = 1
[trace_headers @ 0x7fb8d9603a00] 16          first_slice_segment_in_pic_flag                             1 = 1
[trace_headers @ 0x7fb8d9603a00] 17          no_output_of_prior_pics_flag                                0 = 0
[trace_headers @ 0x7fb8d9603a00] 18          slice_pic_parameter_set_id                                  1 = 0
[trace_headers @ 0x7fb8d9603a00] 19          slice_type                                                011 = 2
[trace_headers @ 0x7fb8d9603a00] 22          slice_sao_luma_flag                                         1 = 1
[trace_headers @ 0x7fb8d9603a00] 23          slice_sao_chroma_flag                                       1 = 1
[trace_headers @ 0x7fb8d9603a00] 24          slice_qp_delta                                        0001110 = 7
[trace_headers @ 0x7fb8d9603a00] 31          slice_loop_filter_across_slices_enabled_flag                1 = 1
[trace_headers @ 0x7fb8d9603a00] 32          num_entry_point_offsets                                   011 = 2
[trace_headers @ 0x7fb8d9603a00] 35          offset_len_minus1                                       00100 = 3
[trace_headers @ 0x7fb8d9603a00] 40          entry_point_offset_minus1[0]                             1110 = 14
[trace_headers @ 0x7fb8d9603a00] 44          entry_point_offset_minus1[1]                             0110 = 6
[trace_headers @ 0x7fb8d9603a00] 48          alignment_bit_equal_to_one                                  1 = 1
[trace_headers @ 0x7fb8d9603a00] 49          alignment_bit_equal_to_zero                                 0 = 0
[trace_headers @ 0x7fb8d9603a00] 50          alignment_bit_equal_to_zero                                 0 = 0
[trace_headers @ 0x7fb8d9603a00] 51          alignment_bit_equal_to_zero                                 0 = 0
[trace_headers @ 0x7fb8d9603a00] 52          alignment_bit_equal_to_zero                                 0 = 0
[trace_headers @ 0x7fb8d9603a00] 53          alignment_bit_equal_to_zero                                 0 = 0
[trace_headers @ 0x7fb8d9603a00] 54          alignment_bit_equal_to_zero                                 0 = 0
[trace_headers @ 0x7fb8d9603a00] 55          alignment_bit_equal_to_zero                                 0 = 0
[trace_headers @ 0x7fb8d9603a00] Packet: 23 bytes, pts 512, dts 512, duration 512.
[trace_headers @ 0x7fb8d9603a00] Slice Segment Header
[trace_headers @ 0x7fb8d9603a00] 0           forbidden_zero_bit                                          0 = 0
[trace_headers @ 0x7fb8d9603a00] 1           nal_unit_type                                          000001 = 1
[trace_headers @ 0x7fb8d9603a00] 7           nuh_layer_id                                           000000 = 0
[trace_headers @ 0x7fb8d9603a00] 13          nuh_temporal_id_plus1                                     001 = 1
[trace_headers @ 0x7fb8d9603a00] 16          first_slice_segment_in_pic_flag                             1 = 1
[trace_headers @ 0x7fb8d9603a00] 17          slice_pic_parameter_set_id                                  1 = 0
[trace_headers @ 0x7fb8d9603a00] 18          slice_type                                                010 = 1
[trace_headers @ 0x7fb8d9603a00] 21          slice_pic_order_cnt_lsb                              00000001 = 1
[trace_headers @ 0x7fb8d9603a00] 29          short_term_ref_pic_set_sps_flag                             0 = 0
[trace_headers @ 0x7fb8d9603a00] 30          num_negative_pics                                         010 = 1
[trace_headers @ 0x7fb8d9603a00] 33          num_positive_pics                                           1 = 0
[trace_headers @ 0x7fb8d9603a00] 34          delta_poc_s0_minus1[0]                                      1 = 0
[trace_headers @ 0x7fb8d9603a00] 35          used_by_curr_pic_s0_flag[0]                                 1 = 1
[trace_headers @ 0x7fb8d9603a00] 36          slice_temporal_mvp_enabled_flag                             1 = 1
[trace_headers @ 0x7fb8d9603a00] 37          slice_sao_luma_flag                                         1 = 1
[trace_headers @ 0x7fb8d9603a00] 38          slice_sao_chroma_flag                                       1 = 1
[trace_headers @ 0x7fb8d9603a00] 39          num_ref_idx_active_override_flag                            0 = 0
[trace_headers @ 0x7fb8d9603a00] 40          luma_log2_weight_denom                                0001000 = 7
[trace_headers @ 0x7fb8d9603a00] 47          delta_chroma_log2_weight_denom                            011 = -1
[trace_headers @ 0x7fb8d9603a00] 50          luma_weight_l0_flag[0]                                      0 = 0
[trace_headers @ 0x7fb8d9603a00] 51          chroma_weight_l0_flag[0]                                    0 = 0
[trace_headers @ 0x7fb8d9603a00] 52          five_minus_max_num_merge_cand                           00100 = 3
[trace_headers @ 0x7fb8d9603a00] 57          slice_qp_delta                                        0001110 = 7
[trace_headers @ 0x7fb8d9603a00] 64          slice_loop_filter_across_slices_enabled_flag                1 = 1
[trace_headers @ 0x7fb8d9603a00] 65          num_entry_point_offsets                                   011 = 2
[trace_headers @ 0x7fb8d9603a00] 68          offset_len_minus1                                         011 = 2
[trace_headers @ 0x7fb8d9603a00] 71          entry_point_offset_minus1[0]                              010 = 2
[trace_headers @ 0x7fb8d9603a00] 74          entry_point_offset_minus1[1]                              011 = 3
[trace_headers @ 0x7fb8d9603a00] 77          alignment_bit_equal_to_one                                  1 = 1
[trace_headers @ 0x7fb8d9603a00] 78          alignment_bit_equal_to_zero                                 0 = 0
[trace_headers @ 0x7fb8d9603a00] 79          alignment_bit_equal_to_zero                                 0 = 0
frame=    2 fps=0.0 q=-1.0 Lsize=       3kB time=00:00:00.04 bitrate= 592.8kbits/s speed=7.63x
video:0kB audio:0kB subtitle:0kB other streams:0kB global headers:2kB muxing overhead: 4768.852539%
liuqideMBP:clang liuqi$

数据比较多,但是当我学习HEVC的解析的时候,这些数据还是需要一个一个位,去解析的,当然,如果只是支持HLS的EXT-X-STREAM-INF中存储CODECS信息的话,只解析Profile、Tier、Level基本上就足够的。也就是最开始那一部分,在参考标准中,Profile、Tier、Level是可以从VPS(Video Parameter Set)中获得、也可以从SPS(Sequence Parameter Set)中获得,考虑到有些编码器不一定带VPS信息,所以只从SPS中读取比较稳妥,然后就可以开启解析SPS的旅程。

从上面输出的内容中,我们可以先拿到SPS部分抽出来单独看:


[trace_headers @ 0x7fb8d9603a00] Sequence Parameter Set
[trace_headers @ 0x7fb8d9603a00] 0           forbidden_zero_bit                                          0 = 0
[trace_headers @ 0x7fb8d9603a00] 1           nal_unit_type                                          100001 = 33
[trace_headers @ 0x7fb8d9603a00] 7           nuh_layer_id                                           000000 = 0
[trace_headers @ 0x7fb8d9603a00] 13          nuh_temporal_id_plus1                                     001 = 1
[trace_headers @ 0x7fb8d9603a00] 16          sps_video_parameter_set_id                               0000 = 0
[trace_headers @ 0x7fb8d9603a00] 20          sps_max_sub_layers_minus1                                 000 = 0
[trace_headers @ 0x7fb8d9603a00] 23          sps_temporal_id_nesting_flag                                1 = 1
[trace_headers @ 0x7fb8d9603a00] 24          general_profile_space                                      00 = 0
[trace_headers @ 0x7fb8d9603a00] 26          general_tier_flag                                           0 = 0
[trace_headers @ 0x7fb8d9603a00] 27          general_profile_idc                                     00001 = 1
[trace_headers @ 0x7fb8d9603a00] 32          general_profile_compatibility_flag[0]                       0 = 0
[trace_headers @ 0x7fb8d9603a00] 56          general_profile_compatibility_flag[24]                      0 = 0
。。。为了节省篇幅,干掉了好多,自己脑补。
[trace_headers @ 0x7fb8d9603a00] 62          general_profile_compatibility_flag[30]                      0 = 0
[trace_headers @ 0x7fb8d9603a00] 63          general_profile_compatibility_flag[31]                      0 = 0
[trace_headers @ 0x7fb8d9603a00] 64          general_progressive_source_flag                             1 = 1
[trace_headers @ 0x7fb8d9603a00] 65          general_interlaced_source_flag                              0 = 0
[trace_headers @ 0x7fb8d9603a00] 66          general_non_packed_constraint_flag                          0 = 0
[trace_headers @ 0x7fb8d9603a00] 67          general_frame_only_constraint_flag                          1 = 1
[trace_headers @ 0x7fb8d9603a00] 68          general_reserved_zero_7bits                           0000000 = 0
[trace_headers @ 0x7fb8d9603a00] 75          general_one_picture_only_constraint_flag                    0 = 0
[trace_headers @ 0x7fb8d9603a00] 76          general_reserved_zero_35bits         000000000000000000000000 = 0
[trace_headers @ 0x7fb8d9603a00] 100         general_reserved_zero_35bits                      00000000000 = 0
[trace_headers @ 0x7fb8d9603a00] 111         general_inbld_flag                                          0 = 0
[trace_headers @ 0x7fb8d9603a00] 112         general_level_idc                                    00111100 = 60
[trace_headers @ 0x7fb8d9603a00] 120         sps_seq_parameter_set_id                                    1 = 0
[trace_headers @ 0x7fb8d9603a00] 121         chroma_format_idc                                         010 = 1
[trace_headers @ 0x7fb8d9603a00] 124         pic_width_in_luma_samples                     000000010110001 = 176
[trace_headers @ 0x7fb8d9603a00] 139         pic_height_in_luma_samples                    000000010010001 = 144
[trace_headers @ 0x7fb8d9603a00] 154         conformance_window_flag                                     0 = 0
[trace_headers @ 0x7fb8d9603a00] 155         bit_depth_luma_minus8                                       1 = 0
[trace_headers @ 0x7fb8d9603a00] 156         bit_depth_chroma_minus8                                     1 = 0
[trace_headers @ 0x7fb8d9603a00] 157         log2_max_pic_order_cnt_lsb_minus4                       00101 = 4
[trace_headers @ 0x7fb8d9603a00] 162         sps_sub_layer_ordering_info_present_flag                    1 = 1
[trace_headers @ 0x7fb8d9603a00] 163         sps_max_dec_pic_buffering_minus1[0]                     00101 = 4
[trace_headers @ 0x7fb8d9603a00] 168         sps_max_num_reorder_pics[0]                               011 = 2
[trace_headers @ 0x7fb8d9603a00] 171         sps_max_latency_increase_plus1[0]                       00110 = 5
[trace_headers @ 0x7fb8d9603a00] 176         log2_min_luma_coding_block_size_minus3                      1 = 0
[trace_headers @ 0x7fb8d9603a00] 177         log2_diff_max_min_luma_coding_block_size                00100 = 3
[trace_headers @ 0x7fb8d9603a00] 182         log2_min_luma_transform_block_size_minus2                   1 = 0
[trace_headers @ 0x7fb8d9603a00] 183         log2_diff_max_min_luma_transform_block_size             00100 = 3
[trace_headers @ 0x7fb8d9603a00] 188         max_transform_hierarchy_depth_inter                         1 = 0
[trace_headers @ 0x7fb8d9603a00] 189         max_transform_hierarchy_depth_intra                         1 = 0
[trace_headers @ 0x7fb8d9603a00] 190         scaling_list_enabled_flag                                   0 = 0
[trace_headers @ 0x7fb8d9603a00] 191         amp_enabled_flag                                            0 = 0
[trace_headers @ 0x7fb8d9603a00] 192         sample_adaptive_offset_enabled_flag                         1 = 1
[trace_headers @ 0x7fb8d9603a00] 193         pcm_enabled_flag                                            0 = 0
[trace_headers @ 0x7fb8d9603a00] 194         num_short_term_ref_pic_sets                                 1 = 0
[trace_headers @ 0x7fb8d9603a00] 195         long_term_ref_pics_present_flag                             0 = 0
[trace_headers @ 0x7fb8d9603a00] 196         sps_temporal_mvp_enabled_flag                               1 = 1
[trace_headers @ 0x7fb8d9603a00] 197         strong_intra_smoothing_enabled_flag                         1 = 1
[trace_headers @ 0x7fb8d9603a00] 198         vui_parameters_present_flag                                 1 = 1
[trace_headers @ 0x7fb8d9603a00] 199         aspect_ratio_info_present_flag                              1 = 1
[trace_headers @ 0x7fb8d9603a00] 200         aspect_ratio_idc                                     00000001 = 1
[trace_headers @ 0x7fb8d9603a00] 208         overscan_info_present_flag                                  0 = 0
[trace_headers @ 0x7fb8d9603a00] 209         video_signal_type_present_flag                              1 = 1
[trace_headers @ 0x7fb8d9603a00] 210         video_format                                              101 = 5
[trace_headers @ 0x7fb8d9603a00] 213         video_full_range_flag                                       0 = 0
[trace_headers @ 0x7fb8d9603a00] 214         colour_description_present_flag                             0 = 0
[trace_headers @ 0x7fb8d9603a00] 215         chroma_loc_info_present_flag                                0 = 0
[trace_headers @ 0x7fb8d9603a00] 216         neutral_chroma_indication_flag                              0 = 0
[trace_headers @ 0x7fb8d9603a00] 217         field_seq_flag                                              0 = 0
[trace_headers @ 0x7fb8d9603a00] 218         frame_field_info_present_flag                               0 = 0
[trace_headers @ 0x7fb8d9603a00] 219         default_display_window_flag                                 0 = 0
[trace_headers @ 0x7fb8d9603a00] 220         vui_timing_info_present_flag                                1 = 1
[trace_headers @ 0x7fb8d9603a00] 221         vui_num_units_in_tick        00000000000000000000000000000001 = 1
[trace_headers @ 0x7fb8d9603a00] 253         vui_time_scale               00000000000000000000000000011001 = 25
[trace_headers @ 0x7fb8d9603a00] 285         vui_poc_proportional_to_timing_flag                         0 = 0
[trace_headers @ 0x7fb8d9603a00] 286         vui_hrd_parameters_present_flag                             0 = 0
[trace_headers @ 0x7fb8d9603a00] 287         bitstream_restriction_flag                                  0 = 0
[trace_headers @ 0x7fb8d9603a00] 288         sps_extension_present_flag                                  0 = 0
[trace_headers @ 0x7fb8d9603a00] 289         rbsp_stop_one_bit                                           1 = 1
[trace_headers @ 0x7fb8d9603a00] 290         rbsp_alignment_zero_bit                                     0 = 0
[trace_headers @ 0x7fb8d9603a00] 291         rbsp_alignment_zero_bit                                     0 = 0
[trace_headers @ 0x7fb8d9603a00] 292         rbsp_alignment_zero_bit                                     0 = 0
[trace_headers @ 0x7fb8d9603a00] 293         rbsp_alignment_zero_bit                                     0 = 0
[trace_headers @ 0x7fb8d9603a00] 294         rbsp_alignment_zero_bit                                     0 = 0
[trace_headers @ 0x7fb8d9603a00] 295         rbsp_alignment_zero_bit   

然后拿出参考标准对比着看一下内容:

解析的顺序如下:

图片
图片

解析的时候需要注意几个点,就是处理文件中的数据位RBSP先,也就是处理0x000003,而处理RBSP的相关操作在参考标准中也有详细的说明:

图片

如果看不懂的话,可以翻开参考标准,找到对应的章节,然后用google翻译翻译一下,或者有道翻译也行。

好了,HEVC 的 profile、tier、level 的解析到这里应该就可以搞定了,解析出来后,怎么存呢?需要看一下HLS怎么说的,因为只看RFC8216的话,理解起来可能稍微麻烦一些,所以苹果给出了参考例子:

参考链接如下:

https://developer.apple.com/documentation/http_live_streaming/hls_authoring_specification_for_apple_devices/hls_authoring_specification_for_apple_devices_appendixes

直接跳到Example Playlist一节:


The following is an example of a playlist with Dolby Vision, HDR10, and SDR content at resolutions from 720p to 4K.

#EXTM3U
#EXT-X-VERSION:7
#EXT-X-INDEPENDENT-SEGMENTS
#EXT-X-STREAM-INF:AVERAGE-BANDWIDTH=2778321,BANDWIDTH=3971374,VIDEO-RANGE=SDR,CODECS="hvc1.2.4.L123.B0",RESOLUTION=1280x720,FRAME-RATE=23.976,CLOSED-CAPTIONS=NONE,HDCP-LEVEL=NONE
sdr_720/prog_index.m3u8
#EXT-X-STREAM-INF:AVERAGE-BANDWIDTH=6759875,BANDWIDTH=10022043,VIDEO-RANGE=SDR,CODECS="hvc1.2.4.L123.B0",RESOLUTION=1920x1080,FRAME-RATE=23.976,CLOSED-CAPTIONS=NONE,HDCP-LEVEL=TYPE-0
sdr_1080/prog_index.m3u8
#EXT-X-STREAM-INF:AVERAGE-BANDWIDTH=20985770,BANDWIDTH=28058971,VIDEO-RANGE=SDR,CODECS="hvc1.2.4.L150.B0",RESOLUTION=3840x2160,FRAME-RATE=23.976,CLOSED-CAPTIONS=NONE,HDCP-LEVEL=TYPE-1
sdr_2160/prog_index.m3u8
#EXT-X-STREAM-INF:AVERAGE-BANDWIDTH=3385450,BANDWIDTH=5327059,VIDEO-RANGE=PQ,CODECS="dvh1.05.01",RESOLUTION=1280x720,FRAME-RATE=23.976,CLOSED-CAPTIONS=NONE,HDCP-LEVEL=NONE
dolby_720/prog_index.m3u8
#EXT-X-STREAM-INF:AVERAGE-BANDWIDTH=7999361,BANDWIDTH=12876596,VIDEO-RANGE=PQ,CODECS="dvh1.05.03",RESOLUTION=1920x1080,FRAME-RATE=23.976,CLOSED-CAPTIONS=NONE,HDCP-LEVEL=TYPE-0
dolby_1080/prog_index.m3u8
#EXT-X-STREAM-INF:AVERAGE-BANDWIDTH=24975091,BANDWIDTH=30041698,VIDEO-RANGE=PQ,CODECS="dvh1.05.06",RESOLUTION=3840x2160,FRAME-RATE=23.976,CLOSED-CAPTIONS=NONE,HDCP-LEVEL=TYPE-1
dolby_2160/prog_index.m3u8
#EXT-X-STREAM-INF:AVERAGE-BANDWIDTH=3320040,BANDWIDTH=5280654,VIDEO-RANGE=PQ,CODECS="hvc1.2.4.L123.B0",RESOLUTION=1280x720,FRAME-RATE=23.976,CLOSED-CAPTIONS=NONE,HDCP-LEVEL=NONE
hdr10_720/prog_index.m3u8
#EXT-X-STREAM-INF:AVERAGE-BANDWIDTH=7964551,BANDWIDTH=12886714,VIDEO-RANGE=PQ,CODECS="hvc1.2.4.L123.B0",RESOLUTION=1920x1080,FRAME-RATE=23.976,CLOSED-CAPTIONS=NONE,HDCP-LEVEL=TYPE-0
hdr10_1080/prog_index.m3u8
#EXT-X-STREAM-INF:AVERAGE-BANDWIDTH=24833402,BANDWIDTH=29983769,VIDEO-RANGE=PQ,CODECS="hvc1.2.4.L150.B0",RESOLUTION=3840x2160,FRAME-RATE=23.976,CLOSED-CAPTIONS=NONE,HDCP-LEVEL=TYPE-1
hdr10_2160/prog_index.m3u8
#EXT-X-I-FRAME-STREAM-INF:AVERAGE-BANDWIDTH=248586,BANDWIDTH=593626,VIDEO-RANGE=SDR,CODECS="hvc1.2.4.L123.B0",RESOLUTION=1280x720,HDCP-LEVEL=NONE,URI="sdr_720/iframe_index.m3u8"
#EXT-X-I-FRAME-STREAM-INF:AVERAGE-BANDWIDTH=399790,BANDWIDTH=956552,VIDEO-RANGE=SDR,CODECS="hvc1.2.4.L123.B0",RESOLUTION=1920x1080,HDCP-LEVEL=TYPE-0,URI="sdr_1080/iframe_index.m3u8"
#EXT-X-I-FRAME-STREAM-INF:AVERAGE-BANDWIDTH=826971,BANDWIDTH=1941397,VIDEO-RANGE=SDR,CODECS="hvc1.2.4.L150.B0",RESOLUTION=3840x2160,HDCP-LEVEL=TYPE-1,URI="sdr_2160/iframe_index.m3u8"
#EXT-X-I-FRAME-STREAM-INF:AVERAGE-BANDWIDTH=232253,BANDWIDTH=573073,VIDEO-RANGE=PQ,CODECS="dvh1.05.01",RESOLUTION=1280x720,HDCP-LEVEL=NONE,URI="dolby_720/iframe_index.m3u8"
#EXT-X-I-FRAME-STREAM-INF:AVERAGE-BANDWIDTH=365337,BANDWIDTH=905037,VIDEO-RANGE=PQ,CODECS="dvh1.05.03",RESOLUTION=1920x1080,HDCP-LEVEL=TYPE-0,URI="dolby_1080/iframe_index.m3u8"
#EXT-X-I-FRAME-STREAM-INF:AVERAGE-BANDWIDTH=739114,BANDWIDTH=1893236,VIDEO-RANGE=PQ,CODECS="dvh1.05.06",RESOLUTION=3840x2160,HDCP-LEVEL=TYPE-1,URI="dolby_2160/iframe_index.m3u8"
#EXT-X-I-FRAME-STREAM-INF:AVERAGE-BANDWIDTH=232511,BANDWIDTH=572673,VIDEO-RANGE=PQ,CODECS="hvc1.2.4.L123.B0",RESOLUTION=1280x720,HDCP-LEVEL=NONE,URI="hdr10_720/iframe_index.m3u8"
#EXT-X-I-FRAME-STREAM-INF:AVERAGE-BANDWIDTH=364552,BANDWIDTH=905053,VIDEO-RANGE=PQ,CODECS="hvc1.2.4.L123.B0",RESOLUTION=1920x1080,HDCP-LEVEL=TYPE-0,URI="hdr10_1080/iframe_index.m3u8"
#EXT-X-I-FRAME-STREAM-INF:AVERAGE-BANDWIDTH=739757,BANDWIDTH=1895477,VIDEO-RANGE=PQ,CODECS="hvc1.2.4.L150.B0",RESOLUTION=3840x2160,HDCP-LEVEL=TYPE-1,URI="hdr10_2160/iframe_index.m3u8"
The HEVC codec values are described in ISO/IEC 14496-15.
A short description is 
"Codec.Profile.Flags.TierLevel.Constraints",
so in hvc1.2.4.L123.B0, 
the 2 means Main 10 profile, 
and the L123 means normal tier, level 4.1.

The Dolby Video codec values are described in
Dolby Vision streams within the HTTP Live Streaming
format v1.1. A short description is Codec.Profile.Level,
so in dvh1.05.03 the Profile is 5 and the Level is 3.

上面的信息中能够看到有相关的解释,重点是最后两段。

CODECS信息展示的顺序为“Codec.Profile.Flags.TierLevel.Constraints”

从example的例子可以拿出来看到是这样:”hvc1.2.4.L150.B0″

而从最开始我们的input文件中拿到的信息,应该是这样”hvc1.1.4.L60.B01″

因为profile和level读到的值是这样:

[trace_headers @ 0x7fb8d9603a00] 27          general_profile_idc                                     00001 = 1
[trace_headers @ 0x7fb8d9603a00] 112         general_level_idc         

所以代码实现是这样:


if (st->codecpar->codec_id == AV_CODEC_ID_HEVC) {
        uint8_t *data = st->codecpar->extradata;
        int profile = FF_PROFILE_UNKNOWN;
        int level = FF_LEVEL_UNKNOWN;

        if (st->codecpar->profile != FF_PROFILE_UNKNOWN)
            profile = st->codecpar->profile;
        if (st->codecpar->level != FF_LEVEL_UNKNOWN)
            level = st->codecpar->level;

        /* check the boundary of data which from current position is small than extradata_size */
        while (data && (data - st->codecpar->extradata + 19) < st->codecpar->extradata_size) {
            /* get HEVC SPS NAL and seek to profile_tier_level */
            if (!(data[0] | data[1] | data[2]) && data[3] == 1 && ((data[4] & 0x7E) == 0x42)) {
                uint8_t *rbsp_buf;
                int remain_size = 0;
                int rbsp_size = 0;
                /* skip start code + nalu header */
                data += 6;
                /* process by reference General NAL unit syntax */
                remain_size = st->codecpar->extradata_size - (data - st->codecpar->extradata);
                rbsp_buf = ff_nal_unit_extract_rbsp(data, remain_size, &rbsp_size, 0);
                if (!rbsp_buf)
                    return;
                if (rbsp_size < 13) {
                    av_freep(&rbsp_buf);
                    break;
                }
                /* skip sps_video_parameter_set_id   u(4),
                 *      sps_max_sub_layers_minus1    u(3),
                 *  and sps_temporal_id_nesting_flag u(1) */
                profile = rbsp_buf[1] & 0x1f;
                /* skip 8 + 8 + 32 + 4 + 43 + 1 bit */
                level = rbsp_buf[12];
                av_freep(&rbsp_buf);
                break;
            }
            data++;
        }
        if (st->codecpar->codec_tag == MKTAG('h','v','c','1') &&
            profile != FF_PROFILE_UNKNOWN &&
            level != FF_LEVEL_UNKNOWN) {
            snprintf(attr, sizeof(attr), "%s.%d.4.L%d.B01", av_fourcc2str(st->codecpar->codec_tag), profile, level);
        } else
            goto fail;
    }

到这里在 FFmpeg 中解析 HEVC 的 extradata 数据就完成了,接下来看一下执行的效果:

./ffmpeg -i input.mp4 -c copy -bsf:v trace_headers -y -tag:v hvc1 -master_pl_name index.m3u8  output.m3u8

说明一下:因为ffmpeg默认生成mp4的时候是Hev1的存储,所以这里需要手动设置一下tag为hvc1,因为苹果推荐这样。

然后查看一下生成的m3u8内容


#EXTM3U
#EXT-X-VERSION:3
#EXT-X-STREAM-INF:BANDWIDTH=6710,RESOLUTION=176x144,CODECS="hvc1.1.4.L60.B01"
output.m3u8

完全符合预期,到这里,使用 FFmpeg 分析编码的数据信息的操作就完成了。

作者:大师兄悟空 公众号:流媒体技术

版权声明:本文内容转自互联网,本文观点仅代表作者本人。本站仅提供信息存储空间服务,所有权归原作者所有。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至1393616908@qq.com 举报,一经查实,本站将立刻删除。

(0)

相关推荐

发表回复

登录后才能评论