avc_unittest.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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 <string.h> 6 7#include "base/basictypes.h" 8#include "media/base/stream_parser_buffer.h" 9#include "media/formats/mp4/avc.h" 10#include "media/formats/mp4/box_definitions.h" 11#include "testing/gtest/include/gtest/gtest.h" 12 13namespace media { 14namespace mp4 { 15 16static const uint8 kNALU1[] = { 0x01, 0x02, 0x03 }; 17static const uint8 kNALU2[] = { 0x04, 0x05, 0x06, 0x07 }; 18static const uint8 kExpected[] = { 19 0x00, 0x00, 0x00, 0x01, 0x01, 0x02, 0x03, 20 0x00, 0x00, 0x00, 0x01, 0x04, 0x05, 0x06, 0x07 }; 21 22static const uint8 kExpectedParamSets[] = { 23 0x00, 0x00, 0x00, 0x01, 0x67, 0x12, 24 0x00, 0x00, 0x00, 0x01, 0x67, 0x34, 25 0x00, 0x00, 0x00, 0x01, 0x68, 0x56, 0x78}; 26 27class AVCConversionTest : public testing::TestWithParam<int> { 28 protected: 29 void MakeInputForLength(int length_size, std::vector<uint8>* buf) { 30 buf->clear(); 31 for (int i = 1; i < length_size; i++) 32 buf->push_back(0); 33 buf->push_back(sizeof(kNALU1)); 34 buf->insert(buf->end(), kNALU1, kNALU1 + sizeof(kNALU1)); 35 36 for (int i = 1; i < length_size; i++) 37 buf->push_back(0); 38 buf->push_back(sizeof(kNALU2)); 39 buf->insert(buf->end(), kNALU2, kNALU2 + sizeof(kNALU2)); 40 } 41}; 42 43TEST_P(AVCConversionTest, ParseCorrectly) { 44 std::vector<uint8> buf; 45 MakeInputForLength(GetParam(), &buf); 46 EXPECT_TRUE(AVC::ConvertFrameToAnnexB(GetParam(), &buf)); 47 EXPECT_EQ(buf.size(), sizeof(kExpected)); 48 EXPECT_EQ(0, memcmp(kExpected, &buf[0], sizeof(kExpected))); 49} 50 51TEST_P(AVCConversionTest, ParsePartial) { 52 std::vector<uint8> buf; 53 MakeInputForLength(GetParam(), &buf); 54 buf.pop_back(); 55 EXPECT_FALSE(AVC::ConvertFrameToAnnexB(GetParam(), &buf)); 56 // This tests a buffer ending in the middle of a NAL length. For length size 57 // of one, this can't happen, so we skip that case. 58 if (GetParam() != 1) { 59 MakeInputForLength(GetParam(), &buf); 60 buf.erase(buf.end() - (sizeof(kNALU2) + 1), buf.end()); 61 EXPECT_FALSE(AVC::ConvertFrameToAnnexB(GetParam(), &buf)); 62 } 63} 64 65TEST_P(AVCConversionTest, ParseEmpty) { 66 std::vector<uint8> buf; 67 EXPECT_TRUE(AVC::ConvertFrameToAnnexB(GetParam(), &buf)); 68 EXPECT_EQ(0u, buf.size()); 69} 70 71INSTANTIATE_TEST_CASE_P(AVCConversionTestValues, 72 AVCConversionTest, 73 ::testing::Values(1, 2, 4)); 74 75TEST_F(AVCConversionTest, ConvertConfigToAnnexB) { 76 AVCDecoderConfigurationRecord avc_config; 77 avc_config.sps_list.resize(2); 78 avc_config.sps_list[0].push_back(0x67); 79 avc_config.sps_list[0].push_back(0x12); 80 avc_config.sps_list[1].push_back(0x67); 81 avc_config.sps_list[1].push_back(0x34); 82 avc_config.pps_list.resize(1); 83 avc_config.pps_list[0].push_back(0x68); 84 avc_config.pps_list[0].push_back(0x56); 85 avc_config.pps_list[0].push_back(0x78); 86 87 std::vector<uint8> buf; 88 EXPECT_TRUE(AVC::ConvertConfigToAnnexB(avc_config, &buf)); 89 EXPECT_EQ(0, memcmp(kExpectedParamSets, &buf[0], 90 sizeof(kExpectedParamSets))); 91} 92 93} // namespace mp4 94} // namespace media 95