capvideo_init.cpp ( File view )

  • By rarshion 2014-11-04
  • View(s):131
  • Download(s):3
  • Point(s): 2
			#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <ctype.h>
#include <errno.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <asm/types.h> 
#include <linux/videodev2.h>
#include <time.h>
#include "Video.hpp"
#include "struct.hpp"

#include "capvideo.h"
#include "SsbSipH264Encode.h"
#include "MfcDriver.h"

/***************** camera  etc *******************/
int vwidth = 640;
int vheight = 480;
int frames = 0;
long size;
int cam_c_fp = -1;
char *cam_name= "/dev/video0";
struct vbuffer {

     //   void *                  start;
        unsigned char*          start;
        size_t                  length;

};
struct vbuffer *      vbuffers         = NULL;
unsigned int     vn_buffers       = 4;

#define CLEAR(x) memset (&(x), 0, sizeof (x))
/***********************************************************/
/***************** The Vedio attribut  *******************/
 extern int  br_v ; //value brightness[-3-0-3] 
 extern int cont_v; //value contrast [-2-0-2]
 extern int  sat_v; //value saturation [-3-0-3]
 extern int  wb_v ; //value  wb 1.Auto 2.sunny 3.cloudy 4.office 5.Home
 extern int  rc_v ; //value resolution [1-3]
/***********************************************************/
/***************** MFC H264encoding etc *******************/
#define TRUE  1
#define FALSE 0
#define SHARED_BUF_NUM						100
#define MFC_LINE_BUF_SIZE_PER_INSTANCE		              (204800)
#define YUV_FRAME_NUM						300  
 int		film_cnt;
 int		frame_count=0;
 int 		encoding_flag = FALSE;
 int 		finished;
 FILE           *encoded_fp;
/***********************************************************/


/***************** MFC *************************************/
 void 	*handle; 
/* MFC functions */
 void *mfc_encoder_init(int width, int height, int frame_rate, int bitrate, int gop_num);
 void *mfc_encoder_exe(void *handle, unsigned char *yuv_buf, int frame_size, int first_frame, long *size);
 void mfc_encoder_free(void *handle);
/***********************************************************/

void errno_exit(const char* s);
/*
{

  //printf(stderr, "%s error %d, %s\n", s, errno, strerror (errno));
  exit(EXIT_FAILURE);

}
*/

int cam_init()    
{

	int dev_fp = -1;
	dev_fp = open(cam_name, O_RDWR);
	if (dev_fp < 0) 
       {

		printf("error open %s\n",cam_name);
		errno_exit("error open camera");
	
}
        else 
        {

            printf("success to open%s\n",cam_name);
        
}

	return dev_fp;


}



void vmmapInit(void)
{

  printf("-----start vmmapInit------\n");
  struct v4l2_requestbuffers req;
  CLEAR (req);

  req.count               = 4;
  req.type                = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  req.memory              = V4L2_MEMORY_MMAP;

  if (-1 == ioctl(cam_c_fp, VIDIOC_REQBUFS, &req)) 
  {

      errno_exit("reqbufs erro");
  
}
  else 
  {

      printf("reqbufs success\n");
  
}

  vbuffers =(vbuffer*)calloc(req.count, sizeof(*vbuffers));

  if (!vbuffers)
  {

     errno_exit("calloc vbuffers erro");
  
}
  else
  {

    printf("success to malloc buffers\n");
  
}

  printf("the req.count is %d\n",req.count);

  for (vn_buffers = 0; vn_buffers < req.count; ++vn_buffers) 
 {

    struct v4l2_buffer buf;

    CLEAR (buf);

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

    if (-1 == ioctl(cam_c_fp, VIDIOC_QUERYBUF, &buf))
    {

        errno_exit("erro to init VIDIOC_QUERYBUF");
    
}
    else
    {

       printf("success to init VIDIOC_QUERYBUF\n");
    
}

    vbuffers[vn_buffers].length = buf.length;
    vbuffers[vn_buffers].start =
     (unsigned char*) mmap (NULL /* start anywhere */, buf.length, PROT_READ | PROT_WRITE /* required */, MAP_SHARED /* recommended */, cam_c_fp, buf.m.offset);

    if (MAP_FAILED == vbuffers[vn_buffers].start)
     {

         errno_exit("erro to init mmap");
     
}
     else
     {

         printf("success to init mmap\n");
     
}
  
}

}

void capvideo_start()
{

  printf("---capvideo_start\n");
   struct v4l2_control vctl;
   struct v4l2_streamparm vstrp;
   struct v4l2_captureparm vcapt;


  int start = 1,ret=1;
  ret = ioctl(cam_c_fp, VIDIOC_STREAMON, &start);
  if (ret < 0) 
  {

    errno_exit("V4L2 : ioctl on VIDIOC_STREAMON failed");
  
}
  else
  {

     printf("V4L2 : ioctl on VIDIOC_STREAMON success\n");
  
}


  //2.camera change to other resolution
   vcapt.capturemode = V4L2_MODE_HIGHQUALITY; //change to the QXGA
   vcapt.timeperframe.numerator = 6;  
   vstrp.parm.capture = vcapt;

   if(-1==ioctl(cam_c_fp,VIDIOC_S_PARM,&vstrp))
   {

      errno_exit("erro to set_set_capturesspram\n");
   
}
   else 
   {

      printf("success to set_capturesspram\n");
   
}
  
  //3.image special effect
  vctl.id = V4L2_CID_DO_WHITE_BALANCE; //set while_balance
 // ctl.value = wb_v;
  vctl.value = 2;
  if(-1==ioctl(cam_c_fp,VIDIOC_S_CTRL,&vctl))
  {

     errno_exit("erro to set_white_balance\n");
  
}
  else 
  {

      printf("success to set_white_balance\n");
   
}

   vctl.id = V4L2_CID_BRIGHTNESS;  //set  brightness
   //ctl.value=br_v;
   vctl.value=1;
   if(-1==ioctl(cam_c_fp,VIDIOC_S_CTRL,&vctl))
   {

      errno_exit("erro to set_brightness\n");
   
}
   else 
   {

      printf("success to set_brightness\n");
   
}


}


void MFC_init()
{

        printf("MFC_init\n");

      

}

//void capvideo_init(struct VideoAttr& attr)
void capvideo_init()
{

	printf("---start video_init ---\n");

        int sret=0,found = 0,tmp=0;
	int k_id ; 
	unsigned int addr = 0;
  /*
         rc_v   = attr.resolution;
         br_v   = attr.bright; 
         cont_v = attr.contrast; 
         sat_v  = attr.saturation;
          wb_v  = attr.whitebalace; 

       printf("%d%d%d%d%d",attr.resolution,attr.bright,attr.contrast, attr.saturation,attr.whitebalace);
*/
 
     /* v4l2   etc  */
	struct v4l2_capability cap;
	struct v4l2_format codec_fmt;
     /*           */

	cam_c_fp = cam_init();


        /* Get capability */
	if((tmp=ioctl(cam_c_fp, VIDIOC_QUERYCAP, &cap))<0) 
       {

		errno_exit("VIDIOC_QUERYCAP error\n");
		return ;
	
}
       else 
        {

                printf("success to vidioc_querycap\n");
        
}

        /* Check the type - preview(OVERLAY) */
	if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) 
        {

		errno_exit("V4L2 : Can not capture(V4L2_CAP_VIDEO_CAPTURE is false)\n");
		exit(1);
	
}
        else
        {

             printf("V4L2 : capture(V4L2_CAP_VIDEO_CAPTURE is success)\n");
        
}

	/* Set format */
	codec_fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
	codec_fmt.fmt.pix.width = vwidth; 
	codec_fmt.fmt.pix.height = vheight; 	
	codec_fmt.fmt.pix.pixelformat= V4L2_PIX_FMT_YUV420; 
	sret = ioctl(cam_c_fp , VIDIOC_S_FMT, &codec_fmt);

	if (sret < 0) 
        {

		errno_exit("V4L2 : ioctl on VIDIOC_S_FMT failled\n");
		exit(1);
	
}
        else
        {

            printf("V4L2 : ioctl on VIDIOC_S_FMT sucess\n");
        
}

        vmmapInit();
        capvideo_start();
        MFC_init();

    printf("finish capvedio init\n");

   sleep(10);
    

}





/***************** MFC driver function *****************/
void *mfc_encoder_init(int width, int height, int frame_rate, int bitrate, int gop_num)
{

	int				frame_size;
	void			*handle;
	int				ret;


	frame_size	= (width * height * 3) >> 1;

	handle = SsbSipH264EncodeInit(width, height, frame_rate, bitrate, gop_num);
	if (handle == NULL) 
        {

		return NULL;
	
}
        else
        {

              printf("SsbSipH264EncodeInit success \n");
        
}

	ret = SsbSipH264EncodeExe(handle);

	return handle;

}

void *mfc_encoder_exe(void *handle, unsigned char *yuv_buf, int frame_size, int first_frame, long *size)
{

	unsigned char	*p_inbuf, *p_outbuf;
	int				hdr_size;
	int				ret;
        
   
	p_inbuf = (unsigned char*)SsbSipH264EncodeGetInBuf(handle, 0);

	memcpy(p_inbuf, yuv_buf, frame_size);

	ret = SsbSipH264EncodeExe(handle);
	if (first_frame) {

		SsbSipH264EncodeGetConfig(handle, H264_ENC_GETCONF_HEADER_SIZE, &hdr_size);
		//printf("Header Size : %d\n", hdr_size);
	
}

	p_outbuf = (unsigned char*)SsbSipH264EncodeGetOutBuf(handle, size);

	return p_outbuf;

}


void mfc_encoder_free(void *handle)
{

	SsbSipH264EncodeDeInit(handle);

}

			
...
Expand> <Close

Want complete source code? Download it here

Point(s): 2

Download
0 lines left, continue to read
Sponsored links

File list

Tips: You can preview the content of files by clicking file names^_^
Name Size Date
arm_capture.o7.44 kB23-09-14 11:04
arm_capture_init.o6.25 kB23-09-14 11:04
arm_capture_uinit.o2.11 kB23-09-14 11:04
arm_capvideo.o18.83 kB23-09-14 11:04
arm_capvideo_init.o6.44 kB23-09-14 11:04
arm_capvideo_uinit.o2.16 kB23-09-14 11:04
arm_Client.o102.76 kB03-11-14 23:23
arm_ClientPort99.95 kB03-11-14 23:23
arm_Common.o2.89 kB23-09-14 11:04
arm_Connector.o37.29 kB23-09-14 11:04
arm_control146.15 kB04-11-14 12:37
arm_Father9.37 kB23-09-14 08:26
arm_Functions.o2.02 kB23-09-14 11:04
arm_Lock.o1.60 kB23-09-14 11:04
arm_Logger.o7.59 kB23-09-14 11:04
arm_Mutex.o6.47 kB23-09-14 11:04
arm_Sem.o4.98 kB23-09-14 11:04
arm_ShareBuff.o21.13 kB23-09-14 11:04
arm_ShareMemory.o7.59 kB23-09-14 11:04
arm_ShmIPS.o12.88 kB23-09-14 11:04
arm_Socket.o1.16 kB23-09-14 11:04
arm_SocketOps.o5.43 kB23-09-14 11:04
arm_SocketStream.o6.77 kB23-09-14 11:04
arm_SsbSipH264Encode.o4.00 kB23-09-14 11:04
arm_TcpClient.o50.54 kB23-09-14 11:04
arm_Thread.o5.96 kB23-09-14 11:04
arm_Transform.o11.05 kB23-09-14 11:04
arm_Video.o22.58 kB23-09-14 11:04
capture.cpp4.76 kB12-12-13 15:12
capture.cpp~4.76 kB23-11-13 09:17
capture.h1,009.00 B23-11-13 09:17
capture.o5.05 kB23-09-14 11:03
capture_init.cpp9.78 kB12-12-13 14:49
capture_init.cpp~9.78 kB12-12-13 14:49
capture_init.o6.09 kB23-09-14 11:03
capture_uinit.cpp1.32 kB23-11-13 09:17
capture_uinit.o1.78 kB23-09-14 11:03
capvideo.cpp8.17 kB07-01-14 19:34
capvideo.cpp~8.15 kB23-11-13 09:17
capvideo.h527.00 B23-11-13 09:17
capvideo.o12.45 kB23-09-14 11:03
capvideo_init.cpp8.04 kB23-11-13 09:17
capvideo_init.o6.01 kB23-09-14 11:03
capvideo_uinit.cpp1.69 kB23-11-13 09:17
capvideo_uinit.o1.82 kB23-09-14 11:03
Client.cpp6.93 kB03-11-14 23:19
Client.cpp~6.91 kB30-03-14 18:48
Client.hpp1.03 kB23-11-13 09:17
Client.o66.13 kB03-11-14 23:23
Client11_19.pro1.27 kB23-11-13 09:17
ClientPort87.28 kB03-11-14 23:23
Common.cpp358.00 B23-11-13 09:17
Common.hpp308.00 B23-11-13 09:17
Common.o2.57 kB23-09-14 11:03
Connector.cpp2.37 kB23-09-14 10:28
Connector.cpp~2.37 kB16-03-14 20:18
Connector.hpp715.00 B16-03-14 21:59
Connector.hpp~715.00 B23-11-13 09:17
Connector.o24.09 kB23-09-14 11:03
control128.54 kB04-11-14 12:37
Control.cpp11.11 kB04-11-14 12:29
Control.cpp~10.58 kB07-04-14 21:25
<DeviceControl.cpp>0.00 B23-09-14 11:09
DeviceControl.hpp171.00 B23-09-14 11:09
Father12.37 kB23-09-14 08:25
Father.cpp1.89 kB21-02-14 15:48
Father.cpp~1.89 kB23-11-13 09:17
Functions.cpp852.00 B23-11-13 09:17
Functions.hpp317.00 B23-11-13 09:17
Functions.o1.48 kB23-09-14 11:03
IIPS.hpp328.00 B23-11-13 09:17
IMutex.hpp194.00 B23-11-13 09:17
IPC.hpp824.00 B23-11-13 09:17
Lock.cpp168.00 B23-11-13 09:17
Lock.hpp453.00 B23-11-13 09:17
Lock.o1.21 kB23-09-14 11:03
Logger.cpp1.95 kB23-11-13 09:17
Logger.hpp1.40 kB23-11-13 09:17
Logger.o6.40 kB23-09-14 11:03
LogMsg.c1.22 kB23-11-13 09:17
LogMsg.h370.00 B23-11-13 09:17
main.cpp1.24 kB23-11-13 09:17
makefile4.00 kB09-04-14 22:33
makefile~4.00 kB23-11-13 09:17
MfcDriver.h1.71 kB23-11-13 09:17
MfcDrvParams.h4.41 kB23-11-13 09:17
Mutex.cpp543.00 B23-11-13 09:17
Mutex.hpp284.00 B23-11-13 09:17
Mutex.o5.23 kB23-09-14 11:03
Sem.cpp1.01 kB23-11-13 09:17
Sem.hpp504.00 B23-11-13 09:17
Sem.o4.15 kB23-09-14 11:03
ShareBuff.cpp621.00 B23-11-13 09:17
ShareBuff.hpp301.00 B23-11-13 09:17
ShareBuff.o12.41 kB23-09-14 11:03
ShareMemory.cpp1.31 kB23-11-13 09:17
ShareMemory.hpp706.00 B28-03-14 21:41
ShareMemory.hpp~705.00 B28-03-14 21:25
ShareMemory.o6.66 kB23-09-14 11:03
ShmIPS.cpp797.00 B23-11-13 09:17
ShmIPS.hpp771.00 B23-11-13 09:17
ShmIPS.o9.46 kB23-09-14 11:03
Socket.cpp252.00 B23-11-13 09:17
Socket.hpp304.00 B23-11-13 09:17
Socket.o1.02 kB23-09-14 11:03
SocketOps.cpp2.25 kB23-11-13 09:17
SocketOps.hpp566.00 B23-11-13 09:17
SocketOps.o4.96 kB23-09-14 11:03
SocketStream.cpp2.03 kB23-11-13 09:17
SocketStream.hpp685.00 B23-11-13 09:17
SocketStream.o5.71 kB23-09-14 11:03
SsbSipH264Encode.c9.73 kB23-11-13 09:17
SsbSipH264Encode.h1.85 kB23-11-13 09:17
SsbSipH264Encode.o2.63 kB23-09-14 11:03
struct.hpp1.44 kB23-09-14 10:46
TcpClient.cpp1.22 kB23-11-13 09:17
TcpClient.hpp588.00 B23-11-13 09:17
TcpClient.o31.48 kB23-09-14 11:03
Thread.cpp1.56 kB23-11-13 09:17
Thread.hpp1.07 kB23-11-13 09:17
Thread.o5.08 kB23-09-14 11:03
ThreadAttr.cpp1.71 kB23-11-13 09:17
ThreadAttr.hpp651.00 B23-11-13 09:17
Transform.cpp1.26 kB23-11-13 09:17
Transform.hpp547.00 B23-11-13 09:17
Transform.o7.17 kB23-09-14 11:03
Uncopyable.hpp238.00 B28-03-14 21:30
Uncopyable.hpp~239.00 B28-03-14 21:30
Video.cpp1.05 kB23-11-13 09:17
Video.hpp536.00 B23-11-13 09:17
Video.o13.39 kB23-09-14 11:03
<Client2013-12-15>0.00 B04-11-14 22:07
...
Sponsored links

capvideo_init.cpp (337.95 kB)

Need 2 point
Your Point(s)

Your Point isn't enough.

Get point immediately by PayPal

More(Debit card / Credit card / PayPal Credit / Online Banking)

Submit your source codes. Get more point

LOGIN

Don't have an account? Register now
Need any help?
Mail to: support@codeforge.com

切换到中文版?

CodeForge Chinese Version
CodeForge English Version

Where are you going?

^_^"Oops ...

Sorry!This guy is mysterious, its blog hasn't been opened, try another, please!
OK

Warm tip!

CodeForge to FavoriteFavorite by Ctrl+D