-
Notifications
You must be signed in to change notification settings - Fork 1
/
Encoder.m
306 lines (230 loc) · 12.9 KB
/
Encoder.m
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
clc;
clear all;
close all;
%%
% Reads Video.
videoSequence = VideoReader('walk_qcif.avi');
% Stores first 10 frames.
count = 1;
while (hasFrame(videoSequence) && count <11);
frameNumber(count).cdata = readFrame(videoSequence);
count = count+1;
end
originalFrameCopy = frameNumber; % Keeps copy of Original Frames.
%%
% Intialize size for database and stores matrices which will finally be copied to database for transmission.
encoderBufferZigzagLumaAC = cell(1,5);
encoderBufferDifferentialLumaDC = cell(1,5);
encoderBufferZigzagCbAC = cell(1,5);
encoderBufferDifferentialCbDC = cell(1,5);
encoderBufferZigzagCrAC = cell(1,5);
encoderBufferDifferentialCrDC = cell(1,5);
encoderBufferMVCurrX = cell(1,5);
encoderBufferMVCurrY = cell(1,5);
encoderBufferIndicationMatrix = cell(1,5);
%% Encoder.
for number = 6:(count-1);
% Displays Original Current Frame at Encoder in RGB format.
figure();
imshow(frameNumber(number).cdata);
title(['Original Current Frame ' int2str(number) ' at ENCODER in RGB Format']);
% Converts RGB to YCbCr.
currentFrameYCbCr = rgb2ycbcr(frameNumber(number).cdata);
currentFrameY = currentFrameYCbCr(:,:,1); % Extracts Luminance Component.
currentFrameCb = currentFrameYCbCr(:,:,2); % Extracts Chrominance(Cb) Component.
currentFrameCr = currentFrameYCbCr(:,:,3); % Extracts Chrominance(Cr) Component.
% Size of each Frame.
[row, column] = size(currentFrameY);
% 4:2:0 Downsampling.
subSampledYCurrentFrame = currentFrameY;
subSampledCbCurrentFrame = currentFrameCb(1:2:end, 1:2:end);
subSampledCrCurrentFrame = currentFrameCr(1:2:end, 1:2:end);
% Intializes Motion Vector Matrix to store the values.
motionVectorXValue = zeros(row/16,column/16,'double');
motionVectorYValue = zeros(row/16,column/16,'double');
% Initializes Difference Frames for Luma and Chroma components.
differenceFrameLuma = zeros(row, column, 'double');
differenceFrameCb = zeros(row/2, column/2, 'double');
differenceFrameCr = zeros(row/2, column/2, 'double');
% Initializes Indication matrix.
indicationMatrix = zeros(row/16,column/16,'double');
% Initializes Luma and Chroma components for Reference Frame.
subSampledYReferenceFrame = zeros(row, column, 'double');
subSampledCbReferenceFrame = zeros(row/2, column/2, 'double');
subSampledCrReferenceFrame = zeros(row/2, column/2, 'double');
%% Motion Estimation.
% If Current Frame is not I frame, we need to find MV and Difference Matrix.
if(number ~= 6)
% Displays Original Reconstructed Reference Frame at Encoder in RGB format.
figure();
imshow(frameNumber(number-1).cdata);
title(['Reference Frame ' int2str(number-1) ' at ENCODER in RGB Format']);
% Converts RGB to YCbCr.
referenceFrameYCbCr = rgb2ycbcr(frameNumber(number-1).cdata);
referenceFrameY = referenceFrameYCbCr(:,:,1); % Extracts Luminance Component.
referenceFrameCb = referenceFrameYCbCr(:,:,2); % Extracts Chrominance(Cb) Component.
referenceFrameCr = referenceFrameYCbCr(:,:,3); % Extracts Chrominance(Cr) Component.
% 4:2:0 Downsampling.
subSampledYReferenceFrame = referenceFrameY;
subSampledCbReferenceFrame = referenceFrameCb(1:2:end, 1:2:end);
subSampledCrReferenceFrame = referenceFrameCr(1:2:end, 1:2:end);
% Extracts Reference and Current Frame Blocks for Motion Estimation.
for p = 0:((row/16)-1);
for q = 0:((column/16)-1);
currentMacroBlock = double(currentFrameY((p*16)+1:(p+1)*16, (q*16)+1:(q+1)*16));
% Creates Search Window of suitable size.
if((((q*16)+1)-8)<=0)
startColumnSearchWindow = 1;
else
startColumnSearchWindow = (((q*16)+1)-8);
end
if((((p*16)+1)-8)<=0)
startRowSearchWindow = 1;
else
startRowSearchWindow = (((p*16)+1)-8);
end
if(((q+1)*16)+8 >= column)
endColumnSearchWindow = column;
else
endColumnSearchWindow = ((q+1)*16)+8;
end
if(((p+1)*16)+8 >= row)
endRowSearchWindow = row;
else
endRowSearchWindow = ((p+1)*16)+8;
end
referenceSW = double(referenceFrameY(startRowSearchWindow : endRowSearchWindow, startColumnSearchWindow : endColumnSearchWindow));
% Stores relative X, Y values and difference MB.
[ xShift, yShift, differenceFrameLuma((p*16)+1:(p+1)*16, (q*16)+1:(q+1)*16)] = exhaustiveSearchAlgorithm( currentMacroBlock, referenceSW);
% Calculates Motion Vector.
motionVectorXValue(p+1,q+1) = (p*16 + 1) - (xShift - 1 + startRowSearchWindow);
motionVectorYValue(p+1,q+1) = (q*16 + 1) - (yShift - 1 + startColumnSearchWindow);
end
end
figure;
imshow(uint8(differenceFrameLuma));
title(['Luma Component of Difference frame for Current Frame No. ' int2str(number+1) ' at ENCODER']);
% Plots Motion Vector.
[x,y] = meshgrid(1:((column/16)),1:((row/16)));
figure;
quiver(x,y, motionVectorXValue, motionVectorYValue);
title(['Motion Vector for Frame No. ' int2str(number+1)]);
% Creates Difference Matrix for Chroma components.
for p=0:(row/16)-1;
for q=0:(column/16)-1;
movementXChroma = floor(motionVectorXValue(p+1,q+1)/2);
movementYChroma = floor(motionVectorYValue(p+1,q+1)/2);
differenceFrameCb((p*8)+1:(p+1)*8, (q*8)+1:(q+1)*8) = double(subSampledCbCurrentFrame((p*8)+1:(p+1)*8, (q*8)+1:(q+1)*8)) - double(subSampledCbReferenceFrame(((p*8)+1-movementXChroma):(((p+1)*8)-movementXChroma), ((q*8)+1-movementYChroma):(((q+1)*8)-movementYChroma)));
differenceFrameCr((p*8)+1:(p+1)*8, (q*8)+1:(q+1)*8) = double(subSampledCrCurrentFrame((p*8)+1:(p+1)*8, (q*8)+1:(q+1)*8)) - double(subSampledCrReferenceFrame(((p*8)+1-movementXChroma):(((p+1)*8)-movementXChroma), ((q*8)+1-movementYChroma):(((q+1)*8)-movementYChroma)));
end
end
end
% If Current Frame is I frame, our differenceFrame* matrix will be zero.
% So we need to copy subSampled*CurrentFrame matrix value to them.
if(number==6)
differenceFrameLuma = double(subSampledYCurrentFrame);
differenceFrameCb = double(subSampledCbCurrentFrame);
differenceFrameCr = double(subSampledCrCurrentFrame);
end
% Difference Matrix and MV for Current Frame are derived.
%% Discrete Cosine Transform (DCT).
[rowY, colY] = size(differenceFrameLuma);
[rowCbCr,colCbCr] = size(differenceFrameCb);
% DCT works on 8x8 matrix. Thus padding is required if size is not a multiple of 8x8.
padderY = zeros(mod(rowY,8),mod(colY,8));
padderCbCr = zeros(mod(rowCbCr,8),mod(colCbCr,8));
% Padding in case size is not a multiple of 8x8 or 16x16.
YDCTMatrix = [differenceFrameLuma; double(padderY)];
CbDCTMatrix = [differenceFrameCb; double(padderCbCr)];
CrDCTMatrix = [differenceFrameCr; double(padderCbCr)];
% Size of padded matrix.
[rowPaddedY, colPaddedY] = size(YDCTMatrix);
[rowPaddedCbCr, colPaddedCbCr] = size(CbDCTMatrix);
% Performs DCT for Luma component, processing 8x8 blocks at a time.
for m = 0:((rowPaddedY/8)-1);
for n = 0:((colPaddedY/8)-1);
YDCTCurrentFrame((m*8)+1:(m+1)*8, (n*8)+1:(n+1)*8) = dct2(YDCTMatrix((m*8)+1:(m+1)*8, (n*8)+1:(n+1)*8));
end
end
% pDCT = @dct2;
% YDCTCurrentFrame = blkproc (YDCTMatrix, [8 8], pDCT); % Calculates DCT for Luma component
% Performs DCT for Chroma components, processing 8x8 blocks at a time.
for m = 0:((rowPaddedCbCr/8)-1);
for n = 0:((colPaddedCbCr/8)-1);
CbDCTCurrentFrame((m*8)+1:(m+1)*8, (n*8)+1:(n+1)*8) = dct2(CbDCTMatrix((m*8)+1:(m+1)*8, (n*8)+1:(n+1)*8));
CrDCTCurrentFrame((m*8)+1:(m+1)*8, (n*8)+1:(n+1)*8) = dct2(CrDCTMatrix((m*8)+1:(m+1)*8, (n*8)+1:(n+1)*8));
end
end
% CbDCTCurrentFrame = blkproc (CbDCTMatrix, [8 8], pDCT);
% CrDCTCurrentFrame = blkproc (CrDCTMatrix, [8 8], pDCT);
%% Quantization.
quantizationValue = 28;
YQuantized = round(YDCTCurrentFrame/quantizationValue);
CbQuantized = round(CbDCTCurrentFrame/quantizationValue);
CrQuantized = round(CrDCTCurrentFrame/quantizationValue);
%% Zig-Zag Scan.
% Performs Zig-Zag for difference Luma and Chroma components.
% Passing 2nd argument in zigZag function helps in prevention of
% sending those MBs which have MV (0,0) and SAD less than 128.
[currentLumaDC, zigZagCurrentLumaAC] = zigZag(YQuantized, indicationMatrix, 2);
[currentCbDC, zigZagCurrentCbAC] = zigZag(CbQuantized, indicationMatrix, 1);
[currentCrDC, zigZagCurrentCrAC] = zigZag(CrQuantized, indicationMatrix, 1);
%% Calculates Differential DC coefficients.
differentialCurrentLumaDC = differentialCoding(currentLumaDC);
differentialCurrentCbDC = differentialCoding(currentCbDC);
differentialCurrentCrDC = differentialCoding(currentCrDC);
%% Saves the parameters for Transmission.
encoderBufferZigzagLumaAC{1,number-5} = zigZagCurrentLumaAC;
encoderBufferDifferentialLumaDC{1,number-5} = differentialCurrentLumaDC;
encoderBufferZigzagCbAC{1,number-5} = zigZagCurrentCbAC;
encoderBufferDifferentialCbDC{1,number-5} = differentialCurrentCbDC;
encoderBufferZigzagCrAC{1,number-5} = zigZagCurrentCrAC;
encoderBufferDifferentialCrDC{1,number-5} = differentialCurrentCrDC;
encoderBufferMVCurrX{1,number-5} = motionVectorXValue;
encoderBufferMVCurrY{1,number-5} = motionVectorYValue;
encoderBufferIndicationMatrix{1,number-5} = indicationMatrix;
%% Inbuild Decoder.
[reconstructedCurrentLuma, reconstructedCurrentCb, reconstructedCurrentCr] = inbuildDecoder(YQuantized, CbQuantized, CrQuantized, motionVectorXValue, motionVectorYValue, subSampledYReferenceFrame, subSampledCbReferenceFrame, subSampledCrReferenceFrame, indicationMatrix);
%% Upsampling using Row Column Replication.
% Luma does not need upsampling.
upsampledCurrentLuma = reconstructedCurrentLuma;
% Upsamples Cb and Cr components using Row Column Replication.
upsampledCurrentCb = zeros (row, column, 'uint8'); %Creates zeros matrix for Cb component.
upsampledCurrentCr = zeros (row, column, 'uint8'); %Creates zeros matrix for Cr component.
upsampledCurrentCb(1:2:end,1:2:end)=reconstructedCurrentCb(1:end,1:end);
upsampledCurrentCb(2:2:end,:)=upsampledCurrentCb(1:2:end,:);
upsampledCurrentCb(:,2:2:end)=upsampledCurrentCb(:,1:2:end);
upsampledCurrentCr(1:2:end,1:2:end)= reconstructedCurrentCr(1:end,1:end);
upsampledCurrentCr(2:2:end,:)=upsampledCurrentCr(1:2:end,:);
upsampledCurrentCr(:,2:2:end)=upsampledCurrentCr(:,1:2:end);
% Concatenates for YCbCr image.
reconstructedYCbCr = cat(3, upsampledCurrentLuma, upsampledCurrentCb, upsampledCurrentCr);
% Converts YCbCr to RGB format.
reconstructedRGB = ycbcr2rgb(reconstructedYCbCr);
% Displays reconstructed current frame at Encoder.
figure();
imshow(reconstructedRGB);
title(['Reconstructed Current RGB frame ' int2str(number) ' at ENCODER']);
%% Peak Signal to Noise Ratio.
Y_Error = upsampledCurrentLuma - currentFrameY; % Error in Luma component.
MSEY = sum(sum(Y_Error.^2))/(row*column); % Calculates MSE for Luma component.
PSNRY = 10*log10((255.^2)/MSEY); % Calculates PSNR.
display(['Calculated PSNR for Luma component of Current Frame no. ' int2str(number) ' is = ' num2str(PSNRY)]);
%% Refills the Database.
frameNumber(number).cdata(:,:,1) = reconstructedRGB(:,:,1);
frameNumber(number).cdata(:,:,2) = reconstructedRGB(:,:,2);
frameNumber(number).cdata(:,:,3) = reconstructedRGB(:,:,3);
end
%% Intializes Database for Transmission.
database = cell(1,9);
database{1,1} = encoderBufferZigzagLumaAC;
database{1,2} = encoderBufferDifferentialLumaDC;
database{1,3} = encoderBufferZigzagCbAC;
database{1,4} = encoderBufferDifferentialCbDC;
database{1,5} = encoderBufferZigzagCrAC;
database{1,6} = encoderBufferDifferentialCrDC;
database{1,7} = encoderBufferMVCurrX;
database{1,8} = encoderBufferMVCurrY;
database{1,9} = encoderBufferIndicationMatrix;
%% Decoder.
MCSHomework4_Decoder(database);