www.pudn.com > phread2.rar > phread2.c
#include#define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include #include "common/st1kiapi.h" #include #include #include #include #include #include #include #include //#include #include //#include //#include #include #include "common/twi.c" /* #ifdef _MSC_VER #include #endif */ #define GOP_LENGTH 250 /*********************************************************************************/ /*********************************************************************************/ #ifdef TM_IN_SYS_TIME #include #else #include #endif #include "ppifcd.h" #define VERSION "2.2" #define WIDTH 720 #define HEIGHT 576 #define SIZE (WIDTH * HEIGHT) #define AVERAGE 100 /* * Return the difference between two struct timevals in microseconds */ long tvdelta(struct timeval *t1, struct timeval *t2) { long delta, usec; delta = 1000000 * (t1->tv_sec - t2->tv_sec); usec = t1->tv_usec; if (t1->tv_usec < t2->tv_usec) { usec += 1000000; delta -= 1000000; } delta += (usec - t2->tv_usec); return delta; } long getoffset(void) { int i; struct timeval t, o; long delta = 0; for(i=0; i < AVERAGE ; i++ ) { gettimeofday(&o, NULL); gettimeofday(&t, NULL); delta += tvdelta(&t, &o); } return (delta / AVERAGE); } /****************************************************************PPIbegin*******************************************************/ int ppifcd2() { int width = 352; int height = 288; int fd; uint8_t *buffer, *buffer2; struct timeval o, t; long delta, offset; uint8_t *buffer3; int sel=0; uint8_t *BUF, *Y, *U, *V, *BUF2; int k1, k2; int size; int f, i; st1ki_context *p_st; size = width * height; buffer = (uint8_t *) malloc (SIZE << 1); buffer2 = (uint8_t *) malloc (size << 1); //buffer3 = (uint8_t *) malloc (size * 3 / 2); Ydata=(uint8_t *) malloc (size); Udata=(unit8_t *) malloc(size>>2); Vdata=(unit8_t *) malloc(size>>2); offset = getoffset(); /* Open /dev/ppi */ fd = open("/dev/ppi0", O_RDONLY,0); if (fd == -1) { printf("Could not open dev/ppi : %d \n",errno); free(buffer); exit(1); } /* Read the raw image data from the PPI */ gettimeofday(&o, NULL); read(fd, buffer, 1234); gettimeofday(&t, NULL); delta = tvdelta(&t, &o); //pthread_mutex_lock (&count_lock);//设置互斥锁,set lock for(f = 0; f < 2; f++) { BUF = buffer2; BUF2 = buffer; for(k2 = 0; k2 < 288; k2 += 1) { BUF2 += 16; for(k1 = 0; k1 < 704; k1 += 4) { *BUF++ = *BUF2++; *BUF++ = *BUF2++; *BUF++ = *BUF2++; *BUF++ = *BUF2++; BUF2 += 4; } BUF2 += 16; } Y = Ydata;//p_st->y; U = Udata;//p_st->u; V = Vdata;//p_st->v; BUF = buffer2; for (k2 = 0; k2 < 288; k2 += 2) { for (k1 = 0; k1 < 352; k1 += 2) { *U++ = *BUF++; *Y++ = *BUF++; BUF++; *Y++ = *BUF++; } for (k1 = 0; k1 < 352; k1 += 2) { BUF++; *Y++ = *BUF++; *V++ = *BUF++; *Y++ = *BUF++; } } if(sel==0) { Y1=Ydata; U1=Udata; V1=Udata; } if(sel==1) { Y2=Ydata; U2=Ydata; V2=Vdata; } sel++; read(fd, buffer, 1234); } close(fd); free(buffer); free(buffer2); free(Ydata); free(Udata); free(Vdata); buffer=NULL; buffer2=NULL; Ydata=NULL; Udata=NULL; Vdata=NULL; return 0; } int ppifcd() { int width = 352; int height = 288; int fd; uint8_t *buffer, *buffer2; struct timeval o, t; long delta, offset; uint8_t *buffer3; uint8_t *BUF, *Y, *U, *V, *BUF2; int k1, k2; int size; int f, i; st1ki_context *p_st; size = width * height; buffer = (uint8_t *) malloc (SIZE << 1); buffer2 = (uint8_t *) malloc (size << 1); //buffer3 = (uint8_t *) malloc (size * 3 / 2); Ydata=(uint8_t *) malloc (size); Udata=(unit8_t *) malloc(size>>2); Vdata=(unit8_t *) malloc(size>>2); offset = getoffset(); /* Open /dev/ppi */ fd = open("/dev/ppi0", O_RDONLY,0); if (fd == -1) { printf("Could not open dev/ppi : %d \n",errno); free(buffer); exit(1); } /* Read the raw image data from the PPI */ gettimeofday(&o, NULL); read(fd, buffer, 1234); gettimeofday(&t, NULL); delta = tvdelta(&t, &o); for(f = 0; ; f++) { BUF = buffer2; BUF2 = buffer; for(k2 = 0; k2 < 288; k2 += 1) { BUF2 += 16; for(k1 = 0; k1 < 704; k1 += 4) { *BUF++ = *BUF2++; *BUF++ = *BUF2++; *BUF++ = *BUF2++; *BUF++ = *BUF2++; BUF2 += 4; } BUF2 += 16; } Y = p_st->y; U = p_st->u; V = p_st->v; BUF = buffer2; for (k2 = 0; k2 < 288; k2 += 2) { for (k1 = 0; k1 < 352; k1 += 2) { *U++ = *BUF++; *Y++ = *BUF++; BUF++; *Y++ = *BUF++; } for (k1 = 0; k1 < 352; k1 += 2) { BUF++; *Y++ = *BUF++; *V++ = *BUF++; *Y++ = *BUF++; } } if(selet==1) { Y1=Ydata; U1=Udata; V1=Udata; } if(selet==2) { Y2=Ydata; U2=Ydata; V2=Vdata; } if(selet==0) { Y3=Ydata; U3=Udata; V3=Vdata; } read(fd, buffer, 1234); } //pthread_mutex_unlock (&count_lock);//互斥锁到此为止,end of lock close(fd); free(buffer); free(buffer2); free(Ydata); free(Udata); free(Vdata); buffer=NULL; buffer2=NULL; Ydata=NULL; Udata=NULL; Vdata=NULL; return 0; } /************************************PPIEND******************************************************************************/ /*********************************************decoderbegin******************************************************************/ int decoder(int frame) { char out_name[80] = {"test0.264"}; int width = 352; int height = 288; int bitrate = 200; float fps = 8.0F; int fd; uint8_t *buffer, *buffer2; struct timeval o, t; long delta, offset; uint8_t *buffer3; uint8_t *BUF, *Y, *U, *V, *BUF2; int k1, k2; time_t time1, time2; st1ki_context *p_st; FILE *fp_out;//*fp_inp, int size; int f, i; size = width * height; time(&time1); // begining time of encoding fp_out = fopen(out_name, "wb"); if(fp_out == NULL) { printf("open output file failed:%s\n", out_name); return -2; } if(!st1ki_create_context(&p_st, width, height, fps, bitrate)) return -3; printf("\n-------------------- Encoding Begin --------------------\n"); printf("H.264 output file: %s\n", out_name); printf("width=%d, height=%d, bitrate=%dkbps, framerate=%.2ffps\n", width, height, bitrate, fps); st1ki_set_context_default(p_st); st1ki_set_context_gop(p_st, GOP_LENGTH); for(f=0,fy=Y1; p_st->u=U1; p_st->V=V1; } if(selet==1) { p_st->y=Y2; p_st->u=U2; p_st->V=V2; } if(selet==2) { p_st->y=Y3; p_st->u=U3; p_st->v=V3; } //开辟三个空间,不用上锁! //count=1;//设置互斥锁变量值,使能互斥锁, lock st1ki_encode_one_frame(p_st); fwrite(p_st->stream, 1, p_st->stream_size, fp_out); //count=0;//destroy lock selet++; if(selet>=3) { selet=0; } printf("%5d frames finished", f + 1); } st1ki_destroy_context(p_st); //free(p_st); time(&time2); // end time of encoding printf("\n total used %4d seconds\n", (int)(time2-time1)); // print out encoding total time printf("Mission accomplished ! \n"); #ifdef _MSC_VER Sleep(2000); #endif return 0; } int count=0; int selet=0; unsigned char *Y1,*U1,*V1,*Y2,*U2,*V2,*Y3,*U3,*V3; Y1=(unsigned char *) malloc(size); U1=(unsigned char *) malloc(size>>2); V1=(unsigned char *) malloc(size>>2); Y2=(unsigned char *) malloc(size); U2=(unsigned char *) malloc(size>>2); V2=(unsigned char *) malloc(size>>2); Y3=(unsigned char *) malloc(size); U3=(unsigned char *) malloc(size>>2); V3=(unsigned char *) malloc(size>>2); int main(int argc, char *argv[]) { int errnum; //pthread_t ppi; pthread_t encode; count=argv[1]; twi(); ppifcd2(); //pthread_cond_init();//init errnum = pthread_create(&encode, NULL, &encoder, &count); if (errnum!=0) { fprintf(stderr, "cannot create thread to deal with the encode connection.\n"); return 1; while(errnum!=0); } ppifcd(); }