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 #ifndef ANDROID_C2_SOFT_AV1_DEC_H_
18 #define ANDROID_C2_SOFT_AV1_DEC_H_
19 
20 #include <inttypes.h>
21 
22 #include <SimpleC2Component.h>
23 #include "aom/aom_decoder.h"
24 #include "aom/aomdx.h"
25 
26 namespace android {
27 
28 struct C2SoftAomDec : public SimpleC2Component {
29     class IntfImpl;
30 
31     C2SoftAomDec(const char* name, c2_node_id_t id,
32                  const std::shared_ptr<IntfImpl>& intfImpl);
33     virtual ~C2SoftAomDec();
34 
35     // From SimpleC2Component
36     c2_status_t onInit() override;
37     c2_status_t onStop() override;
38     void onReset() override;
39     void onRelease() override;
40     c2_status_t onFlush_sm() override;
41     void process(const std::unique_ptr<C2Work>& work,
42                  const std::shared_ptr<C2BlockPool>& pool) override;
43     c2_status_t drain(uint32_t drainMode,
44                       const std::shared_ptr<C2BlockPool>& pool) override;
45 
46    private:
47     std::shared_ptr<IntfImpl> mIntf;
48     aom_codec_ctx_t* mCodecCtx;
49 
50     uint32_t mWidth;
51     uint32_t mHeight;
52     bool mSignalledOutputEos;
53     bool mSignalledError;
54 
55     #ifdef FILE_DUMP_ENABLE
56     char mInFile[200];
57     char mOutFile[200];
58     #endif /* FILE_DUMP_ENABLE */
59 
60     nsecs_t mTimeStart = 0;   // Time at the start of decode()
61     nsecs_t mTimeEnd = 0;     // Time at the end of decode()
62 
63     status_t initDecoder();
64     status_t destroyDecoder();
65     void finishWork(uint64_t index, const std::unique_ptr<C2Work>& work,
66                     const std::shared_ptr<C2GraphicBlock>& block);
67     bool outputBuffer(const std::shared_ptr<C2BlockPool>& pool,
68                       const std::unique_ptr<C2Work>& work);
69 
70     c2_status_t drainInternal(uint32_t drainMode,
71                               const std::shared_ptr<C2BlockPool>& pool,
72                               const std::unique_ptr<C2Work>& work);
73 
74     C2_DO_NOT_COPY(C2SoftAomDec);
75 };
76 
77 #ifdef FILE_DUMP_ENABLE
78 
79 #define INPUT_DUMP_PATH "/data/local/tmp/temp/av1"
80 #define INPUT_DUMP_EXT "webm"
81 #define OUTPUT_DUMP_PATH "/data/local/tmp/temp/av1"
82 #define OUTPUT_DUMP_EXT "av1"
83 #define GENERATE_FILE_NAMES()                                                 \
84     {                                                                         \
85         nsecs_t now = systemTime();                                           \
86         ALOGD("GENERATE_FILE_NAMES");                                         \
87         sprintf(mInFile, "%s_%" PRId64 ".%s", INPUT_DUMP_PATH,                \
88                 now, INPUT_DUMP_EXT);                                         \
89         strcpy(mOutFile, "");                                                 \
90         sprintf(mOutFile, "%s_%" PRId64 ".%s", OUTPUT_DUMP_PATH,              \
91                 now, OUTPUT_DUMP_EXT);                                        \
92     }
93 
94 #define CREATE_DUMP_FILE(m_filename)                     \
95     {                                                    \
96         FILE* fp = fopen(m_filename, "wb");              \
97         if (fp != NULL) {                                \
98             ALOGD("Opened file %s", m_filename);         \
99             fclose(fp);                                  \
100         } else {                                         \
101             ALOGD("Could not open file %s", m_filename); \
102         }                                                \
103     }
104 #define DUMP_TO_FILE(m_filename, m_buf, m_size)              \
105     {                                                        \
106         FILE* fp = fopen(m_filename, "ab");                  \
107         if (fp != NULL && m_buf != NULL) {                   \
108             int i;                                           \
109             ALOGD("Dump to file!");                          \
110             i = fwrite(m_buf, 1, m_size, fp);                \
111             if (i != (int)m_size) {                          \
112                 ALOGD("Error in fwrite, returned %d", i);    \
113                 perror("Error in write to file");            \
114             }                                                \
115             fclose(fp);                                      \
116         } else {                                             \
117             ALOGD("Could not write to file %s", m_filename); \
118             if (fp != NULL) fclose(fp);                      \
119         }                                                    \
120     }
121 #else /* FILE_DUMP_ENABLE */
122 #define INPUT_DUMP_PATH
123 #define INPUT_DUMP_EXT
124 #define OUTPUT_DUMP_PATH
125 #define OUTPUT_DUMP_EXT
126 #define GENERATE_FILE_NAMES()
127 #define CREATE_DUMP_FILE(m_filename)
128 #define DUMP_TO_FILE(m_filename, m_buf, m_size)
129 #endif /* FILE_DUMP_ENABLE */
130 
131 }  // namespace android
132 
133 #endif  // ANDROID_C2_SOFT_AV1_DEC_H_
134