1 // Copyright (C) 2019 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/GoldfishMediaDefs.h"
18 #include "host-common/MediaHostRenderer.h"
19 #include "host-common/MediaSnapshotHelper.h"
20 #include "host-common/MediaSnapshotState.h"
21 #include "host-common/MediaVideoHelper.h"
22 #include "host-common/MediaVpxDecoderPlugin.h"
23 #include "host-common/VpxPingInfoParser.h"
24 
25 #include <stddef.h>
26 #include <stdint.h>
27 #include <vector>
28 
29 extern "C" {
30 #include "vpx/vp8dx.h"
31 #include "vpx/vpx_codec.h"
32 #include "vpx/vpx_decoder.h"
33 #include "vpx/vpx_image.h"
34 }
35 
36 namespace android {
37 namespace emulation {
38 
39 class MediaVpxDecoderGeneric : public MediaVpxDecoderPlugin {
40 public:
41     virtual void initVpxContext(void* ptr) override;
42     virtual void decodeFrame(void* ptr) override;
43     virtual void getImage(void* ptr) override;
44     virtual void flush(void* ptr) override;
45     virtual void destroyVpxContext(void* ptr) override;
46     virtual void sendMetadata(void* ptr) override;
47 
48     explicit MediaVpxDecoderGeneric(VpxPingInfoParser parser,
49                                     MediaCodecType type);
50     virtual ~MediaVpxDecoderGeneric();
51 
52     virtual void save(base::Stream* stream) const override;
53     bool load(base::Stream* stream) override;
54 
55     friend MediaSnapshotHelper;
56 
57 protected:
58     // this is required by save/load
type()59     virtual int type() const override { return PLUGIN_TYPE_GENERIC; }
vpxtype()60     virtual int vpxtype() const override {
61         return mType == MediaCodecType::VP8Codec ? 8 : 9;
62     }
version()63     virtual int version() const override { return mParser.version(); }
64 
65 private:
66     MediaCodecType mType;  // vp8 or vp9
67     VpxPingInfoParser mParser;
68     MediaHostRenderer mRenderer;
69 
70     int mNumFramesDecoded = 0;
71 
72     bool mUseGpuTexture = false;
73     bool mTrialPeriod = true;
74     // at any point of time, only one of the following is valid
75     std::unique_ptr<MediaVideoHelper> mHwVideoHelper;
76     std::unique_ptr<MediaVideoHelper> mSwVideoHelper;
77     std::unique_ptr<MediaVideoHelper> mVideoHelper;
78 
79     void fetchAllFrames();
80     void createAndInitSoftVideoHelper();
81 
82     // color aspects related
83 private:
84     // default is 601, limited range, sRGB
85     MetadataParam mMetadata = {1, 4, 2, 3};
86 
87 private:
88     void decode_internal(const uint8_t* data, size_t len, uint64_t pts);
89     void oneShotDecode(const uint8_t* data, size_t len, uint64_t pts);
90     void try_decode(const uint8_t* data, size_t len, uint64_t pts);
91     mutable MediaSnapshotHelper mSnapshotHelper;
92 };
93 
94 }  // namespace emulation
95 }  // namespace android
96