1 // Copyright (C) 2020 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #include "host-common/MediaSnapshotState.h"
18 #include "host-common/MediaVideoHelper.h"
19 
20 #include <cstdint>
21 #include <list>
22 #include <memory>
23 #include <mutex>
24 #include <string>
25 #include <vector>
26 
27 extern "C" {
28 #include "vpx/vp8dx.h"
29 #include "vpx/vpx_codec.h"
30 #include "vpx/vpx_decoder.h"
31 #include "vpx/vpx_image.h"
32 }
33 
34 #include <stdio.h>
35 #include <string.h>
36 
37 #include <stddef.h>
38 
39 namespace android {
40 namespace emulation {
41 
42 class MediaVpxVideoHelper : public MediaVideoHelper {
43 public:
44     MediaVpxVideoHelper(int type, int threads);
45     ~MediaVpxVideoHelper() override;
46 
47     // return true if success; false otherwise
48     bool init() override;
49     void decode(const uint8_t* frame,
50                 size_t szBytes,
51                 uint64_t inputPts) override;
52     void flush() override;
53     void deInit() override;
54 
55 private:
56     int mType = 0;
57     int mThreadCount = 1;
58 
59     // vpx stuff
60     std::unique_ptr<vpx_codec_ctx_t> mCtx;
61 
62     // Owned by the vpx context. Needs to be initialized to nullptr on the first
63     // vpx_codec_get_frame() call.
64     vpx_codec_iter_t mIter = nullptr;
65 
66     // Owned by the vpx context. mImg is set when calling vpx_codec_get_frame().
67     // mImg remains valid until the next vpx_codec_decode() is called.
68     vpx_image_t* mImg = nullptr;
69 
70     void fetchAllFrames();
71     // helper methods
72     void copyImgToGuest(vpx_image_t* mImg, std::vector<uint8_t>& byteBuffer);
73     void copyYV12FrameToOutputBuffer(size_t outputBufferWidth,
74                                      size_t outputBufferHeight,
75                                      size_t imgWidth,
76                                      size_t imgHeight,
77                                      int32_t bpp,
78                                      uint8_t* dst,
79                                      const uint8_t* srcY,
80                                      const uint8_t* srcU,
81                                      const uint8_t* srcV,
82                                      size_t srcYStride,
83                                      size_t srcUStride,
84                                      size_t srcVStride);
85 
86 };  // MediaVpxVideoHelper
87 
88 }  // namespace emulation
89 }  // namespace android
90