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
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
// 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.
 
#include "image.h"
 
#include <assert.h>
#include <string.h>
 
#include "key_string.h"
#include "media_type.h"
#include "utils.h"
 
void GetPixFmtNumDen(const PixelFormat& fmt, int& num, int& den)
{
    num = 0;
    den = 1;
    switch (fmt) {
        case PIX_FMT_RGB332:
            num = 1;
            break;
        case PIX_FMT_YUV420P:
        case PIX_FMT_NV12:
        case PIX_FMT_NV21:
        case PIX_FMT_FBC0:
            num = 3;
            den = 2;
            break;
        case PIX_FMT_YUV422P:
        case PIX_FMT_NV16:
        case PIX_FMT_NV61:
        case PIX_FMT_YUYV422:
        case PIX_FMT_UYVY422:
        case PIX_FMT_RGB565:
        case PIX_FMT_BGR565:
        case PIX_FMT_FBC2:
            num = 2;
            break;
        case PIX_FMT_RGB888:
        case PIX_FMT_BGR888:
            num = 3;
            break;
        case PIX_FMT_ARGB8888:
        case PIX_FMT_ABGR8888:
            num = 4;
            break;
        default:
            LOG("unsupport get num/den for pixel fmt: %d\n", fmt);
    }
}
 
int CalPixFmtSize(const PixelFormat& fmt, const int width, const int height, int align)
{
    int num = 0;
    int den = 0;
    int extra_hdr_size = 0;
    int pix_fmt_size = 0;
    GetPixFmtNumDen(fmt, num, den);
    // fbc image: fbc hdr + fbc body.
    // fbc w,h must be 16 align, and body offset must be 4k align
    if (fmt == PIX_FMT_FBC0 || fmt == PIX_FMT_FBC2) {
        align = 16;
        extra_hdr_size = UPALIGNTO(width, align) * UPALIGNTO(height, align) / 16;
        extra_hdr_size = UPALIGNTO(extra_hdr_size, 4096);
    }
    // mpp always require buffer align by align value
    if (align > 0)
        pix_fmt_size = UPALIGNTO(width, align) * UPALIGNTO(height, align) * num / den;
    else {
        pix_fmt_size = width * height * num / den;
    }
 
    return (extra_hdr_size + pix_fmt_size);
}
 
static const struct PixFmtStringEntry {
    PixelFormat fmt;
    const char* type_str;
} pix_fmt_string_map[] = {
    {PIX_FMT_YUV420P, IMAGE_YUV420P}, {PIX_FMT_NV12, IMAGE_NV12},         {PIX_FMT_NV21, IMAGE_NV21},
    {PIX_FMT_YUV422P, IMAGE_YUV422P}, {PIX_FMT_NV16, IMAGE_NV16},         {PIX_FMT_NV61, IMAGE_NV61},
    {PIX_FMT_YUYV422, IMAGE_YUYV422}, {PIX_FMT_UYVY422, IMAGE_UYVY422},   {PIX_FMT_RGB332, IMAGE_RGB332},
    {PIX_FMT_RGB565, IMAGE_RGB565},   {PIX_FMT_BGR565, IMAGE_BGR565},     {PIX_FMT_RGB888, IMAGE_RGB888},
    {PIX_FMT_BGR888, IMAGE_BGR888},   {PIX_FMT_ARGB8888, IMAGE_ARGB8888}, {PIX_FMT_ABGR8888, IMAGE_ABGR8888},
    {PIX_FMT_FBC0, IMAGE_FBC0},       {PIX_FMT_FBC0, IMAGE_FBC2}};
 
PixelFormat StringToPixFmt(const char* type)
{
    if (!type) {
        return PIX_FMT_NONE;
    }
    FIND_ENTRY_TARGET_BY_STRCMP(type, pix_fmt_string_map, type_str, fmt)
    return PIX_FMT_NONE;
}
 
const char* PixFmtToString(PixelFormat fmt)
{
    FIND_ENTRY_TARGET(fmt, pix_fmt_string_map, fmt, type_str)
    return nullptr;
}
 
namespace easymedia
{
    bool ParseImageInfoFromMap(std::map<std::string, std::string>& params, ImageInfo& info, bool input)
    {
        std::string value;
        const char* type = input ? KEY_INPUTDATATYPE : KEY_OUTPUTDATATYPE;
        CHECK_EMPTY(value, params, type)
        info.pix_fmt = StringToPixFmt(value.c_str());
        if (info.pix_fmt == PIX_FMT_NONE) {
            LOG("unsupport pix fmt %s\n", value.c_str());
            return false;
        }
        CHECK_EMPTY(value, params, KEY_BUFFER_WIDTH)
        info.width = std::stoi(value);
        CHECK_EMPTY(value, params, KEY_BUFFER_HEIGHT)
        info.height = std::stoi(value);
        CHECK_EMPTY(value, params, KEY_BUFFER_VIR_WIDTH)
        info.vir_width = std::stoi(value);
        CHECK_EMPTY(value, params, KEY_BUFFER_VIR_HEIGHT)
        info.vir_height = std::stoi(value);
        return true;
    }
 
    std::string to_param_string(const ImageInfo& info, bool input)
    {
        std::string s;
        const char* fmt = PixFmtToString(info.pix_fmt);
        if (!fmt) {
            return s;
        }
        if (input) {
            PARAM_STRING_APPEND(s, KEY_INPUTDATATYPE, fmt);
        } else {
            PARAM_STRING_APPEND(s, KEY_OUTPUTDATATYPE, fmt);
        }
        PARAM_STRING_APPEND_TO(s, KEY_BUFFER_WIDTH, info.width);
        PARAM_STRING_APPEND_TO(s, KEY_BUFFER_HEIGHT, info.height);
        PARAM_STRING_APPEND_TO(s, KEY_BUFFER_VIR_WIDTH, info.vir_width);
        PARAM_STRING_APPEND_TO(s, KEY_BUFFER_VIR_HEIGHT, info.vir_height);
        return s;
    }
 
    std::string TwoImageRectToString(const std::vector<ImageRect>& src_dst)
    {
        char r[128] = {0};
        assert(src_dst[0].x < 10000 && src_dst[0].y < 10000);
        assert(src_dst[0].w < 10000 && src_dst[0].h < 10000);
        assert(src_dst[1].x < 10000 && src_dst[1].y < 10000);
        assert(src_dst[1].w < 10000 && src_dst[1].h < 10000);
        snprintf(r, sizeof(r), "(%d,%d,%d,%d)%s(%d,%d,%d,%d)", src_dst[0].x, src_dst[0].y, src_dst[0].w, src_dst[0].h,
                 KEY_RIGHT_DIRECTION, src_dst[1].x, src_dst[1].y, src_dst[1].w, src_dst[1].h);
        return r;
    }
 
    std::vector<ImageRect> StringToTwoImageRect(const std::string& str_rect)
    {
        std::vector<ImageRect> ret;
        const char* s = nullptr;
        if (str_rect.empty() || !(s = strstr(str_rect.c_str(), KEY_RIGHT_DIRECTION))) {
            return std::move(ret);
        }
        const char* args[2] = {str_rect.c_str(), s + sizeof(KEY_RIGHT_DIRECTION) - 1};
        for (int i = 0; i < 2; i++) {
            ImageRect rect = {0, 0, 0, 0};
            int r = sscanf(args[i], "(%d,%d,%d,%d)", &rect.x, &rect.y, &rect.w, &rect.h);
            if (r != 4) {
                LOG("Fail to sscanf(ret=%d) : %m\n", r);
                ret.clear();
                return std::move(ret);
            }
            ret.push_back(std::move(rect));
        }
 
        return std::move(ret);
    }
 
    std::string ImageRectToString(const ImageRect& src_dst)
    {
        char r[64] = {0};
        assert(src_dst.x >= 0 && src_dst.y >= 0);
        assert(src_dst.x < 10000 && src_dst.y < 10000);
        assert(src_dst.w < 10000 && src_dst.h < 10000);
        snprintf(r, sizeof(r), "(%d,%d,%d,%d)", src_dst.x, src_dst.y, src_dst.w, src_dst.h);
        return r;
    }
 
    std::vector<ImageRect> StringToImageRect(const std::string& str_rect)
    {
        std::vector<ImageRect> ret;
        const char* start = nullptr;
        const char* delimiter = nullptr;
 
        if (str_rect.empty()) {
            return std::move(ret);
        }
 
        start = str_rect.c_str();
        while (start) {
            delimiter = start;
            // (x,y,w,h) format check.
            for (int i = 0; i < 4; i++) {
                if (i == 3) {
                    delimiter = strstr(delimiter, ")");
                } else {
                    delimiter = strstr(delimiter, ",");
                }
                if (!delimiter) {
                    return std::move(ret);
                }
                delimiter += 1;
            }
 
            ImageRect rect = {0, 0, 0, 0};
            int r = sscanf(start, "(%d,%d,%d,%d)", &rect.x, &rect.y, &rect.w, &rect.h);
            if (r != 4) {
                LOG("Fail to sscanf(ret=%d) : %m\n", r);
                return std::move(ret);
            }
 
            ret.push_back(std::move(rect));
            start = strstr(delimiter, "(");
        }
 
        return std::move(ret);
    }
 
} // namespace easymedia