Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(232)

Side by Side Diff: media/filters/h264_bitstream_buffer.h

Issue 333253002: Add VaapiVideoEncodeAccelerator for HW-accelerated video encode. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
10 #ifndef MEDIA_FILTERS_H264_BITSTREAM_BUFFER_H_
11 #define MEDIA_FILTERS_H264_BITSTREAM_BUFFER_H_
12
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"
18
19 namespace media {
20
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();
28
29 // Discard all data and reset the buffer for reuse.
30 void Reset();
31
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 DCHECK_GE(val, 0);
37 AppendU64(num_bits, static_cast<uint64>(val));
38 }
39
40 void AppendBits(size_t num_bits, bool val) {
41 DCHECK_EQ(num_bits, 1);
42 AppendBool(val);
43 }
44
45 // Append a one-bit bool/flag value |val| to the stream.
46 void AppendBool(bool val);
47
48 // Append a signed value in |val| in Exp-Golomb code.
49 void AppendSE(int val);
50
51 // Append an unsigned value in |val| in Exp-Golomb code.
52 void AppendUE(unsigned int val);
53
54 // Start a new NALU of type |nalu_type| and with given |nal_ref_idc|
55 // (see spec). Note, that until FinishNALU() is called, some of the bits
56 // may not be flushed into the buffer and the data will not be correctly
57 // aligned with trailing bits.
58 void BeginNALU(H264NALU::Type nalu_type, int nal_ref_idc);
59
60 // Finish current NALU. This will flush any cached bits and correctly align
61 // the buffer with RBSP trailing bits. This MUST be called for the stream
62 // returned by data() to be correct.
63 void FinishNALU();
64
65 // Return number of full bytes in the stream. Note that FinishNALU() has to
66 // be called to flush cached bits, or the return value will not include them.
67 size_t BytesInBuffer();
68
69 // Return a pointer to the stream. FinishNALU() must be called before
70 // accessing the stream, otherwise some bits may still be cached and not
71 // in the buffer.
72 uint8* data();
73
74 private:
75 FRIEND_TEST_ALL_PREFIXES(H264BitstreamBufferAppendBitsTest,
76 AppendAndVerifyBits);
77
78 // Allocate additional memory (kGrowBytes bytes) for the buffer.
79 void Grow();
80
81 // Append |num_bits| bits from U64 value |val| (in host endianness).
82 void AppendU64(size_t num_bits, uint64 val);
83
84 // Flush any cached bits in the reg with byte granularity, i.e. enough
85 // bytes to flush all pending bits, but not more.
86 void FlushReg();
87
88 typedef uint64 RegType;
89 enum {
90 // Sizes of reg_.
91 kRegByteSize = sizeof(RegType),
92 kRegBitSize = kRegByteSize * 8,
93 // Amount of bytes to grow the buffer by when we run out of
94 // previously-allocated memory for it.
95 kGrowBytes = 4096,
96 };
97
98 COMPILE_ASSERT(kGrowBytes >= kRegByteSize,
99 kGrowBytes_must_be_larger_than_kRegByteSize);
100
101 // Unused bits left in reg_.
102 size_t bits_left_in_reg_;
103
104 // Cache for appended bits. Bits are flushed to data_ with kRegByteSize
105 // granularity, i.e. when reg_ becomes full, or when an explicit FlushReg()
106 // is called.
107 RegType reg_;
108
109 // Current capacity of data_, in bytes.
110 size_t capacity_;
111
112 // Current byte offset in data_ (points to the start of unwritten bits).
113 size_t pos_;
114
115 // Buffer for stream data.
116 uint8* data_;
117 };
118
119 } // namespace media
120
121 #endif // MEDIA_FILTERS_H264_BITSTREAM_BUFFER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698