linux视频采集X264编码

#include "stdint.h"
#include <stdlib.h>
#include "x264.h"
#include "x264_config.h"
#include "stdio.h"

/*
	SOCK ADD

*/
#include<sys/types.h>
#include<sys/socket.h>
#include<errno.h>
#include<string.h>
#include<unistd.h>
#include<netinet/in.h>
#define PORT 8888
#define BUFFER_SIZE 1024
#define DSTIP "192.168.1.10"
#define TEXT "message"
int sockfd,sendbytes;
char buf[BUFFER_SIZE];
struct hostent *host;
struct sockaddr_in serv_addr;
/*
 * v4l2Setp1.c
 *
 *  Created on: 2011-8-9
 *      Author: jcracker
 */
#include<stdio.h>
#include<linux/videodev2.h>
#include<fcntl.h>
#include<malloc.h>
#include<sys/mman.h>
#include <assert.h>
int fd;
unsigned int min;
#define DEVICE "/dev/video0"
#define CLEAR(x) memset (&(x), 0, sizeof (x))
#define WIDTH 176
#define HEIGHT 144
#define FPS 6
static unsigned int n_buffers = 0;
struct buffer {
	void * start;
	size_t length;
};
struct x264_encoder{
	x264_param_t param;
	x264_t *x264;
	x264_picture_t *pic_in;
	x264_nal_t *headers;
	x264_nal_t *nals;
	int i_nal;
};
struct x264_encoder *x264_en=NULL ;
FILE *x264Stream;
int i_frame;
#define TOTAL_FRAME_NUMBER 50;

struct v4l2_capability cap;
struct v4l2_cropcap cropcap;
struct v4l2_crop crop;
struct v4l2_format fmt;
struct v4l2_requestbuffers req; //向驱动申请帧缓冲的请求,里面包含申请的个数
struct buffer * buffers = NULL, *buffers_target =NULL;




void deviceClose(){

		if (-1 == close(fd))
			printf("close device failed\n");

		fd = -1;

}

void deviceUninit(){

	unsigned int i;

	for(i = 0;i<n_buffers;++i ){

		if(-1 == munmap(buffers[i].start,buffers[i].length)){
			printf("wrong~~~munmap\n");
		}
	}
	free(buffers);
}


void captrueStop(){
	enum v4l2_buf_type type;

	type	=	V4L2_BUF_TYPE_VIDEO_CAPTURE;
	if(-1 == ioctl(fd,VIDIOC_STREAMOFF,&type)){
		printf("wrong~~ VIDIOC_STREAMOFF\n");
	}
}

/**
 * 读取一帧
 */
int frameRead(){

	struct v4l2_buffer buf;

	CLEAR(buf);

	buf.type	=	V4L2_BUF_TYPE_VIDEO_CAPTURE;
	buf.memory	=	V4L2_MEMORY_MMAP;

	if(-1 == ioctl(fd,VIDIOC_DQBUF,&buf)){ // 从视频缓冲区的输出队列中取得一个已经保存有一帧视频数据的视频缓冲区;
		printf("wrong!VIDIOC_DQBUF\n");
	}

	/**
	 * assert的作用是现计算表达式 expression ,如果其值为假(即为0),那么它先向stderr打印一条出错信息,
		然后通过调用 abort 来终止程序运行。
	 */
	assert(buf.index < n_buffers);
	assert(buffers[buf.index].start);
	
	//videoProcess(buffers[buf.index].start);

x264Process(buf);

	return 1;
}

void mainLoop(){
	unsigned int count;
	count = TOTAL_FRAME_NUMBER;
	x264Init();
	
	for(i_frame=0;i_frame<=count;i_frame++){
		//作用不明
		for(;;){
			fd_set fds;
			struct timeval tv;
			int r;

			FD_ZERO(&fds);
			FD_SET(fd,&fds);

			/*超时设置*/
			tv.tv_sec	=	2;
			tv.tv_usec	=	0;

			r = select(fd+1,&fds,NULL,NULL,&tv);

			if(-1 == r || 0 == r){
				printf("select wrong!\n");
			}

			if(frameRead()){ //------>去读取帧
				break;
			}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值