1 // Copyright (c) 2011 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.
5 #include "base/files/file_path.h"
6 #include "base/logging.h"
7 #include "base/path_service.h"
8 #include "media/base/media.h"
9 #include "media/ffmpeg/ffmpeg_common.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 using base::TimeDelta
;
16 static AVIndexEntry kIndexEntries
[] = {
17 // pos, timestamp, flags, size, min_distance
18 { 0, 0, AVINDEX_KEYFRAME
, 0, 0 },
19 { 2000, 1000, AVINDEX_KEYFRAME
, 0, 0 },
20 { 3000, 2000, 0, 0, 0 },
21 { 5000, 3000, AVINDEX_KEYFRAME
, 0, 0 },
22 { 6000, 4000, 0, 0, 0 },
23 { 8000, 5000, AVINDEX_KEYFRAME
, 0, 0 },
24 { 9000, 6000, AVINDEX_KEYFRAME
, 0, 0 },
25 { 11500, 7000, AVINDEX_KEYFRAME
, 0, 0 },
28 static const AVRational kTimeBase
= { 1, 1000 };
30 class FFmpegCommonTest
: public testing::Test
{
33 virtual ~FFmpegCommonTest();
38 DISALLOW_COPY_AND_ASSIGN(FFmpegCommonTest
);
41 static bool InitFFmpeg() {
42 static bool initialized
= false;
47 PathService::Get(base::DIR_MODULE
, &path
);
48 return media::InitializeMediaLibrary(path
);
51 FFmpegCommonTest::FFmpegCommonTest() {
53 stream_
.time_base
= kTimeBase
;
54 stream_
.index_entries
= kIndexEntries
;
55 stream_
.index_entries_allocated_size
= sizeof(kIndexEntries
);
56 stream_
.nb_index_entries
= arraysize(kIndexEntries
);
59 FFmpegCommonTest::~FFmpegCommonTest() {}
61 TEST_F(FFmpegCommonTest
, TimeBaseConversions
) {
62 int64 test_data
[][5] = {
63 {1, 2, 1, 500000, 1 },
64 {1, 3, 1, 333333, 1 },
65 {1, 3, 2, 666667, 2 },
68 for (size_t i
= 0; i
< arraysize(test_data
); ++i
) {
72 time_base
.num
= static_cast<int>(test_data
[i
][0]);
73 time_base
.den
= static_cast<int>(test_data
[i
][1]);
75 TimeDelta time_delta
= ConvertFromTimeBase(time_base
, test_data
[i
][2]);
77 EXPECT_EQ(time_delta
.InMicroseconds(), test_data
[i
][3]);
78 EXPECT_EQ(ConvertToTimeBase(time_base
, time_delta
), test_data
[i
][4]);
82 TEST_F(FFmpegCommonTest
, VerifyFormatSizes
) {
83 for (AVSampleFormat format
= AV_SAMPLE_FMT_NONE
;
84 format
< AV_SAMPLE_FMT_NB
;
85 format
= static_cast<AVSampleFormat
>(format
+ 1)) {
86 SampleFormat sample_format
= AVSampleFormatToSampleFormat(format
);
87 if (sample_format
== kUnknownSampleFormat
) {
88 // This format not supported, so skip it.
92 // Have FFMpeg compute the size of a buffer of 1 channel / 1 frame
93 // with 1 byte alignment to make sure the sizes match.
94 int single_buffer_size
= av_samples_get_buffer_size(NULL
, 1, 1, format
, 1);
95 int bytes_per_channel
= SampleFormatToBytesPerChannel(sample_format
);
96 EXPECT_EQ(bytes_per_channel
, single_buffer_size
);
100 TEST_F(FFmpegCommonTest
, UTCDateToTime_Valid
) {
102 EXPECT_TRUE(FFmpegUTCDateToTime("2012-11-10 12:34:56", &result
));
104 base::Time::Exploded exploded
;
105 result
.UTCExplode(&exploded
);
106 EXPECT_TRUE(exploded
.HasValidValues());
107 EXPECT_EQ(2012, exploded
.year
);
108 EXPECT_EQ(11, exploded
.month
);
109 EXPECT_EQ(6, exploded
.day_of_week
);
110 EXPECT_EQ(10, exploded
.day_of_month
);
111 EXPECT_EQ(12, exploded
.hour
);
112 EXPECT_EQ(34, exploded
.minute
);
113 EXPECT_EQ(56, exploded
.second
);
114 EXPECT_EQ(0, exploded
.millisecond
);
117 TEST_F(FFmpegCommonTest
, UTCDateToTime_Invalid
) {
118 const char* invalid_date_strings
[] = {
130 "2012-11-10-12 12:34:56",
132 "2012-11-10 12:34:56:78",
133 "ABCD-11-10 12:34:56",
134 "2012-EF-10 12:34:56",
135 "2012-11-GH 12:34:56",
136 "2012-11-10 IJ:34:56",
137 "2012-11-10 12:JL:56",
138 "2012-11-10 12:34:MN",
139 "2012-11-10 12:34:56.123",
140 "2012-11-1012:34:56",
141 "2012-11-10 12:34:56 UTC",
144 for (size_t i
= 0; i
< arraysize(invalid_date_strings
); ++i
) {
145 const char* date_string
= invalid_date_strings
[i
];
147 EXPECT_FALSE(FFmpegUTCDateToTime(date_string
, &result
))
148 << "date_string '" << date_string
<< "'";
149 EXPECT_TRUE(result
.is_null());