[M120 Migration][MM] Enable video hole when in full-screen mode in the public profile.
[platform/framework/web/chromium-efl.git] / media / filters / h264_bitstream_buffer.h
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.
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
10 #ifndef MEDIA_FILTERS_H264_BITSTREAM_BUFFER_H_
11 #define MEDIA_FILTERS_H264_BITSTREAM_BUFFER_H_
12
13 #include <stddef.h>
14 #include <stdint.h>
15
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"
23
24 namespace media {
25
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> {
34  public:
35   H264BitstreamBuffer();
36
37   H264BitstreamBuffer(const H264BitstreamBuffer&) = delete;
38   H264BitstreamBuffer& operator=(const H264BitstreamBuffer&) = delete;
39
40   // Discard all data and reset the buffer for reuse.
41   void Reset();
42
43   // Append |num_bits| bits to the stream from |val|.
44   // |val| is interpreted in the host endianness.
45   template <typename T>
46   void AppendBits(size_t num_bits, T val) {
47     AppendU64(num_bits, static_cast<uint64_t>(val));
48   }
49
50   void AppendBits(size_t num_bits, bool val) {
51     DCHECK_EQ(num_bits, 1ul);
52     AppendBool(val);
53   }
54
55   // Append a one-bit bool/flag value |val| to the stream.
56   void AppendBool(bool val);
57
58   // Append a signed value in |val| in Exp-Golomb code.
59   void AppendSE(int val);
60
61   // Append an unsigned value in |val| in Exp-Golomb code.
62   void AppendUE(unsigned int val);
63
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);
69
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.
73   void FinishNALU();
74
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
79   // be correct.
80   void Flush();
81
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;
85
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
88   // them.
89   size_t BitsInBuffer() const;
90
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
93   // in the buffer.
94   const uint8_t* data() const;
95
96  private:
97   friend class base::RefCountedThreadSafe<H264BitstreamBuffer>;
98   ~H264BitstreamBuffer();
99
100   FRIEND_TEST_ALL_PREFIXES(H264BitstreamBufferAppendBitsTest,
101                            AppendAndVerifyBits);
102
103   // Allocate additional memory (kGrowBytes bytes) for the buffer.
104   void Grow();
105
106   // Append |num_bits| bits from U64 value |val| (in host endianness).
107   void AppendU64(size_t num_bits, uint64_t val);
108
109   // Flush any cached bits in the reg with byte granularity, i.e. enough
110   // bytes to flush all pending bits, but not more.
111   void FlushReg();
112
113   typedef uint64_t RegType;
114   enum {
115     // Sizes of reg_.
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.
120     kGrowBytes = 4096,
121   };
122
123   static_assert(kGrowBytes >= kRegByteSize,
124                 "kGrowBytes must be larger than kRegByteSize");
125
126   // Unused bits left in reg_.
127   size_t bits_left_in_reg_;
128
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()
131   // is called.
132   RegType reg_;
133
134   // Current capacity of data_, in bytes.
135   size_t capacity_;
136
137   // Current byte offset in data_ (points to the start of unwritten bits).
138   size_t pos_;
139   // Current last bit in data_ (points to the start of unwritten bit).
140   size_t bits_in_buffer_;
141
142   // Buffer for stream data.
143   raw_ptr<uint8_t, DanglingUntriaged | AllowPtrArithmetic> data_;
144 };
145
146 }  // namespace media
147
148 #endif  // MEDIA_FILTERS_H264_BITSTREAM_BUFFER_H_