OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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/video_frame.h" |
| 16 #include "media/filters/h264_parser.h" |
| 17 |
| 18 namespace media { |
| 19 |
| 20 // Holds one or more NALUs as a raw bitstream buffer in H.264 Annex-B format. |
| 21 // Note that this class currently does NOT insert emulation prevention |
| 22 // three-byte sequences (spec 7.3.1). |
| 23 class MEDIA_EXPORT H264BitstreamBuffer { |
| 24 public: |
| 25 H264BitstreamBuffer(); |
| 26 ~H264BitstreamBuffer(); |
| 27 |
| 28 // Discard all data and reset the buffer for reuse. |
| 29 void Reset(); |
| 30 |
| 31 // Append |num_bits| bits to the stream from |val|. |
| 32 // |val| is interpreted in the host endianness. |
| 33 template <typename T> |
| 34 void AppendBits(size_t num_bits, T val) { |
| 35 DCHECK_GE(val, 0); |
| 36 AppendU64(num_bits, static_cast<uint64>(val)); |
| 37 } |
| 38 |
| 39 void AppendBits(size_t num_bits, bool val) { |
| 40 DCHECK_EQ(num_bits, 1); |
| 41 AppendBool(val); |
| 42 } |
| 43 |
| 44 // Append a one-bit bool/flag value |val| to the stream. |
| 45 void AppendBool(bool val); |
| 46 |
| 47 // Append a signed value in |val| in Exp-Golomb code. |
| 48 void AppendSE(int val); |
| 49 |
| 50 // Append an unsigned value in |val| in Exp-Golomb code. |
| 51 void AppendUE(unsigned int val); |
| 52 |
| 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); |
| 58 |
| 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(); |
| 63 |
| 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(); |
| 67 |
| 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(); |
| 72 |
| 73 private: |
| 74 FRIEND_TEST_ALL_PREFIXES(H264BitstreamBufferAppendBitsTest, |
| 75 AppendAndVerifyBits); |
| 76 |
| 77 // Allocate additional memory (kGrowBytes bytes) for the buffer. |
| 78 void Grow(); |
| 79 |
| 80 // Append |num_bits| bits from U64 value |val| (in host endianness). |
| 81 void AppendU64(size_t num_bits, uint64 val); |
| 82 |
| 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(); |
| 86 |
| 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, |
| 95 }; |
| 96 |
| 97 COMPILE_ASSERT(kGrowBytes >= kRegByteSize, |
| 98 kGrowBytes_must_be_larger_than_kRegByteSize); |
| 99 |
| 100 // Unused bits left in reg_. |
| 101 size_t bits_left_in_reg_; |
| 102 |
| 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_; |
| 107 |
| 108 // Current capacity of data_, in bytes. |
| 109 size_t capacity_; |
| 110 |
| 111 // Current byte offset in data_ (points to the start of unwritten bits). |
| 112 size_t pos_; |
| 113 |
| 114 // Buffer for stream data. |
| 115 uint8* data_; |
| 116 }; |
| 117 |
| 118 } // namespace media |
| 119 |
| 120 #endif // MEDIA_FILTERS_H264_BITSTREAM_BUFFER_H_ |
OLD | NEW |