tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

H264.h (24723B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 #ifndef MP4_DEMUXER_H264_H_
      6 #define MP4_DEMUXER_H264_H_
      7 
      8 #include <stdint.h>
      9 
     10 #include "ErrorList.h"
     11 #include "mozilla/AlreadyAddRefed.h"
     12 #include "mozilla/Maybe.h"
     13 #include "mozilla/Result.h"
     14 #include "mozilla/Span.h"
     15 #include "mozilla/gfx/Point.h"
     16 #include "mozilla/gfx/Types.h"
     17 #include "nsTArray.h"
     18 
     19 namespace mozilla {
     20 class BitReader;
     21 class MediaByteBuffer;
     22 class MediaRawData;
     23 
     24 enum H264_PROFILE {
     25  H264_PROFILE_UNKNOWN = 0,
     26  H264_PROFILE_BASE = 0x42,
     27  H264_PROFILE_MAIN = 0x4D,
     28  H264_PROFILE_EXTENDED = 0x58,
     29  H264_PROFILE_HIGH = 0x64,
     30 };
     31 
     32 enum class H264_LEVEL {
     33  H264_LEVEL_1 = 10,
     34  H264_LEVEL_1_b = 11,
     35  H264_LEVEL_1_1 = 11,
     36  H264_LEVEL_1_2 = 12,
     37  H264_LEVEL_1_3 = 13,
     38  H264_LEVEL_2 = 20,
     39  H264_LEVEL_2_1 = 21,
     40  H264_LEVEL_2_2 = 22,
     41  H264_LEVEL_3 = 30,
     42  H264_LEVEL_3_1 = 31,
     43  H264_LEVEL_3_2 = 32,
     44  H264_LEVEL_4 = 40,
     45  H264_LEVEL_4_1 = 41,
     46  H264_LEVEL_4_2 = 42,
     47  H264_LEVEL_5 = 50,
     48  H264_LEVEL_5_1 = 51,
     49  H264_LEVEL_5_2 = 52,
     50  H264_LEVEL_6 = 60,
     51  H264_LEVEL_6_1 = 61,
     52  H264_LEVEL_6_2 = 62
     53 };
     54 
     55 // Spec 7.4.2.1
     56 #define MAX_SPS_COUNT 32
     57 #define MAX_PPS_COUNT 256
     58 
     59 // NAL unit types
     60 enum NAL_TYPES {
     61  H264_NAL_SLICE = 1,
     62  H264_NAL_DPA = 2,
     63  H264_NAL_DPB = 3,
     64  H264_NAL_DPC = 4,
     65  H264_NAL_IDR_SLICE = 5,
     66  H264_NAL_SEI = 6,
     67  H264_NAL_SPS = 7,
     68  H264_NAL_PPS = 8,
     69  H264_NAL_AUD = 9,
     70  H264_NAL_END_SEQUENCE = 10,
     71  H264_NAL_END_STREAM = 11,
     72  H264_NAL_FILLER_DATA = 12,
     73  H264_NAL_SPS_EXT = 13,
     74  H264_NAL_PREFIX = 14,
     75  H264_NAL_AUXILIARY_SLICE = 19,
     76  H264_NAL_SLICE_EXT = 20,
     77  H264_NAL_SLICE_EXT_DVC = 21,
     78 };
     79 
     80 // ITU-T H.264 (V15) (08/2024), 7.3.1 NAL unit syntax.
     81 struct MOZ_STACK_CLASS H264NALU final {
     82  H264NALU(const uint8_t* aData MOZ_LIFETIME_BOUND, uint32_t aByteCount);
     83  H264NALU() = default;
     84 
     85  uint8_t mNalUnitType;
     86  // This points to the full content of the NALU, which can be used to extract
     87  // the RBSP.
     88  const Span<const uint8_t> mNALU;
     89 };
     90 
     91 // According to ITU-T Rec H.264 (2017/04) Table 7.6.
     92 enum SLICE_TYPES {
     93  P_SLICE = 0,
     94  B_SLICE = 1,
     95  I_SLICE = 2,
     96  SP_SLICE = 3,
     97  SI_SLICE = 4,
     98 };
     99 
    100 struct SPSData {
    101  bool operator==(const SPSData& aOther) const;
    102  bool operator!=(const SPSData& aOther) const;
    103 
    104  gfx::YUVColorSpace ColorSpace() const;
    105  gfx::ColorDepth ColorDepth() const;
    106 
    107  bool valid = {};
    108 
    109  /* Decoded Members */
    110  /*
    111    pic_width is the decoded width according to:
    112    pic_width = ((pic_width_in_mbs_minus1 + 1) * 16)
    113                - (frame_crop_left_offset + frame_crop_right_offset) * 2
    114   */
    115  uint32_t pic_width = {};
    116  /*
    117    pic_height is the decoded height according to:
    118    pic_height = (2 - frame_mbs_only_flag) * ((pic_height_in_map_units_minus1 +
    119    1) * 16)
    120                 - (frame_crop_top_offset + frame_crop_bottom_offset) * 2
    121   */
    122  uint32_t pic_height = {};
    123 
    124  bool interlaced = {};
    125 
    126  /*
    127   Displayed size.
    128   display_width and display_height are adjusted according to the display
    129   sample aspect ratio.
    130   */
    131  uint32_t display_width = {};
    132  uint32_t display_height = {};
    133 
    134  float sample_ratio = {};
    135 
    136  uint32_t crop_left = {};
    137  uint32_t crop_right = {};
    138  uint32_t crop_top = {};
    139  uint32_t crop_bottom = {};
    140 
    141  /*
    142    H264 decoding parameters according to ITU-T H.264 (T-REC-H.264-201402-I/en)
    143   http://www.itu.int/rec/T-REC-H.264-201402-I/en
    144   */
    145 
    146  bool constraint_set0_flag = {};
    147  bool constraint_set1_flag = {};
    148  bool constraint_set2_flag = {};
    149  bool constraint_set3_flag = {};
    150  bool constraint_set4_flag = {};
    151  bool constraint_set5_flag = {};
    152 
    153  /*
    154    profile_idc and level_idc indicate the profile and level to which the coded
    155    video sequence conforms when the SVC sequence parameter set is the active
    156    SVC sequence parameter set.
    157   */
    158  uint8_t profile_idc = {};
    159  uint8_t level_idc = {};
    160 
    161  /*
    162    seq_parameter_set_id identifies the sequence parameter set that is referred
    163    to by the picture parameter set. The value of seq_parameter_set_id shall be
    164    in the range of 0 to 31, inclusive.
    165   */
    166  uint8_t seq_parameter_set_id = {};
    167 
    168  /*
    169    chroma_format_idc specifies the chroma sampling relative to the luma
    170    sampling as specified in clause 6.2. The value of chroma_format_idc shall be
    171    in the range of 0 to 3, inclusive. When chroma_format_idc is not present,
    172    it shall be inferred to be equal to 1 (4:2:0 chroma format).
    173    When profile_idc is equal to 183, chroma_format_idc shall be equal to 0
    174    (4:0:0 chroma format).
    175   */
    176  uint8_t chroma_format_idc = {};
    177 
    178  /*
    179    bit_depth_luma_minus8 specifies the bit depth of the samples of the luma
    180    array and the value of the luma quantisation parameter range offset
    181    QpBdOffset Y , as specified by
    182      BitDepth Y = 8 + bit_depth_luma_minus8 (7-3)
    183      QpBdOffset Y = 6 * bit_depth_luma_minus8 (7-4)
    184    When bit_depth_luma_minus8 is not present, it shall be inferred to be equal
    185    to 0. bit_depth_luma_minus8 shall be in the range of 0 to 6, inclusive.
    186  */
    187  uint8_t bit_depth_luma_minus8 = {};
    188 
    189  /*
    190    bit_depth_chroma_minus8 specifies the bit depth of the samples of the chroma
    191    arrays and the value of the chroma quantisation parameter range offset
    192    QpBdOffset C , as specified by
    193      BitDepth C = 8 + bit_depth_chroma_minus8 (7-5)
    194      QpBdOffset C = 6 * bit_depth_chroma_minus8 (7-6)
    195    When bit_depth_chroma_minus8 is not present, it shall be inferred to be
    196    equal to 0. bit_depth_chroma_minus8 shall be in the range of 0 to 6,
    197    inclusive.
    198  */
    199  uint8_t bit_depth_chroma_minus8 = {};
    200 
    201  /*
    202    separate_colour_plane_flag equal to 1 specifies that the three colour
    203    components of the 4:4:4 chroma format are coded separately.
    204    separate_colour_plane_flag equal to 0 specifies that the colour components
    205    are not coded separately. When separate_colour_plane_flag is not present,
    206    it shall be inferred to be equal to 0. When separate_colour_plane_flag is
    207    equal to 1, the primary coded picture consists of three separate components,
    208    each of which consists of coded samples of one colour plane (Y, Cb or Cr)
    209    that each use the monochrome coding syntax. In this case, each colour plane
    210    is associated with a specific colour_plane_id value.
    211   */
    212  bool separate_colour_plane_flag = {};
    213 
    214  /*
    215     seq_scaling_matrix_present_flag equal to 1 specifies that the flags
    216     seq_scaling_list_present_flag[ i ] for i = 0..7 or
    217     i = 0..11 are present. seq_scaling_matrix_present_flag equal to 0 specifies
    218     that these flags are not present and the sequence-level scaling list
    219     specified by Flat_4x4_16 shall be inferred for i = 0..5 and the
    220     sequence-level scaling list specified by Flat_8x8_16 shall be inferred for
    221     i = 6..11. When seq_scaling_matrix_present_flag is not present, it shall be
    222     inferred to be equal to 0.
    223     */
    224  bool seq_scaling_matrix_present_flag = {};
    225 
    226  /*
    227    log2_max_frame_num_minus4 specifies the value of the variable
    228    MaxFrameNum that is used in frame_num related derivations as
    229    follows:
    230 
    231     MaxFrameNum = 2( log2_max_frame_num_minus4 + 4 ). The value of
    232    log2_max_frame_num_minus4 shall be in the range of 0 to 12, inclusive.
    233   */
    234  uint8_t log2_max_frame_num = {};
    235 
    236  /*
    237    pic_order_cnt_type specifies the method to decode picture order
    238    count (as specified in subclause 8.2.1). The value of
    239    pic_order_cnt_type shall be in the range of 0 to 2, inclusive.
    240   */
    241  uint8_t pic_order_cnt_type = {};
    242 
    243  /*
    244    log2_max_pic_order_cnt_lsb_minus4 specifies the value of the
    245    variable MaxPicOrderCntLsb that is used in the decoding
    246    process for picture order count as specified in subclause
    247    8.2.1 as follows:
    248 
    249    MaxPicOrderCntLsb = 2( log2_max_pic_order_cnt_lsb_minus4 + 4 )
    250 
    251    The value of log2_max_pic_order_cnt_lsb_minus4 shall be in
    252    the range of 0 to 12, inclusive.
    253   */
    254  uint8_t log2_max_pic_order_cnt_lsb = {};
    255 
    256  /*
    257    delta_pic_order_always_zero_flag equal to 1 specifies that
    258    delta_pic_order_cnt[ 0 ] and delta_pic_order_cnt[ 1 ] are
    259    not present in the slice headers of the sequence and shall
    260    be inferred to be equal to 0.
    261   */
    262  bool delta_pic_order_always_zero_flag = {};
    263 
    264  /*
    265    offset_for_non_ref_pic is used to calculate the picture
    266    order count of a non-reference picture as specified in
    267    8.2.1. The value of offset_for_non_ref_pic shall be in the
    268    range of -231 to 231 - 1, inclusive.
    269   */
    270  int8_t offset_for_non_ref_pic = {};
    271 
    272  /*
    273    offset_for_top_to_bottom_field is used to calculate the
    274    picture order count of a bottom field as specified in
    275    subclause 8.2.1. The value of offset_for_top_to_bottom_field
    276    shall be in the range of -231 to 231 - 1, inclusive.
    277   */
    278  int8_t offset_for_top_to_bottom_field = {};
    279 
    280  /*
    281    max_num_ref_frames specifies the maximum number of short-term and
    282    long-term reference frames, complementary reference field pairs,
    283    and non-paired reference fields that may be used by the decoding
    284    process for inter prediction of any picture in the
    285    sequence. max_num_ref_frames also determines the size of the sliding
    286    window operation as specified in subclause 8.2.5.3. The value of
    287    max_num_ref_frames shall be in the range of 0 to MaxDpbFrames (as
    288    specified in subclause A.3.1 or A.3.2), inclusive.
    289   */
    290  uint32_t max_num_ref_frames = {};
    291 
    292  /*
    293    gaps_in_frame_num_value_allowed_flag specifies the allowed
    294    values of frame_num as specified in subclause 7.4.3 and the
    295    decoding process in case of an inferred gap between values of
    296    frame_num as specified in subclause 8.2.5.2.
    297   */
    298  bool gaps_in_frame_num_allowed_flag = {};
    299 
    300  /*
    301    pic_width_in_mbs_minus1 plus 1 specifies the width of each
    302    decoded picture in units of macroblocks.  16 macroblocks in a row
    303   */
    304  uint32_t pic_width_in_mbs = {};
    305 
    306  /*
    307    pic_height_in_map_units_minus1 plus 1 specifies the height in
    308    slice group map units of a decoded frame or field.  16
    309    macroblocks in each column.
    310   */
    311  uint32_t pic_height_in_map_units = {};
    312 
    313  /*
    314    frame_mbs_only_flag equal to 0 specifies that coded pictures of
    315    the coded video sequence may either be coded fields or coded
    316    frames. frame_mbs_only_flag equal to 1 specifies that every
    317    coded picture of the coded video sequence is a coded frame
    318    containing only frame macroblocks.
    319   */
    320  bool frame_mbs_only_flag = {};
    321 
    322  /*
    323    mb_adaptive_frame_field_flag equal to 0 specifies no
    324    switching between frame and field macroblocks within a
    325    picture. mb_adaptive_frame_field_flag equal to 1 specifies
    326    the possible use of switching between frame and field
    327    macroblocks within frames. When mb_adaptive_frame_field_flag
    328    is not present, it shall be inferred to be equal to 0.
    329   */
    330  bool mb_adaptive_frame_field_flag = {};
    331 
    332  /*
    333    direct_8x8_inference_flag specifies the method used in the derivation
    334    process for luma motion vectors for B_Skip, B_Direct_16x16 and B_Direct_8x8
    335    as specified in clause 8.4.1.2. When frame_mbs_only_flag is equal to 0,
    336    direct_8x8_inference_flag shall be equal to 1.
    337  */
    338  bool direct_8x8_inference_flag = {};
    339 
    340  /*
    341    frame_cropping_flag equal to 1 specifies that the frame cropping
    342    offset parameters follow next in the sequence parameter
    343    set. frame_cropping_flag equal to 0 specifies that the frame
    344    cropping offset parameters are not present.
    345   */
    346  bool frame_cropping_flag = {};
    347  uint32_t frame_crop_left_offset = {};
    348  uint32_t frame_crop_right_offset = {};
    349  uint32_t frame_crop_top_offset = {};
    350  uint32_t frame_crop_bottom_offset = {};
    351 
    352  // VUI Parameters
    353 
    354  /*
    355    vui_parameters_present_flag equal to 1 specifies that the
    356    vui_parameters( ) syntax structure as specified in Annex E is
    357    present. vui_parameters_present_flag equal to 0 specifies that
    358    the vui_parameters( ) syntax structure as specified in Annex E
    359    is not present.
    360   */
    361  bool vui_parameters_present_flag = {};
    362 
    363  /*
    364   aspect_ratio_info_present_flag equal to 1 specifies that
    365   aspect_ratio_idc is present. aspect_ratio_info_present_flag
    366   equal to 0 specifies that aspect_ratio_idc is not present.
    367   */
    368  bool aspect_ratio_info_present_flag = {};
    369 
    370  /*
    371    aspect_ratio_idc specifies the value of the sample aspect
    372    ratio of the luma samples. Table E-1 shows the meaning of
    373    the code. When aspect_ratio_idc indicates Extended_SAR, the
    374    sample aspect ratio is represented by sar_width and
    375    sar_height. When the aspect_ratio_idc syntax element is not
    376    present, aspect_ratio_idc value shall be inferred to be
    377    equal to 0.
    378   */
    379  uint8_t aspect_ratio_idc = {};
    380  uint32_t sar_width = {};
    381  uint32_t sar_height = {};
    382 
    383  /*
    384    video_signal_type_present_flag equal to 1 specifies that video_format,
    385    video_full_range_flag and colour_description_present_flag are present.
    386    video_signal_type_present_flag equal to 0, specify that video_format,
    387    video_full_range_flag and colour_description_present_flag are not present.
    388   */
    389  bool video_signal_type_present_flag = {};
    390 
    391  /*
    392    overscan_info_present_flag equal to1 specifies that the
    393    overscan_appropriate_flag is present. When overscan_info_present_flag is
    394    equal to 0 or is not present, the preferred display method for the video
    395    signal is unspecified (Unspecified).
    396   */
    397  bool overscan_info_present_flag = {};
    398  /*
    399    overscan_appropriate_flag equal to 1 indicates that the cropped decoded
    400    pictures output are suitable for display using overscan.
    401    overscan_appropriate_flag equal to 0 indicates that the cropped decoded
    402    pictures output contain visually important information in the entire region
    403    out to the edges of the cropping rectangle of the picture
    404   */
    405  bool overscan_appropriate_flag = {};
    406 
    407  /*
    408    video_format indicates the representation of the pictures as specified in
    409    Table E-2, before being coded in accordance with this
    410    Recommendation | International Standard. When the video_format syntax
    411    element is not present, video_format value shall be inferred to be equal
    412    to 5. (Unspecified video format)
    413   */
    414  uint8_t video_format = {};
    415 
    416  /*
    417    video_full_range_flag indicates the black level and range of the luma and
    418    chroma signals as derived from E′Y, E′PB, and E′PR or E′R, E′G, and E′B
    419    real-valued component signals.
    420    When the video_full_range_flag syntax element is not present, the value of
    421    video_full_range_flag shall be inferred to be equal to 0.
    422   */
    423  bool video_full_range_flag = {};
    424 
    425  /*
    426    colour_description_present_flag equal to1 specifies that colour_primaries,
    427    transfer_characteristics and matrix_coefficients are present.
    428    colour_description_present_flag equal to 0 specifies that colour_primaries,
    429    transfer_characteristics and matrix_coefficients are not present.
    430   */
    431  bool colour_description_present_flag = {};
    432 
    433  /*
    434    colour_primaries indicates the chromaticity coordinates of the source
    435    primaries as specified in Table E-3 in terms of the CIE 1931 definition of
    436    x and y as specified by ISO 11664-1.
    437    When the colour_primaries syntax element is not present, the value of
    438    colour_primaries shall be inferred to be equal to 2 (the chromaticity is
    439    unspecified or is determined by the application).
    440   */
    441  uint8_t colour_primaries = {};
    442 
    443  /*
    444    transfer_characteristics indicates the opto-electronic transfer
    445    characteristic of the source picture as specified in Table E-4 as a function
    446    of a linear optical intensity input Lc with a nominal real-valued range of 0
    447    to 1.
    448    When the transfer_characteristics syntax element is not present, the value
    449    of transfer_characteristics shall be inferred to be equal to 2
    450    (the transfer characteristics are unspecified or are determined by the
    451    application).
    452   */
    453  uint8_t transfer_characteristics = {};
    454 
    455  uint8_t matrix_coefficients = {};
    456  bool chroma_loc_info_present_flag = {};
    457  /*
    458    The value of chroma_sample_loc_type_top_field and
    459    chroma_sample_loc_type_bottom_field shall be in the range of 0 to 5,
    460    inclusive
    461  */
    462  uint8_t chroma_sample_loc_type_top_field = {};
    463  uint8_t chroma_sample_loc_type_bottom_field = {};
    464 
    465  bool scaling_matrix_present = {};
    466  uint8_t scaling_matrix4x4[6][16] = {};
    467  uint8_t scaling_matrix8x8[6][64] = {};
    468 
    469  SPSData();
    470 };
    471 
    472 struct SEIRecoveryData {
    473  /*
    474    recovery_frame_cnt specifies the recovery point of output pictures in output
    475    order. All decoded pictures in output order are indicated to be correct or
    476    approximately correct in content starting at the output order position of
    477    the reference picture having the frame_num equal to the frame_num of the VCL
    478    NAL units for the current access unit incremented by recovery_frame_cnt in
    479    modulo MaxFrameNum arithmetic. recovery_frame_cnt shall be in the range of 0
    480    to MaxFrameNum − 1, inclusive.
    481  */
    482  uint32_t recovery_frame_cnt = 0;
    483  /*
    484    exact_match_flag indicates whether decoded pictures at and subsequent to the
    485    specified recovery point in output order derived by starting the decoding
    486    process at the access unit associated with the recovery point SEI message
    487    shall be an exact match to the pictures that would be produced by starting
    488    the decoding process at the location of a previous IDR access unit in the
    489    NAL unit stream. The value 0 indicates that the match need not be exact and
    490    the value 1 indicates that the match shall be exact.
    491  */
    492  bool exact_match_flag = false;
    493  /*
    494    broken_link_flag indicates the presence or absence of a broken link in the
    495    NAL unit stream at the location of the recovery point SEI message */
    496  bool broken_link_flag = false;
    497  /*
    498    changing_slice_group_idc equal to 0 indicates that decoded pictures are
    499    correct or approximately correct in content at and subsequent to the
    500    recovery point in output order when all macroblocks of the primary coded
    501    pictures are decoded within the changing slice group period
    502  */
    503  uint8_t changing_slice_group_idc = 0;
    504 };
    505 
    506 class H264 {
    507 public:
    508  /* Check if out of band extradata contains a SPS NAL */
    509  static bool HasSPS(const mozilla::MediaByteBuffer* aExtraData);
    510  // Extract SPS and PPS NALs from aSample by looking into each NALs.
    511  // aSample must be in AVCC format.
    512  static already_AddRefed<mozilla::MediaByteBuffer> ExtractExtraData(
    513      const mozilla::MediaRawData* aSample);
    514  // Return true if both extradata are equal.
    515  static bool CompareExtraData(const mozilla::MediaByteBuffer* aExtraData1,
    516                               const mozilla::MediaByteBuffer* aExtraData2);
    517 
    518  // Ensure that SPS data makes sense, Return true if SPS data was, and false
    519  // otherwise. If false, then content will be adjusted accordingly.
    520  static bool EnsureSPSIsSane(SPSData& aSPS);
    521 
    522  static bool DecodeSPSFromExtraData(const mozilla::MediaByteBuffer* aExtraData,
    523                                     SPSData& aDest);
    524  /* Decode SPS NAL RBSP and fill SPSData structure */
    525  static bool DecodeSPS(const mozilla::MediaByteBuffer* aSPS, SPSData& aDest);
    526 
    527  // If the given aExtraData is valid, return the aExtraData.max_num_ref_frames
    528  // clamped to be in the range of [4, 16]; otherwise return 4.
    529  static uint32_t ComputeMaxRefFrames(
    530      const mozilla::MediaByteBuffer* aExtraData);
    531 
    532  enum class FrameType {
    533    // IDR is a special iframe, according to the spec T-REC-H.264-202408, 3.69 :
    534    // "An IDR picture causes the decoding process to mark all reference
    535    // pictures as "unused for reference" immediately after the decoding of the
    536    // IDR picture. All coded pictures that follow an IDR picture in decoding
    537    // order can be decoded without inter prediction from any picture that
    538    // precedes the IDR picture in decoding order."
    539    I_FRAME_IDR,
    540    I_FRAME_OTHER,
    541    OTHER,
    542    INVALID,
    543  };
    544 
    545  // Returns the frame type. Returns I_FRAME if the sample is an IDR
    546  // (Instantaneous Decoding Refresh) Picture.
    547  static FrameType GetFrameType(const mozilla::MediaRawData* aSample);
    548 
    549  /* From a NAL, extract the SVC temporal id, per H264 spec Annex G, 7.3.1.1 */
    550  static Result<int, nsresult> ExtractSVCTemporalId(const uint8_t* aData,
    551                                                    size_t aLength);
    552 
    553  // Create a dummy extradata, useful to create a decoder and test the
    554  // capabilities of the decoder.
    555  static already_AddRefed<mozilla::MediaByteBuffer> CreateExtraData(
    556      uint8_t aProfile, uint8_t aConstraints, H264_LEVEL aLevel,
    557      const gfx::IntSize& aSize);
    558  static void WriteExtraData(mozilla::MediaByteBuffer* aDestExtraData,
    559                             const uint8_t aProfile, const uint8_t aConstraints,
    560                             const uint8_t aLevel,
    561                             const Span<const uint8_t> aSPS,
    562                             const Span<const uint8_t> aPPS);
    563 
    564 private:
    565  friend class SPSNAL;
    566 
    567  /* Extract RAW BYTE SEQUENCE PAYLOAD from NAL content.
    568     Returns nullptr if invalid content.
    569     This is compliant to ITU H.264 7.3.1 Syntax in tabular form NAL unit syntax
    570   */
    571  static already_AddRefed<mozilla::MediaByteBuffer> DecodeNALUnit(
    572      const uint8_t* aNAL, size_t aLength);
    573  static already_AddRefed<mozilla::MediaByteBuffer> EncodeNALUnit(
    574      const uint8_t* aNAL, size_t aLength);
    575  static bool vui_parameters(mozilla::BitReader& aBr, SPSData& aDest);
    576  // Read HRD parameters, all data is ignored.
    577  static void hrd_parameters(mozilla::BitReader& aBr);
    578  static uint8_t NumSPS(const mozilla::MediaByteBuffer* aExtraData);
    579  // Decode SEI payload and return true if the SEI NAL indicates a recovery
    580  // point.
    581  static bool DecodeRecoverySEI(const mozilla::MediaByteBuffer* aSEI,
    582                                SEIRecoveryData& aDest);
    583  // Decode NAL Slice payload and return true if its slice type is I slice or SI
    584  // slice.
    585  static bool DecodeISlice(const mozilla::MediaByteBuffer* aSlice);
    586 };
    587 
    588 /*
    589 * ISO/IEC 14496-15 : avcC.
    590 * aligned(8) class AVCDecoderConfigurationRecord {
    591 *   unsigned int(8) configurationVersion = 1;
    592 *   unsigned int(8) AVCProfileIndication;
    593 *   unsigned int(8) profile_compatibility;
    594 *   unsigned int(8) AVCLevelIndication;
    595 *   bit(6) reserved = '111111'b;
    596 *   unsigned int(2) lengthSizeMinusOne;
    597 *   bit(3) reserved = '111'b;
    598 *   unsigned int(5) numOfSequenceParameterSets;
    599 *   for (i = 0; i < numOfSequenceParameterSets; i++) {
    600 *     unsigned int(16) sequenceParameterSetLength;
    601 *     bit(8 * sequenceParameterSetLength) sequenceParameterSetNALUnit;
    602 *   }
    603 *   unsigned int(8) numOfPictureParameterSets;
    604 *   for (i = 0; i < numOfPictureParameterSets; i++) {
    605 *     unsigned int(16) pictureParameterSetLength;
    606 *     bit(8 * pictureParameterSetLength) pictureParameterSetNALUnit;
    607 *   }
    608 *   if (AVCProfileIndication != 66 && AVCProfileIndication != 77 &&
    609 *       AVCProfileIndication != 88) {
    610 *     bit(6) reserved = '111111'b;
    611 *     unsigned int(2) chroma_format;
    612 *     bit(5) reserved = '11111'b;
    613 *     unsigned int(3) bit_depth_luma_minus8;
    614 *     bit(5) reserved = '11111'b;
    615 *     unsigned int(3) bit_depth_chroma_minus8;
    616 *     unsigned int(8) numOfSequenceParameterSetExt;
    617 *     for (i = 0; i < numOfSequenceParameterSetExt; i++) {
    618 *       unsigned int(16) sequenceParameterSetExtLength;
    619 *       bit(8 * sequenceParameterSetExtLength) sequenceParameterSetExtNALUnit;
    620 *     }
    621 *   }
    622 * };
    623 */
    624 struct AVCCConfig final {
    625 public:
    626  // The extradata (from sample or directly given) should have a lifetime equal
    627  // to or longer than AVCCConfig.
    628  static Result<AVCCConfig, nsresult> Parse(
    629      const mozilla::MediaRawData* aSample);
    630  static Result<AVCCConfig, nsresult> Parse(
    631      const mozilla::MediaByteBuffer* aExtraData);
    632 
    633  uint8_t NALUSize() const { return mLengthSizeMinusOne + 1; }
    634 
    635  uint8_t mConfigurationVersion;
    636  uint8_t mAVCProfileIndication;
    637  uint8_t mProfileCompatibility;
    638  uint8_t mAVCLevelIndication;
    639  uint8_t mLengthSizeMinusOne;
    640  nsTArray<H264NALU> mSPSs;
    641  nsTArray<H264NALU> mPPSs;
    642  // Following members are optional.
    643  Maybe<uint8_t> mChromaFormat;
    644  Maybe<uint8_t> mBitDepthLumaMinus8;
    645  Maybe<uint8_t> mBitDepthChromaMinus8;
    646  nsTArray<H264NALU> mSPSExts;
    647 
    648  uint32_t NumSPS() const { return mSPSs.Length(); }
    649  uint32_t NumPPS() const { return mPPSs.Length(); }
    650  uint32_t NumSPSExt() const { return mSPSExts.Length(); }
    651 
    652  // This method is used when the attributes of AVCCConfig are modified, and
    653  // then you want to create a new byte buffer based on the updated attributes.
    654  // If the updated attributes make the config invalid, this method will return
    655  // nullptr.
    656  already_AddRefed<mozilla::MediaByteBuffer> CreateNewExtraData() const;
    657 
    658 private:
    659  AVCCConfig() = default;
    660 };
    661 
    662 }  // namespace mozilla
    663 
    664 #endif  // MP4_DEMUXER_H264_H_