-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathxdma_mpeg2encode.c
372 lines (280 loc) · 13.4 KB
/
xdma_mpeg2encode.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/timeb.h>
// function : dev_read
// description : read data from device to local memory (buffer), (i.e. device-to-host)
// parameter :
// dev_fd : device instance
// addr : source address in the device
// buffer : buffer base pointer
// size : data size
// return:
// int : 0=success, -1=failed
int dev_read (int dev_fd, uint64_t addr, void *buffer, uint64_t size) {
if ( addr != lseek(dev_fd, addr, SEEK_SET) ) // seek
return -1; // seek failed
if ( size != read(dev_fd, buffer, size) ) // read device to buffer
return -1; // read failed
return 0;
}
// function : dev_write
// description : write data from local memory (buffer) to device, (i.e. host-to-device)
// parameter :
// dev_fd : device instance
// addr : target address in the device
// buffer : buffer base pointer
// size : data size
// return:
// int : 0=success, -1=failed
int dev_write (int dev_fd, uint64_t addr, void *buffer, uint64_t size) {
if ( addr != lseek(dev_fd, addr, SEEK_SET) ) // seek
return -1; // seek failed
if ( size != write(dev_fd, buffer, size) ) // write device from buffer
return -1; // write failed
return 0;
}
// function : dev_read_uint64
// description : read a uint64 value (8 bytes) from device to local, (i.e. device-to-host)
// parameter :
// dev_fd : device instance
// addr : source address in the device
// p_value : uint64 value's pointer
// return:
// int : 0=success, -1=failed
int dev_read_uint64 (int dev_fd, uint64_t addr, uint64_t *p_value) {
return dev_read(dev_fd, addr, (void*)p_value, sizeof(uint64_t));
}
// function : dev_write_uint64
// description : write a uint64 value (8 bytes) from to device, (i.e. host-to-device)
// parameter :
// dev_fd : device instance
// addr : target address in the device
// value : uint64 value to write
// return:
// int : 0=success, -1=failed
int dev_write_uint64 (int dev_fd, uint64_t addr, uint64_t value) {
return dev_write(dev_fd, addr, (void*)&value, sizeof(uint64_t));
}
// mpeg2encoder device's address map --------------------------------------------------------------------------------
#define ADDR_RESET_AND_SEQUENCE_CONTROL 0x00000000UL
#define ADDR_VIDEO_FRAME_SIZE 0x00000008UL
#define ADDR_OUT_BUF_CONTROL 0x00000010UL
#define ADDR_BASE_IN_PIXELS 0x01000000UL
#define ADDR_BASE_OUT_BUF 0x01000000UL
// function : mpeg2encoder_reset
// description : reset the mpeg2encoder in the device
// return:
// int : 0=success, -1=failed
int mpeg2encoder_reset (int dev_w_fd) {
if ( dev_write_uint64(dev_w_fd, ADDR_RESET_AND_SEQUENCE_CONTROL, 0x0) ) // reset
return -1;
return dev_write_uint64(dev_w_fd, ADDR_RESET_AND_SEQUENCE_CONTROL, 0x1) ; // reset release
}
// function : mpeg2encoder_set_sequence_stop
// description : send a "sequence stop" signal to the device, which indicate this is the end of a video sequence
// return:
// int : 0=success, -1=failed
int mpeg2encoder_set_sequence_stop (int dev_w_fd) {
return dev_write_uint64(dev_w_fd, ADDR_RESET_AND_SEQUENCE_CONTROL, 0x3) ; // sequence stop, note that the lowest bit is set to 1 to keep the reset released
}
// function : mpeg2encoder_set_video_frame_size
// description : set the frame width and height of the video sequence to be encoded
// parameter :
// xsize16 : frame width / 16 . e.g. for width = 640, xsize16 = 40
// ysize16 : frame height / 16 . e.g. for height = 480, ysize16 = 30
// return:
// int : 0=success, -1=failed
int mpeg2encoder_set_video_frame_size (int dev_w_fd, int xsize16, int ysize16) {
uint64_t reg_value;
reg_value = ysize16;
reg_value <<= 32;
reg_value |= xsize16;
return dev_write_uint64(dev_w_fd, ADDR_VIDEO_FRAME_SIZE, reg_value);
}
// function : mpeg2encoder_put_pixels
// description : put raw YUV pixels to the device, which will be encoded to the MPEG2 stream
// return:
// int : 0=success, -1=failed
int mpeg2encoder_put_pixels (int dev_w_fd, void *buffer, uint64_t size) {
return dev_write(dev_w_fd, ADDR_BASE_IN_PIXELS, buffer, size);
}
// function : mpeg2encoder_get_outbuf
// description : get data (encoded MPEG2 stream) from the out buffer of the device
// return:
// int : 0=success, -1=failed
int mpeg2encoder_get_outbuf (int dev_r_fd, int dev_w_fd, void *buffer, int *p_size, int *p_overflow) { // buffer must have 0x200000 (2MB) space at least
int offset = 0;
for (;;) {
do { // wait until outbuf not empty
uint64_t reg_value;
if ( dev_read_uint64(dev_r_fd, ADDR_OUT_BUF_CONTROL, ®_value) )
return -1;
*p_size = reg_value & 0x7ffffff;
*p_overflow = ((reg_value >> 32) & 1);
} while (*p_size <= 0) ;
if (offset >= *p_size) // no more available data in out buffer
break;
if ( dev_read(dev_r_fd, ADDR_BASE_OUT_BUF+offset, (void*)((char*)buffer+offset), *p_size-offset ) )
return -1;
offset = *p_size;
}
return dev_write_uint64(dev_w_fd, ADDR_OUT_BUF_CONTROL, 0UL); // clear the out buffer
}
// function : get_millisecond
// description : get time in millisecond
uint64_t get_millisecond () {
struct timeb tb;
ftime(&tb);
return (uint64_t)tb.millitm + (uint64_t)tb.time * 1000UL;
// tb.time is the number of seconds since 00:00:00 January 1, 1970 UTC time;
// tb.millitm is the number of milliseconds in a second
}
// function : parse_int
// description : get a int value from string (support hexadecimal and decimal)
int parse_int (char *string, int *pvalue) {
if ( string[0] == '0' && string[1] == 'x' ) // HEX format "0xXXXXXXXX"
return sscanf( &(string[2]), "%x", pvalue);
else // DEC format
return sscanf( string , "%d", pvalue);
}
char USAGE [] =
"Usage: \n"
" %s <dev_r_name> <dev_w_name> <in_file_name> <out_file_name> <video_frame_width> <video_frame_height>\n"
"\n"
"Where:\n"
" in_file_name : .yuv file which contains the raw YUV pixels of a video,\n"
" must be in YUYV format (a type of YUV 4:2:2 packed format)\n"
" out_file_name : .m2v file which contains the MPEG2 stream,\n"
"\n"
"Example:\n"
" %s /dev/xdma0_c2h_0 /dev/xdma0_h2c_0 in.yuv out.m2v 640 480\n"
"\n" ;
// video frame size limitations (limited by the device) -----------------------------------------------------------------
#define VIDEO_FRAME_XSIZE16_MIN 4 // min frame width = 4 * 16 = 64
#define VIDEO_FRAME_XSIZE16_MAX 64 // max frame width = 4 * 64 = 1024
#define VIDEO_FRAME_YSIZE16_MIN 4 // min frame height = 4 * 16 = 64
#define VIDEO_FRAME_YSIZE16_MAX 64 // max frame height = 4 * 64 = 1024
#define IN_BUF_SIZE 0x200000
#define OUT_BUF_SIZE 0x200000
int main (int argc, char *argv[]) {
static char in_buffer [IN_BUF_SIZE]; // this buffer is for the RAW pixels which is to send to the device
static char out_buffer [OUT_BUF_SIZE]; // this buffer is for the MPEG2 stream get from the device
int ret = -1;
uint64_t millisecond;
char usage_string [1024];
char *dev_r_name, *dev_w_name, *in_file_name, *out_file_name;
int xsize, ysize;
int dev_r_fd = -1, dev_w_fd = -1;
FILE *in_fp = NULL, *out_fp = NULL;
sprintf(usage_string, USAGE, argv[0], argv[0] );
if (argc < 7) { // not enough argument
puts(usage_string);
return -1;
}
dev_r_name = argv[1];
dev_w_name = argv[2];
in_file_name = argv[3];
out_file_name = argv[4];
if ( 0 == parse_int(argv[5], &xsize) ) { // parse video width (xsize) from command line argument
puts(usage_string);
return -1;
}
if ( 0 == parse_int(argv[6], &ysize) ) { // parse video height (ysize) from command line argument
puts(usage_string);
return -1;
}
// print information -----------------------------------
printf("device : %s %s\n" , dev_r_name, dev_w_name);
printf("input yuv file : %s\n" , in_file_name);
printf("output m2v file : %s\n" , out_file_name);
printf("video size : %d x %d\n" , xsize, ysize );
// check video frame size -----------------------------------
if ( xsize % 16 != 0 ) {
printf("*** ERROR: xsize must be multiple of 16\n");
return -1;
}
if ( ysize % 16 != 0 ) {
printf("*** ERROR: ysize must be multiple of 16\n");
return -1;
}
if ( xsize < 16*VIDEO_FRAME_XSIZE16_MIN || xsize > 16*VIDEO_FRAME_XSIZE16_MAX ) {
printf("*** ERROR: xsize must not smaller than %d or larger than %d", 16*VIDEO_FRAME_XSIZE16_MIN, 16*VIDEO_FRAME_XSIZE16_MAX);
return -1;
}
if ( ysize < 16*VIDEO_FRAME_YSIZE16_MIN || ysize > 16*VIDEO_FRAME_YSIZE16_MAX ) {
printf("*** ERROR: ysize must not smaller than %d or larger than %d", 16*VIDEO_FRAME_YSIZE16_MIN, 16*VIDEO_FRAME_YSIZE16_MAX);
return -1;
}
dev_r_fd = open(dev_r_name, O_RDWR); // open device
if (dev_r_fd < 0) {
printf("*** ERROR: failed to open device %s\n", dev_r_name);
goto close_and_clear;
}
dev_w_fd = open(dev_w_name, O_RDWR); // open device
if (dev_w_fd < 0) {
printf("*** ERROR: failed to open device %s\n", dev_w_name);
goto close_and_clear;
}
in_fp = fopen(in_file_name, "rb"); // open file for read
if (in_fp == NULL) {
printf("*** ERROR: failed to open file %s for read\n", in_file_name);
goto close_and_clear;
}
out_fp = fopen(out_file_name, "wb"); // open file for write
if (out_fp == NULL) {
printf("*** ERROR: failed to open file %s for write\n", out_file_name);
goto close_and_clear;
}
millisecond = get_millisecond(); // get start time
if ( mpeg2encoder_reset(dev_w_fd) ) { // reset the mpeg2encoder device
printf("*** ERROR: failed to reset device\n");
goto close_and_clear;
}
if ( mpeg2encoder_set_video_frame_size(dev_w_fd, xsize/16, ysize/16) ) { // set video frame size
printf("*** ERROR: failed to set video frame size\n");
goto close_and_clear;
}
do {
int in_len, out_len, out_overflow;
in_len = fread((void*)in_buffer, 1, IN_BUF_SIZE, in_fp); // file -> in_buffer (raw YUV pixels)
if ( mpeg2encoder_put_pixels(dev_w_fd, (void*)in_buffer, in_len) ) { // in_buffer -> device (raw YUV pixels)
printf("*** ERROR: failed to put pixels\n");
goto close_and_clear;
}
if ( feof(in_fp) ) // if input file is ended, send "video sequence stop" signal
if ( mpeg2encoder_set_sequence_stop(dev_w_fd) ) {
printf("*** ERROR: failed to set sequence stop\n");
goto close_and_clear;
}
if ( mpeg2encoder_get_outbuf (dev_r_fd, dev_w_fd, (void*)out_buffer, &out_len, &out_overflow) ) { // device -> out_buffer (MPEG2 stream)
printf("*** ERROR: failed to get data from device\n");
goto close_and_clear;
}
if ( out_overflow )
printf("*** WARNING: device's out buffer overflow, out data stream is partly corrupted\n");
if ( out_len != fwrite((void*)out_buffer, 1, out_len, out_fp) ) { // out_buffer -> file (MPEG2 stream)
printf("*** ERROR: failed to write %s\n", out_file_name);
goto close_and_clear;
}
} while ( ! feof(in_fp) ) ;
millisecond = get_millisecond() - millisecond; // get time consumption
millisecond = (millisecond > 0) ? millisecond : 1; // avoid divide-by-zero
printf("input size = %ld\n", ftell(in_fp) );
printf("output size = %ld\n", ftell(out_fp) );
printf("compression ratio = %.1lf\n", (double) ftell(in_fp) / (ftell(out_fp) + 1) );
printf("time=%lu ms input throughput = %.1lf KBps\n", millisecond, (double) ftell(in_fp) / millisecond );
ret = 0;
close_and_clear:
if (dev_r_fd >= 0)
close(dev_r_fd);
if (dev_w_fd >= 0)
close(dev_r_fd);
if (in_fp)
fclose(in_fp);
if (out_fp)
fclose(out_fp);
return ret;
}