1ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang/*
2ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang *  Copyright (c) 2012 The WebM project authors. All Rights Reserved.
3ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang *
4ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang *  Use of this source code is governed by a BSD-style license
5ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang *  that can be found in the LICENSE file in the root of the source
6ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang *  tree. An additional intellectual property rights grant can be found
7ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang *  in the file PATENTS.  All contributing project authors may
8ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang *  be found in the AUTHORS file in the root of the source tree.
9ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang */
10ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#ifndef TEST_WEBM_VIDEO_SOURCE_H_
11ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#define TEST_WEBM_VIDEO_SOURCE_H_
12ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#include <cstdarg>
13ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#include <cstdio>
14ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#include <cstdlib>
15ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#include <new>
16ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#include <string>
17ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian#include "../tools_common.h"
18ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian#include "../webmdec.h"
19ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#include "test/video_source.h"
20ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
21ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuangnamespace libvpx_test {
22ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
23ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang// This class extends VideoSource to allow parsing of WebM files,
24ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang// so that we can do actual file decodes.
25ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuangclass WebMVideoSource : public CompressedVideoSource {
26ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang public:
27ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  explicit WebMVideoSource(const std::string &file_name)
28ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang      : file_name_(file_name),
29ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian        vpx_ctx_(new VpxInputContext()),
30ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian        webm_ctx_(new WebmInputContext()),
31ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang        buf_(NULL),
32ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang        buf_sz_(0),
33ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang        frame_(0),
34ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang        end_of_file_(false) {
35ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  }
36ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
37ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  virtual ~WebMVideoSource() {
38ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian    if (vpx_ctx_->file != NULL)
39ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian      fclose(vpx_ctx_->file);
40ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian    webm_free(webm_ctx_);
41ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian    delete vpx_ctx_;
42ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian    delete webm_ctx_;
43ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  }
44ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
45ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  virtual void Init() {
46ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  }
47ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
48ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  virtual void Begin() {
49ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian    vpx_ctx_->file = OpenTestDataFile(file_name_);
50ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian    ASSERT_TRUE(vpx_ctx_->file != NULL) << "Input file open failed. Filename: "
51ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang        << file_name_;
52ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
53ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian    ASSERT_EQ(file_is_webm(webm_ctx_, vpx_ctx_), 1) << "file is not WebM";
54ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
55ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang    FillFrame();
56ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  }
57ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
58ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  virtual void Next() {
59ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang    ++frame_;
60ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang    FillFrame();
61ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  }
62ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
63ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  void FillFrame() {
64ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian    ASSERT_TRUE(vpx_ctx_->file != NULL);
65ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian    const int status = webm_read_frame(webm_ctx_, &buf_, &buf_sz_, &buf_sz_);
66ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian    ASSERT_GE(status, 0) << "webm_read_frame failed";
67ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian    if (status == 1) {
68ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian      end_of_file_ = true;
69ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang    }
70ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  }
71ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
72ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  virtual const uint8_t *cxdata() const {
73ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang    return end_of_file_ ? NULL : buf_;
74ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  }
752ec72e65689c948e92b826ae1e867bf369e72f13Vignesh Venkatasubramanian  virtual size_t frame_size() const { return buf_sz_; }
762ec72e65689c948e92b826ae1e867bf369e72f13Vignesh Venkatasubramanian  virtual unsigned int frame_number() const { return frame_; }
77ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
78ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang protected:
79ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  std::string file_name_;
80ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian  VpxInputContext *vpx_ctx_;
81ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian  WebmInputContext *webm_ctx_;
82ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  uint8_t *buf_;
83ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  size_t buf_sz_;
84ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  unsigned int frame_;
85ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  bool end_of_file_;
86ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang};
87ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
88ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang}  // namespace libvpx_test
89ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
90ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#endif  // TEST_WEBM_VIDEO_SOURCE_H_
91