1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include "goldfish_media_utils.h"
20 #include "goldfish_vpx_defs.h"
21 #include <SimpleC2Component.h>
22 
23 namespace android {
24 
25 struct C2GoldfishVpxDec : public SimpleC2Component {
26     class IntfImpl;
27 
28     C2GoldfishVpxDec(const char *name, c2_node_id_t id,
29                      const std::shared_ptr<IntfImpl> &intfImpl);
30     virtual ~C2GoldfishVpxDec();
31 
32     // From SimpleC2Component
33     c2_status_t onInit() override;
34     c2_status_t onStop() override;
35     void onReset() override;
36     void onRelease() override;
37     c2_status_t onFlush_sm() override;
38     void process(const std::unique_ptr<C2Work> &work,
39                  const std::shared_ptr<C2BlockPool> &pool) override;
40     c2_status_t drain(uint32_t drainMode,
41                       const std::shared_ptr<C2BlockPool> &pool) override;
42 
43   private:
44     enum {
45         MODE_VP8,
46         MODE_VP9,
47     } mMode;
48 
49     struct ConversionQueue;
50 
51     class ConverterThread : public Thread {
52       public:
53         explicit ConverterThread(
54             const std::shared_ptr<Mutexed<ConversionQueue>> &queue);
55         ~ConverterThread() override = default;
56         bool threadLoop() override;
57 
58       private:
59         std::shared_ptr<Mutexed<ConversionQueue>> mQueue;
60     };
61 
62     // create context that talks to host decoder: it needs to use
63     // pool to decide whether decoding to host color buffer ot
64     // decode to guest bytebuffer when pool cannot fetch valid host
65     // color buffer id
66     void checkContext(const std::shared_ptr<C2BlockPool> &pool);
67     bool mEnableAndroidNativeBuffers{true};
68 
69     void setup_ctx_parameters(vpx_codec_ctx_t *ctx, int hostColorBufferId = -1);
70 
71     std::shared_ptr<C2StreamColorAspectsTuning::output> mColorAspects;
72 
73 
74     std::shared_ptr<IntfImpl> mIntf;
75     vpx_codec_ctx_t *mCtx;
76     bool mFrameParallelMode; // Frame parallel is only supported by VP9 decoder.
77     vpx_image_t *mImg;
78 
79     uint32_t mWidth;
80     uint32_t mHeight;
81     bool mSignalledOutputEos;
82     bool mSignalledError;
83 
84     // this is VP8 only
85     uint64_t mLastPts { 0 };
86 
87     C2Color::range_t m_range;
88     C2Color::primaries_t m_primaries;
89     C2Color::transfer_t m_transfer;
90     C2Color::matrix_t m_matrix;
91 
92     struct ConversionQueue {
93         std::list<std::function<void()>> entries;
94         Condition cond;
95         size_t numPending{0u};
96     };
97     std::shared_ptr<Mutexed<ConversionQueue>> mQueue;
98     std::vector<sp<ConverterThread>> mConverterThreads;
99 
100     status_t initDecoder();
101     status_t destroyDecoder();
102     void finishWork(uint64_t index, const std::unique_ptr<C2Work> &work,
103                     const std::shared_ptr<C2GraphicBlock> &block);
104     status_t outputBuffer(const std::shared_ptr<C2BlockPool> &pool,
105                           const std::unique_ptr<C2Work> &work);
106     c2_status_t drainInternal(uint32_t drainMode,
107                               const std::shared_ptr<C2BlockPool> &pool,
108                               const std::unique_ptr<C2Work> &work);
109 
110     MetaDataColorAspects mSentMetadata = {1, 0, 0, 0};
111     void sendMetadata();
112 
113     C2_DO_NOT_COPY(C2GoldfishVpxDec);
114 };
115 
116 } // namespace android
117