h264_common.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef COMMON_VIDEO_H264_H264_COMMON_H_
  11. #define COMMON_VIDEO_H264_H264_COMMON_H_
  12. #include <stddef.h>
  13. #include <stdint.h>
  14. #include <vector>
  15. #include "rtc_base/buffer.h"
  16. namespace webrtc {
  17. namespace H264 {
  18. // The size of a full NALU start sequence {0 0 0 1}, used for the first NALU
  19. // of an access unit, and for SPS and PPS blocks.
  20. const size_t kNaluLongStartSequenceSize = 4;
  21. // The size of a shortened NALU start sequence {0 0 1}, that may be used if
  22. // not the first NALU of an access unit or an SPS or PPS block.
  23. const size_t kNaluShortStartSequenceSize = 3;
  24. // The size of the NALU type byte (1).
  25. const size_t kNaluTypeSize = 1;
  26. enum NaluType : uint8_t {
  27. kSlice = 1,
  28. kIdr = 5,
  29. kSei = 6,
  30. kSps = 7,
  31. kPps = 8,
  32. kAud = 9,
  33. kEndOfSequence = 10,
  34. kEndOfStream = 11,
  35. kFiller = 12,
  36. kStapA = 24,
  37. kFuA = 28
  38. };
  39. enum SliceType : uint8_t { kP = 0, kB = 1, kI = 2, kSp = 3, kSi = 4 };
  40. struct NaluIndex {
  41. // Start index of NALU, including start sequence.
  42. size_t start_offset;
  43. // Start index of NALU payload, typically type header.
  44. size_t payload_start_offset;
  45. // Length of NALU payload, in bytes, counting from payload_start_offset.
  46. size_t payload_size;
  47. };
  48. // Returns a vector of the NALU indices in the given buffer.
  49. std::vector<NaluIndex> FindNaluIndices(const uint8_t* buffer,
  50. size_t buffer_size);
  51. // Get the NAL type from the header byte immediately following start sequence.
  52. NaluType ParseNaluType(uint8_t data);
  53. // Methods for parsing and writing RBSP. See section 7.4.1 of the H264 spec.
  54. //
  55. // The following sequences are illegal, and need to be escaped when encoding:
  56. // 00 00 00 -> 00 00 03 00
  57. // 00 00 01 -> 00 00 03 01
  58. // 00 00 02 -> 00 00 03 02
  59. // And things in the source that look like the emulation byte pattern (00 00 03)
  60. // need to have an extra emulation byte added, so it's removed when decoding:
  61. // 00 00 03 -> 00 00 03 03
  62. //
  63. // Decoding is simply a matter of finding any 00 00 03 sequence and removing
  64. // the 03 emulation byte.
  65. // Parse the given data and remove any emulation byte escaping.
  66. std::vector<uint8_t> ParseRbsp(const uint8_t* data, size_t length);
  67. // Write the given data to the destination buffer, inserting and emulation
  68. // bytes in order to escape any data the could be interpreted as a start
  69. // sequence.
  70. void WriteRbsp(const uint8_t* bytes, size_t length, rtc::Buffer* destination);
  71. } // namespace H264
  72. } // namespace webrtc
  73. #endif // COMMON_VIDEO_H264_H264_COMMON_H_