1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73

media / parsers / h264_bit_reader_unittest.cc [blame]

// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/parsers/h264_bit_reader.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace media {

TEST(H264BitReaderTest, ReadStreamWithoutEscapeAndTrailingZeroBytes) {
  H264BitReader reader;
  const unsigned char rbsp[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xa0};
  int dummy = 0;

  EXPECT_TRUE(reader.Initialize(rbsp, sizeof(rbsp)));

  EXPECT_TRUE(reader.ReadBits(1, &dummy));
  EXPECT_EQ(dummy, 0x00);
  EXPECT_EQ(reader.NumBitsLeft(), 47);
  EXPECT_TRUE(reader.HasMoreRBSPData());

  EXPECT_TRUE(reader.ReadBits(8, &dummy));
  EXPECT_EQ(dummy, 0x02);
  EXPECT_EQ(reader.NumBitsLeft(), 39);
  EXPECT_TRUE(reader.HasMoreRBSPData());

  EXPECT_TRUE(reader.ReadBits(31, &dummy));
  EXPECT_EQ(dummy, 0x23456789);
  EXPECT_EQ(reader.NumBitsLeft(), 8);
  EXPECT_TRUE(reader.HasMoreRBSPData());

  EXPECT_TRUE(reader.ReadBits(1, &dummy));
  EXPECT_EQ(dummy, 1);
  EXPECT_EQ(reader.NumBitsLeft(), 7);
  EXPECT_TRUE(reader.HasMoreRBSPData());

  EXPECT_TRUE(reader.ReadBits(1, &dummy));
  EXPECT_EQ(dummy, 0);
  EXPECT_EQ(reader.NumBitsLeft(), 6);
  EXPECT_FALSE(reader.HasMoreRBSPData());
}

TEST(H264BitReaderTest, SingleByteStream) {
  H264BitReader reader;
  const unsigned char rbsp[] = {0x18};
  int dummy = 0;

  EXPECT_TRUE(reader.Initialize(rbsp, sizeof(rbsp)));
  EXPECT_EQ(reader.NumBitsLeft(), 8);
  EXPECT_TRUE(reader.HasMoreRBSPData());

  EXPECT_TRUE(reader.ReadBits(4, &dummy));
  EXPECT_EQ(dummy, 0x01);
  EXPECT_EQ(reader.NumBitsLeft(), 4);
  EXPECT_FALSE(reader.HasMoreRBSPData());
}

TEST(H264BitReaderTest, StopBitOccupyFullByte) {
  H264BitReader reader;
  const unsigned char rbsp[] = {0xab, 0x80};
  int dummy = 0;

  EXPECT_TRUE(reader.Initialize(rbsp, sizeof(rbsp)));
  EXPECT_EQ(reader.NumBitsLeft(), 16);
  EXPECT_TRUE(reader.HasMoreRBSPData());

  EXPECT_TRUE(reader.ReadBits(8, &dummy));
  EXPECT_EQ(dummy, 0xab);
  EXPECT_EQ(reader.NumBitsLeft(), 8);
  EXPECT_FALSE(reader.HasMoreRBSPData());
}

}  // namespace media