Create Project for repo.or.cz
[vp.git] / test / h264enc_test.cpp
blob5ec5095dfe152cf2b8d651d21a40c0a088a3978b
1 /*******************************************************************************
2 * File Name : h264enc_test.c
3 *
4 * Author : Henry He
5 * Created Time : Sat 31 Oct 2009 04:26:05 PM CST
6 * Description :
7 ******************************************************************************/
10 /*******************************************************************************
11 * Desc : Includes Files
12 ******************************************************************************/
14 #include "mfc/common.h"
16 #include <sys/time.h>
17 #include <sys/mman.h>
21 /*******************************************************************************
22 * Desc : Macro Definations
23 ******************************************************************************/
26 /*******************************************************************************
27 * Desc : Type Definations
28 ******************************************************************************/
31 /*******************************************************************************
32 * Desc : Global Variables
33 ******************************************************************************/
36 /*******************************************************************************
37 * Desc : File Variables
38 ******************************************************************************/
46 unsigned int measureTime(struct timeval *start, struct timeval *stop)
48 unsigned int sec, usec, time;
50 sec = stop->tv_sec - start->tv_sec;
51 if(stop->tv_usec >= start->tv_usec)
53 usec = stop->tv_usec - start->tv_usec;
55 else
57 usec = stop->tv_usec + 1000000 - start->tv_usec;
58 sec--;
60 time = sec*1000 + usec/1000;
61 return time;
77 int Test_H264_Encoder(int argc, char **argv)
79 int in_fd;
80 int out_fd;
81 char *in_addr;
82 int file_size;
84 int frame_count;
85 int backup_frame_cnt;
86 int frame_size;
88 int width;
89 int height;
90 int frame_rate;
91 int bitrate;
92 int gop_num;
94 void *p_outbuf;
95 UINT32 size;
97 int hdr_size;
98 // int para_change[2];
99 int frame_cnt = 0;
100 struct stat s;
102 H264Encoder zheEncoder;
105 struct timeval start;
106 struct timeval stop;
107 unsigned int time = 0;
109 int nRetVal;
112 if (argc != 8) {
113 printf("Usage : mfc <YUV file name> <output filename> <width> <height> ");
114 printf("<frame rate> <bitrate> <GOP number>\n");
115 return -1;
118 // in/out file open
119 in_fd = open(argv[1], O_RDONLY);
120 out_fd = open(argv[2], O_RDWR | O_CREAT | O_TRUNC, 0644);
122 if( (in_fd < 0) || (out_fd < 0) ) {
123 printf("input/output file open error\n");
124 return -1;
127 // get input file size
128 fstat(in_fd, &s);
129 file_size = s.st_size;
131 // mapping input file to memory
132 in_addr = (char *)mmap(0, file_size, PROT_READ, MAP_SHARED, in_fd, 0);
133 if(in_addr == NULL) {
134 printf("input file memory mapping failed\n");
135 return -1;
138 width = atoi(argv[3]);
139 height = atoi(argv[4]);
140 frame_rate = atoi(argv[5]);
141 bitrate = atoi(argv[6]);
142 gop_num = atoi(argv[7]);
144 frame_size = (width * height * 3) >> 1;
145 frame_count = file_size / frame_size;
147 printf("file_size : %d, frame_size : %d, frame count : %d\n", file_size, frame_size, frame_count);
150 nRetVal = zheEncoder.Create (width, height, frame_rate, bitrate, gop_num);
151 if (nRetVal < 0) {
152 LOG_MSG(LOG_ERROR, "Test_Encoder", "create encoder failed\n");
153 return -1;
156 backup_frame_cnt = frame_count;
158 while (frame_count > 0)
160 printf ("frame count : %d\n", frame_count);
162 // copy YUV data into input buffer
163 gettimeofday(&start, NULL);
165 p_outbuf = zheEncoder.Encode (in_addr, frame_size, size);
166 if (frame_count == backup_frame_cnt) {
167 zheEncoder.GetConfig(H264_ENC_GETCONF_HEADER_SIZE, &hdr_size);
168 printf("Header Size : %d\n", hdr_size);
170 in_addr += frame_size;
172 gettimeofday (&stop, NULL);
173 time += measureTime (&start, &stop);
175 frame_cnt++;
177 write(out_fd, p_outbuf, size);
179 frame_count--;
182 printf("Decoding Time : %u, Frame Count : %d, FPS : %f\n", time, frame_cnt, (float)frame_cnt*1000/time);
184 zheEncoder.Destroy ();
186 munmap(in_addr, file_size);
187 close(in_fd);
188 close(out_fd);
190 return 0;
196 int main ( int argc, char *argv[] )
198 Test_H264_Encoder(argc, argv);
199 return 0;
200 } /* ---------- end of function main ---------- */