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