You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.7 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. MediaMtx (C++) - H264 helpers (MediaMTX uses mediacommon/pkg/codecs/h264)
  3. */
  4. #pragma once
  5. #include <string>
  6. #include <vector>
  7. #include "../../unit/unit.h"
  8. namespace mtx {
  9. namespace h264 {
  10. enum NaluType
  11. {
  12. NALU_NON_IDR = 1,
  13. NALU_IDR = 5,
  14. NALU_SEI = 6,
  15. NALU_SPS = 7,
  16. NALU_PPS = 8,
  17. NALU_AUD = 9,
  18. NALU_STAP_A = 24,
  19. NALU_FU_A = 28
  20. };
  21. inline int naluType(const Bytes & n)
  22. {
  23. return n.empty() ? 0 : (n[0] & 0x1f);
  24. }
  25. struct SpsInfo
  26. {
  27. int width = 0;
  28. int height = 0;
  29. int profileIdc = 0;
  30. int constraints = 0;
  31. int levelIdc = 0;
  32. };
  33. bool parseSps(const Bytes & sps, SpsInfo & info);
  34. // 4d401f style string for SDP profile-level-id and "avc1.4d401f" codec strings
  35. std::string profileLevelId(const Bytes & sps);
  36. // Base64 sprop-parameter-sets ("Z00AH...,aO4...") -> NAL units
  37. std::vector<Bytes> decodeSpropParameterSets(const char * sprop);
  38. // Annex-B byte stream (00 00 00 01 start codes)
  39. Bytes toAnnexB(const std::vector<Bytes> & nalus);
  40. // Annex-B byte stream -> NAL units (3 or 4 byte start codes; AUD and empty units are dropped)
  41. std::vector<Bytes> splitAnnexB(const uint8_t * data, size_t size);
  42. // Length prefixed NAL units (lengthSize 1..4, MP4 / avcC style) -> NAL units
  43. std::vector<Bytes> splitAvcc(const uint8_t * data, size_t size, int lengthSize = 4);
  44. // SPS / PPS of an avcC record (libav extradata of MP4 sources) or of Annex-B extradata
  45. std::vector<Bytes> parameterSetsFromExtradata(const uint8_t * data, size_t size, int * avccLengthSize = nullptr);
  46. // AVCC: 4 byte big-endian lengths (MP4 samples)
  47. Bytes toAvcc(const std::vector<Bytes> & nalus);
  48. // AVCDecoderConfigurationRecord (avcC box payload)
  49. Bytes buildAvcC(const Bytes & sps, const Bytes & pps);
  50. } // namespace h264
  51. } // namespace mtx