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