Mentions légales du service

Skip to content
Snippets Groups Projects
Commit 3c2cc7fd authored by DIB Elian's avatar DIB Elian
Browse files

Changed default overwrite behaviour on linux/other, added comments

parent 965f6211
Branches
No related tags found
No related merge requests found
function frames = read(varargin) function frames = read(varargin)
%READ Summary of this function goes here %READ Write yuv file from a cell array of frames
% Detailed explanation goes here % READ(frames,'name',name) read yuv file with corresponding to name filename
% READ(frames,'recDir',recDir) read from directory recDir
% READ(frames,'subSamp',subSamp) specify YUV sampling scheme
% READ(frames,'writePrecision',writePrecision) specify write data type
% READ(frames,'readPrecision',readPrecision) specify read data type
%% Parse input parameters
subSamps = {'400','420','422','444'}; subSamps = {'400','420','422','444'};
types = {'bit','ubit','int','uint','float','single','double'}; types = {'bit','ubit','int','uint','float','single','double'};
validatePrecision = @(prec) any(validatestring(utils.precision(prec),types)); validatePrecision = @(prec) any(validatestring(utils.precision(prec),types));
defRecDir = fullfile(pwd,'Rec'); defRecDir = fullfile(pwd,'Rec');
p = inputParser; p.StructExpand = true; p.KeepUnmatched = true; p = inputParser; p.StructExpand = true; p.KeepUnmatched = true;
p.addParameter('recDir', defRecDir , @ischar);
p.addParameter('name' , 'sequence', @ischar); p.addParameter('name' , 'sequence', @ischar);
p.addParameter('recDir', defRecDir , @ischar);
p.parse(varargin{:}); p.parse(varargin{:});
yuvParams = p.Results; yuvParams = p.Results;
%% Set remaining default parameters from matfile if it exists
try try
m = get_mat(yuvParams.recDir,yuvParams.name,false); m = get_mat(yuvParams.recDir,yuvParams.name,false);
defSubSamp = m.subSamp; defSubSamp = m.subSamp;
...@@ -26,27 +32,26 @@ catch ...@@ -26,27 +32,26 @@ catch
end end
p.addParameter('subSamp' , defSubSamp , @(x) any(validatestring(x,subSamps) )); p.addParameter('subSamp' , defSubSamp , @(x) any(validatestring(x,subSamps) ));
p.addParameter('readPrecision' , defReadPrecision , validatePrecision);
p.addParameter('writePrecision', defWritePrecision, validatePrecision); p.addParameter('writePrecision', defWritePrecision, validatePrecision);
p.addParameter('readPrecision' , defReadPrecision , validatePrecision);
p.parse(varargin{:}); p.parse(varargin{:});
yuvParams = p.Results; yuvParams = p.Results;
subSamp = yuvParams.subSamp;
readPrecision = yuvParams.readPrecision;
writePrecision = yuvParams.writePrecision;
name = yuvParams.name; name = yuvParams.name;
recDir = yuvParams.recDir; recDir = yuvParams.recDir;
subSamp = yuvParams.subSamp;
writePrecision = yuvParams.writePrecision;
readPrecision = yuvParams.readPrecision;
precision = [writePrecision '=>' readPrecision];
%% Recover yuv size
m = get_mat(recDir,name,false); m = get_mat(recDir,name,false);
yuvSize = m.yuvSize; yuvSize = m.yuvSize;
imgRes = yuvSize(1:2); imgRes = yuvSize(1:2);
imgSize = yuvSize(3:end); imgSize = yuvSize(3:end);
precision = [writePrecision '=>' readPrecision]; %% YUV 400/420/422: define interpolation sampling grids
filename = fullfile(recDir,[name,'.yuv']);
[xq,xgv] = deal(1:imgSize(1)); [xq,xgv] = deal(1:imgSize(1));
[yq,ygv] = deal(1:imgSize(2)); [yq,ygv] = deal(1:imgSize(2));
...@@ -62,22 +67,30 @@ subImgSize = [numel(xgv),numel(ygv)]; ...@@ -62,22 +67,30 @@ subImgSize = [numel(xgv),numel(ygv)];
imgGrid = zeros(imgRes); imgGrid = zeros(imgRes);
%% Read frames iteratively
filename = fullfile(recDir,[name,'.yuv']);
fileID = fopen(filename,'r'); fileID = fopen(filename,'r');
if strcmp(subSamp,'400') if strcmp(subSamp,'400')
frames = arrayfun(@readY,imgGrid,'UniformOutput',false); frames = arrayfun(@readY,imgGrid,'UniformOutput',false);
else else
frames = arrayfun(@readYUV,imgGrid,'UniformOutput',false); frames = arrayfun(@readYUV,imgGrid,'UniformOutput',false);
end end
fclose(fileID); fclose(fileID);
%% Auxiliary function
% Read a single frame of given dimensions
function frame = readFrame(imgSize) function frame = readFrame(imgSize)
frame = fread(fileID,flip(imgSize),precision)'; frame = fread(fileID,flip(imgSize),precision)';
end end
% Read a single Y frame
function Y = readY(~) function Y = readY(~)
Y = readFrame(imgSize); Y = readFrame(imgSize);
end end
%Read Y, U and V frames
function YUV = readYUV(~) function YUV = readYUV(~)
Y = readFrame(imgSize); Y = readFrame(imgSize);
U.Values = readFrame(subImgSize); U.Values = readFrame(subImgSize);
......
function yuvSize = write(frames,varargin) function yuvSize = write(frames,varargin)
%WRITE Summary of this function goes here %WRITE Write yuv file from a cell array of frames
% Detailed explanation goes here % WRITE(frames,'name',name) use name as yuv filename
% WRITE(frames,'refDir',refDir) write in directory refDir
% WRITE(frames,'subSamp',subSamp) specify YUV sampling scheme
% WRITE(frames,'writePrecision',writePrecision) specify write data type
% WRITE(frames,'readPrecision',readPrecision) specify read data type
%% Parse input parameters
subSamps = {'400','420','422','444'}; subSamps = {'400','420','422','444'};
types = {'bit','ubit','int','uint','float','single','double'}; types = {'bit','ubit','int','uint','float','single','double'};
validatePrecision = @(prec) any(validatestring(utils.precision(prec),types)); validatePrecision = @(prec) any(validatestring(utils.precision(prec),types));
defRefDir = fullfile(pwd,'Ref'); defRefDir = fullfile(pwd,'Ref');
p = inputParser; p.StructExpand = true; p.KeepUnmatched = true; p = inputParser; p.StructExpand = true; p.KeepUnmatched = true;
p.addParameter('refDir', defRefDir , @ischar);
p.addParameter('name' , 'sequence', @ischar); p.addParameter('name' , 'sequence', @ischar);
p.addParameter('refDir', defRefDir , @ischar);
p.parse(varargin{:}); p.parse(varargin{:});
yuvParams = p.Results; yuvParams = p.Results;
%% Set remaining default parameters from matfile if it exists
try try
m = get_mat(yuvParams.refDir,yuvParams.name,false); m = get_mat(yuvParams.refDir,yuvParams.name,false);
defSubSamp = m.subSamp; defSubSamp = m.subSamp;
defReadPrecision = m.readPrecision;
defWritePrecision = m.writePrecision; defWritePrecision = m.writePrecision;
defReadPrecision = m.readPrecision;
catch catch
defSubSamp = subSamps{1}; defSubSamp = subSamps{1};
defReadPrecision = class(frames{1});
defWritePrecision = class(frames{1}); defWritePrecision = class(frames{1});
defReadPrecision = class(frames{1});
end end
p.addParameter('subSamp' , defSubSamp , @(x) any(validatestring(x,subSamps) )); p.addParameter('subSamp' , defSubSamp , @(x) any(validatestring(x,subSamps) ));
p.addParameter('readPrecision' , defReadPrecision , validatePrecision);
p.addParameter('writePrecision', defWritePrecision, validatePrecision); p.addParameter('writePrecision', defWritePrecision, validatePrecision);
p.addParameter('readPrecision' , defReadPrecision , validatePrecision);
p.parse(varargin{:}); p.parse(varargin{:});
yuvParams = p.Results; yuvParams = p.Results;
name = yuvParams.name;
refDir = yuvParams.refDir;
subSamp = yuvParams.subSamp; subSamp = yuvParams.subSamp;
readPrecision = yuvParams.readPrecision; readPrecision = yuvParams.readPrecision;
writePrecision = yuvParams.writePrecision; writePrecision = yuvParams.writePrecision;
name = yuvParams.name;
refDir = yuvParams.refDir;
imgRes = size(frames); imgRes = size(frames);
imgSize = size(frames{1}); imgSize = size(frames{1});
yuvSize = [imgRes,imgSize]; yuvSize = [imgRes,imgSize];
%% Save parameters to read yuv file back
m = get_mat(refDir,name,true); m = get_mat(refDir,name,true);
m.yuvSize = yuvSize;
m.subSamp = subSamp;
m.readPrecision = readPrecision;
m.writePrecision = writePrecision;
m.name = name; m.name = name;
m.refDir = refDir; m.refDir = refDir;
m.subSamp = subSamp;
m.writePrecision = writePrecision;
m.readPrecision = readPrecision;
m.yuvSize = yuvSize;
%% Incompatibility (needs better handling)
if (~strcmp(subSamp,'400')&&numel(imgSize)~=3) if (~strcmp(subSamp,'400')&&numel(imgSize)~=3)
error('Incompatible frame size and subsampling'); error('Incompatible frame size and subsampling');
end end
%% YUV 400: pick first channel for every frame
frames = cellfun(@double,frames,'UniformOutput',false); frames = cellfun(@double,frames,'UniformOutput',false);
if strcmp(subSamp,'400') if strcmp(subSamp,'400')
frames = cellfun(@(f) f(:,:,1),frames,'UniformOutput',false); frames = cellfun(@(f) f(:,:,1),frames,'UniformOutput',false);
end end
%% YUV 420/422: define interpolation sampling grids
[xq,xgv] = deal(1:imgSize(1)); [xq,xgv] = deal(1:imgSize(1));
[yq,ygv] = deal(1:imgSize(2)); [yq,ygv] = deal(1:imgSize(2));
...@@ -71,31 +81,38 @@ end ...@@ -71,31 +81,38 @@ end
[U,V] = deal(griddedInterpolant({xgv,ygv},zeros(imgSize(1:2)))); [U,V] = deal(griddedInterpolant({xgv,ygv},zeros(imgSize(1:2))));
if ~exist(refDir,'dir') %% Write frames iteratively
mkdir(refDir);
end
filename = fullfile(refDir,[name,'.yuv']); filename = fullfile(refDir,[name,'.yuv']);
% Checker wether yuv file already exists
if ~exist(filename,'file') if ~exist(filename,'file')
[pathstr,~,~] = fileparts(filename); mkdir(refDir);
if ~exist(pathstr,'dir') else
mkdir(pathstr) if isunix && ~ismac
end warning('No overwriting allowed by default on Linux');
fileID = fopen(filename,'w+'); warning('This is to avoid conflict between concurrent threads on IGRIDA');
if strcmp(subSamp,'400') warning('You can modify this behaviour by commenting this condition in the source code');
cellfun(@writeFrame ,frames,'UniformOutput',false); return;
else
cellfun(@writeFrames,frames,'UniformOutput',false);
end end
fclose(fileID); end
fileID = fopen(filename,'w+');
if strcmp(subSamp,'400')
cellfun(@writeFrame ,frames,'UniformOutput',false);
else else
warning('YUV file already exists'); cellfun(@writeFrames,frames,'UniformOutput',false);
end end
fclose(fileID);
%% Auxiliary functions
% Write a single Y frame assuming 400 sampling scheme
function writeFrame(frame) function writeFrame(frame)
fwrite(fileID,cast(frame',readPrecision),writePrecision); fwrite(fileID,cast(frame',readPrecision),writePrecision);
end end
% Write Y,U and V frames assuming 422/420 sampling scheme
function writeFrames(frames) function writeFrames(frames)
writeFrame(frames(:,:,1)); writeFrame(frames(:,:,1));
U.Values = frames(:,:,2); U.Values = frames(:,:,2);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment