Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix ffmpeg parser error, fix alloc error #74

Merged
merged 2 commits into from
May 30, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 41 additions & 10 deletions package/encode_app/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ MainContext Ctx;
MainContext *pCtx=&Ctx;

static unsigned int alloc_memory(int fd_share_memory, unsigned int size);
static void get_yuv(MainContext *pCtx, char *infilename, int channel);
static int get_yuv(MainContext *pCtx, char *infilename, int channel);
static void write_output(MainContext *pCtx, FILE *out_file, EncOutputStream *src);
static unsigned long int get_time();
static void *isp_output(void *arg);
Expand All @@ -226,7 +226,7 @@ static void endof_encode();
static void exit_handler(int sig);
static void set_QoS();
#if TEST_ISP
static void init_isp();
static int init_isp();
#endif
int init_v4l2();
int init_audio();
Expand All @@ -253,7 +253,7 @@ static unsigned int alloc_memory(int fd_share_memory, unsigned int size)
return allocAlignMem.phyAddr;
}

static void get_yuv(MainContext *pCtx, char *infilename, int channel)
static int get_yuv(MainContext *pCtx, char *infilename, int channel)
{
FILE *fp;
char ch;
Expand All @@ -266,7 +266,7 @@ static void get_yuv(MainContext *pCtx, char *infilename, int channel)

if( (fp=fopen(infilename,"r+b")) == NULL ){
printf("Cannot open yuv file!\n");
return;
return -1;
}

fseek(fp,0L,SEEK_END);
Expand All @@ -284,6 +284,11 @@ static void get_yuv(MainContext *pCtx, char *infilename, int channel)
size = (size + 0xfff) & (~0xfff);

phyAddr = alloc_memory(pCtx->fd_share_memory, size);
if(phyAddr == -1)
{
printf("Not enough memory for input data.\n");
return -1;
}

map_src_pic = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, pCtx->fd_ddr, (uint64_t)phyAddr|0x100000000);

Expand Down Expand Up @@ -311,7 +316,7 @@ static void get_yuv(MainContext *pCtx, char *infilename, int channel)
pCtx->yuv_vAddr[channel] = map_src_pic;
pCtx->yuv_size[channel] = size;
printf("%s>yuv paddr = 0x%x, vaddr 0x%x, yuv_size %d, filesize %d\n", __FUNCTION__, pCtx->yuv_phyAddr, pCtx->yuv_vAddr, pCtx->yuv_size, filesize);
return;
return 0;
}

static void write_output(MainContext *pCtx, FILE *out_file, EncOutputStream *src)
Expand Down Expand Up @@ -1035,13 +1040,18 @@ static void set_QoS()
}

#if TEST_ISP
static void init_isp()
static int init_isp()
{
int src_index=0;

#ifdef ISP_OUTPUT_DUMP
pCtx->yuv_size = frame_size*pCtx->input_frames;
pCtx->yuv_phyAddr = alloc_memory(pCtx->fd_share_memory, pCtx->yuv_size);
if(pCtx->yuv_phyAddr)
{
printf("Not enough memory for isp init.\n");
return -1;
}
pCtx->yuv_vAddr = mmap(NULL, pCtx->yuv_size, PROT_READ|PROT_WRITE, MAP_SHARED, pCtx->fd_ddr, pCtx->yuv_phyAddr);
printf("%s>yuv_vAddr 0x%x, yuv_phyAddr 0x%x, yuv_size %d\n", __FUNCTION__, pCtx->yuv_vAddr, pCtx->yuv_phyAddr, pCtx->yuv_size);
#endif
Expand Down Expand Up @@ -1072,7 +1082,11 @@ static void init_isp()

pCtx->isp_buf_size[0] =(1920 *1080 *3 /2) * ISP_ADDR_BUFFER_CNT;// frame_size * 22;
ds1_info.y_addr = alloc_memory(pCtx->fd_share_memory, pCtx->isp_buf_size[0]);

if(ds1_info.y_addr == -1)
{
printf("Not enough memory for ds1 info.\n");
return -1;
}
pCtx->isp_buf_vaddr[0] = mmap(NULL, pCtx->isp_buf_size[0], PROT_READ|PROT_WRITE, MAP_SHARED, pCtx->fd_ddr, ds1_info.y_addr);
pCtx->isp_buf_paddr[0] = ds1_info.y_addr;
printf("%s>isp_buf_paddr 0x%x, isp_buf_vaddr 0x%x, isp_buf_size %d\n", __FUNCTION__, ds1_info.y_addr, pCtx->isp_buf_vaddr, pCtx->isp_buf_size);
Expand Down Expand Up @@ -1247,6 +1261,11 @@ int init_v4l2()

pCtx->v4l2_buf[j][i].length = (buf.length + 0xfff) & (~0xfff);
pCtx->v4l2_buf[j][i].paddr = alloc_memory(pCtx->fd_share_memory, pCtx->v4l2_buf[j][i].length);
if(pCtx->v4l2_buf[j][i].paddr == -1)
{
printf("Not enough memory for v4l2 buf.\n");
return -1;
}
pCtx->v4l2_buf[j][i].vaddr = mmap(NULL, pCtx->v4l2_buf[j][i].length, PROT_READ|PROT_WRITE, MAP_SHARED, pCtx->fd_ddr, pCtx->v4l2_buf[j][i].paddr);

if(pCtx->v4l2_buf[j][i].vaddr == MAP_FAILED)
Expand Down Expand Up @@ -2307,7 +2326,11 @@ int main(int argc, char *argv[])
{
if(pCtx->ch_en[i] && (pCtx->enable_isp[i] == 0) && (pCtx->enable_v4l2[i] == 0))
{
get_yuv(pCtx, pCtx->infilename[i], i);
if(get_yuv(pCtx, pCtx->infilename[i], i) < 0)
{
endof_encode();
return -1;
}
}
}
}
Expand All @@ -2316,7 +2339,11 @@ int main(int argc, char *argv[])
if(pCtx->isp_enabled == 1)
{
set_QoS();
init_isp();
if(init_isp() < 0)
{
endof_encode();
return -1;
}
}
#endif
if(pCtx->v4l2_enabled == 1)
Expand All @@ -2327,7 +2354,11 @@ int main(int argc, char *argv[])
printf("parse_conf error\n");
return -1;
}
init_v4l2();
if(init_v4l2() < 0)
{
endof_encode();
return -1;
}
}

if(pCtx->audio_enabled == 1)
Expand Down
99 changes: 99 additions & 0 deletions package/ffmpeg_canaan/0023-buildroot-ffmpeg.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
Index: b/libavdevice/v4l2.c
===================================================================
--- a/libavdevice/v4l2.c
+++ b/libavdevice/v4l2.c
@@ -108,8 +108,8 @@ struct video_data {
int fd_ddr;
int fd_share_memory;
int pts;
- int repeat_index;
- unsigned int buf_bytesused;
+ int repeat_index;
+ unsigned int buf_bytesused;
unsigned int reg_QoS_ctrl0;
unsigned int reg_QoS_ctrl1;
unsigned int reg_QoS_ctrl2;
@@ -176,7 +176,12 @@ int parse_conf(char* dev_name, char* fil
cJSON *newitem;
cJSON *object;
cJSON *sensor0;
+ cJSON *sensor1;
cJSON *video;
+
+ char video_used_name[20];
+ char video_width_name[20];
+ char video_height_name[20];
int i = 0;

fp=fopen(filename,"rb");
@@ -203,44 +208,39 @@ int parse_conf(char* dev_name, char* fil
out=cJSON_Print(root);

sensor0 = cJSON_GetObjectItem(root,"sensor0");
- if(sensor0 != NULL)
+ sensor1 = cJSON_GetObjectItem(root,"sensor1");
+ if(sensor0 != NULL && sensor1 != NULL)
{
- // int size=cJSON_GetArraySize(arrayItem);
- if(strcmp(dev_name, "/dev/video3") == 0)
+ snprintf(video_used_name, sizeof(video_used_name), "video%c_used", dev_name[10]);
+ snprintf(video_width_name, sizeof(video_width_name), "video%c_width", dev_name[10]);
+ snprintf(video_height_name, sizeof(video_height_name), "video%c_height", dev_name[10]);
+ printf("dev_name: %s\n", dev_name);
+ printf("video_used_name: %s\n", video_used_name);
+ printf("video_width_name: %s\n", video_width_name);
+ printf("video_height_name: %s\n", video_height_name);
+
+ if((dev_name[10] >= '2') && (dev_name[10] <= '5'))
{
- video = cJSON_GetObjectItem(sensor0,"/dev/video3");
- item = cJSON_GetObjectItem(video, "video3_used");
- cJSON_SetIntValue(item, 1);
-
- item = cJSON_GetObjectItem(video, "video3_width");
- cJSON_SetIntValue(item, width);
-
- item = cJSON_GetObjectItem(video, "video3_height");
- cJSON_SetIntValue(item, height);
+ video = cJSON_GetObjectItem(sensor0,dev_name);
}
- else if(strcmp(dev_name, "/dev/video5") == 0)
+ else if((dev_name[10] >= '6') && (dev_name[10] <= '9'))
{
- video = cJSON_GetObjectItem(sensor0,"/dev/video5");
- item = cJSON_GetObjectItem(video, "video5_used");
- cJSON_SetIntValue(item, 1);
-
- item = cJSON_GetObjectItem(video, "video5_width");
- cJSON_SetIntValue(item, width);
-
- item = cJSON_GetObjectItem(video, "video5_height");
- cJSON_SetIntValue(item, height);
+ video = cJSON_GetObjectItem(sensor1,dev_name);
}
- else if(strcmp(dev_name, "/dev/video9") == 0)
+ else
{
- video = cJSON_GetObjectItem(sensor0,"/dev/video9");
- item = cJSON_GetObjectItem(video, "video9_used");
- cJSON_SetIntValue(item, 1);
- item = cJSON_GetObjectItem(video, "video9_width");
- cJSON_SetIntValue(item, width);
- item = cJSON_GetObjectItem(video, "video9_height");
- cJSON_SetIntValue(item, height);
- }
+ printf("dev name error!\n");
+ return -1;
+ }
+
+ item = cJSON_GetObjectItem(video, video_used_name);
+ cJSON_SetIntValue(item, 1);
+
+ item = cJSON_GetObjectItem(video, video_width_name);
+ cJSON_SetIntValue(item, width);

+ item = cJSON_GetObjectItem(video, video_height_name);
+ cJSON_SetIntValue(item, height);
}
}

28 changes: 28 additions & 0 deletions package/patches/linux/0007-share-memory-align-alloc.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
From a45bbd422e05488a4818106a8f59d6046388b899 Mon Sep 17 00:00:00 2001
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为什么要改kernal? 没有memory不会返回错误?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

之前kernel的share memory align alloc的kernel如果memory不够的话,也不会返回错误,会返回上一次alloc的出来的物理地址,但是又没有真的alloc,所以就会出错。

From: cuiyan <cuiyan@canaan-creative.com>
Date: Sat, 28 May 2022 11:51:36 +0800
Subject: [PATCH] cuiyan

---
drivers/memory/canaan/k510_share_memory.c | 5 +++++
1 file changed, 5 insertions(+)

diff --git a/drivers/memory/canaan/k510_share_memory.c b/drivers/memory/canaan/k510_share_memory.c
index e357c56a..561ce639 100755
--- a/drivers/memory/canaan/k510_share_memory.c
+++ b/drivers/memory/canaan/k510_share_memory.c
@@ -625,6 +625,11 @@ static long share_memory_align_alloc(unsigned long arg)
#endif

alloc_args.phyAddr = BlockPhysical;
+ if(pBlockVirtual == NULL)
+ {
+ copy_to_user((void *)arg, (void *)&alloc_args, sizeof(struct share_memory_alloc_align_args));
+ return -EFAULT;
+ }
if(copy_to_user((void *)arg, (void *)&alloc_args, sizeof(struct share_memory_alloc_align_args)))
return -EFAULT;

--
2.36.1