有这样一份代码,在主机和从机上都能运行,代码用条件编译的方式,读起来很恶心,简直是扰乱思绪,后来我把代码给给开了,果然好读多了。
void
test_host_to_tile(int fd, void* buffer)
{
PRINTF("Sending host-to-TLR :\n");
#ifdef __tile__
printf("Tile PCIE driver will receive YUV files from host .....\n");
while(1)
{
for (int j = 0; j < (FRAME_RATE_YUV *2); j++)
{
do_read(fd, yuv_rcv[j], FRAME_SIZE_YUV);
//j %= (FRAME_RATE_YUV *2);
//Bruce: we should add some sync mechanism for Encoding Tiles, make Encoding Tile to take the data to encode.
//should adopt Medialib methods
//TODO:
#if 0
int* data_temp=(int *)yuv_rcv[j];
printf("received data is : ");
for(int x=0;x <20; x++)
{
printf(" 0x%x ",*data_temp++);
}
#endif
}
}
#else
typedef struct Frame
{
int width;
int height;
int linesize[3];
}pcidata_t;
//while(1)
//{
pcidata_t pcidata;
memset(&pcidata, 0, sizeof(pcidata_t));
pcidata.width = 1280;
pcidata.height = 720;
pcidata.linesize[0] = 1280;
pcidata.linesize[1] = 640;
pcidata.linesize[2] = 640;
for (int i = 0; i < SHOW_TIME; i++)
{
#ifdef PRINTING
struct timeval before, after;
gettimeofday(&before, NULL);
#endif
for (int j = 0; j < FRAME_RATE_YUV; j++)
{
// do_write(fd, &pcidata, sizeof(pcidata_t));
printf("sending #%d (%d bytes@%p)...", j, FRAME_SIZE_YUV, yuv_data[i][j]);
do_write(fd, yuv_data[i][j], FRAME_SIZE_YUV);
printf("done\n");
//delay 30 ms,
usleep(1000*30);
}
//i %= SHOW_TIME;
printf("send 25 frames \n");
#ifdef PRINTING
gettimeofday(&after, NULL);
double secs = after.tv_sec - before.tv_sec;
double usecs = after.tv_usec - before.tv_usec;
double time = secs + usecs * 1e-6;
PRINTF("%f seconds for %d bytes, %f Mb/s\n",
time, TEST_SIZE, (TEST_SIZE * 8) / time / 1e6);
#endif
}
//}
#endif
}
void
test_tile_to_host(int fd, void* buffer)
{
PRINTF("bandwidth TLR-to-host test:\n");
for (int i = 0; i < ITERATIONS; i++)
{
#ifdef PRINTING
struct timeval before, after;
gettimeofday(&before, NULL);
#endif
for (int j = 0; j < (FRAME_RATE_YUV *4); j++)
{
#ifdef __tile__
do_write(fd, buffer, BUFFER_SIZE);
#else
do_read(fd, yuv_host[j], FRAME_SIZE_YUV);
#endif
}
#ifdef PRINTING
gettimeofday(&after, NULL);
double secs = after.tv_sec - before.tv_sec;
double usecs = after.tv_usec - before.tv_usec;
double time = secs + usecs * 1e-6;
PRINTF("%f seconds for %d bytes, %f Mb/s\n",
time, TEST_SIZE, (TEST_SIZE * 8) / time / 1e6);
#endif
}
}
int
main()
{
int fd = open_channel(2);
#ifdef __tile__
//added by Bruce, malloc buffers to store all the frames in 4 seconds
int j;
for (j=0; j< (FRAME_RATE_YUV*2); j++)
{
yuv_rcv[j] = malloc(FRAME_SIZE_YUV);
if (yuv_rcv[j] == NULL)
{
fprintf(stderr, "Failed to allocate buffer.\n");
return 1;
}
memset(yuv_rcv[j], 0, FRAME_SIZE_YUV);
}
printf("Tilera chip allocate all the buffer Ok. \n");
//Bruce: for Tile, we should create other Encoding threads before reading YUV data into buffers
//TODO:
#else
int i,j;
int header =0;
char * yuv_filename = "./outyuvfile.yuv";
//char * yuv_filename = "/opt/same_JM_1080p25_4Mbps.yuv";
//char * output_name = "out.264";
printf("began to allocate buffers \n");
//added by Bruce, malloc buffers to store all the frames in 4 seconds
for (i=0; i< SHOW_TIME; i++)
{
for (j=0; j< FRAME_RATE_YUV; j++)
{
yuv_data[i][j] = malloc(FRAME_SIZE_YUV);
if (yuv_data[i][j] == NULL)
{
fprintf(stderr, "Failed to allocate buffer.\n");
return 1;
}
memset(yuv_data[i][j], 0x0, FRAME_SIZE_YUV);
}
}
printf("X86 Driver allocate all the buffers OK \n");
//read YUV file into buffers
FILE *f=fopen(yuv_filename, "rb");
fseek(f, header,SEEK_SET);
for (i=0; i< SHOW_TIME; i++)
{
for (j=0; j< FRAME_RATE_YUV; j++)
{
int ret= fread(yuv_data[i][j], 1,FRAME_SIZE_YUV,f);
if (ret != FRAME_SIZE_YUV)
{
fprintf(stderr, "Failed to read frames i %d: j %d .\n",i,j);
return 1;
}
}
}
fclose(f);
for (j=0; j< (FRAME_RATE_YUV*2); j++)
{
yuv_host[j] = malloc(FRAME_SIZE_YUV);
if (yuv_host[j] == NULL)
{
fprintf(stderr, "Failed to allocate buffer.\n");
return 1;
}
memset(yuv_host[j], 0, FRAME_SIZE_YUV);
}
printf("Host chip allocate all the buffer Ok. \n");
#endif
#ifdef __tile__
test_host_to_tile(fd,yuv_rcv);
#else
test_host_to_tile(fd, yuv_data);
#endif
// test_tile_to_host(fd, yuv_data);
PRINTF("Bandwidth test complete.\n\n");
close(fd);
return 0;
}
分开后,得到从机上的代码是:
void
test_host_to_tile(int fd, void* buffer)
{
PRINTF("Sending host-to-TLR :\n");
printf("Tile PCIE driver will receive YUV files from host .....\n");
while(1)
{
for (int j = 0; j < (FRAME_RATE_YUV *2); j++)
{
do_read(fd, yuv_rcv[j], FRAME_SIZE_YUV);
//j %= (FRAME_RATE_YUV *2);
//Bruce: we should add some sync mechanism for Encoding Tiles, make Encoding Tile to take the data to encode.
//should adopt Medialib methods
//TODO:
}
}
}
从机test_tile_to_host函数是:
void
test_tile_to_host(int fd, void* buffer)
{
PRINTF("bandwidth TLR-to-host test:\n");
for (int i = 0; i < ITERATIONS; i++)
{
for (int j = 0; j < (FRAME_RATE_YUV *4); j++)
{
do_write(fd, buffer, BUFFER_SIZE);
}
}
}
从机main函数是:
int main() { int fd = open_channel(2); //added by Bruce, malloc buffers to store all the frames in 4 seconds int j; for (j=0; j< (FRAME_RATE_YUV*2); j++) { yuv_rcv[j] = malloc(FRAME_SIZE_YUV); if (yuv_rcv[j] == NULL) { fprintf(stderr, "Failed to allocate buffer.\n"); return 1; } memset(yuv_rcv[j], 0, FRAME_SIZE_YUV); } printf("Tilera chip allocate all the buffer Ok. \n"); //Bruce: for Tile, we should create other Encoding threads before reading YUV data into buffers //TODO: test_host_to_tile(fd,yuv_rcv); // test_tile_to_host(fd, yuv_data); PRINTF("Bandwidth test complete.\n\n"); close(fd); return 0; }
而主机中,三个函数依次为:
host_to_tile
void
test_host_to_tile(int fd, void* buffer)
{
PRINTF("Sending host-to-TLR :\n");
typedef struct Frame
{
int width;
int height;
int linesize[3];
}pcidata_t;
//while(1)
//{
pcidata_t pcidata;
memset(&pcidata, 0, sizeof(pcidata_t));
pcidata.width = 1280;
pcidata.height = 720;
pcidata.linesize[0] = 1280;
pcidata.linesize[1] = 640;
pcidata.linesize[2] = 640;
for (int i = 0; i < SHOW_TIME; i++)
{
struct timeval before, after;
gettimeofday(&before, NULL);
for (int j = 0; j < FRAME_RATE_YUV; j++)
{
// do_write(fd, &pcidata, sizeof(pcidata_t));
printf("sending #%d (%d bytes@%p)...", j, FRAME_SIZE_YUV, yuv_data[i][j]);
do_write(fd, yuv_data[i][j], FRAME_SIZE_YUV);
printf("done\n");
//delay 30 ms,
usleep(1000*30);
}
//i %= SHOW_TIME;
printf("send 25 frames \n");
gettimeofday(&after, NULL);
double secs = after.tv_sec - before.tv_sec;
double usecs = after.tv_usec - before.tv_usec;
double time = secs + usecs * 1e-6;
PRINTF("%f seconds for %d bytes, %f Mb/s\n",
time, TEST_SIZE, (TEST_SIZE * 8) / time / 1e6);
}
//}
}
而tile_to_host代码为
void
test_tile_to_host(int fd, void* buffer)
{
PRINTF("bandwidth TLR-to-host test:\n");
for (int i = 0; i < ITERATIONS; i++)
{
struct timeval before, after;
gettimeofday(&before, NULL);
for (int j = 0; j < (FRAME_RATE_YUV *4); j++)
{
do_read(fd, yuv_host[j], FRAME_SIZE_YUV);
}
gettimeofday(&after, NULL);
double secs = after.tv_sec - before.tv_sec;
double usecs = after.tv_usec - before.tv_usec;
double time = secs + usecs * 1e-6;
PRINTF("%f seconds for %d bytes, %f Mb/s\n",
time, TEST_SIZE, (TEST_SIZE * 8) / time / 1e6);
}
}
而main函数为:
int main() { int fd = open_channel(2); int i,j; int header =0; char * yuv_filename = "./outyuvfile.yuv"; //char * yuv_filename = "/opt/same_JM_1080p25_4Mbps.yuv"; //char * output_name = "out.264"; printf("began to allocate buffers \n"); //added by Bruce, malloc buffers to store all the frames in 4 seconds for (i=0; i< SHOW_TIME; i++) { for (j=0; j< FRAME_RATE_YUV; j++) { yuv_data[i][j] = malloc(FRAME_SIZE_YUV); if (yuv_data[i][j] == NULL) { fprintf(stderr, "Failed to allocate buffer.\n"); return 1; } memset(yuv_data[i][j], 0x0, FRAME_SIZE_YUV); } } printf("X86 Driver allocate all the buffers OK \n"); //read YUV file into buffers FILE *f=fopen(yuv_filename, "rb"); fseek(f, header,SEEK_SET); for (i=0; i< SHOW_TIME; i++) { for (j=0; j< FRAME_RATE_YUV; j++) { int ret= fread(yuv_data[i][j], 1,FRAME_SIZE_YUV,f); if (ret != FRAME_SIZE_YUV) { fprintf(stderr, "Failed to read frames i %d: j %d .\n",i,j); return 1; } } } fclose(f); for (j=0; j< (FRAME_RATE_YUV*2); j++) { yuv_host[j] = malloc(FRAME_SIZE_YUV); if (yuv_host[j] == NULL) { fprintf(stderr, "Failed to allocate buffer.\n"); return 1; } memset(yuv_host[j], 0, FRAME_SIZE_YUV); } printf("Host chip allocate all the buffer Ok. \n"); #endif test_host_to_tile(fd, yuv_data); // test_tile_to_host(fd, yuv_data); PRINTF("Bandwidth test complete.\n\n"); close(fd); return 0; }
转载于:https://blog.51cto.com/likeyiyy/1266911