h264_bitstream_buffer.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. //
  5. // This file contains an implementation of a H264BitstreamBuffer class for
  6. // constructing raw bitstream buffers containing NAL units in
  7. // H.264 Annex-B stream format.
  8. // See H.264 spec Annex B and chapter 7for more details.
  9. #ifndef MEDIA_FILTERS_H264_BITSTREAM_BUFFER_H_
  10. #define MEDIA_FILTERS_H264_BITSTREAM_BUFFER_H_
  11. #include <stddef.h>
  12. #include <stdint.h>
  13. #include "base/gtest_prod_util.h"
  14. #include "base/memory/raw_ptr.h"
  15. #include "base/memory/ref_counted.h"
  16. #include "base/numerics/safe_conversions.h"
  17. #include "media/base/media_export.h"
  18. #include "media/base/video_frame.h"
  19. #include "media/video/h264_parser.h"
  20. namespace media {
  21. // Holds one or more NALUs as a raw bitstream buffer in H.264 Annex-B format.
  22. // Note that this class currently does NOT insert emulation prevention
  23. // three-byte sequences (spec 7.3.1).
  24. // Refcounted as these buffers may be used as arguments to multiple codec jobs
  25. // (e.g. a buffer containing an H.264 SPS NALU may be used as an argument to all
  26. // jobs that use parameters contained in that SPS).
  27. class MEDIA_EXPORT H264BitstreamBuffer
  28. : public base::RefCountedThreadSafe<H264BitstreamBuffer> {
  29. public:
  30. H264BitstreamBuffer();
  31. H264BitstreamBuffer(const H264BitstreamBuffer&) = delete;
  32. H264BitstreamBuffer& operator=(const H264BitstreamBuffer&) = delete;
  33. // Discard all data and reset the buffer for reuse.
  34. void Reset();
  35. // Append |num_bits| bits to the stream from |val|.
  36. // |val| is interpreted in the host endianness.
  37. template <typename T>
  38. void AppendBits(size_t num_bits, T val) {
  39. AppendU64(num_bits, static_cast<uint64_t>(val));
  40. }
  41. void AppendBits(size_t num_bits, bool val) {
  42. DCHECK_EQ(num_bits, 1ul);
  43. AppendBool(val);
  44. }
  45. // Append a one-bit bool/flag value |val| to the stream.
  46. void AppendBool(bool val);
  47. // Append a signed value in |val| in Exp-Golomb code.
  48. void AppendSE(int val);
  49. // Append an unsigned value in |val| in Exp-Golomb code.
  50. void AppendUE(unsigned int val);
  51. // Start a new NALU of type |nalu_type| and with given |nal_ref_idc|
  52. // (see spec). Note, that until FinishNALU() is called, some of the bits
  53. // may not be flushed into the buffer and the data will not be correctly
  54. // aligned with trailing bits.
  55. void BeginNALU(H264NALU::Type nalu_type, int nal_ref_idc);
  56. // Finish current NALU. This will flush any cached bits and correctly align
  57. // the buffer with RBSP trailing bits. This MUST be called for the stream
  58. // returned by data() to be correct.
  59. void FinishNALU();
  60. // Finishes current bit stream. This will flush any cached bits in the reg
  61. // without RBSP trailing bits alignment. e.g. for packed slice header, it is
  62. // not a complete NALU, the slice data and RBSP trailing will be filled by
  63. // user mode driver. This MUST be called for the stream returned by data() to
  64. // be correct.
  65. void Flush();
  66. // Return number of full bytes in the stream. Note that FinishNALU() has to
  67. // be called to flush cached bits, or the return value will not include them.
  68. size_t BytesInBuffer() const;
  69. // Returns number of bits in the stream. Note that FinishNALU() or Flush() has
  70. // to be called to flush cached bits, or the return value will not include
  71. // them.
  72. size_t BitsInBuffer() const;
  73. // Return a pointer to the stream. FinishNALU() must be called before
  74. // accessing the stream, otherwise some bits may still be cached and not
  75. // in the buffer.
  76. const uint8_t* data() const;
  77. private:
  78. friend class base::RefCountedThreadSafe<H264BitstreamBuffer>;
  79. ~H264BitstreamBuffer();
  80. FRIEND_TEST_ALL_PREFIXES(H264BitstreamBufferAppendBitsTest,
  81. AppendAndVerifyBits);
  82. // Allocate additional memory (kGrowBytes bytes) for the buffer.
  83. void Grow();
  84. // Append |num_bits| bits from U64 value |val| (in host endianness).
  85. void AppendU64(size_t num_bits, uint64_t val);
  86. // Flush any cached bits in the reg with byte granularity, i.e. enough
  87. // bytes to flush all pending bits, but not more.
  88. void FlushReg();
  89. typedef uint64_t RegType;
  90. enum {
  91. // Sizes of reg_.
  92. kRegByteSize = sizeof(RegType),
  93. kRegBitSize = kRegByteSize * 8,
  94. // Amount of bytes to grow the buffer by when we run out of
  95. // previously-allocated memory for it.
  96. kGrowBytes = 4096,
  97. };
  98. static_assert(kGrowBytes >= kRegByteSize,
  99. "kGrowBytes must be larger than kRegByteSize");
  100. // Unused bits left in reg_.
  101. size_t bits_left_in_reg_;
  102. // Cache for appended bits. Bits are flushed to data_ with kRegByteSize
  103. // granularity, i.e. when reg_ becomes full, or when an explicit FlushReg()
  104. // is called.
  105. RegType reg_;
  106. // Current capacity of data_, in bytes.
  107. size_t capacity_;
  108. // Current byte offset in data_ (points to the start of unwritten bits).
  109. size_t pos_;
  110. // Current last bit in data_ (points to the start of unwritten bit).
  111. size_t bits_in_buffer_;
  112. // Buffer for stream data.
  113. raw_ptr<uint8_t> data_;
  114. };
  115. } // namespace media
  116. #endif // MEDIA_FILTERS_H264_BITSTREAM_BUFFER_H_