Updating XTBs based on .GRDs from branch master
[chromium-blink-merge.git] / media / filters / h264_bitstream_buffer.h
blobcbe2047452b7109a27226e21331cad1fb8594b44
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.
10 #ifndef MEDIA_FILTERS_H264_BITSTREAM_BUFFER_H_
11 #define MEDIA_FILTERS_H264_BITSTREAM_BUFFER_H_
13 #include "base/gtest_prod_util.h"
14 #include "base/numerics/safe_conversions.h"
15 #include "media/base/media_export.h"
16 #include "media/base/video_frame.h"
17 #include "media/filters/h264_parser.h"
19 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 class MEDIA_EXPORT H264BitstreamBuffer {
25 public:
26 H264BitstreamBuffer();
27 ~H264BitstreamBuffer();
29 // Discard all data and reset the buffer for reuse.
30 void Reset();
32 // Append |num_bits| bits to the stream from |val|.
33 // |val| is interpreted in the host endianness.
34 template <typename T>
35 void AppendBits(size_t num_bits, T val) {
36 AppendU64(num_bits, static_cast<uint64>(val));
39 void AppendBits(size_t num_bits, bool val) {
40 DCHECK_EQ(num_bits, 1ul);
41 AppendBool(val);
44 // Append a one-bit bool/flag value |val| to the stream.
45 void AppendBool(bool val);
47 // Append a signed value in |val| in Exp-Golomb code.
48 void AppendSE(int val);
50 // Append an unsigned value in |val| in Exp-Golomb code.
51 void AppendUE(unsigned int val);
53 // Start a new NALU of type |nalu_type| and with given |nal_ref_idc|
54 // (see spec). Note, that until FinishNALU() is called, some of the bits
55 // may not be flushed into the buffer and the data will not be correctly
56 // aligned with trailing bits.
57 void BeginNALU(H264NALU::Type nalu_type, int nal_ref_idc);
59 // Finish current NALU. This will flush any cached bits and correctly align
60 // the buffer with RBSP trailing bits. This MUST be called for the stream
61 // returned by data() to be correct.
62 void FinishNALU();
64 // Return number of full bytes in the stream. Note that FinishNALU() has to
65 // be called to flush cached bits, or the return value will not include them.
66 size_t BytesInBuffer();
68 // Return a pointer to the stream. FinishNALU() must be called before
69 // accessing the stream, otherwise some bits may still be cached and not
70 // in the buffer.
71 uint8* data();
73 private:
74 FRIEND_TEST_ALL_PREFIXES(H264BitstreamBufferAppendBitsTest,
75 AppendAndVerifyBits);
77 // Allocate additional memory (kGrowBytes bytes) for the buffer.
78 void Grow();
80 // Append |num_bits| bits from U64 value |val| (in host endianness).
81 void AppendU64(size_t num_bits, uint64 val);
83 // Flush any cached bits in the reg with byte granularity, i.e. enough
84 // bytes to flush all pending bits, but not more.
85 void FlushReg();
87 typedef uint64 RegType;
88 enum {
89 // Sizes of reg_.
90 kRegByteSize = sizeof(RegType),
91 kRegBitSize = kRegByteSize * 8,
92 // Amount of bytes to grow the buffer by when we run out of
93 // previously-allocated memory for it.
94 kGrowBytes = 4096,
97 static_assert(kGrowBytes >= kRegByteSize,
98 "kGrowBytes must be larger than kRegByteSize");
100 // Unused bits left in reg_.
101 size_t bits_left_in_reg_;
103 // Cache for appended bits. Bits are flushed to data_ with kRegByteSize
104 // granularity, i.e. when reg_ becomes full, or when an explicit FlushReg()
105 // is called.
106 RegType reg_;
108 // Current capacity of data_, in bytes.
109 size_t capacity_;
111 // Current byte offset in data_ (points to the start of unwritten bits).
112 size_t pos_;
114 // Buffer for stream data.
115 uint8* data_;
118 } // namespace media
120 #endif // MEDIA_FILTERS_H264_BITSTREAM_BUFFER_H_