Add ianwen to watch list for related projects
[chromium-blink-merge.git] / media / ffmpeg / ffmpeg_common_unittest.cc
blob47c3db36ce182b4e56bd200603abcce9eda9bec8
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/logging.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "media/base/media.h"
8 #include "media/ffmpeg/ffmpeg_common.h"
9 #include "media/filters/ffmpeg_glue.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace media {
14 class FFmpegCommonTest : public testing::Test {
15 public:
16 FFmpegCommonTest() {
17 FFmpegGlue::InitializeFFmpeg();
19 ~FFmpegCommonTest() override{};
22 TEST_F(FFmpegCommonTest, OpusAudioDecoderConfig) {
23 AVCodecContext context = {0};
24 context.codec_type = AVMEDIA_TYPE_AUDIO;
25 context.codec_id = AV_CODEC_ID_OPUS;
26 context.channel_layout = CHANNEL_LAYOUT_STEREO;
27 context.channels = 2;
28 context.sample_fmt = AV_SAMPLE_FMT_FLT;
30 // During conversion this sample rate should be changed to 48kHz.
31 context.sample_rate = 44100;
33 AudioDecoderConfig decoder_config;
34 AVCodecContextToAudioDecoderConfig(&context, false, &decoder_config, false);
35 EXPECT_EQ(48000, decoder_config.samples_per_second());
38 TEST_F(FFmpegCommonTest, TimeBaseConversions) {
39 const int64 test_data[][5] = {
40 {1, 2, 1, 500000, 1 },
41 {1, 3, 1, 333333, 1 },
42 {1, 3, 2, 666667, 2 },
45 for (size_t i = 0; i < arraysize(test_data); ++i) {
46 SCOPED_TRACE(i);
48 AVRational time_base;
49 time_base.num = static_cast<int>(test_data[i][0]);
50 time_base.den = static_cast<int>(test_data[i][1]);
52 base::TimeDelta time_delta =
53 ConvertFromTimeBase(time_base, test_data[i][2]);
55 EXPECT_EQ(time_delta.InMicroseconds(), test_data[i][3]);
56 EXPECT_EQ(ConvertToTimeBase(time_base, time_delta), test_data[i][4]);
60 TEST_F(FFmpegCommonTest, VerifyFormatSizes) {
61 for (AVSampleFormat format = AV_SAMPLE_FMT_NONE;
62 format < AV_SAMPLE_FMT_NB;
63 format = static_cast<AVSampleFormat>(format + 1)) {
64 SampleFormat sample_format = AVSampleFormatToSampleFormat(format);
65 if (sample_format == kUnknownSampleFormat) {
66 // This format not supported, so skip it.
67 continue;
70 // Have FFMpeg compute the size of a buffer of 1 channel / 1 frame
71 // with 1 byte alignment to make sure the sizes match.
72 int single_buffer_size = av_samples_get_buffer_size(NULL, 1, 1, format, 1);
73 int bytes_per_channel = SampleFormatToBytesPerChannel(sample_format);
74 EXPECT_EQ(bytes_per_channel, single_buffer_size);
78 TEST_F(FFmpegCommonTest, UTCDateToTime_Valid) {
79 base::Time result;
80 EXPECT_TRUE(FFmpegUTCDateToTime("2012-11-10 12:34:56", &result));
82 base::Time::Exploded exploded;
83 result.UTCExplode(&exploded);
84 EXPECT_TRUE(exploded.HasValidValues());
85 EXPECT_EQ(2012, exploded.year);
86 EXPECT_EQ(11, exploded.month);
87 EXPECT_EQ(6, exploded.day_of_week);
88 EXPECT_EQ(10, exploded.day_of_month);
89 EXPECT_EQ(12, exploded.hour);
90 EXPECT_EQ(34, exploded.minute);
91 EXPECT_EQ(56, exploded.second);
92 EXPECT_EQ(0, exploded.millisecond);
95 TEST_F(FFmpegCommonTest, UTCDateToTime_Invalid) {
96 const char* invalid_date_strings[] = {
97 "",
98 "2012-11-10",
99 "12:34:56",
100 "-- ::",
101 "2012-11-10 12:34:",
102 "2012-11-10 12::56",
103 "2012-11-10 :34:56",
104 "2012-11- 12:34:56",
105 "2012--10 12:34:56",
106 "-11-10 12:34:56",
107 "2012-11 12:34:56",
108 "2012-11-10-12 12:34:56",
109 "2012-11-10 12:34",
110 "2012-11-10 12:34:56:78",
111 "ABCD-11-10 12:34:56",
112 "2012-EF-10 12:34:56",
113 "2012-11-GH 12:34:56",
114 "2012-11-10 IJ:34:56",
115 "2012-11-10 12:JL:56",
116 "2012-11-10 12:34:MN",
117 "2012-11-10 12:34:56.123",
118 "2012-11-1012:34:56",
119 "2012-11-10 12:34:56 UTC",
122 for (size_t i = 0; i < arraysize(invalid_date_strings); ++i) {
123 const char* date_string = invalid_date_strings[i];
124 base::Time result;
125 EXPECT_FALSE(FFmpegUTCDateToTime(date_string, &result))
126 << "date_string '" << date_string << "'";
127 EXPECT_TRUE(result.is_null());
131 } // namespace media