fix[utils]: fix convert format error

Change-Id: I9994ab4671dbf9c0f7b0b0476c9fd2710294805d
Signed-off-by: xueman.ruan <xueman.ruan@rock-chips.com>
This commit is contained in:
xueman.ruan
2023-08-11 16:42:57 +08:00
parent 5d00860530
commit c83da7de68
5 changed files with 88 additions and 27 deletions

View File

@@ -499,7 +499,7 @@ void *thread_decode(void *arg)
if (MPP_FRAME_FMT_IS_YUV(cmd->format) || MPP_FRAME_FMT_IS_RGB(cmd->format)) {
MPP_RET ret = mpi->control(ctx, MPP_DEC_SET_OUTPUT_FORMAT, &cmd->format);
if (ret) {
mpp_err("Failed to set output format %d\n", cmd->format);
mpp_err("Failed to set output format 0x%x\n", cmd->format);
return NULL;
}
}

View File

@@ -514,15 +514,21 @@ RK_S32 mpi_dec_opt_f(void *ctx, const char *next)
MpiDecTestCmd *cmd = (MpiDecTestCmd *)ctx;
if (next) {
cmd->format = (MppFrameFormat)atoi(next);
long number = 0;
MppFrameFormat format = MPP_FMT_BUTT;
if (MPP_FRAME_FMT_IS_YUV(cmd->format) ||
MPP_FRAME_FMT_IS_RGB(cmd->format))
return 1;
if (MPP_OK == str_to_frm_fmt(next, &number)) {
format = (MppFrameFormat)number;
if (MPP_FRAME_FMT_IS_YUV(format) || MPP_FRAME_FMT_IS_RGB(format)) {
cmd->format = format;
return 1;
}
}
}
mpp_err("invalid output format\n");
cmd->format = MPP_FMT_BUTT;
cmd->format = MPP_FMT_YUV420SP;
return 0;
}

View File

@@ -195,32 +195,23 @@ RK_S32 mpi_enc_opt_vstride(void *ctx, const char *next)
RK_S32 mpi_enc_opt_f(void *ctx, const char *next)
{
MpiEncTestArgs *cmd = (MpiEncTestArgs *)ctx;
MppFrameFormat format = MPP_FMT_BUTT;
if (next) {
if (strstr(next, "x") || strstr(next, "X")) {
/* hex value with 0x prefix, use sscanf */
sscanf(next, "0x%x", &format);
} else if (strstr(next, "a") || strstr(next, "A") ||
strstr(next, "b") || strstr(next, "B") ||
strstr(next, "c") || strstr(next, "C") ||
strstr(next, "d") || strstr(next, "D") ||
strstr(next, "e") || strstr(next, "E") ||
strstr(next, "f") || strstr(next, "F")) {
/* hex value without 0x prefix, use sscanf */
sscanf(next, "%x", &format);
} else {
/* decimal value, use atoi */
format = (MppFrameFormat)atoi(next);
}
if (MPP_FRAME_FMT_IS_BE(format) &&
(MPP_FRAME_FMT_IS_YUV(format) || MPP_FRAME_FMT_IS_RGB(format))) {
cmd->format = format;
return 1;
long number = 0;
MppFrameFormat format = MPP_FMT_BUTT;
if (MPP_OK == str_to_frm_fmt(next, &number)) {
format = (MppFrameFormat)number;
if (MPP_FRAME_FMT_IS_BE(format) &&
(MPP_FRAME_FMT_IS_YUV(format) || MPP_FRAME_FMT_IS_RGB(format))) {
cmd->format = format;
return 1;
}
}
}
mpp_err("invalid input format %x\n", format);
mpp_err("invalid input format\n");
cmd->format = MPP_FMT_YUV420SP;
return 0;
}

View File

@@ -17,6 +17,7 @@
#define MODULE_TAG "utils"
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <string.h>
@@ -233,6 +234,16 @@ void dump_mpp_frame_to_file(MppFrame frame, FILE *fp)
mpp_free(tmp);
} break;
case MPP_FMT_RGB888: {
RK_U32 i;
RK_U8 *base_y = base;
RK_U8 *tmp = mpp_malloc(RK_U8, width * height * 3);
for (i = 0; i < height; i++, base_y += h_stride)
fwrite(base_y, 1, width * 3, fp);
mpp_free(tmp);
} break;
default : {
mpp_err("not supported format %d\n", fmt);
} break;
@@ -1511,3 +1522,54 @@ MPP_RET fps_calc_inc(FpsCalc ctx)
return MPP_OK;
}
/*
* @brief convert a string that discribes decimal, octal and hexadecimal
* number to a long integer.
* @param[in] nptr - a string to convert to long integer
* @param[out] number - long integer converted from a string
*/
MPP_RET str_to_frm_fmt(const char *nptr, long *number)
{
MPP_RET ret = MPP_NOK;
if (NULL == nptr || NULL == number) {
mpp_err_f("invalid input nptr %p number %p is_valid %p\n", nptr, number);
ret = MPP_ERR_NULL_PTR;
goto RET;
}
if (nptr) {
char *endptr = NULL; /* pointer to additional chars */
long tmp = 0;
/* reset errno to 0 before call */
errno = 0;
tmp = strtol(nptr, &endptr, 0);
if (nptr == endptr)
mpp_err("format: 0x%lx invalid (no digits found, 0 returned)", tmp);
else if (errno == ERANGE && tmp == LONG_MIN)
mpp_err("format: 0x%lx invalid (underflow occurred)", tmp);
else if (errno == ERANGE && tmp == LONG_MAX)
mpp_err("format: 0x%lx invalid (overflow occurred)", tmp);
else if (errno == EINVAL) /* not in all c99 implementations - gcc OK */
mpp_err("format: 0x%lx invalid (base contains unsupported value)", tmp);
else if (errno != 0 && tmp == 0)
mpp_err("format: 0x%lx invalid (unspecified error occurred)", tmp);
else if (errno == 0 && nptr && *endptr != 0)
mpp_err("format: 0x%lx invalid (additional characters remain)", tmp);
else if (errno == 0 && nptr && !*endptr) {
if (tmp < UINT_MAX && tmp >= 0) {
*number = tmp;
ret = MPP_OK;
} else {
mpp_err("format: 0x%lx invalid (not format value)", tmp);
}
}
}
RET:
return ret;
}

View File

@@ -92,6 +92,8 @@ MPP_RET fps_calc_deinit(FpsCalc ctx);
MPP_RET fps_calc_set_cb(FpsCalc ctx, FpsCalcCb cb);
MPP_RET fps_calc_inc(FpsCalc ctx);
MPP_RET str_to_frm_fmt(const char *nptr, long *number);
#ifdef __cplusplus
}
#endif