1 // Copyright 2014 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
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.
10 #ifndef MEDIA_FILTERS_H264_BITSTREAM_BUFFER_H_
11 #define MEDIA_FILTERS_H264_BITSTREAM_BUFFER_H_
16 #include "base/gtest_prod_util.h"
17 #include "base/memory/raw_ptr.h"
18 #include "base/memory/ref_counted.h"
19 #include "base/numerics/safe_conversions.h"
20 #include "media/base/media_export.h"
21 #include "media/base/video_frame.h"
22 #include "media/video/h264_parser.h"
26 // Holds one or more NALUs as a raw bitstream buffer in H.264 Annex-B format.
27 // Note that this class currently does NOT insert emulation prevention
28 // three-byte sequences (spec 7.3.1).
29 // Refcounted as these buffers may be used as arguments to multiple codec jobs
30 // (e.g. a buffer containing an H.264 SPS NALU may be used as an argument to all
31 // jobs that use parameters contained in that SPS).
32 class MEDIA_EXPORT H264BitstreamBuffer
33 : public base::RefCountedThreadSafe<H264BitstreamBuffer> {
35 H264BitstreamBuffer();
37 H264BitstreamBuffer(const H264BitstreamBuffer&) = delete;
38 H264BitstreamBuffer& operator=(const H264BitstreamBuffer&) = delete;
40 // Discard all data and reset the buffer for reuse.
43 // Append |num_bits| bits to the stream from |val|.
44 // |val| is interpreted in the host endianness.
46 void AppendBits(size_t num_bits, T val) {
47 AppendU64(num_bits, static_cast<uint64_t>(val));
50 void AppendBits(size_t num_bits, bool val) {
51 DCHECK_EQ(num_bits, 1ul);
55 // Append a one-bit bool/flag value |val| to the stream.
56 void AppendBool(bool val);
58 // Append a signed value in |val| in Exp-Golomb code.
59 void AppendSE(int val);
61 // Append an unsigned value in |val| in Exp-Golomb code.
62 void AppendUE(unsigned int val);
64 // Start a new NALU of type |nalu_type| and with given |nal_ref_idc|
65 // (see spec). Note, that until FinishNALU() is called, some of the bits
66 // may not be flushed into the buffer and the data will not be correctly
67 // aligned with trailing bits.
68 void BeginNALU(H264NALU::Type nalu_type, int nal_ref_idc);
70 // Finish current NALU. This will flush any cached bits and correctly align
71 // the buffer with RBSP trailing bits. This MUST be called for the stream
72 // returned by data() to be correct.
75 // Finishes current bit stream. This will flush any cached bits in the reg
76 // without RBSP trailing bits alignment. e.g. for packed slice header, it is
77 // not a complete NALU, the slice data and RBSP trailing will be filled by
78 // user mode driver. This MUST be called for the stream returned by data() to
82 // Return number of full bytes in the stream. Note that FinishNALU() has to
83 // be called to flush cached bits, or the return value will not include them.
84 size_t BytesInBuffer() const;
86 // Returns number of bits in the stream. Note that FinishNALU() or Flush() has
87 // to be called to flush cached bits, or the return value will not include
89 size_t BitsInBuffer() const;
91 // Return a pointer to the stream. FinishNALU() must be called before
92 // accessing the stream, otherwise some bits may still be cached and not
94 const uint8_t* data() const;
97 friend class base::RefCountedThreadSafe<H264BitstreamBuffer>;
98 ~H264BitstreamBuffer();
100 FRIEND_TEST_ALL_PREFIXES(H264BitstreamBufferAppendBitsTest,
101 AppendAndVerifyBits);
103 // Allocate additional memory (kGrowBytes bytes) for the buffer.
106 // Append |num_bits| bits from U64 value |val| (in host endianness).
107 void AppendU64(size_t num_bits, uint64_t val);
109 // Flush any cached bits in the reg with byte granularity, i.e. enough
110 // bytes to flush all pending bits, but not more.
113 typedef uint64_t RegType;
116 kRegByteSize = sizeof(RegType),
117 kRegBitSize = kRegByteSize * 8,
118 // Amount of bytes to grow the buffer by when we run out of
119 // previously-allocated memory for it.
123 static_assert(kGrowBytes >= kRegByteSize,
124 "kGrowBytes must be larger than kRegByteSize");
126 // Unused bits left in reg_.
127 size_t bits_left_in_reg_;
129 // Cache for appended bits. Bits are flushed to data_ with kRegByteSize
130 // granularity, i.e. when reg_ becomes full, or when an explicit FlushReg()
134 // Current capacity of data_, in bytes.
137 // Current byte offset in data_ (points to the start of unwritten bits).
139 // Current last bit in data_ (points to the start of unwritten bit).
140 size_t bits_in_buffer_;
142 // Buffer for stream data.
143 raw_ptr<uint8_t, DanglingUntriaged | AllowPtrArithmetic> data_;
148 #endif // MEDIA_FILTERS_H264_BITSTREAM_BUFFER_H_