chenke
2017-07-19 14d33d997cfc1949fe93664197097c7cbc6e92e3
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#include "PL_Paint.h"
#include "MaterialBuffer.h"
#include "logger.h"
#include "MediaHelper.h"
#include "CvUtil/CvxText.h"
#include <string.h> // for memcpy
#include <opencv2/core/mat.hpp>
#include <opencv2/imgproc.hpp>
 
PLPLType::PLPLType(const wchar_t* _val_s)
{
    //wchar_t* w_val_s = (wchar_t*)val_s;
    //for (int i = 0; i < sizeof(val_s) / sizeof(wchar_t); i++)
    //    w_val_s[i] = _val_s[i];
 
    memcpy(val_s, _val_s, sizeof(val_s));
}
 
PLPLContext::~PLPLContext()
{
    delete (CvxText*)cvxText;
    cvxText = nullptr;
}
 
struct PL_Paint_Internal
{
    uint8_t* buffer;
    size_t buffSize;
 
    PL_Paint_Config config;
    PipeMaterial pmList[2];
    MB_Frame lastMbfBuffOrigin;
    MB_Frame lastMbfBuffCopy;
 
    bool payError;
 
    PL_Paint_Internal() : 
        buffer(nullptr), buffSize(), config(), pmList(),
        lastMbfBuffOrigin(), lastMbfBuffCopy(),
        payError(true)
    {
    }
    
    ~PL_Paint_Internal()
    {
        reset();
    }
    
    void reset()
    {
        delete buffer;
        buffer = nullptr;
        buffSize = 0;
        
        PL_Paint_Config _config;
        config = _config;
        
        PipeMaterial _pm;
        pmList[0] = _pm;
        pmList[1] = _pm;
 
        MB_Frame _lastMbfBuff;
        lastMbfBuffOrigin = _lastMbfBuff;
        lastMbfBuffCopy = _lastMbfBuff;
 
        payError = true;
 
    }
};
 
PipeLineElem* create_PL_Paint()
{
    return new PL_Paint;
}
 
PL_Paint::PL_Paint() : internal(new PL_Paint_Internal)
{
}
 
PL_Paint::~PL_Paint()
{
    delete (PL_Paint_Internal*)internal;
    internal= nullptr;
}
 
bool PL_Paint::init(void* args)
{
    PL_Paint_Internal* in = (PL_Paint_Internal*)internal;
    in->reset();
    
    if (args != nullptr)
    {
        PL_Paint_Config* config = (PL_Paint_Config*)args;
        in->config = *config;
    }
 
    if(in->config.plplCtx->cvxText != nullptr)
    {
        delete (CvxText*)in->config.plplCtx->cvxText;
    }
    
    if(in->config.fontPath.empty())
    {
        LOG_ERROR << "fontPath is null!" << LOG_ENDL;
        return false;
    }
    else if (in->config.fontPath == " ")
    {
        CvxText* cvxText = new CvxText(in->config.fontPath.c_str());
        in->config.plplCtx->cvxText = cvxText;
        cvxText->setBackColor(cvScalar(128, 33, 14));
        CvScalar font = cvScalar(40, 1, 0.2, 1);
        cvxText->setFont(0, &font);
    }
 
    return true;
}
 
void PL_Paint::finit()
{
    PL_Paint_Internal* in = (PL_Paint_Internal*)internal;
    
}
 
bool plplDraw_Rect_YUV420(PLPLContext* plplCtx, MB_Frame* paintMb, int& paramOffset)
{
    int LTX = plplCtx->params[paramOffset + 0].val_i;
    int LTY = plplCtx->params[paramOffset + 1].val_i;
    int RBX = plplCtx->params[paramOffset + 2].val_i;
    int RBY = plplCtx->params[paramOffset + 3].val_i;
    paramOffset += 4;
 
    int src_width = paintMb->width;
    int src_height = paintMb->height;
    uint8_t* src_y = (uint8_t*)(paintMb->buffer);
    uint8_t* src_u = (uint8_t*)(src_y + (src_height * src_width));
    uint8_t* src_v = (uint8_t*)(src_u + (src_height * src_width / 4));
    cv::Mat yMat(cv::Size(src_width, src_height), CV_8UC1, src_y);
    cv::Mat uMat(cv::Size(MH_SUBSAMPLE1(src_width, 2), MH_SUBSAMPLE1(src_height, 2)), CV_8UC1, src_u);
    cv::Mat vMat(cv::Size(MH_SUBSAMPLE1(src_width, 2), MH_SUBSAMPLE1(src_height, 2)), CV_8UC1, src_v);
 
    // void rectangle(Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineType=8, int shift=0)
 
    const PLGH_Color_YUV yuvColor(plplCtx->color_front.toY(), plplCtx->color_front.toU(), plplCtx->color_front.toV());
 
    cv::rectangle(yMat, cv::Point(LTX, LTY), cv::Point(RBX, RBY), CV_RGB(yuvColor.Y, yuvColor.Y, yuvColor.Y), 4);
    cv::rectangle(uMat, cv::Point(MH_SUBSAMPLE1(LTX, 2), MH_SUBSAMPLE1(LTY, 2)), cv::Point(MH_SUBSAMPLE1(RBX, 2), MH_SUBSAMPLE1(RBY, 2)), CV_RGB(yuvColor.U, yuvColor.U, yuvColor.U), 2);
    cv::rectangle(vMat, cv::Point(MH_SUBSAMPLE1(LTX, 2), MH_SUBSAMPLE1(LTY, 2)), cv::Point(MH_SUBSAMPLE1(RBX, 2), MH_SUBSAMPLE1(RBY, 2)), CV_RGB(yuvColor.V, yuvColor.V, yuvColor.V), 2);
    
    return true;
}
 
bool plplDraw_Rect_NV12(PLPLContext* plplCtx, MB_Frame* paintMb, int& paramOffset)
{
    int LTX = plplCtx->params[paramOffset + 0].val_i;
    int LTY = plplCtx->params[paramOffset + 1].val_i;
    int RBX = plplCtx->params[paramOffset + 2].val_i;
    int RBY = plplCtx->params[paramOffset + 3].val_i;
    paramOffset += 4;
 
    int src_width = paintMb->width;
    int src_height = paintMb->height;
    uint8_t* src_y = (uint8_t*)(paintMb->buffer);
    uint8_t* src_uv = (uint8_t*)(src_y + (src_height * src_width));
    cv::Mat yMat(cv::Size(src_width, src_height), CV_8UC1, src_y);
    cv::Mat uvMat(cv::Size(MH_SUBSAMPLE1(src_width, 2), MH_SUBSAMPLE1(src_height, 2)), CV_16UC1, src_uv);
 
    // void rectangle(Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineType=8, int shift=0)
 
    const uint16_t Y =  plplCtx->color_front.toY();
    const uint16_t UV = plplCtx->color_front.toUV();
 
    cv::rectangle(yMat, cv::Point(LTX, LTY), cv::Point(RBX, RBY), CV_RGB(Y, Y, Y), 4);
    cv::rectangle(uvMat, cv::Point(MH_SUBSAMPLE1(LTX, 2), MH_SUBSAMPLE1(LTY, 2)), cv::Point(MH_SUBSAMPLE1(RBX, 2), MH_SUBSAMPLE1(RBY, 2)), CV_RGB(UV, UV, UV), 2);
    
    return true;
}
 
bool plplDraw_Text_YUV420(PLPLContext* plplCtx, MB_Frame* paintMb, int& paramOffset)
{
    return false;
}
 
bool plplDraw_Text_NV12(PLPLContext* plplCtx, MB_Frame* paintMb, int& paramOffset)
{
    int LTX = plplCtx->params[paramOffset + 0].val_i;
    int LTY = plplCtx->params[paramOffset + 1].val_i;
    const char* TXT = plplCtx->params[paramOffset + 2].val_s;
    paramOffset += 3;
 
    int src_width = paintMb->width;
    int src_height = paintMb->height;
    uint8_t* src_y = (uint8_t*)(paintMb->buffer);
    uint8_t* src_uv = (uint8_t*)(src_y + (src_height * src_width));
    cv::Mat yMat(cv::Size(src_width, src_height), CV_8UC1, src_y);
    cv::Mat uvMat(cv::Size(MH_SUBSAMPLE1(src_width, 2), MH_SUBSAMPLE1(src_height, 2)), CV_16UC1, src_uv);
 
    // void rectangle(Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineType=8, int shift=0)
 
    const uint16_t Y =  plplCtx->color_front.toY();
    const uint16_t UV = plplCtx->color_front.toUV();
 
    cv::putText(yMat, TXT, cv::Point(LTX, LTY), CV_FONT_HERSHEY_COMPLEX, 1, CV_RGB(Y, Y, Y));
    cv::putText(uvMat, TXT, cv::Point(MH_SUBSAMPLE1(LTX, 2), MH_SUBSAMPLE1(LTY, 2)), CV_FONT_HERSHEY_COMPLEX, 0.5, CV_RGB(UV, UV, UV));
    
    return true;
}
 
bool plplDraw_WText_NV12(PLPLContext* plplCtx, MB_Frame* paintMb, int& paramOffset)
{
    int LTX = plplCtx->params[paramOffset + 0].val_i;
    int LTY = plplCtx->params[paramOffset + 1].val_i;
    const char* WTXT = plplCtx->params[paramOffset + 2].val_s;
    paramOffset += 3;
 
    int src_width = paintMb->width;
    int src_height = paintMb->height;
    uint8_t* src_y = (uint8_t*)(paintMb->buffer);
    uint8_t* src_uv = (uint8_t*)(src_y + (src_height * src_width));
    cv::Mat yMat(cv::Size(src_width, src_height), CV_8UC1, src_y);
    cv::Mat uvMat(cv::Size(MH_SUBSAMPLE1(src_width, 2), MH_SUBSAMPLE1(src_height, 2)), CV_16UC1, src_uv);
 
    // void rectangle(Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineType=8, int shift=0)
 
    const uint16_t Y =  plplCtx->color_front.toY();
    const uint16_t UV = plplCtx->color_front.toUV();
    
    CvxText* cvText = (CvxText*)plplCtx->cvxText;
    cvText->putText(yMat, (const wchar_t*)WTXT, cv::Point(LTX, LTY), CV_RGB(Y, Y, Y)); // L"中文ABCDabcd"
    cvText->putText(uvMat, (const wchar_t*)WTXT, cv::Point(MH_SUBSAMPLE1(LTX, 2), MH_SUBSAMPLE1(LTY, 2)), CV_RGB(UV, UV, UV)); // L"中文ABCDabcd"
    
    return true;
}
 
bool plplExecutor_YUV(PL_Paint_Internal *in)
{
    MB_Frame* paintMb = &(in->lastMbfBuffOrigin);
    int ret = true;
 
    if (in->config.copyData)
    {
        if (in->buffer == nullptr)
        {
            in->buffer = new uint8_t[in->lastMbfBuffOrigin.buffSize];
            in->buffSize = in->lastMbfBuffOrigin.buffSize;
        }
 
        in->lastMbfBuffCopy = in->lastMbfBuffOrigin;
        in->lastMbfBuffCopy.buffer = in->buffer;
        in->lastMbfBuffCopy.buffSize = in->buffSize;
 
        memcpy(in->lastMbfBuffCopy.buffer, in->lastMbfBuffOrigin.buffer, in->lastMbfBuffCopy.buffSize);
 
        paintMb = &(in->lastMbfBuffCopy);
    }
 
    //for (int i=640;i<2000;i++) ((uint8_t*)paintMb->buffer)[i]=255;
 
    PLPLContext* plplCtx = in->config.plplCtx;
    int paramOffset = 0;
    for (int iCmd = 0; iCmd < in->config.plplCtx->cmds.size(); iCmd++)
    {
        PLPLCmd cmd = plplCtx->cmds[iCmd];
 
        switch(cmd)
        {
            case PLPLC_COLOR:
                //COLOR F/B,R,G,B,A
                if (plplCtx->params[paramOffset + 0].val_i == 'F')
                {
                    plplCtx->color_front = PLGH_Color_RGBA(plplCtx->params[paramOffset + 1].val_i,
                                                     plplCtx->params[paramOffset + 2].val_i,
                                                     plplCtx->params[paramOffset + 3].val_i,
                                                     plplCtx->params[paramOffset + 4].val_i);
                    paramOffset += 5;
                }
                else if (plplCtx->params[paramOffset + 0].val_i == 'B')
                {
                    plplCtx->color_back = PLGH_Color_RGBA(plplCtx->params[paramOffset + 1].val_i,
                                                    plplCtx->params[paramOffset + 2].val_i,
                                                    plplCtx->params[paramOffset + 3].val_i,
                                                    plplCtx->params[paramOffset + 4].val_i);
                    paramOffset += 5;
                }
                else
                {
                    LOG_WARN << "plpl execute error" << LOG_ENDL;
                    return false;
                }
                break;
 
            case PLPLC_FILL:
                plplCtx->fill = plplCtx->params[paramOffset + 0].val_i;
                paramOffset += 1;
                break;
 
            case PLPLC_PEN:
                plplCtx->pen = PLGH_Pen(plplCtx->params[paramOffset + 0].val_i, plplCtx->params[paramOffset + 1].val_i);
                paramOffset += 2;
                break;
 
            case PLPLC_RECT:
                //RECT LTX,LTY,RBX,RBY
                if (paintMb->type == MB_Frame::MBFT_YUV420)
                    ret = plplDraw_Rect_YUV420(plplCtx, paintMb, paramOffset);
                else if (paintMb->type == MB_Frame::MBFT_NV12)
                    ret = plplDraw_Rect_NV12(plplCtx, paintMb, paramOffset);
                else
                    ret = false;
                break;
 
            case PLPLC_TEXT:
                //TEXT LTX,LTY,"STR"
                if (paintMb->type == MB_Frame::MBFT_YUV420)
                    ret = plplDraw_Text_YUV420(plplCtx, paintMb, paramOffset);
                else if (paintMb->type == MB_Frame::MBFT_NV12)
                    ret = plplDraw_Text_NV12(plplCtx, paintMb, paramOffset);
                else
                    ret = false;
                break;
 
            case PLPLC_WTEXT:
                //WTEXT LTX,LTY,"STR"
                if (paintMb->type == MB_Frame::MBFT_NV12)
                    ret = plplDraw_WText_NV12(plplCtx, paintMb, paramOffset);
                else
                    ret = false;
                break;
        }
    }
 
    return ret;
}
 
/*static*/ bool PL_Paint::pay_breaker_MBFT_YUV(const PipeMaterial* pm, void* args)
{
    PL_Paint_Internal* in = (PL_Paint_Internal*)args;
 
    if (pm->type != PipeMaterial::PMT_FRAME)
    {
        LOG_ERROR << "Only support PMT_FRAME" << LOG_ENDL;
        return false;
    }
    
    if (pm->buffer == nullptr)
        return false;
    
    MB_Frame* frame = (MB_Frame*)pm->buffer;
    if (frame->type != MB_Frame::MBFT_YUV420 && frame->type != MB_Frame::MBFT_NV12)
    {
        LOG_ERROR << "Only support MBFT_YUV420 and MBFT_NV12" << LOG_ENDL;
        in->payError = true;
        return false;
    }
 
    in->lastMbfBuffOrigin.type = frame->type;
    in->lastMbfBuffOrigin.buffer = frame->buffer;
    in->lastMbfBuffOrigin.buffSize = frame->buffSize;
    in->lastMbfBuffOrigin.width = frame->width;
    in->lastMbfBuffOrigin.height = frame->height;
    in->lastMbfBuffOrigin.pts = frame->pts;
 
    in->payError = !plplExecutor_YUV(in);
 
    return false;
}
 
bool PL_Paint::pay(const PipeMaterial& pm)
{
    PL_Paint_Internal* in = (PL_Paint_Internal*)internal;
    in->payError = true;
    pm.breake(PipeMaterial::PMT_FRAME, MB_Frame::MBFT_YUV420, PL_Paint::pay_breaker_MBFT_YUV, in);
    pm.breake(PipeMaterial::PMT_FRAME, MB_Frame::MBFT_NV12, PL_Paint::pay_breaker_MBFT_YUV, in);
 
    //#todo support RGB
    
    return !(in->payError);
}
 
bool PL_Paint::gain(PipeMaterial& pm)
{
    PL_Paint_Internal* in = (PL_Paint_Internal*)internal;
 
    if (in->payError)
    {
        pm.former = this;
        return false;
    }
 
    if (!in->config.copyData)
    {
        pm.type = PipeMaterial::PMT_FRAME;
        pm.buffer = &(in->lastMbfBuffOrigin);
        pm.buffSize = 0;
    }
    else
    {
        in->pmList[0].type = PipeMaterial::PMT_FRAME;
        in->pmList[0].buffer = &(in->lastMbfBuffCopy);
        in->pmList[0].buffSize = 0;
        in->pmList[0].former = this;
        
        in->pmList[1].type = PipeMaterial::PMT_FRAME;
        in->pmList[1].buffer = &(in->lastMbfBuffOrigin);
        in->pmList[1].buffSize = 0;
        in->pmList[1].former = this;
        
        pm.type = PipeMaterial::PMT_PM_LIST;
        pm.buffer = in->pmList;
        pm.buffSize = sizeof(in->pmList) / sizeof(PipeMaterial);
    }
    
    pm.former = this;
    return true;
}