webm_video_source.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. namespace libvpx_test {
  21. // This class extends VideoSource to allow parsing of WebM files,
  22. // so that we can do actual file decodes.
  23. class WebMVideoSource : public CompressedVideoSource {
  24. public:
  25. explicit WebMVideoSource(const std::string &file_name)
  26. : file_name_(file_name), vpx_ctx_(new VpxInputContext()),
  27. webm_ctx_(new WebmInputContext()), buf_(NULL), buf_sz_(0), frame_(0),
  28. end_of_file_(false) {}
  29. virtual ~WebMVideoSource() {
  30. if (vpx_ctx_->file != NULL) fclose(vpx_ctx_->file);
  31. webm_free(webm_ctx_);
  32. delete vpx_ctx_;
  33. delete webm_ctx_;
  34. }
  35. virtual void Init() {}
  36. virtual void Begin() {
  37. vpx_ctx_->file = OpenTestDataFile(file_name_);
  38. ASSERT_TRUE(vpx_ctx_->file != NULL)
  39. << "Input file open failed. Filename: " << file_name_;
  40. ASSERT_EQ(file_is_webm(webm_ctx_, vpx_ctx_), 1) << "file is not WebM";
  41. FillFrame();
  42. }
  43. virtual void Next() {
  44. ++frame_;
  45. FillFrame();
  46. }
  47. void FillFrame() {
  48. ASSERT_TRUE(vpx_ctx_->file != NULL);
  49. const int status = webm_read_frame(webm_ctx_, &buf_, &buf_sz_);
  50. ASSERT_GE(status, 0) << "webm_read_frame failed";
  51. if (status == 1) {
  52. end_of_file_ = true;
  53. }
  54. }
  55. void SeekToNextKeyFrame() {
  56. ASSERT_TRUE(vpx_ctx_->file != NULL);
  57. do {
  58. const int status = webm_read_frame(webm_ctx_, &buf_, &buf_sz_);
  59. ASSERT_GE(status, 0) << "webm_read_frame failed";
  60. ++frame_;
  61. if (status == 1) {
  62. end_of_file_ = true;
  63. }
  64. } while (!webm_ctx_->is_key_frame && !end_of_file_);
  65. }
  66. virtual const uint8_t *cxdata() const { return end_of_file_ ? NULL : buf_; }
  67. virtual size_t frame_size() const { return buf_sz_; }
  68. virtual unsigned int frame_number() const { return frame_; }
  69. protected:
  70. std::string file_name_;
  71. VpxInputContext *vpx_ctx_;
  72. WebmInputContext *webm_ctx_;
  73. uint8_t *buf_;
  74. size_t buf_sz_;
  75. unsigned int frame_;
  76. bool end_of_file_;
  77. };
  78. } // namespace libvpx_test
  79. #endif // VPX_TEST_WEBM_VIDEO_SOURCE_H_