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