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
);