Sie sind auf Seite 1von 6

MATLAB Model for MPEG Video Compression Algorithm

close all
clc
%reading video file frame by frame and stored video is used as a pointer to
%access frames one buy on
[video_file,file]=uigetfile({'*.avi';'*.*'},'Open Video');
video=VideoReader(video_file);
nframes=size(video)
%movie(video,1,473)
% separating I frame,B frame and P frame and converting them to grayscale
% image
I_frame=double(rgb2gray(video(1).cdata))
B_frame=double(rgb2gray(video(30).cdata))
P_frame=double(rgb2gray(video(80).cdata))
%to see what images have been captured as I B and P frame
imshow(uint8(I_frame))
imshow(uint8(I_frame))
imshow(uint8(P_frame))
%this is the prediction function which will take I_frame and P_frame as
%Input and return the motion vector and predicted frame. Using predicted
%frame prediction error is generated on the decoder side only compressed
%prediction error and motion vector is availabe. image compression of
%prediction error is much more than normal frame as it has all zeroes
%except where there is difference between predicted_frame and I_frame
%(To check detail how this function works look the .m file for this
%function)
[motion_vector_P,encoder_predicted_P_frame]=forwardprediction(I_frame,P_frame)
prediction_error_P=P_frame-encoder_predicted_P_frame
%for encoding of B_frame we consider both forward prediction and backward
%prediction and combine this two such that generated prediction error frame
%will have a lot compression capabilities
[forward_motion_vector,forward_predicted_B_frame]=forwardprediction(I_frame,B_frame
)
forward_prediction_error=B_frame-forward_predicted_B_frame
%this will generate backward motion vector and backward prediction error
[backward_motion_vector,backward_predicted_B_frame]=backwardprediction(P_frame,B_fr
ame)
backward_prediction_error=B_frame-backward_predicted_B_frame
%this function will generate prediction error and motion vector from
%forward prediction and backward prediction such that the resulted
%preiction error can provide much more compression ratio
%(for detail see .m file for this function)
[motion_vector_B,prediction_error_B]=
encode_B_frame(forward_motion_vector,backward_motion_vector,forward_prediction_erro
r,backward_prediction_error)
%Now from encoder will only send compressed I frame,compressed prediction error for
P and B frame
% Here I have perform only Dct from whole compression
%flow.
I_frame_dct=dct_function(I_frame)
prediction_error_P_dct=dct_function(prediction_error_P)
prediction_error_B_dct=dct_function(prediction_error_B)
%%%quantization not included in c code
quantize_prediction_error_P= quant_funct(prediction_error_P_dct)
quantize_prediction_error_B= quant_funct(prediction_error_B_dct)
quantize_I_frame=quant_funct(I_frame_dct)
%%%%%%%%%%%%%%%%%%%%decoder%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%side%%%%%%%%%%%%%%%%%%%%%
%%dequantization not included in c code
dequantize_prediction_error_P_dct= dequant_funct(quantize_prediction_error_P)
dequantize_prediction_error_B_dct= dequant_funct(quantize_prediction_error_B)
dequantize_I_frame_dct=dequant_funct(quantize_I_frame)
%After receiving compressed frames decoder will decompress these frames.
%here only inverse DCT is performed
I_frame_idct=idct_function(dequantize_I_frame_dct)
prediction_error_P_idct=idct_function(dequantize_prediction_error_P_dct)
prediction_error_B_idct=idct_function(dequantize_prediction_error_B_dct)
%After decompressed Predcition error and motion vector is available decoder
%will generate P frame
decoder_predicted_P_frame=deoderprediction(motion_vector_P,I_frame_idct)
decoded_P_frame=prediction_error_P_idct+decoder_predicted_P_frame
%After decompressed Predcition error and motion vector is available decoder
%will generate B frame
decoder_predicted_B_frame=B_frame_decoder_prediction(motion_vector_B,I_frame_idct,d
ecoded_P_frame)
decoded_B_frame=prediction_error_B_idct+decoder_predicted_B_frame
%plotting of frames from begining to intermidiate stges
subplot(2,7,1)
imshow(uint8(I_frame))
title('encoder I frame')
subplot(2,7,2)
imshow(uint8(P_frame))
title('encoder P frame')
subplot(2,7,3)
imshow(uint8(B_frame))
title('encoder B frame')
subplot(2,7,4)
imshow(uint8(prediction_error_P))
title('prediction error of P')
subplot(2,7,5)
imshow(uint8(prediction_error_B))
title('prediction error of B')
subplot(2,7,6)
imshow(uint8(I_frame_dct))
title('dct of I_frame')
subplot(2,7,7)
imshow(uint8(prediction_error_P_dct))
title('dct of prediction error of P')
subplot(2,7,8)
imshow(uint8(prediction_error_B_dct))
title('dct of prediction error of B')
subplot(2,7,9)
imshow(uint8(I_frame_idct))
title('inverse dct of I_frame')
subplot(2,7,10)
imshow(uint8(prediction_error_P_idct))
title('inverse dct of prediction error of P')
subplot(2,7,11)
imshow(uint8(prediction_error_B_idct))
title('inverse dct of prediction error of B')
subplot(2,7,12)
imshow(uint8(decoded_P_frame))
title('decoder P frame')
subplot(2,7,13)
imshow(uint8(decoded_B_frame))
title('decoder B frame')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Internal
Functions %%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function mb_MAD = MAD(P_block,I_block)
global mb
mb_MAD= sum(sum(abs((P_block-I_block))))/mb^2;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function quantize_frame= quant_funct(frame)
qtz_mtrx=[16 11 10 16 24 40 51 61;12 12 14 19 26 58 60 55;14 13 16 24 40 57 69
56;14 17 22 29 51 87 80 62;18 12 37 56 68 109 103 77;24 35 55 64 81 104 113 92;49
64 78 87 103 121 120 101;72 92 95 98 112 100 103 99];
[m n]=size(frame);
% add ofset of 100 for the elimination of the negative pixel values
img_os= frame+200;
for i=1:8:m
for j=1:8:n
quantize_frame(i:i+7,j:j+7)=round(img_os(i:i+7,j:j+7)./qtz_mtrx);
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function dequantize_frame= dequant_funct(frame)
qtz_mtrx=[16 11 10 16 24 40 51 61;12 12 14 19 26 58 60 55;14 13 16 24 40 57 69
56;14 17 22 29 51 87 80 62;18 12 37 56 68 109 103 77;24 35 55 64 81 104 113 92;49
64 78 87 103 121 120 101;72 92 95 98 112 100 103 99];
[m n]=size(frame);
for i=1:8:m
for j=1:8:n
dequantize_frame_os(i:i+7,j:j+7)=frame(i:i+7,j:j+7).*qtz_mtrx;
end
end
% add ofset of 100 for the elimination of the negative pixel values
dequantize_frame=dequantize_frame_os-200;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function frame_dct=dct_function(frame)
[m n]=size(frame);
for a=1:8:m
for b=1:8:n
frame_dct(a:a+7,b:b+7)=dct2(frame(a:a+7,b:b+7));
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function frame_idct=idct_function(frame)
[m n]=size(frame);
for a=1:8:m
for b=1:8:n
frame_idct(a:a+7,b:b+7)=idct2(frame(a:a+7,b:b+7));
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [motion_vector,new_B_frame]=forwardprediction(I_frame,B_frame)
global mb_MAD mb_MAD_3d prediction_error mb range
[m n]=size(I_frame);
threshold=16;
mb=16;
range=20;
vector_size =(m*n)/(mb^2);
motion_vector = zeros(3,vector_size);
mb_MAD=256*ones(15,15);
%-----Block Matching Algorithm----------------%
count =1;
for i=1:mb:m-mb+1
for j=1:mb:n-mb+1
min=512;
k1=0;
l1=0;
for k= -range:range
for l = -range:range
if ((i+k)<1||(j+l)<1||(i+k)+mb-1>m||(j+l)+mb-1>n)
continue;
end
%MAD function stands for Mean Absolute Difference ,which
%decide weather two macro block are matching or not
%depending on allowed threshold
min_MAD=MAD(B_frame(i:i+mb-1,j:j+mb-1),I_frame((i+k):(i+k+mb-1),
(j+l):(j+l+mb-1)));
if min_MAD<min
min=min_MAD;
k1=k;
l1=l;
end
end
end
%comparing with threshold value to check intra-MBs---%
if min > threshold
new_B_frame(i:i+mb-1,j:j+mb-1) = I_frame(i:i+mb-1,j:j+mb-1);
motion_vector(1,count) = 0; %Horizontal motion vector
motion_vector(2,count) = 0; %vertical motion vector
motion_vector(3,count) = 0;
else
new_B_frame(i:i+mb-1,j:j+mb-1)= I_frame((i+k1):(i+k1+mb-1),(j+l1):
(j+l1+mb-1));
motion_vector(1,count) = k1; %Horizontal motion vector
motion_vector(2,count) = l1; %vertical motion vector
motion_vector(3,count) = 0;
end
count=count+1;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [motion_vector,new_B_frame]=backwardprediction(I_frame,B_frame)
global mb_MAD mb_MAD_3d prediction_error mb range
[m n]=size(I_frame);
threshold=16;
mb=16;
range=20;
vector_size =(m*n)/(mb^2);
motion_vector = zeros(3,vector_size);
mb_MAD=256*ones(15,15);
%-----Block Matching Algorithm----------------%
count =1;
for i=1:mb:m-mb+1
for j=1:mb:n-mb+1
min=512;
k1=0;
l1=0;
for k= -range:range
for l = -range:range
if ((i+k)<1||(j+l)<1||(i+k)+mb-1>m||(j+l)+mb-1>n)
continue;
end
min_MAD=MAD(B_frame(i:i+mb-1,j:j+mb-1),I_frame((i+k):(i+k+mb-1),
(j+l):(j+l+mb-1)));
if min_MAD<min
min=min_MAD;
k1=k;
l1=l;
end
end
end
%comparing with threshold value to check intra-MBs---%
if min > threshold
new_B_frame(i:i+mb-1,j:j+mb-1) = I_frame(i:i+mb-1,j:j+mb-1);
motion_vector(1,count) = 0; %Horizontal motion vector
motion_vector(2,count) = 0; %vertical motion vector
motion_vector(3,count) = 1;
else
new_B_frame(i:i+mb-1,j:j+mb-1)= I_frame((i+k1):(i+k1+mb-1),(j+l1):
(j+l1+mb-1));
motion_vector(1,count) = k1; %Horizontal motion vector
motion_vector(2,count) = l1; %vertical motion vector
motion_vector(3,count) = 1;

end
count=count+1;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [motion_vector,prediction_error]=
encode_B_frame(forward_motion_vector,backward_motion_vector,forward_prediction_erro
r,backward_prediction_error)
global mb
cnt=1;
mb=16;
[m n]=size(forward_prediction_error);
vector_size =(m*n)/(mb^2);
motion_vector = zeros(3,vector_size);
for i=1:mb:m-mb+1
for j=1:mb:n-mb+1
energy_fp=sum(sum(forward_prediction_error(i:i+mb-1,j:j+mb-1).^2))
energy_bp=sum(sum(backward_prediction_error(i:i+mb-1,j:j+mb-1).^2))
%energy defines that which block will provide more compressionto
%ratio according to that particular macroblock is selected for
%prediction error
if energy_fp > energy_bp
prediction_error(i:i+mb-1,j:j+mb-1) = backward_prediction_error(i:i+mb-
1,j:j+mb-1);
motion_vector(1,cnt) = backward_motion_vector(1,cnt); %Horizontal
motion vector
motion_vector(2,cnt) = backward_motion_vector(2,cnt); %vertical motion
vector
motion_vector(3,cnt) = backward_motion_vector(3,cnt);
else
prediction_error(i:i+mb-1,j:j+mb-1) = forward_prediction_error(i:i+mb-
1,j:j+mb-1);
motion_vector(1,cnt) = forward_motion_vector(1,cnt); %Horizontal
motion vector
motion_vector(2,cnt) = forward_motion_vector(2,cnt); %vertical motion
vector
motion_vector(3,cnt) = forward_motion_vector(3,cnt);
end
cnt=cnt+1;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function decoder_predicted_frame=deoderprediction(motion_vector,I_frame)
global mb
cnt=1;
mb=16;
[m n]=size(I_frame);
vector_size=(m*n)/(mb^2);
for i=1:mb:m-mb+1
for j=1:mb:n-mb+1
k1=motion_vector(1,cnt); %Horizontal motion vector
l1=motion_vector(2,cnt);
decoder_predicted_frame(i:i+mb-1,j:j+mb-1)= I_frame((i+k1):(i+k1+mb-1),
(j+l1):(j+l1+mb-1));
cnt=cnt+1;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function
decoder_predicted_B_frame=B_frame_decoder_prediction(motion_vector,I_frame,P_frame)
global mb
cnt=1;
mb=16;
[m n]=size(I_frame);
vector_size=(m*n)/(mb^2);
for i=1:mb:m-mb+1
for j=1:mb:n-mb+1
k1=motion_vector(1,cnt); %Horizontal motion vector
l1=motion_vector(2,cnt);
decide=motion_vector(3,cnt);
if(decide==0)
decoder_predicted_B_frame(i:i+mb-1,j:j+mb-1)= I_frame((i+k1):(i+k1+mb-
1),(j+l1):(j+l1+mb-1));
else
decoder_predicted_B_frame(i:i+mb-1,j:j+mb-1)= P_frame((i+k1):(i+k1+mb-
1),(j+l1):(j+l1+mb-1));
end
cnt=cnt+1;
end
end

Das könnte Ihnen auch gefallen