广东的网站建设,网站维护中 源码,在网站加上一个模块怎么做,成都营销策划公司排行榜1. 前言
限于作者能力水平#xff0c;本文可能存在谬误#xff0c;因此而给读者带来的损失#xff0c;作者不做任何承诺。
2. 概述
VPU 是用来进行图像、视频数据进行硬件编、解码的硬件模块。内部集成了 Encoder、Decoder 功能部件进行图像、视频数据进行硬件编、解码本文可能存在谬误因此而给读者带来的损失作者不做任何承诺。
2. 概述
VPU 是用来进行图像、视频数据进行硬件编、解码的硬件模块。内部集成了 Encoder、Decoder 功能部件进行图像、视频数据进行硬件编、解码以加速处理。
3. VPU 工作原理
3.1 VPU 编码工作流程 ---------------| --------- |
输入数据 --|-| Encoder |-|- 编码后的输出数据| --------- || || --------- || | Decoder | || --------- |---------------3.2 VPU解码工作流程 ---------------| --------- || | Encoder | || --------- || || --------- |输入数据 --|-| Decoder |-|- 解码后的输出数据| --------- |---------------4. Linux 下的 VPU
4.1 驱动架构
VPU驱动 可基于 V4L2子系统 框架完成。 1. 分别为 Encoder 和 Decoder 各注册1个 /dev/videoX 设备(总共2个video设备)。
/* 注册 Encoder 设备 */
vfd-vfl_dir VFL_DIR_M2M;
video_register_device(vfd, VFL_TYPE_GRABBER, ...)/* 注册 Decoder 设备 */
vfd-vfl_dir VFL_DIR_M2M;
video_register_device(vfd, VFL_TYPE_GRABBER, ...)设备数据传输方向为 VFL_DIR_M2M , 表明设备是设备完成的功能内存间的数据传输拷贝。 2. 在 open() 调用中在打开文件句柄的私有数据 file_private 绑定设备 buffer 队列(vb2_queue)的类型、接口、IO模式、数据传输方向等。 这里以 Encoder 的 open() 调用为例加以说明:
/* Encoder【输入】数据队列初始化 */
encoder_vq_input.type V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
encoder_vq_input.io_modes VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
encoder_vq_input.ops xxx_vpu_encoder_qops;
encoder_vq_input.mem_ops vb2_dma_contig_memops;
...
vb2_queue_init(encoder_vq_input);/* Encoder【输出】数据队列初始化 */
encoder_vq_output.type V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
encoder_vq_output.io_modes VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
encoder_vq_output.ops xxx_vpu_encoder_qops;
encoder_vq_output.mem_ops vb2_dma_contig_memops;
...
vb2_queue_init(encoder_vq_output);...4.2 用户空间编程框架(Encoder编码示例)
/* 打开设备(/dev/videoX为Encoder设备) */
fd open(/dev/videoX, O_RDWR);/* 设置输入、输出数据格式 *//* 设置编码【输入】数据格式 */
fmt.type V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
...
ioctl(fd, VIDIOC_S_FMT, fmt);
/* 设置编码【输出】数据格式 */
fmt.type V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
...
ioctl(fd, VIDIOC_S_FMT, fmt);/* 请求输入、输出buffer然后映射内核buffer到用户空间(IO模式为 V4l2_MEMORY_MMAP) *//* 请求【输入】buffer并映射到用户空间 */
rb.type V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
rb.memory V4l2_MEMORY_MMAP;
rb.count 1;
ioctl(fd, VIDIOC_REQBUFS, rb);buf.index i;
buf.type V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
buf.memory V4l2_MEMORY_MMAP;
buf.length num_input_planes;
buf.m.planes input_planes;
ioctl(fd, VIDIOC_QUERYBUF, buf);input_buffer.start mmap(0, ..., PROT_READ|PROT_WRITE, ...);
input_buffer.length ...;/* 请求【输出】buffer并映射到用户空间 */
rb.type V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
rb.memory V4l2_MEMORY_MMAP;
rb.count 1;
ioctl(fd, VIDIOC_REQBUFS, rb);buf.index i;
buf.type V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
buf.memory V4l2_MEMORY_MMAP;
buf.length num_output_planes;
buf.m.planes output_planes;
ioctl(fd, VIDIOC_QUERYBUF, buf);output_buffer.start mmap(0, ..., PROT_READ|PROT_WRITE, ...);
output_buffer.length ...;/* 将【输出】buffer入队然后开启【输出流】 */
buf.index i;
buf.type V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
buf.memory V4l2_MEMORY_MMAP;
buf.length num_output_planes;
buf.m.planes output_planes;
output_planes[i].bytesused output_planes[i].length;
ioctl(fd, VIDIOC_QBUF, buf);type V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
ioctl(fd, VIDIOC_STREAMON, type);/* 设置编码输入数据将【输入】buffer入队然后开启【输入流】 */
/* 设置编码输入数据 */
memcpy(input_buffer.start, input_data, input_data_size);buf.index i;
buf.type V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
buf.memory V4l2_MEMORY_MMAP;
buf.length num_input_planes;
buf.m.planes input_planes;
input_planes[i].bytesused input_planes[i].length;
ioctl(fd, VIDIOC_QBUF, buf);type V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
ioctl(fd, VIDIOC_STREAMON, type);/* 出队编码队列(vb2_queue)中就绪的【输出缓冲】 */
(vb2_buffer/v4l2_buffer, vb2_plane/v4l2_plane)
buf.type V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
buf.memory V4L2_MEM_TYPE;
buf.length num_output_planes;
buf.m.planes output_planes;
ioctl(fd, VIDIOC_DQBUF, buf);/* 拷贝编码好的数据到目的缓冲(假定 output plane 数目为1) */
memcpy(output_data, output_buffer.start, buf.m.planes[0].bytesused);/* 关闭设备 */
close(fd);4.3 VPU 驱动工作流程小结 VPU-----------------------------| ----------------------- || | Encoder | || | ----------------- | |---|-|-| encoding buffer |-|-|---^ | | ----------------- | | |输入数据队列(vb2_queue) | | ----------------------- | | 输出数据队列(vb2_queue)----------------------- | | | | -----------------------| vb2_buffer[] |--| | ----------------------- | |-- | vb2_buffer[] |----------------------- | | | Decoder | | | -----------------------v | | ----------------- | | |---|-|-| decoding buffer |-|-|---| | ----------------- | || ----------------------- |-----------------------------Encoder/Decoder完成编、解码动作后:
(1) 拷贝编、解码后的数据到输出队列中某个vb2_buffer的缓冲: memcpy(output_buffer, input_buffer, size);
(2) 标记输入数据队列中某个vb2_buffer中的数据编、解码完成: vb2_buffer_done(in_vb, VB2_BUF_STATE_DONE);
(3) 设置输出缓冲负载(输出数据大小): vb2_set_plane_payload(out_vb, 0, size);
(4) 标记输出数据队列中某个vb2_buffer中的数据编、解码输出数据就绪: vb2_buffer_done(out_vb, VB2_BUF_STATE_DONE);4.4 示例
这是一个实际的范例来自 FrienlyARM 的方案 NanoPC-T3 Plus 。该方案基于 S5P6818 的 SoC 。
4.4.1 FrienlyARM的方案内核NX VPU驱动补丁
官方自带的VPU驱动编解码的部分有些问题我对它做了如下修改
/** drivers/media/platform/nx-vpu/nx_vpu_enc_v4l2.c */
void vpu_enc_get_seq_info(struct nx_vpu_ctx *ctx)
{.../* 注释下面这一段代码 *//*{struct nx_vpu_buf *dst_mb;unsigned long flags;spin_lock_irqsave(ctx-dev-irqlock, flags);dst_mb list_entry(ctx-strm_queue.next, struct nx_vpu_buf,list);list_del(dst_mb-list);ctx-strm_queue_cnt--;vb2_set_plane_payload(dst_mb-vb, 0, ctx-strm_size);vb2_buffer_done(dst_mb-vb, VB2_BUF_STATE_DONE);spin_unlock_irqrestore(ctx-dev-irqlock, flags);}*/
}static void nx_vpu_enc_buf_queue(struct vb2_buffer *vb)
{...if (vq-type V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {...} else if (vq-type V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {buf-used 0;if (ctx-img_fmt.num_planes 1)NX_DbgMsg(INFO_MSG, adding to src: %p(%08lx)\n,vb, (unsigned long)nx_vpu_mem_plane_addr(ctx, vb, 0));else if (ctx-img_fmt.num_planes 2)NX_DbgMsg(INFO_MSG, adding to src: %p(%08lx, %08lx)\n,vb, (unsigned long)nx_vpu_mem_plane_addr(ctx, vb, 0),(unsigned long)nx_vpu_mem_plane_addr(ctx, vb, 1));else if (ctx-img_fmt.num_planes 3)NX_DbgMsg(INFO_MSG, adding to src: %p(%08lx, %08lx, %08lx)\n,vb, (unsigned long)nx_vpu_mem_plane_addr(ctx, vb, 0),(unsigned long)nx_vpu_mem_plane_addr(ctx, vb, 1),(unsigned long)nx_vpu_mem_plane_addr(ctx, vb, 2));}...
}int nx_vpu_enc_open(struct nx_vpu_ctx *ctx)
{...ctx-vq_img.io_modes VB2_MMAP | VB2_USERPTR | VB2_DMABUF;......ctx-vq_strm.io_modes VB2_MMAP | VB2_USERPTR | VB2_DMABUF;...
}/** drivers\media\platform\nx-vpu\nx_vpu_v4l2.c*/
#define DST_QUEUE_OFF_BASE (1 30)int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *buf)
{struct nx_vpu_ctx *ctx fh_to_ctx(file-private_data);int ret 0;FUNC_IN();...if (buf-type V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {...} else if (buf-type V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {...//buf-m.planes[0].m.mem_offset DST_QUEUE_OFF_BASE;/* Adjust MMAP memory offsets for the CAPTURE queue */if (buf-memory V4L2_MEMORY_MMAP /* !V4L2_TYPE_IS_OUTPUT(ctx-vq_img-type)*/) {if (V4L2_TYPE_IS_MULTIPLANAR(ctx-vq_img.type)) {int i;for (i 0; i buf-length; i)buf-m.planes[i].m.mem_offset DST_QUEUE_OFF_BASE;} else {buf-m.offset DST_QUEUE_OFF_BASE;}}} else {...}return ret;
}我为 S5P6818 的 VPU 编写了一个测试程序 nxvpu-yuv2jpg.c 该程序用于将 YUV420 或 GREY 格式数据转换为 MJEPG 格式数据实现代码见 S5P6818 VPU测试范例代码 。
5. 参考资料
https://wiki.friendlyelec.com/wiki/index.php/NanoPC-T3_Plus/zh
文章转载自: http://www.morning.wrcgy.cn.gov.cn.wrcgy.cn http://www.morning.zwgbz.cn.gov.cn.zwgbz.cn http://www.morning.ybshj.cn.gov.cn.ybshj.cn http://www.morning.gcfg.cn.gov.cn.gcfg.cn http://www.morning.drhnj.cn.gov.cn.drhnj.cn http://www.morning.ghryk.cn.gov.cn.ghryk.cn http://www.morning.rgnq.cn.gov.cn.rgnq.cn http://www.morning.hcbky.cn.gov.cn.hcbky.cn http://www.morning.gkgr.cn.gov.cn.gkgr.cn http://www.morning.wcqxj.cn.gov.cn.wcqxj.cn http://www.morning.lpgw.cn.gov.cn.lpgw.cn http://www.morning.dmtbs.cn.gov.cn.dmtbs.cn http://www.morning.zttjs.cn.gov.cn.zttjs.cn http://www.morning.xxrgt.cn.gov.cn.xxrgt.cn http://www.morning.bzjpn.cn.gov.cn.bzjpn.cn http://www.morning.pzcjq.cn.gov.cn.pzcjq.cn http://www.morning.cbynh.cn.gov.cn.cbynh.cn http://www.morning.hwlk.cn.gov.cn.hwlk.cn http://www.morning.nrpp.cn.gov.cn.nrpp.cn http://www.morning.wschl.cn.gov.cn.wschl.cn http://www.morning.rpjyl.cn.gov.cn.rpjyl.cn http://www.morning.tlpgp.cn.gov.cn.tlpgp.cn http://www.morning.zgdnd.cn.gov.cn.zgdnd.cn http://www.morning.wlqll.cn.gov.cn.wlqll.cn http://www.morning.hlfnh.cn.gov.cn.hlfnh.cn http://www.morning.rhwty.cn.gov.cn.rhwty.cn http://www.morning.zqbrw.cn.gov.cn.zqbrw.cn http://www.morning.kdfqx.cn.gov.cn.kdfqx.cn http://www.morning.qfcnp.cn.gov.cn.qfcnp.cn http://www.morning.pkrtz.cn.gov.cn.pkrtz.cn http://www.morning.btlsb.cn.gov.cn.btlsb.cn http://www.morning.pslzp.cn.gov.cn.pslzp.cn http://www.morning.wdhhz.cn.gov.cn.wdhhz.cn http://www.morning.mnsmb.cn.gov.cn.mnsmb.cn http://www.morning.qwrb.cn.gov.cn.qwrb.cn http://www.morning.ykmkz.cn.gov.cn.ykmkz.cn http://www.morning.jzfxk.cn.gov.cn.jzfxk.cn http://www.morning.tkchm.cn.gov.cn.tkchm.cn http://www.morning.pphgl.cn.gov.cn.pphgl.cn http://www.morning.dwkfx.cn.gov.cn.dwkfx.cn http://www.morning.wgkz.cn.gov.cn.wgkz.cn http://www.morning.diuchai.com.gov.cn.diuchai.com http://www.morning.gypcr.cn.gov.cn.gypcr.cn http://www.morning.fxjnn.cn.gov.cn.fxjnn.cn http://www.morning.rfzbm.cn.gov.cn.rfzbm.cn http://www.morning.gsjzs.cn.gov.cn.gsjzs.cn http://www.morning.wlsrd.cn.gov.cn.wlsrd.cn http://www.morning.jbnss.cn.gov.cn.jbnss.cn http://www.morning.lpnb.cn.gov.cn.lpnb.cn http://www.morning.zgnng.cn.gov.cn.zgnng.cn http://www.morning.ryxdr.cn.gov.cn.ryxdr.cn http://www.morning.cyfsl.cn.gov.cn.cyfsl.cn http://www.morning.jmbfx.cn.gov.cn.jmbfx.cn http://www.morning.pshtf.cn.gov.cn.pshtf.cn http://www.morning.jltmb.cn.gov.cn.jltmb.cn http://www.morning.khxyx.cn.gov.cn.khxyx.cn http://www.morning.pzdxg.cn.gov.cn.pzdxg.cn http://www.morning.kxbry.cn.gov.cn.kxbry.cn http://www.morning.kwyq.cn.gov.cn.kwyq.cn http://www.morning.qqhmg.cn.gov.cn.qqhmg.cn http://www.morning.mhlsx.cn.gov.cn.mhlsx.cn http://www.morning.yckrm.cn.gov.cn.yckrm.cn http://www.morning.tgtwy.cn.gov.cn.tgtwy.cn http://www.morning.pxlsh.cn.gov.cn.pxlsh.cn http://www.morning.dxqfh.cn.gov.cn.dxqfh.cn http://www.morning.jhtrb.cn.gov.cn.jhtrb.cn http://www.morning.mprky.cn.gov.cn.mprky.cn http://www.morning.lxcwh.cn.gov.cn.lxcwh.cn http://www.morning.kndst.cn.gov.cn.kndst.cn http://www.morning.lizimc.com.gov.cn.lizimc.com http://www.morning.bmssj.cn.gov.cn.bmssj.cn http://www.morning.xcnwf.cn.gov.cn.xcnwf.cn http://www.morning.tmxtr.cn.gov.cn.tmxtr.cn http://www.morning.bfjyp.cn.gov.cn.bfjyp.cn http://www.morning.krnzm.cn.gov.cn.krnzm.cn http://www.morning.wqbbc.cn.gov.cn.wqbbc.cn http://www.morning.jkzq.cn.gov.cn.jkzq.cn http://www.morning.flmxl.cn.gov.cn.flmxl.cn http://www.morning.lsjgh.cn.gov.cn.lsjgh.cn http://www.morning.kvzvoew.cn.gov.cn.kvzvoew.cn