xref: /aosp_15_r20/external/libaom/test/webm_video_source.h (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1 /*
2  * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3  *
4  * 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.
10  */
11 #ifndef AOM_TEST_WEBM_VIDEO_SOURCE_H_
12 #define AOM_TEST_WEBM_VIDEO_SOURCE_H_
13 #include <cstdarg>
14 #include <cstdio>
15 #include <cstdlib>
16 #include <new>
17 #include <string>
18 #include "common/tools_common.h"
19 #include "common/webmdec.h"
20 #include "test/video_source.h"
21 
22 namespace libaom_test {
23 
24 // This class extends VideoSource to allow parsing of WebM files,
25 // so that we can do actual file decodes.
26 class WebMVideoSource : public CompressedVideoSource {
27  public:
WebMVideoSource(const std::string & file_name)28   explicit WebMVideoSource(const std::string &file_name)
29       : file_name_(file_name), aom_ctx_(new AvxInputContext()),
30         webm_ctx_(new WebmInputContext()), buf_(nullptr), buf_sz_(0),
31         frame_sz_(0), frame_number_(0), end_of_file_(false) {}
32 
~WebMVideoSource()33   ~WebMVideoSource() override {
34     if (aom_ctx_->file != nullptr) fclose(aom_ctx_->file);
35     webm_free(webm_ctx_);
36     delete aom_ctx_;
37     delete webm_ctx_;
38   }
39 
Init()40   void Init() override {
41     ASSERT_NE(aom_ctx_, nullptr);
42     ASSERT_NE(webm_ctx_, nullptr);
43   }
44 
Begin()45   void Begin() override {
46     ASSERT_NE(aom_ctx_, nullptr);
47     ASSERT_NE(webm_ctx_, nullptr);
48     aom_ctx_->file = OpenTestDataFile(file_name_);
49     ASSERT_NE(aom_ctx_->file, nullptr)
50         << "Input file open failed. Filename: " << file_name_;
51 
52     ASSERT_EQ(file_is_webm(webm_ctx_, aom_ctx_), 1) << "file is not WebM";
53 
54     FillFrame();
55   }
56 
Next()57   void Next() override {
58     ++frame_number_;
59     FillFrame();
60   }
61 
FillFrame()62   void FillFrame() {
63     ASSERT_NE(aom_ctx_, nullptr);
64     ASSERT_NE(webm_ctx_, nullptr);
65     ASSERT_NE(aom_ctx_->file, nullptr);
66     const int status = webm_read_frame(webm_ctx_, &buf_, &frame_sz_, &buf_sz_);
67     ASSERT_GE(status, 0) << "webm_read_frame failed";
68     if (status == 1) {
69       end_of_file_ = true;
70     }
71   }
72 
SeekToNextKeyFrame()73   void SeekToNextKeyFrame() {
74     ASSERT_NE(aom_ctx_, nullptr);
75     ASSERT_NE(webm_ctx_, nullptr);
76     ASSERT_NE(aom_ctx_->file, nullptr);
77     do {
78       const int status =
79           webm_read_frame(webm_ctx_, &buf_, &frame_sz_, &buf_sz_);
80       ASSERT_GE(status, 0) << "webm_read_frame failed";
81       ++frame_number_;
82       if (status == 1) {
83         end_of_file_ = true;
84       }
85     } while (!webm_ctx_->is_key_frame && !end_of_file_);
86   }
87 
cxdata()88   const uint8_t *cxdata() const override {
89     return end_of_file_ ? nullptr : buf_;
90   }
frame_size()91   size_t frame_size() const override { return frame_sz_; }
frame_number()92   unsigned int frame_number() const override { return frame_number_; }
93 
94  protected:
95   std::string file_name_;
96   AvxInputContext *aom_ctx_;
97   WebmInputContext *webm_ctx_;
98   uint8_t *buf_;  // Owned by webm_ctx_ and freed when webm_ctx_ is freed.
99   size_t buf_sz_;
100   size_t frame_sz_;
101   unsigned int frame_number_;
102   bool end_of_file_;
103 };
104 
105 }  // namespace libaom_test
106 
107 #endif  // AOM_TEST_WEBM_VIDEO_SOURCE_H_
108