Home | History | Annotate | Download | only in test
      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 VPX_TEST_WEBM_VIDEO_SOURCE_H_
     11 #define VPX_TEST_WEBM_VIDEO_SOURCE_H_
     12 #include <cstdarg>
     13 #include <cstdio>
     14 #include <cstdlib>
     15 #include <new>
     16 #include <string>
     17 #include "../tools_common.h"
     18 #include "../webmdec.h"
     19 #include "test/video_source.h"
     20 
     21 namespace libvpx_test {
     22 
     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), vpx_ctx_(new VpxInputContext()),
     29         webm_ctx_(new WebmInputContext()), buf_(NULL), buf_sz_(0), frame_(0),
     30         end_of_file_(false) {}
     31 
     32   virtual ~WebMVideoSource() {
     33     if (vpx_ctx_->file != NULL) fclose(vpx_ctx_->file);
     34     webm_free(webm_ctx_);
     35     delete vpx_ctx_;
     36     delete webm_ctx_;
     37   }
     38 
     39   virtual void Init() {}
     40 
     41   virtual void Begin() {
     42     vpx_ctx_->file = OpenTestDataFile(file_name_);
     43     ASSERT_TRUE(vpx_ctx_->file != NULL)
     44         << "Input file open failed. Filename: " << file_name_;
     45 
     46     ASSERT_EQ(file_is_webm(webm_ctx_, vpx_ctx_), 1) << "file is not WebM";
     47 
     48     FillFrame();
     49   }
     50 
     51   virtual void Next() {
     52     ++frame_;
     53     FillFrame();
     54   }
     55 
     56   void FillFrame() {
     57     ASSERT_TRUE(vpx_ctx_->file != NULL);
     58     const int status = webm_read_frame(webm_ctx_, &buf_, &buf_sz_);
     59     ASSERT_GE(status, 0) << "webm_read_frame failed";
     60     if (status == 1) {
     61       end_of_file_ = true;
     62     }
     63   }
     64 
     65   void SeekToNextKeyFrame() {
     66     ASSERT_TRUE(vpx_ctx_->file != NULL);
     67     do {
     68       const int status = webm_read_frame(webm_ctx_, &buf_, &buf_sz_);
     69       ASSERT_GE(status, 0) << "webm_read_frame failed";
     70       ++frame_;
     71       if (status == 1) {
     72         end_of_file_ = true;
     73       }
     74     } while (!webm_ctx_->is_key_frame && !end_of_file_);
     75   }
     76 
     77   virtual const uint8_t *cxdata() const { return end_of_file_ ? NULL : buf_; }
     78   virtual size_t frame_size() const { return buf_sz_; }
     79   virtual unsigned int frame_number() const { return frame_; }
     80 
     81  protected:
     82   std::string file_name_;
     83   VpxInputContext *vpx_ctx_;
     84   WebmInputContext *webm_ctx_;
     85   uint8_t *buf_;
     86   size_t buf_sz_;
     87   unsigned int frame_;
     88   bool end_of_file_;
     89 };
     90 
     91 }  // namespace libvpx_test
     92 
     93 #endif  // VPX_TEST_WEBM_VIDEO_SOURCE_H_
     94