nalu_rewriter.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (c) 2015 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. */
  11. #ifndef SDK_OBJC_FRAMEWORK_CLASSES_VIDEOTOOLBOX_NALU_REWRITER_H_
  12. #define SDK_OBJC_FRAMEWORK_CLASSES_VIDEOTOOLBOX_NALU_REWRITER_H_
  13. #include "modules/video_coding/codecs/h264/include/h264.h"
  14. #include <CoreMedia/CoreMedia.h>
  15. #include <vector>
  16. #include "common_video/h264/h264_common.h"
  17. #include "rtc_base/buffer.h"
  18. using webrtc::H264::NaluIndex;
  19. namespace webrtc {
  20. // Converts a sample buffer emitted from the VideoToolbox encoder into a buffer
  21. // suitable for RTP. The sample buffer is in avcc format whereas the rtp buffer
  22. // needs to be in Annex B format. Data is written directly to |annexb_buffer|.
  23. bool H264CMSampleBufferToAnnexBBuffer(CMSampleBufferRef avcc_sample_buffer,
  24. bool is_keyframe,
  25. rtc::Buffer* annexb_buffer);
  26. // Converts a buffer received from RTP into a sample buffer suitable for the
  27. // VideoToolbox decoder. The RTP buffer is in annex b format whereas the sample
  28. // buffer is in avcc format.
  29. // If |is_keyframe| is true then |video_format| is ignored since the format will
  30. // be read from the buffer. Otherwise |video_format| must be provided.
  31. // Caller is responsible for releasing the created sample buffer.
  32. bool H264AnnexBBufferToCMSampleBuffer(const uint8_t* annexb_buffer,
  33. size_t annexb_buffer_size,
  34. CMVideoFormatDescriptionRef video_format,
  35. CMSampleBufferRef* out_sample_buffer,
  36. CMMemoryPoolRef memory_pool);
  37. // Returns a video format description created from the sps/pps information in
  38. // the Annex B buffer. If there is no such information, nullptr is returned.
  39. // The caller is responsible for releasing the description.
  40. CMVideoFormatDescriptionRef CreateVideoFormatDescription(
  41. const uint8_t* annexb_buffer,
  42. size_t annexb_buffer_size);
  43. // Helper class for reading NALUs from an RTP Annex B buffer.
  44. class AnnexBBufferReader final {
  45. public:
  46. AnnexBBufferReader(const uint8_t* annexb_buffer, size_t length);
  47. ~AnnexBBufferReader();
  48. AnnexBBufferReader(const AnnexBBufferReader& other) = delete;
  49. void operator=(const AnnexBBufferReader& other) = delete;
  50. // Returns a pointer to the beginning of the next NALU slice without the
  51. // header bytes and its length. Returns false if no more slices remain.
  52. bool ReadNalu(const uint8_t** out_nalu, size_t* out_length);
  53. // Returns the number of unread NALU bytes, including the size of the header.
  54. // If the buffer has no remaining NALUs this will return zero.
  55. size_t BytesRemaining() const;
  56. // Reset the reader to start reading from the first NALU
  57. void SeekToStart();
  58. // Seek to the next position that holds a NALU of the desired type,
  59. // or the end if no such NALU is found.
  60. // Return true if a NALU of the desired type is found, false if we
  61. // reached the end instead
  62. bool SeekToNextNaluOfType(H264::NaluType type);
  63. private:
  64. // Returns the the next offset that contains NALU data.
  65. size_t FindNextNaluHeader(const uint8_t* start,
  66. size_t length,
  67. size_t offset) const;
  68. const uint8_t* const start_;
  69. std::vector<NaluIndex> offsets_;
  70. std::vector<NaluIndex>::iterator offset_;
  71. const size_t length_;
  72. };
  73. // Helper class for writing NALUs using avcc format into a buffer.
  74. class AvccBufferWriter final {
  75. public:
  76. AvccBufferWriter(uint8_t* const avcc_buffer, size_t length);
  77. ~AvccBufferWriter() {}
  78. AvccBufferWriter(const AvccBufferWriter& other) = delete;
  79. void operator=(const AvccBufferWriter& other) = delete;
  80. // Writes the data slice into the buffer. Returns false if there isn't
  81. // enough space left.
  82. bool WriteNalu(const uint8_t* data, size_t data_size);
  83. // Returns the unused bytes in the buffer.
  84. size_t BytesRemaining() const;
  85. private:
  86. uint8_t* const start_;
  87. size_t offset_;
  88. const size_t length_;
  89. };
  90. } // namespace webrtc
  91. #endif // SDK_OBJC_FRAMEWORK_CLASSES_VIDEOTOOLBOX_NALU_REWRITER_H_