Home | History | Annotate | Download | only in ffmpeg
      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/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"
     11 
     12 using base::TimeDelta;
     13 
     14 namespace media {
     15 
     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 },
     26 };
     27 
     28 static const AVRational kTimeBase = { 1, 1000 };
     29 
     30 class FFmpegCommonTest : public testing::Test {
     31  public:
     32   FFmpegCommonTest();
     33   virtual ~FFmpegCommonTest();
     34 
     35  protected:
     36   AVStream stream_;
     37 
     38   DISALLOW_COPY_AND_ASSIGN(FFmpegCommonTest);
     39 };
     40 
     41 static bool InitFFmpeg() {
     42   static bool initialized = false;
     43   if (initialized) {
     44     return true;
     45   }
     46   base::FilePath path;
     47   PathService::Get(base::DIR_MODULE, &path);
     48   return media::InitializeMediaLibrary(path);
     49 }
     50 
     51 FFmpegCommonTest::FFmpegCommonTest() {
     52   CHECK(InitFFmpeg());
     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);
     57 }
     58 
     59 FFmpegCommonTest::~FFmpegCommonTest() {}
     60 
     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 },
     66   };
     67 
     68   for (size_t i = 0; i < arraysize(test_data); ++i) {
     69     SCOPED_TRACE(i);
     70 
     71     AVRational time_base;
     72     time_base.num = static_cast<int>(test_data[i][0]);
     73     time_base.den = static_cast<int>(test_data[i][1]);
     74 
     75     TimeDelta time_delta = ConvertFromTimeBase(time_base, test_data[i][2]);
     76 
     77     EXPECT_EQ(time_delta.InMicroseconds(), test_data[i][3]);
     78     EXPECT_EQ(ConvertToTimeBase(time_base, time_delta), test_data[i][4]);
     79   }
     80 }
     81 
     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.
     89       continue;
     90     }
     91 
     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);
     97   }
     98 }
     99 
    100 TEST_F(FFmpegCommonTest, UTCDateToTime_Valid) {
    101   base::Time result;
    102   EXPECT_TRUE(FFmpegUTCDateToTime("2012-11-10 12:34:56", &result));
    103 
    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);
    115 }
    116 
    117 TEST_F(FFmpegCommonTest, UTCDateToTime_Invalid) {
    118   const char* invalid_date_strings[] = {
    119     "",
    120     "2012-11-10",
    121     "12:34:56",
    122     "-- ::",
    123     "2012-11-10 12:34:",
    124     "2012-11-10 12::56",
    125     "2012-11-10 :34:56",
    126     "2012-11- 12:34:56",
    127     "2012--10 12:34:56",
    128     "-11-10 12:34:56",
    129     "2012-11 12:34:56",
    130     "2012-11-10-12 12:34:56",
    131     "2012-11-10 12:34",
    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",
    142   };
    143 
    144   for (size_t i = 0; i < arraysize(invalid_date_strings); ++i) {
    145     const char* date_string = invalid_date_strings[i];
    146     base::Time result;
    147     EXPECT_FALSE(FFmpegUTCDateToTime(date_string, &result))
    148         << "date_string '" << date_string << "'";
    149     EXPECT_TRUE(result.is_null());
    150   }
    151 }
    152 
    153 
    154 }  // namespace media
    155