Home | History | Annotate | Download | only in filters
      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 #include "media/filters/h264_bit_reader.h"
      6 #include "testing/gtest/include/gtest/gtest.h"
      7 
      8 namespace media {
      9 
     10 TEST(H264BitReaderTest, ReadStreamWithoutEscapeAndTrailingZeroBytes) {
     11   H264BitReader reader;
     12   const unsigned char rbsp[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xa0};
     13   int dummy = 0;
     14 
     15   EXPECT_TRUE(reader.Initialize(rbsp, sizeof(rbsp)));
     16 
     17   EXPECT_TRUE(reader.ReadBits(1, &dummy));
     18   EXPECT_EQ(dummy, 0x00);
     19   EXPECT_EQ(reader.NumBitsLeft(), 47);
     20   EXPECT_TRUE(reader.HasMoreRBSPData());
     21 
     22   EXPECT_TRUE(reader.ReadBits(8, &dummy));
     23   EXPECT_EQ(dummy, 0x02);
     24   EXPECT_EQ(reader.NumBitsLeft(), 39);
     25   EXPECT_TRUE(reader.HasMoreRBSPData());
     26 
     27   EXPECT_TRUE(reader.ReadBits(31, &dummy));
     28   EXPECT_EQ(dummy, 0x23456789);
     29   EXPECT_EQ(reader.NumBitsLeft(), 8);
     30   EXPECT_TRUE(reader.HasMoreRBSPData());
     31 
     32   EXPECT_TRUE(reader.ReadBits(1, &dummy));
     33   EXPECT_EQ(dummy, 1);
     34   EXPECT_EQ(reader.NumBitsLeft(), 7);
     35   EXPECT_TRUE(reader.HasMoreRBSPData());
     36 
     37   EXPECT_TRUE(reader.ReadBits(1, &dummy));
     38   EXPECT_EQ(dummy, 0);
     39   EXPECT_EQ(reader.NumBitsLeft(), 6);
     40   EXPECT_FALSE(reader.HasMoreRBSPData());
     41 }
     42 
     43 TEST(H264BitReaderTest, SingleByteStream) {
     44   H264BitReader reader;
     45   const unsigned char rbsp[] = {0x18};
     46   int dummy = 0;
     47 
     48   EXPECT_TRUE(reader.Initialize(rbsp, sizeof(rbsp)));
     49   EXPECT_EQ(reader.NumBitsLeft(), 8);
     50   EXPECT_TRUE(reader.HasMoreRBSPData());
     51 
     52   EXPECT_TRUE(reader.ReadBits(4, &dummy));
     53   EXPECT_EQ(dummy, 0x01);
     54   EXPECT_EQ(reader.NumBitsLeft(), 4);
     55   EXPECT_FALSE(reader.HasMoreRBSPData());
     56 }
     57 
     58 TEST(H264BitReaderTest, StopBitOccupyFullByte) {
     59   H264BitReader reader;
     60   const unsigned char rbsp[] = {0xab, 0x80};
     61   int dummy = 0;
     62 
     63   EXPECT_TRUE(reader.Initialize(rbsp, sizeof(rbsp)));
     64   EXPECT_EQ(reader.NumBitsLeft(), 16);
     65   EXPECT_TRUE(reader.HasMoreRBSPData());
     66 
     67   EXPECT_TRUE(reader.ReadBits(8, &dummy));
     68   EXPECT_EQ(dummy, 0xab);
     69   EXPECT_EQ(reader.NumBitsLeft(), 8);
     70   EXPECT_FALSE(reader.HasMoreRBSPData());
     71 }
     72 
     73 }  // namespace media
     74