capvideo.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"
#include"ShareBuff.hpp"
#include"Functions.hpp"


extern int vwidth;
extern int vheight;
extern int frames;
extern long size;
extern int cam_c_fp;
extern char *cam_name;
extern FILE *encoded_fp;

struct vbuffer {

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

};
extern struct vbuffer*  vbuffers;
extern unsigned int  vn_buffers;
#define CLEAR(x) memset (&(x), 0, sizeof (x))

/***************** 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  
 extern int	film_cnt;
 extern int	frame_count;
 extern int 	encoding_flag;
 extern int 	finished;
/***********************************************************/


/***************** MFC *************************************/
 extern  void *handle; 
 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);
/***********************************************************/

using namespace  std;

ShareBuff g_shareBuff;
extern Video g_video;

/*
void transform(unsigned char s1, unsigned char s2,
        unsigned char& r, unsigned char& g, unsigned char& b)
{

    r = g = b = 0;

    unsigned char k;

    r |= (s1&31);
    r <<= 3;
    r |= (s1&7);


    b |= (s2&248);
    k  = (s2&56);
    k >>= 3;
    b |= k;


    g = (s2&7);
    g <<= 5;
    k = (s1&224);
    k >>= 3;

    g |= k;
    k >>= 2;
    g |= (k&3);

}

int read_data(int fp, char *buf, int width, int height, int bpp)
{


	int ret=0;

	if ((ret = read(fp, buf,(vwidth*pheight*bpp/8)))!=(vwidth*pheight*bpp/8)) 
	{

	        printf("read %d--0x%x\n",ret,ret);
		return 0;
	
}

	return ret;

}

void writeImageToFile(unsigned int size,char* vf_buff)
{

 
  
	frames++;  //the frames 1,2,3****n
	sprintf(filename,"video/%d.bmp",frames);

   	bmpFile=fopen(filename, "w+");
 
        if(!bmpFile) 
        {

           printf("failed to open test.bmp\n");
        
}
        else
        {

            printf("success to open bmp\n");
        
}
        fwrite(bmp_head_t,1,66,bmpFile);
        fwrite(vf_buff,1,size,bmpFile);
   	fclose(bmpFile);

   

}

char* getVideoPictureFileName()
{

    char* buff = NULL;
    while( (buff = g_shareBuff.popBuff()) == NULL )
    {

        Functions::Sleep(500);
    
}

    writeImageToFile(size, buff);

    free(buff);

    return filename;

}

void* startCaptureVideo(void*arg)
{

    bool* isRunning = (bool*)arg;

    while( *isRunning )
    {


        vf_buff = (char*)malloc(size);
        if( vf_buff == NULL )
        {

            printf("malloc for vf_buff error\n");
            break;
        
}

        if( !read_data(cam_fp, vf_buff, vwidth, pheight, fb_bpp) )
        {

            printf("read error\n");
            break;
        
}

       Functions::Sleep(150);

       g_shareBuff.pushBuff(vf_buff);
    
}

}



void writejpg(unsigned char* img)
{

   printf("---start jpegwrite-----\n");
  struct jpeg_compress_struct cinfo;
  struct jpeg_error_mgr jerr;
  int jpegQuality=80;

  JSAMPROW row_pointer[1];
  frames++;
  sprintf(txtname,"video/%d.jpg",frames);


  FILE *outfile = fopen(txtname, "wb" );
  if (!outfile) 
 {

    exit(0);
  
}
  else
  {

   //  printf("success to open jpg\n");
  
}
  // create jpeg data
 
  cinfo.err = jpeg_std_error( &jerr );
  jpeg_create_compress(&cinfo);
  jpeg_stdio_dest(&cinfo, outfile);

  // set image parameters
  cinfo.image_width = vwidth;	
  cinfo.image_height = pheight;
  cinfo.input_components = 3;
  cinfo.in_color_space = JCS_RGB;

  // set jpeg compression parameters to default
  jpeg_set_defaults(&cinfo);
  // and then adjust quality setting
  jpeg_set_quality(&cinfo, jpegQuality, TRUE);
  // start compress 
  jpeg_start_compress(&cinfo, TRUE);
  // feed data
 

  while (cinfo.next_scanline < cinfo.image_height) {
 	
    row_pointer[0] = &img[cinfo.next_scanline * cinfo.image_width *  cinfo.input_components];
    jpeg_write_scanlines(&cinfo, row_pointer, 1);
  
}


  jpeg_finish_compress(&cinfo);
  jpeg_destroy_compress(&cinfo);

  fclose(outfile);


}








char* writejpegfile()
{

     int i=0;

     unsigned char s1, s2;
     unsigned char d1, d2, d3;
 
     // printf("start video \n");

    if (!read_data(cam_fp, &vf_buff[0], vwidth, pheight, fb_bpp))          
    {

		printf("read error\n");
    
}
    else 
    {

             
     
}

         src = (unsigned char*)vf_buff;
         tmpe = (unsigned char*)dst;
        
   while(tmpe<end)
  {

       transform(*src,*(src+1),*tmpe,*(tmpe+1),*(tmpe+2)); 
       src += 2;
       tmpe += 3;

   
}


       writejpg(dst);

      //return filename;

    // return txtname;


}
*/


char*  capvideo()
{

        char file_name[100];
        long int yuv_cnt=0;

	int start, ret;
        long int frame_num = 200;  
	int key;
        int YUV_FRAME_BUFFER_SIZE = vwidth * vheight *3/2 ; 

        unsigned char	*encoded_buf;        
        long encoded_size = YUV_FRAME_BUFFER_SIZE;

        handle = mfc_encoder_init(vwidth, vheight, 40, 1200, 40);

        struct v4l2_buffer buf;
        CLEAR (buf);
        buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
        buf.memory = V4L2_MEMORY_MMAP;
 
       film_cnt=0; 
			

	sprintf(&file_name[0], "video/Cam_encoding_%dx%d-%d.264", vwidth, vheight, ++film_cnt);
printf("Name of encoded file : Cam_encoding_%dx%d-%d.264\n", vwidth, vheight, film_cnt);

	/* file create/open, note to "wb" */
	encoded_fp = fopen(&file_name[0], "wb");
	if (!encoded_fp) 
       {

	  printf("encoded_fp erro \n");
       
}
        else
       {
 
          printf("encoded_fp success \n");
       
}

	/* Codec start */
    printf("start codec \n");
   time_t start_time,now;
   time(&start_time);
   for(yuv_cnt=0; yuv_cnt < frame_num; yuv_cnt++)
   {

      frame_count++;
      if (-1 == ioctl(cam_c_fp, VIDIOC_DQBUF, &buf))
      {

         printf("erro to vidioc_dqbuf\n");
      
}
      else 
      {

            // printf("success to vidioc_dqbuf\n");
      
}

      if(frame_count == 1)
      {

          printf("this is the first frame in file\n");
	  encoded_buf =(unsigned char*) mfc_encoder_exe(handle,vbuffers[buf.index].start, YUV_FRAME_BUFFER_SIZE, 1, &encoded_size);
      
}
      else
      {

	encoded_buf = (unsigned char*)mfc_encoder_exe(handle, vbuffers[buf.index].start, YUV_FRAME_BUFFER_SIZE, 0, &encoded_size);			
       
}
       fwrite(encoded_buf, 1, encoded_size, encoded_fp);
       if (-1 == ioctl(cam_c_fp, VIDIOC_QBUF, &buf))
       {

             printf("VIDIOC_QBUF erro");
       
}
       else 
       {

          //  printf("success to vidioc_qbuf\n");
       
}
       if((frame_count%100==0)&&(yuv_cnt<frame_num-2))
       {

           //100 frames in a .264File
            fclose(encoded_fp);
            mfc_encoder_free(handle);
            printf("push the %s in capvideo.cpp",&file_name[0]);
            g_video.pushPictureName(&file_name[0]); //Push the H264 file into the shareMemory
            handle = mfc_encoder_init(vwidth,vheight, 35, 1000, 40);
            sprintf(&file_name[0], "video/Cam_encoding_%dx%d-%d.264", vwidth,vheight, ++film_cnt);
            printf("Name of encoded file : Cam_encoding_%dx%d-%d.264\n", vwidth,vheight, film_cnt);
            encoded_fp = fopen(&file_name[0], "wb");
	    if (!encoded_fp) 
            {

	       printf("encoded_fp erro \n");
            
}
            else
            {
 
                printf("encoded_fp success \n");
             
}
            frame_count = 0; //In a new file,Write the Video Write Head  
        
}
    
}
            time(&now);
            printf("now time %d\n",now-start_time);
            frame_count = 0;
            fclose(encoded_fp); 
            mfc_encoder_free(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.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