1 /*
2  * Copyright (C) 2024 Huawei Device Co., Ltd.
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 
16 #ifndef TEMPORAL_SCALABILITY_H
17 #define TEMPORAL_SCALABILITY_H
18 
19 #include <cstdint>
20 #include <shared_mutex>
21 #include <unordered_map>
22 #include "av_common.h"
23 #include "block_queue.h"
24 #include "buffer/avbuffer.h"
25 
26 namespace OHOS {
27 namespace MediaAVCodec {
28 constexpr double DEFAULT_FRAMERATE = 30.0;
29 constexpr int32_t DEFAULT_I_FRAME_INTERVAL = 2000;
30 constexpr int32_t MIN_TEMPORAL_GOPSIZE = 2;
31 constexpr int32_t DEFAULT_TEMPORAL_GOPSIZE = 4;
32 constexpr int32_t DEFAULT_GOPSIZE = 60;
33 
34 class TemporalScalability {
35 public:
36     explicit TemporalScalability(const std::string &name);
37     virtual ~TemporalScalability();
38     bool svcLTR_ = false; // true: LTR
39     void ValidateTemporalGopParam(Format &format);
40     uint32_t GetFirstBufferIndex();
41     void StoreAVBuffer(uint32_t index, std::shared_ptr<Media::AVBuffer> buffer);
42     void ConfigureLTR(uint32_t index);
43     void SetDisposableFlag(std::shared_ptr<Media::AVBuffer> buffer);
44     void SetBlockQueueActive();
45 
46 private:
47     std::string name_;
48     bool isMarkLTR_ = false;
49     bool isUseLTR_ = false;
50     int32_t ltrPoc_ = 0;
51     int32_t poc_ = 0;
52     int32_t temporalPoc_ = 0;
53     uint32_t inputFrameCounter_ = 0;
54     uint32_t outputFrameCounter_ = 0;
55     int32_t frameNum_ = 0;
56     int32_t gopSize_ = DEFAULT_GOPSIZE;
57     int32_t temporalGopSize_ = 0;
58     int32_t tRefMode_ = 0;
59     std::shared_mutex inputBufMutex_;
60     std::shared_mutex frameFlagMapMutex_;
61     std::unordered_map<uint32_t, uint32_t> frameFlagMap_;
62     std::unordered_map<uint32_t, std::shared_ptr<Media::AVBuffer>> inputBufferMap_;
63     std::shared_ptr<BlockQueue<uint32_t>> inputIndexQueue_;
64     bool IsLTRSolution();
65     int32_t LTRFrameNumCalculate(int32_t tGopSize) const;
66     void MarkLTRDecision();
67     static int32_t LTRPocDecision(int32_t tPoc);
68     void AdjacentJumpLTRDecision();
69     void UniformlyScaledLTRDecision();
70     void LTRDecision();
71     uint32_t DisposableDecision() const;
72 };
73 
74 } // namespace MediaAVCodec
75 } // namespace OHOS
76 #endif // TEMPORAL_SCALABILITY_H