-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw2.cp
246 lines (204 loc) · 6.51 KB
/
hw2.cp
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
//
// Writing faster code Spring 2009
// Software Optimizations Optimizations
//
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <chrono>
//#include "HPClock.h"
#define CLIP(x) ((x<0?0:(x>255?255:x)))
typedef struct {
unsigned short bfType;
unsigned long bfSize;
unsigned short bfReserved1;
unsigned short bfReserved2;
unsigned long bfOffBits;
} BITMAPFILEHEADER_T;
typedef struct {
unsigned long biSize;
long biWidth;
long biHeight;
unsigned short biPlanes;
unsigned short biBitCount;
unsigned long biCompression;
unsigned long biSizeImage;
long biXPelsPerMeter;
long biYPelsPerMeter;
unsigned long biClrUsed;
unsigned long biClrImportant;
} BITMAPINFOHEADER_T;
// convert YUV to BMP
void yuv2bmp(unsigned char *pFrame[3], int nWidth, int nHeight, unsigned char *pBBuf, int *nBSize){
BITMAPINFOHEADER_T stInfoHdr;
BITMAPFILEHEADER_T stFileHdr;
unsigned char* pRGBData;
int i, j, k, Y, Cb, Cr;
/* BMP file header*/
stFileHdr.bfType = 0x4D42;
stFileHdr.bfReserved1 = 0;
stFileHdr.bfReserved2 = 0;
stFileHdr.bfOffBits = 14 + 40;
stFileHdr.bfSize = stFileHdr.bfOffBits + nWidth*nHeight*3;
/* info header */
stInfoHdr.biSize = sizeof(BITMAPINFOHEADER_T);
stInfoHdr.biWidth = nWidth;
stInfoHdr.biHeight = nHeight*-1;
stInfoHdr.biPlanes = 1;
stInfoHdr.biBitCount = 24;
stInfoHdr.biCompression = 0;
stInfoHdr.biSizeImage = nWidth*nHeight*3;
stInfoHdr.biXPelsPerMeter = 0;
stInfoHdr.biYPelsPerMeter = 0;
stInfoHdr.biClrUsed = 0;
stInfoHdr.biClrImportant = 0;
memcpy(pBBuf, (char*)&(stFileHdr.bfType), 2);
memcpy(pBBuf+2, (char*)&(stFileHdr.bfSize), 4);
memcpy(pBBuf+6, (char*)&(stFileHdr.bfReserved1), 2);
memcpy(pBBuf+8, (char*)&(stFileHdr.bfReserved2), 2);
memcpy(pBBuf+10, (char*)&(stFileHdr.bfOffBits), 4);
memcpy(pBBuf+14, (char*)&stInfoHdr, 40);
pRGBData = pBBuf + stFileHdr.bfOffBits;
for(i = 0, k = 0; i < nWidth*3; i+=3, k++){
for(j = 0; j < nHeight; j++){
Y = (unsigned char) pFrame[0][j*nWidth+k];
Cr = (unsigned char)pFrame[2][j*nWidth+k];
Cb = (unsigned char)pFrame[1][j*nWidth+k];
pRGBData[nWidth*3*j+i+0] = CLIP(Y + 1.772*(Cb-128)); // B
pRGBData[nWidth*3*j+i+1] = CLIP(Y - 0.34414*(Cb-128) - 0.71414*(Cr-128)); // G
pRGBData[nWidth*3*j+i+2] = CLIP(Y + 1.402*(Cr-128)); // R
}
}
*nBSize = stFileHdr.bfSize;
}
void Usage (){
printf("\nUsage:\n\thw2 -i <input YUV file> -w <width> -h <height> -o <output bmp file> -n <number of frames to convert>\n");
}
int main(int argc, char *argv[]){
int i,j,k, nIdx;
//HPClock oClock;
unsigned char *pYUVData[3], *pBMPData;
int nWidth, nHeight, nNumFrames=1;
FILE *fpIn, *fpOut;
fpIn = fpOut = NULL;
if(argc < 11){
Usage();
exit(0);
}
nIdx = 1;
while(nIdx < argc){
switch(argv[nIdx][1]){
case 'i':
case 'I':
nIdx++;
fpIn = fopen(argv[nIdx], "rb");
if(fpIn == NULL){
printf("\nCannot open input file: %s", argv[nIdx]);
return 0;
}
break;
case 'o':
case 'O':
nIdx++;
fpOut = fopen(argv[nIdx], "wb");
if(fpOut == NULL){
printf("\nCannot open output file: %s", argv[nIdx]);
return 0;
}
break;
case 'w':
case 'W':
nIdx++;
nWidth = atoi(argv[nIdx]);
break;
case 'h':
case 'H':
nIdx++;
nHeight = atoi(argv[nIdx]);
break;
case 'n':
case 'N':
nIdx++;
nNumFrames = atoi(argv[nIdx]);
break;
default:
printf("\nUnknown option: -%c", argv[nIdx][1]);
Usage();
exit(0);
break;
}
nIdx++;
}
// YUV data buffer
pYUVData[0] = new unsigned char [nWidth*nHeight];
pYUVData[1] = new unsigned char [nWidth*nHeight];
pYUVData[2] = new unsigned char [nWidth*nHeight];
// buffer for RGB bitmap; BMP header is 54 buyes
pBMPData = new unsigned char [nWidth*nHeight*3+54];
int nBMPSize = 0;
double nBTime = 0.0;
/* Each YUV frame has Y, U, and V components.
The data for each component is stored in consecutive memory locations.
The Y component size is nWidth*nHeight bytes.
This corresponds to nHeight lines with nWidth pixels per line.
The U and V components are each (nWidth/2)*(nHeight/2) bytes.
The size of YUV frame is: nWidth*nHeight + 2*(nWidth/2)*(nHeight/2) = 1.5*nWidth*nHeight
The YUV files used in this HW do now have any header.
*/
for(;nNumFrames>0;nNumFrames--){
// read the YUV frame
// read Y component
int nBytesRead = fread(pYUVData[0],1, nWidth*nHeight, fpIn);
if(nBytesRead < nWidth*nHeight){
printf("\nNot enough data to read YUV frame");
break;
}
// read U component
nBytesRead = fread(pYUVData[1],1, nWidth*nHeight/4, fpIn);
if(nBytesRead < nWidth*nHeight/4){
printf("\nNot enough data to read YUV frame");
break;
}
// read V component
nBytesRead = fread(pYUVData[2],1, nWidth*nHeight/4, fpIn);
if(nBytesRead < nWidth*nHeight/4){
printf("\nNot enough data to read YUV frame");
break;
}
//oClock.Start();
// upsample YUV to 444 format before converting to RGB
// U and V components are upsampled to nWidth*nHeight
// resolution for RGB conversion
std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now(); //start time
k = 0;
int nUVPitch = nWidth/2;
unsigned char *pUData, *pVData, *pUDest0, *pVDest0, *pUDest1, *pVDest1;
for(i = nHeight - 1; i >=0 ; i-=2){
pUData = pYUVData[1]+(i/2)*nUVPitch;
pVData = pYUVData[2]+(i/2)*nUVPitch;
pUDest0 = pYUVData[1]+i*nWidth;
pVDest0 = pYUVData[2]+i*nWidth;
pUDest1 = pYUVData[1]+(i-1)*nWidth;
pVDest1 = pYUVData[2]+(i-1)*nWidth;
for(j = 0, k = 0; j < nWidth/2; j++, k+=2){
pUDest0[k] = pUDest0[k+1] = pUDest1[k] = pUDest1[k+1] = pUData[j];
pVDest0[k] = pVDest0[k+1] = pVDest1[k] = pVDest1[k+1] = pVData[j];
}
}
yuv2bmp(pYUVData, nWidth, nHeight, pBMPData, &nBMPSize);
//nBTime += oClock.Stop();
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); //end time
std::chrono::steady_clock::duration runtime = end - start; //runtime computation
nBTime = std::chrono::duration_cast<std::chrono::nanoseconds>(runtime).count();
}
printf("\nBase BMP Encoding Time in nanoseconds: %f\n", nBTime);
fwrite(pBMPData, 1, nBMPSize, fpOut);
fclose(fpIn);
fclose(fpOut);
delete [] pYUVData[0];
delete [] pYUVData[1];
delete [] pYUVData[2];
delete [] pBMPData;
return 0;
}