v4l2_buf回调函数的玩耍

近日游玩看了videobuf2相关的videobuf2-v4l2.c,vidoebuf-core.c,
video-vmalloc.c,videobuf2-memops.c,videobuf2-internal.h,
有些感悟,写如下:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <stddef.h>

#define dprintk(level, fmt, arg...)					      \
	do {								      \
			printf("vb2: %s: " fmt, __func__, ## arg); \
	} while (0)

#define log_memop(vb, op)						\
	dprintk(2, "call_memop(%p, %d, %s)%s\n",			\
		(vb)->vb2_queue, (vb)->index, #op,			\
		(vb)->vb2_queue->mem_ops->op ? "" : " (nop)")

#define call_memop(vb, op, args...)					\
({									\
	struct vb2_queue *_q = (vb)->vb2_queue;				\
									\
	log_memop(vb, op);						\
	_q->mem_ops->op ? _q->mem_ops->op(args) : 0;		\
})

#define log_qop(q, op)							\
	dprintk(2, "call_qop(%p, %s)%s\n", q, #op,			\
		(q)->ops->op ? "" : " (nop)")

#define call_qop(q, op, args...)					\
({									\
	int err;							\
									\
	log_qop(q, op);							\
	err = (q)->ops->op ? (q)->ops->op(args) : 0;			\
	if (!err)							\
		(q)->cnt_ ## op++;					\
	err;								\
})

#define log_vb_qop(vb, op, args...)					\
	dprintk(2, "call_vb_qop(%p, %d, %s)%s\n",			\
		(vb)->vb2_queue, (vb)->index, #op,			\
		(vb)->vb2_queue->ops->op ? "" : " (nop)")



#define call_vb_qop(vb, op, args...)					\
({									\
									\
	log_vb_qop(vb, op);						\
	(vb)->vb2_queue->ops->op ?				\
		(vb)->vb2_queue->ops->op(args) : 0;			\
})


struct vb2_queue;
#define VB2_MAX_PLANES (8)

struct vb2_buffer    // 代表一个video buffer 
{
	struct vb2_queue*vb2_queue;  // 全局vb2队列 
	unsigned int index;
	
	unsigned int cnt_buf_queue;
};

struct vb2_mem_ops
{
	void*(*alloc)(void*alloc_ctx,unsigned long size);
	void (*put)(void*buf_priv);
	void (*prepare)(void*buf_priv);
	void (*finish)(void*buf_priv);
	int  (*mmap)(void*buf_priv,int vma);
};

struct vb2_ops
{
	int (*queue_setup)(struct vb2_queue *q, const void *parg,
	unsigned int *num_buffers, unsigned int *num_planes,
	unsigned int sizes[], void *alloc_ctxs[]);
   
	int(*buf_init)(struct vb2_buffer*vb);
	int(*buf_prepare)(struct vb2_buffer*vb);
	void(*buf_queue)(struct vb2_buffer*vb);
};

struct vb2_buf_ops
{	
  int (*fill_user_buffer)(struct vb2_buffer *vb, void *pb);
  int(*set_timestamp)(struct vb2_buffer*vb,const void*pb);
};

struct vb2_queue
{
	const struct vb2_ops*ops;  //  buffer相关操作的回调
	const struct vb2_mem_ops*mem_ops;  // 内存分配相关的回调
	const struct vb2_buf_ops*buf_ops;  // buffer相关的回调
	unsigned int			plane_sizes[VB2_MAX_PLANES];
	void *alloc_ctx[VB2_MAX_PLANES];
	
	
	unsigned int cnt_queue_setup;
	unsigned int cnt_wait_prepare;
	unsigned int cnt_wait_finish;
	unsigned int cnt_start_streaming;
	unsigned int cnt_stop_streaming;
};

static int vb2_queue_setup(struct vb2_queue*vq,const void*
parg,unsigned int*num_buffers,unsigned int*nplanes,unsigned int sizes[],
void*alloc_ctxs[])
{
	if(*num_buffers > 32)
		*num_buffers  = 32;
	*nplanes  = 1;
	sizes[0] = 1920*1080*2;
	printf("vb2_queue_setup \n");
}

static void vb2_buf_queue(struct vb2_buffer*vb)
{
	printf("vb2_buf_queue \n");
}


static void*vb2_vmalloc_alloc(void*alloc_ctx,unsigned long size)
{
	printf("vb2_vmalloc_alloc\n");
}

static  int vb2_vmalloc_mmap(void*buf_priv,int vma)
{
	printf("vb2_vmalloc_mmap\n");
}

static  int vb2_set_timestamp(struct vb2_buffer*vb,const void*pb)
{
	printf("vb2_set_timestamp\n");
}


static struct vb2_ops vb2_queue_qops = 
{ 
  .queue_setup = vb2_queue_setup,
  .buf_queue   = vb2_buf_queue,
};


static struct vb2_mem_ops vb2_vmalloc_memops = 
{
	.alloc  = vb2_vmalloc_alloc,
	.mmap   = vb2_vmalloc_mmap,
};

static const struct vb2_buf_ops v4l2_buf_ops = 
{
	.set_timestamp  = vb2_set_timestamp,
};


struct vb2_queue  vb2 = 
{
	.ops = &vb2_queue_qops,
	.mem_ops = &vb2_vmalloc_memops,
	.buf_ops = &v4l2_buf_ops,
};

struct vb2_buffer  vb_buffer = 
{
	.vb2_queue = &vb2,
};


int main()
{
	unsigned int num_buffers, allocated_buffers, num_planes = 0;
	int ret = 0;

	call_memop(&vb_buffer,alloc,&vb2.alloc_ctx,num_buffers);
	
	ret = call_qop(&vb2, queue_setup, &vb2, NULL, &num_buffers, &num_planes,
		   vb2.plane_sizes, vb2.alloc_ctx);
		   
    call_vb_qop(&vb_buffer, buf_queue, &vb_buffer);	
		  
		  
	return 0;
}

root@ubuntu:/nfsroot/callback# ./a.out
vb2: main: call_memop(0x804a080, 0, alloc)
vb2_vmalloc_alloc
vb2: main: call_qop(0x804a080, queue_setup)
vb2_queue_setup
vb2: main: call_vb_qop(0x804a080, 0, buf_queue)
vb2_buf_queue

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

今天6点半起床10点半睡觉和今天早晚运动

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值