aboutsummaryrefslogtreecommitdiff
path: root/webrtc/modules/video_processing/main/test/unit_test/readYUV420file.m
blob: 03013efd3a6ce99486ec48f147939dca0e370669 (plain)
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
function [Y,U,V] = readYUV420file(filename, width, height)
% [Y,U,V] = readYUVfile(filename, width, height)

fid = fopen(filename,'rb');
if fid==-1
    error(['Cannot open file ' filename]);
end

% Number of pixels per image
nPx=width*height;

% nPx bytes luminance, nPx/4 bytes U, nPx/4 bytes V
frameSizeBytes = nPx*1.5; 

% calculate number of frames
fseek(fid,0,'eof'); % move to end of file
fileLen=ftell(fid); % number of bytes
fseek(fid,0,'bof'); % rewind to start

% calculate number of frames
numFrames = floor(fileLen/frameSizeBytes);

Y=uint8(zeros(height,width,numFrames));
U=uint8(zeros(height/2,width/2,numFrames));
V=uint8(zeros(height/2,width/2,numFrames));

[X,nBytes]=fread(fid, frameSizeBytes, 'uchar');

for k=1:numFrames
    
    % Store luminance
    Y(:,:,k)=uint8(reshape(X(1:nPx), width, height).');
    
    % Store U channel
    U(:,:,k)=uint8(reshape(X(nPx + (1:nPx/4)), width/2, height/2).');

    % Store V channel
    V(:,:,k)=uint8(reshape(X(nPx + nPx/4 + (1:nPx/4)), width/2, height/2).');
    
    % Read next frame
    [X,nBytes]=fread(fid, frameSizeBytes, 'uchar');
end

    
fclose(fid);