2014-04-01 01:34:59 +00:00
|
|
|
// Copyright 2014 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
//
|
|
|
|
// This file contains an implementation of an H264 Annex-B video stream parser.
|
|
|
|
|
2017-12-20 00:56:36 +00:00
|
|
|
#ifndef PACKAGER_MEDIA_CODECS_H264_BIT_READER_H_
|
|
|
|
#define PACKAGER_MEDIA_CODECS_H264_BIT_READER_H_
|
2014-04-01 01:34:59 +00:00
|
|
|
|
2014-09-30 23:52:58 +00:00
|
|
|
#include <stdint.h>
|
2014-04-01 01:34:59 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/base/macros.h"
|
2014-04-01 01:34:59 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2014-04-01 01:34:59 +00:00
|
|
|
namespace media {
|
|
|
|
|
2016-02-22 23:32:22 +00:00
|
|
|
// A class to provide bit-granularity reading of H.264/H.265 streams.
|
2014-04-01 01:34:59 +00:00
|
|
|
// This is not a generic bit reader class, as it takes into account
|
|
|
|
// H.264 stream-specific constraints, such as skipping emulation-prevention
|
|
|
|
// bytes and stop bits. See spec for more details.
|
2016-03-14 19:02:21 +00:00
|
|
|
class H26xBitReader {
|
2014-04-01 01:34:59 +00:00
|
|
|
public:
|
2016-03-14 19:02:21 +00:00
|
|
|
H26xBitReader();
|
|
|
|
~H26xBitReader();
|
2014-04-01 01:34:59 +00:00
|
|
|
|
|
|
|
// Initialize the reader to start reading at |data|, |size| being size
|
|
|
|
// of |data| in bytes.
|
|
|
|
// Return false on insufficient size of stream..
|
2014-09-30 21:52:21 +00:00
|
|
|
bool Initialize(const uint8_t* data, off_t size);
|
2014-04-01 01:34:59 +00:00
|
|
|
|
|
|
|
// Read |num_bits| next bits from stream and return in |*out|, first bit
|
|
|
|
// from the stream starting at |num_bits| position in |*out|.
|
|
|
|
// |num_bits| may be 1-32, inclusive.
|
|
|
|
// Return false if the given number of bits cannot be read (not enough
|
|
|
|
// bits in the stream), true otherwise.
|
|
|
|
bool ReadBits(int num_bits, int* out);
|
|
|
|
|
2016-02-22 23:32:22 +00:00
|
|
|
// Read a single bit and return in |*out|.
|
|
|
|
// Return false if the bit cannot be read (not enough bits in the stream),
|
|
|
|
// true otherwise.
|
|
|
|
bool ReadBool(bool* out) {
|
|
|
|
int value;
|
|
|
|
if (!ReadBits(1, &value))
|
|
|
|
return false;
|
|
|
|
*out = (value != 0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skips the given number of bits (does not have to be less than 32 bits).
|
|
|
|
// Return false if there aren't enough bits in the stream, true otherwise.
|
|
|
|
bool SkipBits(int num_bits);
|
|
|
|
|
2016-03-04 00:19:36 +00:00
|
|
|
// Exp-Golomb code parsing as specified in chapter 9.1 of the spec.
|
|
|
|
// Read one unsigned exp-Golomb code from the stream and return in |*val|.
|
|
|
|
bool ReadUE(int* val);
|
|
|
|
|
|
|
|
// Read one signed exp-Golomb code from the stream and return in |*val|.
|
|
|
|
bool ReadSE(int* val);
|
|
|
|
|
2014-04-01 01:34:59 +00:00
|
|
|
// Return the number of bits left in the stream.
|
|
|
|
off_t NumBitsLeft();
|
|
|
|
|
|
|
|
// See the definition of more_rbsp_data() in spec.
|
|
|
|
bool HasMoreRBSPData();
|
|
|
|
|
|
|
|
// Return the number of emulation prevention bytes already read.
|
|
|
|
size_t NumEmulationPreventionBytesRead();
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Advance to the next byte, loading it into curr_byte_.
|
|
|
|
// Return false on end of stream.
|
|
|
|
bool UpdateCurrByte();
|
|
|
|
|
|
|
|
// Pointer to the next unread (not in curr_byte_) byte in the stream.
|
2014-09-30 21:52:21 +00:00
|
|
|
const uint8_t* data_;
|
2014-04-01 01:34:59 +00:00
|
|
|
|
|
|
|
// Bytes left in the stream (without the curr_byte_).
|
|
|
|
off_t bytes_left_;
|
|
|
|
|
|
|
|
// Contents of the current byte; first unread bit starting at position
|
|
|
|
// 8 - num_remaining_bits_in_curr_byte_ from MSB.
|
|
|
|
int curr_byte_;
|
|
|
|
|
|
|
|
// Number of bits remaining in curr_byte_
|
|
|
|
int num_remaining_bits_in_curr_byte_;
|
|
|
|
|
|
|
|
// Used in emulation prevention three byte detection (see spec).
|
|
|
|
// Initially set to 0xffff to accept all initial two-byte sequences.
|
|
|
|
int prev_two_bytes_;
|
|
|
|
|
|
|
|
// Number of emulation preventation bytes (0x000003) we met.
|
|
|
|
size_t emulation_prevention_bytes_;
|
|
|
|
|
2016-03-14 19:02:21 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(H26xBitReader);
|
2014-04-01 01:34:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|
2014-04-01 01:34:59 +00:00
|
|
|
|
2017-12-20 00:56:36 +00:00
|
|
|
#endif // PACKAGER_MEDIA_CODECS_H264_BIT_READER_H_
|