加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
main.c 4.90 KB
一键复制 编辑 原始数据 按行查看 历史
ReviewClouds 提交于 2020-11-27 18:37 . init
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "./uvc/uvc_control.h"
#include "./uvc/uvc_video.h"
#include "./uvc/mpi_enc.h"
#include "./uvc/drm.h"
#include "./uvc/yuv.h"
#if 1
typedef void (*callback_for_uvc)(const void *buffer, size_t size,
void* extra_data, size_t extra_size);
callback_for_uvc cb_for_uvc = NULL;
void register_callback_for_uvc(callback_for_uvc cb)
{
cb_for_uvc = cb;
}
int main(int argc, char* argv[])
{
char *buffer;
size_t size;
int i = 0;
int width, height;
int y, uv;
int extra_cnt = 0;
size = 640 * 480 * 3 / 2;
buffer = (char*)malloc(size);
if (!buffer) {
printf("buffer alloc fail.\n");
return -1;
}
y = width * height / 4;
memset(buffer, 128, y);
memset(buffer + y, 64, y);
memset(buffer + y * 2, 128, y);
memset(buffer + y * 3, 192, y);
uv = width * height / 8;
memset(buffer + y * 4, 0, uv);
memset(buffer + y * 4 + uv, 64, uv);
memset(buffer + y * 4 + uv * 2, 128, uv);
memset(buffer + y * 4 + uv * 3, 192, uv);
register_callback_for_uvc(uvc_read_camera_buffer);
uvc_control_run();
while(1) {
extra_cnt++;
if (cb_for_uvc)
cb_for_uvc(buffer, size, &extra_cnt, sizeof(extra_cnt));
usleep(30000);
}
uvc_control_join();
return 0;
}
#else
int Convert(char *input, char *output)
{
int h, v;
unsigned char *buff_in;
unsigned char *buff_out;
FILE *in = fopen(input, "rw+");
if (in == NULL)
{
printf("open file %s failed\n", input);
return -1;
}
FILE *out = fopen(output, "w+");
if (in == NULL)
{
printf("open file %s failed\n", output);
return -1;
}
buff_in = (unsigned char *)malloc(1824 * 944 * 1.5);
buff_out = (unsigned char *)malloc(1824 * 944 * 3);
fread(buff_in, 1824 * 944 * 1.5, 1, in);
for (v = 0; v < 472; v++)
{
for (h = 0; h < 912; h++)
{
buff_out[v * 2 * 1824 * 3 + 3 * 2 * h + 1] = buff_in[1824 * 944 + v * 1824 + 2 * h];
buff_out[v * 2 * 1824 * 3 + 3 * 2 * h + 3 + 1] = buff_in[1824 * 944 + v * 1824 + 2 * h];
buff_out[1824 * 3 + v * 2 * 1824 * 3 + 3 * 2 * h + 1] = buff_in[1824 * 944 + v * 1824 + 2 * h];
buff_out[1824 * 3 + v * 2 * 1824 * 3 + 3 * 2 * h + 3 + 1] = buff_in[1824 * 944 + v * 1824 + 2 * h];
buff_out[v * 2 * 1824 * 3 + 3 * 2 * h + 2] = buff_in[1824 * 944 + v * 1824 + 2 * h + 1];
buff_out[v * 2 * 1824 * 3 + 3 * 2 * h + 3 + 2] = buff_in[1824 * 944 + v * 1824 + 2 * h + 1];
buff_out[1824 * 3 + v * 2 * 1824 * 3 + 3 * 2 * h + 2] = buff_in[1824 * 944 + v * 1824 + 2 * h + 1];
buff_out[1824 * 3 + v * 2 * 1824 * 3 + 3 * 2 * h + 3 + 2] = buff_in[1824 * 944 + v * 1824 + 2 * h + 1];
}
}
for (v = 0; v < 944; v++)
{
for (h = 0; h < 1824; h++)
{
buff_out[v * 1824 * 3 + 3 * h] = buff_in[v * 1824 + h];
}
}
fwrite(buff_out, 1824 * 944, 3, out);
fclose(in);
fclose(out);
free(buff_in);
free(buff_out);
return 0;
}
int main(int argc, char **argv)
{
int fd;
FILE *fp;
int ret;
unsigned int handle;
char *buffer;
char *output;
int handle_fd;
size_t size;
int i = 0;
int width, height;
int y, uv;
int extra_cnt = 0;
uint32_t flags = 0;
char name[] = "/usr/bin/test_image.nv12";
width = 640;
height = 480;
#if 0
fd = drm_open();
if (fd < 0)
return -1;
size = width * height * 3 / 2;
ret = drm_alloc(fd, size, 16, &handle, 0);
if (ret)
return -1;
ret = drm_handle_to_fd(fd, handle, &handle_fd, 0);
if (ret)
return -1;
buffer = (char *)drm_map_buffer(fd, handle, size);
if (!buffer)
{
printf("drm map buffer fail.\n");
return -1;
}
y = width * height / 4;
memset(buffer, 128, y);
memset(buffer + y, 64, y);
memset(buffer + y * 2, 128, y);
memset(buffer + y * 3, 192, y);
uv = width * height / 8;
memset(buffer + y * 4, 0, uv);
memset(buffer + y * 4 + uv, 64, uv);
memset(buffer + y * 4 + uv * 2, 128, uv);
memset(buffer + y * 4 + uv * 3, 192, uv);
#endif
size = 640 * 480 * 3 / 2;
buffer = malloc(size);
if (!buffer)
{
printf("buffer alloc fail.\n");
return -1;
}
fp = fopen(name, "rb");
if (!fp)
{
printf("open %s fail.\n", name);
memset(buffer, 0, size);
}
else
{
fread(buffer, 1, size, fp);
fclose(fp);
}
NV12_to_YUYV(640, 480, buffer, output);
uvc_control_run();
while (1)
{
extra_cnt++;
uvc_read_camera_buffer(output, handle_fd, size, &extra_cnt, sizeof(extra_cnt));
usleep(30000);
}
uvc_control_join();
drm_unmap_buffer(buffer, size);
drm_free(fd, handle);
drm_close(fd);
return 0;
}
#endif
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化