xuxiuxi
2017-07-23 94e704acde59df60bd42f6f90b94d462d1bfa37c
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "PL_SocketGainer.h"
#include "MaterialBuffer.h"
#include "logger.h"
 
struct PL_SocketGainer_Internal
{
    uint8_t* buffer;
    size_t buffSize;
    size_t buffSizeMax;
    bool payError;
    
    PipeMaterial::PipeMaterialBufferType lastPmType;
    MB_Frame lastFrame;
    PL_SocketGainer_Config config;
 
    PL_SocketGainer_Internal() : 
        buffer(nullptr), buffSize(0), buffSizeMax(0), payError(true), 
        lastPmType(PipeMaterial::PMT_NONE), lastFrame(), config()
    {
    }
    
    ~PL_SocketGainer_Internal()
    {
        delete[] buffer;
        buffer = nullptr;
    }
    
    void reset()
    {
        buffSize = 0;
        payError = true;
        
        lastPmType = PipeMaterial::PMT_NONE;
        
        MB_Frame _lastFrame;
        lastFrame = _lastFrame;
        
        PL_SocketGainer_Config _config;
        config = _config;
 
        if (buffer != nullptr)
        {
            delete[] buffer;
            buffer = nullptr;
            buffSizeMax = 0;
        }
    }
};
 
PipeLineElem* create_PL_SocketGainer()
{
    return new PL_SocketGainer;
}
 
PL_SocketGainer::PL_SocketGainer() : internal(new PL_SocketGainer_Internal)
{
}
 
PL_SocketGainer::~PL_SocketGainer()
{
    delete (PL_SocketGainer_Internal*)internal;
    internal= nullptr;
}
 
bool PL_SocketGainer::init(void* args)
{
    PL_SocketGainer_Internal* in = (PL_SocketGainer_Internal*)internal;
    in->reset();
    
    if (args != nullptr)
    {
        PL_SocketGainer_Config* config = (PL_SocketGainer_Config*)args;
        in->config = *config;
    }
    
    if (in->config.toWidth <= 0 || in->config.toHeight <= 0)
    {
        LOG_ERROR << "Config toWidth and toHeight should > 0" << std::endl;
        return false;
    }
    
    return true;
}
 
void PL_SocketGainer::finit()
{
    PL_SocketGainer_Internal* in = (PL_SocketGainer_Internal*)internal;
    
}
 
bool PL_SocketGainer::pay(const PipeMaterial& pm)
{
    PL_SocketGainer_Internal* in = (PL_SocketGainer_Internal*)internal;
    
    in->payError = true;
    
    if (pm.buffer == nullptr)
        return false;
    
    bool ret = false;
    
    in->lastPmType = pm.type;
    
    switch(pm.type)
    {
    case PipeMaterial::PMT_BYTES:
    {
        //if (in->config.defaultBytesType <= 0 || 
        //    in->config.defaultBytesWidth <= 0 || in->config.defaultBytesHeight <= 0)
        //{
        //    LOG_ERROR << "defaultBytesType/defaultBytesWidth/defaultBytesHeight not set" << std::endl;
        //    return false;
        //}
        //
        //ret = image_scale(in, (uint8_t*)pm.buffer, pm.buffSize, (MB_Frame::MBFType)(in->config.defaultBytesType), 
        //    in->config.defaultBytesWidth, in->config.defaultBytesHeight);
    }
    break;
    case PipeMaterial::PMT_FRAME:
    {
        //MB_Frame* frame = (MB_Frame*)pm.buffer;
        //switch(frame->type)
        //{
        //case MB_Frame::MBFT_YUV420:
        //case MB_Frame::MBFT_BGRA:
        //    in->lastFrame = *frame;
        //    ret = image_scale(in, (uint8_t*)frame->buffer, frame->buffSize, frame->type, 
        //        frame->width, frame->height);
        //    break;
        //default:
        //    LOG_ERROR << "Only support MBFT_YUV420 / MBFT_BGRA" << std::endl;
        //    return false;
        //}
    }
    break;
    default:
        LOG_ERROR << "Only support PMT_BYTES / PMT_FRAME" << std::endl;
        return false;
    }
    
    in->payError = !ret;
    return ret;
}
 
bool PL_SocketGainer::gain(PipeMaterial& pm)
{
    PL_SocketGainer_Internal* in = (PL_SocketGainer_Internal*)internal;
 
    PipeMaterial newPm;
    newPm.type = PipeMaterial::PMT_NONE;
    newPm.former = this;
    
    switch(in->lastPmType)
    {
    case PipeMaterial::PMT_BYTES:
    {
        newPm.type = PipeMaterial::PMT_BYTES;
        newPm.buffer = in->buffer;
        newPm.buffSize = in->buffSize;
    }
    break;
    case PipeMaterial::PMT_FRAME:
    {
        newPm.type = PipeMaterial::PMT_FRAME;
        newPm.buffer = &(in->lastFrame);
        newPm.buffSize = 0;
 
        in->lastFrame.buffer = in->buffer;
        in->lastFrame.buffSize = in->buffSize;
        in->lastFrame.width = in->config.toWidth;
        in->lastFrame.height = in->config.toHeight;
    }
    break;
    default:
        LOG_ERROR << "Only support PMT_BYTES / PMT_FRAME" << std::endl;
    }
 
    pm = newPm;
    return !in->payError;
}