hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright 2019 Fuzhou Rockchip Electronics Co., Ltd. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
 
#ifndef EASYMEDIA_ENCODER_H_
#define EASYMEDIA_ENCODER_H_
 
#include <mutex>
 
#ifdef __cplusplus
 
    #include "codec.h"
    #include "media_reflector.h"
 
namespace easymedia
{
 
    DECLARE_FACTORY(Encoder)
 
    // usage: REFLECTOR(Encoder)::Create<T>(codecname, param)
    // T must be the final class type exposed to user
    DECLARE_REFLECTOR(Encoder)
 
    #define DEFINE_ENCODER_FACTORY(REAL_PRODUCT, FINAL_EXPOSE_PRODUCT)                                                 \
        DEFINE_MEDIA_CHILD_FACTORY(REAL_PRODUCT, REAL_PRODUCT::GetCodecName(), FINAL_EXPOSE_PRODUCT, Encoder)          \
        DEFINE_MEDIA_CHILD_FACTORY_EXTRA(REAL_PRODUCT)                                                                 \
        DEFINE_MEDIA_NEW_PRODUCT_BY(REAL_PRODUCT, Encoder, Init() != true)
 
    class Encoder : public Codec
    {
      public:
        virtual ~Encoder() = default;
        virtual bool InitConfig(const MediaConfig& cfg);
    };
 
    // self define by user
    class ParameterBuffer
    {
      public:
        ParameterBuffer(size_t st = sizeof(int)) : size(st), ptr(nullptr)
        {
            if (sizeof(int) != st && st != 0) {
                ptr = malloc(st);
                if (!ptr) {
                    size = 0;
                }
            }
        }
        ~ParameterBuffer()
        {
            if (ptr) {
                free(ptr);
            }
        }
        size_t GetSize()
        {
            return size;
        }
        int GetValue()
        {
            return value;
        }
        void SetValue(int v)
        {
            value = v;
        }
        void* GetPtr()
        {
            return ptr;
        }
        void SetPtr(void* data, size_t data_len)
        {
            if (ptr && ptr != data) {
                free(ptr);
            }
            ptr = data;
            size = data_len;
        }
 
      private:
        size_t size;
        int value;
        void* ptr;
    };
 
    #define DEFINE_VIDEO_ENCODER_FACTORY(REAL_PRODUCT) DEFINE_ENCODER_FACTORY(REAL_PRODUCT, VideoEncoder)
 
    class _API VideoEncoder : public Encoder
    {
      public:
        // changes
        static const uint32_t kQPChange = (1 << 0);
        static const uint32_t kFrameRateChange = (1 << 1);
        static const uint32_t kBitRateChange = (1 << 2);
        static const uint32_t kForceIdrFrame = (1 << 3);
        static const uint32_t kOSDDataChange = (1 << 4);
        static const uint32_t kOSDPltChange = (1 << 5);
        static const uint32_t kMoveDetectionFlow = (1 << 6);
        static const uint32_t kROICfgChange = (1 << 7);
        static const uint32_t kRcModeChange = (1 << 8);
        static const uint32_t kRcQualityChange = (1 << 9);
        static const uint32_t kSplitChange = (1 << 10);
        static const uint32_t kGopChange = (1 << 11);
        static const uint32_t kGopModeChange = (1 << 12);
        static const uint32_t kProfileChange = (1 << 13);
        static const uint32_t kUserDataChange = (1 << 14);
        // enable fps/bps statistics.
        static const uint32_t kEnableStatistics = (1 << 31);
 
        VideoEncoder() : codec_type(CODEC_TYPE_NONE)
        {
        }
        virtual ~VideoEncoder() = default;
        void RequestChange(uint32_t change, std::shared_ptr<ParameterBuffer> value);
        virtual void QueryChange(uint32_t change, void* value, int32_t size);
 
      protected:
        bool HasChangeReq()
        {
            return !change_list.empty();
        }
        std::pair<uint32_t, std::shared_ptr<ParameterBuffer>> PeekChange();
 
        CodecType codec_type;
 
      private:
        std::mutex change_mtx;
        std::list<std::pair<uint32_t, std::shared_ptr<ParameterBuffer>>> change_list;
 
        DECLARE_PART_FINAL_EXPOSE_PRODUCT(Encoder)
    };
 
    #define DEFINE_AUDIO_ENCODER_FACTORY(REAL_PRODUCT) DEFINE_ENCODER_FACTORY(REAL_PRODUCT, AudioEncoder)
 
    class _API AudioEncoder : public Encoder
    {
      public:
        AudioEncoder() : codec_type(CODEC_TYPE_NONE)
        {
        }
        virtual ~AudioEncoder() = default;
        virtual int GetNbSamples()
        {
            return 0;
        }
 
      protected:
        CodecType codec_type;
 
        DECLARE_PART_FINAL_EXPOSE_PRODUCT(Encoder)
    };
 
} // namespace easymedia
 
#endif
 
#endif // EASYMEDIA_ENCODER_H_