houxiao
2017-07-19 a88fc40a16cefc8248baa4b6aab7b7577e0ba4cc
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include "PL_Scale.h"
#include "MaterialBuffer.h"
#include "logger.h"
#include <libyuv.h>
 
struct PL_Scale_Internal
{
    uint8_t* buffer;
    size_t buffSize;
    size_t buffSizeMax;
    bool payError;
    
    PipeMaterial::PipeMaterialBufferType lastPmType;
    MB_Frame lastFrame;
    PL_Scale_Config config;
 
    PL_Scale_Internal() : 
        buffer(nullptr), buffSize(0), buffSizeMax(0), payError(true), 
        lastPmType(PipeMaterial::PMT_NONE), lastFrame(), config()
    {
    }
    
    ~PL_Scale_Internal()
    {
        delete[] buffer;
        buffer = nullptr;
    }
    
    void reset()
    {
        buffSize = 0;
        payError = true;
        
        lastPmType = PipeMaterial::PMT_NONE;
        
        MB_Frame _lastFrame;
        lastFrame = _lastFrame;
        
        PL_Scale_Config _config;
        config = _config;
 
        if (buffer != nullptr)
        {
            delete[] buffer;
            buffer = nullptr;
            buffSizeMax = 0;
        }
    }
};
 
PipeLineElem* create_PL_Scale()
{
    return new PL_Scale;
}
 
PL_Scale::PL_Scale() : internal(new PL_Scale_Internal)
{
}
 
PL_Scale::~PL_Scale()
{
    delete (PL_Scale_Internal*)internal;
    internal= nullptr;
}
 
bool PL_Scale::init(void* args)
{
    PL_Scale_Internal* in = (PL_Scale_Internal*)internal;
    in->reset();
    
    if (args != nullptr)
    {
        PL_Scale_Config* config = (PL_Scale_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_Scale::finit()
{
    PL_Scale_Internal* in = (PL_Scale_Internal*)internal;
    
}
 
bool image_scale(PL_Scale_Internal* in, 
    uint8_t* srcBuffer, MB_Frame::MBFType srcType, uint16_t srcWidth, uint16_t srcHeight)
{
#define SUBSAMPLE(v, a) ((((v) + (a) - 1)) / (a))
 
    const int dst_width = in->config.toWidth;
    const int dst_height = in->config.toHeight;
 
    size_t dstSizeMax = 0;
    if (srcType == MB_Frame::MBFT_YUV420||srcType == MB_Frame::MBFT_NV12)
        dstSizeMax = in->config.toWidth * in->config.toHeight * 1.5;
    else if (srcType == MB_Frame::MBFT_BGRA)
        dstSizeMax = in->config.toWidth * in->config.toHeight * 4;
    else
    {
        LOG_ERROR << "srcType only support MBFT_YUV420 and MBFT_BGRA" << std::endl;
        return false;
    }
 
    if (in->buffer == nullptr || in->buffSizeMax < dstSizeMax)
    {
        if (in->buffer != nullptr)
            delete[] in->buffer;
        in->buffer = new uint8_t[dstSizeMax];
        in->buffSizeMax = dstSizeMax;
        LOG_INFO << "image_scale alloc buffer size=" << dstSizeMax << std::endl;
    }
 
    if (srcType == MB_Frame::MBFT_YUV420)
    {
        uint8_t* src_y = srcBuffer;
        uint8_t* src_u = src_y + srcWidth * srcHeight;
        uint8_t* src_v = src_u + srcWidth * srcHeight / 4;
        uint8_t* dst_y = in->buffer;
        uint8_t* dst_u = dst_y + dst_width * dst_height;
        uint8_t* dst_v = dst_u + dst_width * dst_height / 4;
        
        libyuv::I420Scale(
            src_y, srcWidth,     
            src_u, SUBSAMPLE(srcWidth, 2), 
            src_v, SUBSAMPLE(srcWidth, 2), 
            srcWidth, srcHeight, 
            dst_y, dst_width,     
            dst_u, SUBSAMPLE(dst_width, 2), 
            dst_v, SUBSAMPLE(dst_width, 2), 
            dst_width, dst_height, 
            (libyuv::FilterMode)(in->config.filterMode));
            
        in->buffSize = dstSizeMax;
    }
    else if (srcType == MB_Frame::MBFT_NV12)
    {
        const uint8_t* src_y = (const uint8_t*)(srcBuffer);
        const uint8_t* src_uv = (const uint8_t*)(src_y + (srcHeight * srcWidth));
        if (srcWidth != dst_width || srcHeight != dst_height)
        {
            // RK3288, 1920->640: 2.8~12ms, avg=4ms
 
            uint8_t* dst_y = (uint8_t*)(in->buffer);
            uint8_t* dst_uv = (uint8_t*)(dst_y + (dst_height * dst_width));
 
            libyuv::ScalePlane(src_y, srcWidth,
                               srcWidth, srcHeight,
                               dst_y, dst_width,
                               dst_width, dst_height,
                               libyuv::kFilterNone);
 
            libyuv::ScalePlane_16((uint16*)src_uv, SUBSAMPLE(srcWidth, 2),
                                  SUBSAMPLE(srcWidth, 2), SUBSAMPLE(srcHeight, 2),
                                  (uint16*)dst_uv, SUBSAMPLE(dst_width, 2),
                                  SUBSAMPLE(dst_width, 2), SUBSAMPLE(dst_height, 2),
                                  libyuv::kFilterNone);
            in->buffSize = dstSizeMax;
        }
        else if (srcType == MB_Frame::MBFT_BGRA)
        {
            //#todo
            LOG_ERROR << "srcType only support MBFT_YUV420 and MBFT_NV12" << std::endl;
            return false;
        }
        else
        {
            LOG_ERROR << "srcType only support MBFT_YUV420 and MBFT_NV12" << std::endl;
            return false;
        }
        return true;
    }
}
 
bool PL_Scale::pay(const PipeMaterial& pm)
{
    PL_Scale_Internal* in = (PL_Scale_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, (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->type,
                frame->width, frame->height);
            break;
        default:
            LOG_ERROR << "Only support MBFT_YUV420 / MBFT_BGRA" << std::endl;
            return false;
        }
    }
    break;
        case PipeMaterial::PMT_PM_LIST:
        {
            // break pm list into single pm(s)
 
            MB_Frame* ppm = (MB_Frame*)pm.buffer;
            for (size_t i = 0; i < pm.buffSize; i++, ppm++)
            {
                if (ppm->type== PipeMaterial::PMT_FRAME)
                {
                    MB_Frame* frame = (MB_Frame*)ppm->buffer;
                    switch(frame->type)
                    {
                        case MB_Frame::MBFT_YUV420:
                        case MB_Frame::MBFT_BGRA:
                        case MB_Frame::MBFT_NV12:
                            in->lastFrame = *frame;
                            ret = image_scale(in, (uint8_t*)frame->buffer,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_Scale::gain(PipeMaterial& pm)
{
    PL_Scale_Internal* in = (PL_Scale_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:
    case PipeMaterial::PMT_PM_LIST:
    {
        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;
}