1/*
2 * Copyright (C) 2017 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_AAC_DEC_H_
18#define ANDROID_C2_SOFT_AAC_DEC_H_
19
20#include <SimpleC2Component.h>
21
22
23#include "aacdecoder_lib.h"
24#include "DrcPresModeWrap.h"
25
26namespace android {
27
28struct C2SoftAacDec : public SimpleC2Component {
29    class IntfImpl;
30
31    C2SoftAacDec(const char *name, c2_node_id_t id, const std::shared_ptr<IntfImpl> &intfImpl);
32    virtual ~C2SoftAacDec();
33
34    // From SimpleC2Component
35    c2_status_t onInit() override;
36    c2_status_t onStop() override;
37    void onReset() override;
38    void onRelease() override;
39    c2_status_t onFlush_sm() override;
40    void process(
41            const std::unique_ptr<C2Work> &work,
42            const std::shared_ptr<C2BlockPool> &pool) override;
43    c2_status_t drain(
44            uint32_t drainMode,
45            const std::shared_ptr<C2BlockPool> &pool) override;
46
47private:
48    enum {
49        kNumDelayBlocksMax      = 8,
50    };
51
52    std::shared_ptr<IntfImpl> mIntf;
53
54    HANDLE_AACDECODER mAACDecoder;
55    CStreamInfo *mStreamInfo;
56    bool mIsFirst;
57    size_t mInputBufferCount;
58    size_t mOutputBufferCount;
59    bool mSignalledError;
60    struct Info {
61        uint64_t frameIndex;
62        size_t bufferSize;
63        uint64_t timestamp;
64        std::vector<int32_t> decodedSizes;
65    };
66    std::list<Info> mBuffersInfo;
67
68    CDrcPresModeWrapper mDrcWrap;
69
70    enum {
71        NONE,
72        AWAITING_DISABLED,
73        AWAITING_ENABLED
74    } mOutputPortSettingsChange;
75
76    void initPorts();
77    status_t initDecoder();
78    bool isConfigured() const;
79    void drainDecoder();
80
81    void drainRingBuffer(
82            const std::unique_ptr<C2Work> &work,
83            const std::shared_ptr<C2BlockPool> &pool,
84            bool eos);
85    c2_status_t drainInternal(
86            uint32_t drainMode,
87            const std::shared_ptr<C2BlockPool> &pool,
88            const std::unique_ptr<C2Work> &work);
89
90//      delay compensation
91    bool mEndOfInput;
92    bool mEndOfOutput;
93    int32_t mOutputDelayCompensated;
94    int32_t mOutputDelayRingBufferSize;
95    short *mOutputDelayRingBuffer;
96    int32_t mOutputDelayRingBufferWritePos;
97    int32_t mOutputDelayRingBufferReadPos;
98    int32_t mOutputDelayRingBufferFilled;
99    bool outputDelayRingBufferPutSamples(INT_PCM *samples, int numSamples);
100    int32_t outputDelayRingBufferGetSamples(INT_PCM *samples, int numSamples);
101    int32_t outputDelayRingBufferSamplesAvailable();
102    int32_t outputDelayRingBufferSpaceLeft();
103
104    C2_DO_NOT_COPY(C2SoftAacDec);
105};
106
107}  // namespace android
108
109#endif  // ANDROID_C2_SOFT_AAC_DEC_H_
110