blob: 286f69cbf1c081919ab19c520c11551448b3a39e [file] [log] [blame]
John Koleszar119c9812013-06-13 12:42:05 -07001/*
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
John Koleszar119c9812013-06-13 12:42:05 -07003 *
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07004 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
John Koleszar119c9812013-06-13 12:42:05 -070010 */
11#ifndef TEST_WEBM_VIDEO_SOURCE_H_
12#define TEST_WEBM_VIDEO_SOURCE_H_
13#include <cstdarg>
14#include <cstdio>
15#include <cstdlib>
16#include <new>
17#include <string>
Vignesh Venkatasubramanian4fd63172014-04-14 14:19:50 -070018#include "../tools_common.h"
19#include "../webmdec.h"
John Koleszar119c9812013-06-13 12:42:05 -070020#include "test/video_source.h"
21
Yaowu Xuc27fc142016-08-22 16:08:15 -070022namespace libaom_test {
John Koleszar119c9812013-06-13 12:42:05 -070023
John Koleszar119c9812013-06-13 12:42:05 -070024// This class extends VideoSource to allow parsing of WebM files,
25// so that we can do actual file decodes.
26class WebMVideoSource : public CompressedVideoSource {
27 public:
28 explicit WebMVideoSource(const std::string &file_name)
Yaowu Xuf883b422016-08-30 14:01:10 -070029 : file_name_(file_name), aom_ctx_(new AvxInputContext()),
clang-format3a826f12016-08-11 17:46:05 -070030 webm_ctx_(new WebmInputContext()), buf_(NULL), buf_sz_(0), frame_(0),
31 end_of_file_(false) {}
John Koleszar119c9812013-06-13 12:42:05 -070032
33 virtual ~WebMVideoSource() {
Yaowu Xuf883b422016-08-30 14:01:10 -070034 if (aom_ctx_->file != NULL) fclose(aom_ctx_->file);
James Zern96f88952014-04-19 09:29:26 -070035 webm_free(webm_ctx_);
Yaowu Xuf883b422016-08-30 14:01:10 -070036 delete aom_ctx_;
Vignesh Venkatasubramanian4fd63172014-04-14 14:19:50 -070037 delete webm_ctx_;
John Koleszar119c9812013-06-13 12:42:05 -070038 }
39
clang-format3a826f12016-08-11 17:46:05 -070040 virtual void Init() {}
John Koleszar119c9812013-06-13 12:42:05 -070041
42 virtual void Begin() {
Yaowu Xuf883b422016-08-30 14:01:10 -070043 aom_ctx_->file = OpenTestDataFile(file_name_);
44 ASSERT_TRUE(aom_ctx_->file != NULL) << "Input file open failed. Filename: "
clang-format3a826f12016-08-11 17:46:05 -070045 << file_name_;
John Koleszar119c9812013-06-13 12:42:05 -070046
Yaowu Xuf883b422016-08-30 14:01:10 -070047 ASSERT_EQ(file_is_webm(webm_ctx_, aom_ctx_), 1) << "file is not WebM";
John Koleszar119c9812013-06-13 12:42:05 -070048
49 FillFrame();
50 }
51
52 virtual void Next() {
53 ++frame_;
54 FillFrame();
55 }
56
57 void FillFrame() {
Yaowu Xuf883b422016-08-30 14:01:10 -070058 ASSERT_TRUE(aom_ctx_->file != NULL);
Vignesh Venkatasubramanianfa99c372016-04-25 13:46:42 -070059 const int status = webm_read_frame(webm_ctx_, &buf_, &buf_sz_);
Vignesh Venkatasubramanian4fd63172014-04-14 14:19:50 -070060 ASSERT_GE(status, 0) << "webm_read_frame failed";
61 if (status == 1) {
62 end_of_file_ = true;
John Koleszar119c9812013-06-13 12:42:05 -070063 }
John Koleszar119c9812013-06-13 12:42:05 -070064 }
65
hkuangbe6aead2015-01-27 12:26:28 -080066 void SeekToNextKeyFrame() {
Yaowu Xuf883b422016-08-30 14:01:10 -070067 ASSERT_TRUE(aom_ctx_->file != NULL);
hkuangbe6aead2015-01-27 12:26:28 -080068 do {
Vignesh Venkatasubramanianfa99c372016-04-25 13:46:42 -070069 const int status = webm_read_frame(webm_ctx_, &buf_, &buf_sz_);
hkuangbe6aead2015-01-27 12:26:28 -080070 ASSERT_GE(status, 0) << "webm_read_frame failed";
71 ++frame_;
72 if (status == 1) {
73 end_of_file_ = true;
74 }
75 } while (!webm_ctx_->is_key_frame && !end_of_file_);
76 }
77
clang-format3a826f12016-08-11 17:46:05 -070078 virtual const uint8_t *cxdata() const { return end_of_file_ ? NULL : buf_; }
Tom Fineganeb2325e2014-02-19 14:17:55 -080079 virtual size_t frame_size() const { return buf_sz_; }
80 virtual unsigned int frame_number() const { return frame_; }
John Koleszar119c9812013-06-13 12:42:05 -070081
82 protected:
83 std::string file_name_;
Yaowu Xuf883b422016-08-30 14:01:10 -070084 AvxInputContext *aom_ctx_;
Vignesh Venkatasubramanian4fd63172014-04-14 14:19:50 -070085 WebmInputContext *webm_ctx_;
John Koleszar119c9812013-06-13 12:42:05 -070086 uint8_t *buf_;
87 size_t buf_sz_;
88 unsigned int frame_;
89 bool end_of_file_;
90};
91
Yaowu Xuc27fc142016-08-22 16:08:15 -070092} // namespace libaom_test
John Koleszar119c9812013-06-13 12:42:05 -070093
94#endif // TEST_WEBM_VIDEO_SOURCE_H_