summaryrefslogtreecommitdiffstats
path: root/media/video/ffmpeg_video_decode_engine_unittest.cc
blob: 8a53f6df06cdd53b54d4169e1c58f9d2fd90ef1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/memory/scoped_ptr.h"
#include "base/message_loop.h"
#include "media/base/data_buffer.h"
#include "media/base/mock_ffmpeg.h"
#include "media/base/mock_task.h"
#include "media/base/pipeline.h"
#include "media/video/ffmpeg_video_decode_engine.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using ::testing::_;
using ::testing::DoAll;
using ::testing::Return;
using ::testing::ReturnNull;
using ::testing::SetArgumentPointee;
using ::testing::StrictMock;

namespace media {

static const int kWidth = 320;
static const int kHeight = 240;
static const AVRational kFrameRate = { 100, 1 };

static void InitializeFrame(uint8_t* data, int width, AVFrame* frame) {
  frame->data[0] = data;
  frame->data[1] = data;
  frame->data[2] = data;
  frame->linesize[0] = width;
  frame->linesize[1] = width / 2;
  frame->linesize[2] = width / 2;
}

ACTION_P(DecodeComplete, decoder) {
  decoder->set_video_frame(arg0);
}

ACTION_P2(DemuxComplete, engine, buffer) {
  engine->ConsumeVideoSample(buffer);
}

ACTION_P(SaveInitializeResult, engine) {
  engine->set_video_codec_info(arg0);
}

class FFmpegVideoDecodeEngineTest : public testing::Test,
                                    public VideoDecodeEngine::EventHandler {
 public:
  FFmpegVideoDecodeEngineTest()
      : config_(kCodecH264, kWidth, kHeight,
                kFrameRate.num, kFrameRate.den, NULL, 0) {

    // Setup FFmpeg structures.
    frame_buffer_.reset(new uint8[kWidth * kHeight]);
    memset(&yuv_frame_, 0, sizeof(yuv_frame_));
    InitializeFrame(frame_buffer_.get(), kWidth, &yuv_frame_);

    memset(&codec_context_, 0, sizeof(codec_context_));
    memset(&codec_, 0, sizeof(codec_));

    buffer_ = new DataBuffer(1);

    test_engine_.reset(new FFmpegVideoDecodeEngine());

    VideoFrame::CreateFrame(VideoFrame::YV12,
                            kWidth,
                            kHeight,
                            kNoTimestamp,
                            kNoTimestamp,
                            &video_frame_);
  }

  ~FFmpegVideoDecodeEngineTest() {
    test_engine_.reset();
  }

  void Initialize() {
    EXPECT_CALL(mock_ffmpeg_, AVCodecAllocContext())
        .WillOnce(Return(&codec_context_));
    EXPECT_CALL(mock_ffmpeg_, AVCodecFindDecoder(CODEC_ID_H264))
        .WillOnce(Return(&codec_));
    EXPECT_CALL(mock_ffmpeg_, AVCodecAllocFrame())
        .WillOnce(Return(&yuv_frame_));
    EXPECT_CALL(mock_ffmpeg_, AVCodecThreadInit(&codec_context_, 2))
        .WillOnce(Return(0));
    EXPECT_CALL(mock_ffmpeg_, AVCodecOpen(&codec_context_, &codec_))
        .WillOnce(Return(0));
    EXPECT_CALL(mock_ffmpeg_, AVCodecClose(&codec_context_))
        .WillOnce(Return(0));
    EXPECT_CALL(mock_ffmpeg_, AVFree(&yuv_frame_))
        .Times(1);
    EXPECT_CALL(mock_ffmpeg_, AVFree(&codec_context_))
        .Times(1);

    EXPECT_CALL(*this, OnInitializeComplete(_))
       .WillOnce(SaveInitializeResult(this));
    test_engine_->Initialize(MessageLoop::current(), this, NULL, config_);
    EXPECT_TRUE(info_.success);
  }

  void Decode() {
    EXPECT_CALL(mock_ffmpeg_, AVInitPacket(_));
    EXPECT_CALL(mock_ffmpeg_,
                AVCodecDecodeVideo2(&codec_context_, &yuv_frame_, _, _))
        .WillOnce(DoAll(SetArgumentPointee<2>(1),  // Simulate 1 byte frame.
                        Return(0)));

    EXPECT_CALL(*this, ProduceVideoSample(_))
        .WillOnce(DemuxComplete(test_engine_.get(), buffer_));
    EXPECT_CALL(*this, ConsumeVideoFrame(_, _))
        .WillOnce(DecodeComplete(this));
    test_engine_->ProduceVideoFrame(video_frame_);
  }

  void ChangeDimensions(int width, int height) {
    frame_buffer_.reset(new uint8[width * height]);
    InitializeFrame(frame_buffer_.get(), width, &yuv_frame_);
    codec_context_.width = width;
    codec_context_.height = height;
  }

  // VideoDecodeEngine::EventHandler implementation.
  MOCK_METHOD2(ConsumeVideoFrame,
               void(scoped_refptr<VideoFrame> video_frame,
                    const PipelineStatistics& statistics));
  MOCK_METHOD1(ProduceVideoSample,
               void(scoped_refptr<Buffer> buffer));
  MOCK_METHOD1(OnInitializeComplete,
               void(const VideoCodecInfo& info));
  MOCK_METHOD0(OnUninitializeComplete, void());
  MOCK_METHOD0(OnFlushComplete, void());
  MOCK_METHOD0(OnSeekComplete, void());
  MOCK_METHOD0(OnError, void());
  MOCK_METHOD1(OnFormatChange, void(VideoStreamInfo stream_info));

  // Used by gmock actions.
  void set_video_frame(scoped_refptr<VideoFrame> video_frame) {
    video_frame_ = video_frame;
  }

  void set_video_codec_info(const VideoCodecInfo& info) {
    info_ = info;
  }

 protected:
  VideoDecoderConfig config_;
  VideoCodecInfo info_;
  scoped_refptr<VideoFrame> video_frame_;
  scoped_ptr<FFmpegVideoDecodeEngine> test_engine_;
  scoped_array<uint8_t> frame_buffer_;
  StrictMock<MockFFmpeg> mock_ffmpeg_;

  AVFrame yuv_frame_;
  AVCodecContext codec_context_;
  AVCodec codec_;
  scoped_refptr<DataBuffer> buffer_;

 private:
  DISALLOW_COPY_AND_ASSIGN(FFmpegVideoDecodeEngineTest);
};

TEST_F(FFmpegVideoDecodeEngineTest, Initialize_Normal) {
  Initialize();
}

TEST_F(FFmpegVideoDecodeEngineTest, Initialize_FindDecoderFails) {
  // Test avcodec_find_decoder() returning NULL.
  EXPECT_CALL(mock_ffmpeg_, AVCodecAllocContext())
      .WillOnce(Return(&codec_context_));
  EXPECT_CALL(mock_ffmpeg_, AVCodecFindDecoder(CODEC_ID_H264))
      .WillOnce(ReturnNull());
  EXPECT_CALL(mock_ffmpeg_, AVCodecAllocFrame())
      .WillOnce(Return(&yuv_frame_));
  EXPECT_CALL(mock_ffmpeg_, AVCodecClose(&codec_context_))
      .WillOnce(Return(0));
  EXPECT_CALL(mock_ffmpeg_, AVFree(&yuv_frame_))
      .Times(1);
  EXPECT_CALL(mock_ffmpeg_, AVFree(&codec_context_))
      .Times(1);

  EXPECT_CALL(*this, OnInitializeComplete(_))
     .WillOnce(SaveInitializeResult(this));
  test_engine_->Initialize(MessageLoop::current(), this, NULL, config_);
  EXPECT_FALSE(info_.success);
}

// Note There are 2 threads for FFmpeg-mt.
TEST_F(FFmpegVideoDecodeEngineTest, Initialize_InitThreadFails) {
  // Test avcodec_thread_init() failing.
  EXPECT_CALL(mock_ffmpeg_, AVCodecAllocContext())
      .WillOnce(Return(&codec_context_));
  EXPECT_CALL(mock_ffmpeg_, AVCodecFindDecoder(CODEC_ID_H264))
      .WillOnce(Return(&codec_));
  EXPECT_CALL(mock_ffmpeg_, AVCodecAllocFrame())
      .WillOnce(Return(&yuv_frame_));
  EXPECT_CALL(mock_ffmpeg_, AVCodecThreadInit(&codec_context_, 2))
      .WillOnce(Return(-1));
  EXPECT_CALL(mock_ffmpeg_, AVCodecClose(&codec_context_))
      .WillOnce(Return(0));
  EXPECT_CALL(mock_ffmpeg_, AVFree(&yuv_frame_))
      .Times(1);
  EXPECT_CALL(mock_ffmpeg_, AVFree(&codec_context_))
      .Times(1);

  EXPECT_CALL(*this, OnInitializeComplete(_))
     .WillOnce(SaveInitializeResult(this));
  test_engine_->Initialize(MessageLoop::current(), this, NULL, config_);
  EXPECT_FALSE(info_.success);
}

TEST_F(FFmpegVideoDecodeEngineTest, Initialize_OpenDecoderFails) {
  // Test avcodec_open() failing.
  EXPECT_CALL(mock_ffmpeg_, AVCodecAllocContext())
      .WillOnce(Return(&codec_context_));
  EXPECT_CALL(mock_ffmpeg_, AVCodecFindDecoder(CODEC_ID_H264))
      .WillOnce(Return(&codec_));
  EXPECT_CALL(mock_ffmpeg_, AVCodecAllocFrame())
      .WillOnce(Return(&yuv_frame_));
  EXPECT_CALL(mock_ffmpeg_, AVCodecThreadInit(&codec_context_, 2))
      .WillOnce(Return(0));
  EXPECT_CALL(mock_ffmpeg_, AVCodecOpen(&codec_context_, &codec_))
      .WillOnce(Return(-1));
  EXPECT_CALL(mock_ffmpeg_, AVCodecClose(&codec_context_))
      .WillOnce(Return(0));
  EXPECT_CALL(mock_ffmpeg_, AVFree(&yuv_frame_))
      .Times(1);
  EXPECT_CALL(mock_ffmpeg_, AVFree(&codec_context_))
      .Times(1);

  EXPECT_CALL(*this, OnInitializeComplete(_))
     .WillOnce(SaveInitializeResult(this));
  test_engine_->Initialize(MessageLoop::current(), this, NULL, config_);
  EXPECT_FALSE(info_.success);
}

TEST_F(FFmpegVideoDecodeEngineTest, DecodeFrame_Normal) {
  Initialize();

  // We rely on FFmpeg for timestamp and duration reporting.  The one tricky
  // bit is calculating the duration when |repeat_pict| > 0.
  const base::TimeDelta kTimestamp = base::TimeDelta::FromMicroseconds(123);
  const base::TimeDelta kDuration = base::TimeDelta::FromMicroseconds(15000);
  yuv_frame_.repeat_pict = 1;
  yuv_frame_.reordered_opaque = kTimestamp.InMicroseconds();

  // Simulate decoding a single frame.
  Decode();

  // |video_frame_| timestamp is 0 because we set the timestamp based off
  // the buffer timestamp.
  EXPECT_EQ(0, video_frame_->GetTimestamp().ToInternalValue());
  EXPECT_EQ(kDuration.ToInternalValue(),
            video_frame_->GetDuration().ToInternalValue());
}

TEST_F(FFmpegVideoDecodeEngineTest, DecodeFrame_0ByteFrame) {
  Initialize();

  // Expect a bunch of avcodec calls.
  EXPECT_CALL(mock_ffmpeg_, AVInitPacket(_))
      .Times(2);
  EXPECT_CALL(mock_ffmpeg_,
              AVCodecDecodeVideo2(&codec_context_, &yuv_frame_, _, _))
      .WillOnce(DoAll(SetArgumentPointee<2>(0),  // Simulate 0 byte frame.
                      Return(0)))
      .WillOnce(DoAll(SetArgumentPointee<2>(1),  // Simulate 1 byte frame.
                      Return(0)));

  EXPECT_CALL(*this, ProduceVideoSample(_))
      .WillOnce(DemuxComplete(test_engine_.get(), buffer_))
      .WillOnce(DemuxComplete(test_engine_.get(), buffer_));
  EXPECT_CALL(*this, ConsumeVideoFrame(_, _))
      .WillOnce(DecodeComplete(this));
  test_engine_->ProduceVideoFrame(video_frame_);

  EXPECT_TRUE(video_frame_.get());
}

TEST_F(FFmpegVideoDecodeEngineTest, DecodeFrame_DecodeError) {
  Initialize();

  // Expect a bunch of avcodec calls.
  EXPECT_CALL(mock_ffmpeg_, AVInitPacket(_));
  EXPECT_CALL(mock_ffmpeg_,
              AVCodecDecodeVideo2(&codec_context_, &yuv_frame_, _, _))
      .WillOnce(Return(-1));

  EXPECT_CALL(*this, ProduceVideoSample(_))
      .WillOnce(DemuxComplete(test_engine_.get(), buffer_));
  EXPECT_CALL(*this, OnError());

  test_engine_->ProduceVideoFrame(video_frame_);
}

TEST_F(FFmpegVideoDecodeEngineTest, DecodeFrame_LargerWidth) {
  Initialize();
  ChangeDimensions(kWidth * 2, kHeight);
  Decode();
}

TEST_F(FFmpegVideoDecodeEngineTest, DecodeFrame_SmallerWidth) {
  Initialize();
  ChangeDimensions(kWidth / 2, kHeight);
  Decode();
}

TEST_F(FFmpegVideoDecodeEngineTest, DecodeFrame_LargerHeight) {
  Initialize();
  ChangeDimensions(kWidth, kHeight * 2);
  Decode();
}

TEST_F(FFmpegVideoDecodeEngineTest, DecodeFrame_SmallerHeight) {
  Initialize();
  ChangeDimensions(kWidth, kHeight / 2);
  Decode();
}

TEST_F(FFmpegVideoDecodeEngineTest, GetSurfaceFormat) {
  Initialize();

  // YV12 formats.
  codec_context_.pix_fmt = PIX_FMT_YUV420P;
  EXPECT_EQ(VideoFrame::YV12, test_engine_->GetSurfaceFormat());
  codec_context_.pix_fmt = PIX_FMT_YUVJ420P;
  EXPECT_EQ(VideoFrame::YV12, test_engine_->GetSurfaceFormat());

  // YV16 formats.
  codec_context_.pix_fmt = PIX_FMT_YUV422P;
  EXPECT_EQ(VideoFrame::YV16, test_engine_->GetSurfaceFormat());
  codec_context_.pix_fmt = PIX_FMT_YUVJ422P;
  EXPECT_EQ(VideoFrame::YV16, test_engine_->GetSurfaceFormat());

  // Invalid value.
  codec_context_.pix_fmt = PIX_FMT_NONE;
  EXPECT_EQ(VideoFrame::INVALID, test_engine_->GetSurfaceFormat());
}

}  // namespace media