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
12using base::TimeDelta;
13
14namespace media {
15
16static 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
28static const AVRational kTimeBase = { 1, 1000 };
29
30class 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
41static 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
51FFmpegCommonTest::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
59FFmpegCommonTest::~FFmpegCommonTest() {}
60
61TEST_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
82TEST_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}  // namespace media
101