%% basic params
z = 60; % zoom level (so results can be seen in an iconic fashion
w=1920/z; h=1080/z; % size of frame to render
nOrig = 8; % # of frames to process

%% Simulate the Canon HG10, pre-telecine 
f = createRenderContext(w,h);
d1920x1080p24 = cell(nOrig,1);
d1440x1080p24 = cell(nOrig,1);
d1440x1080i48 = cell(nOrig,1);
for i=1:nOrig
  % render the frame with a solid background and a letter
  cla;
  rectangle('Position',[-1 -1 2 2],'FaceColor',[1-mod(i,2) 0 mod(i,2)]);
  text(0,0, char('A'+i-1), 'FontSize',0.80*h, 'FontWeight','bold', 'Color','white', 'HorizontalAlignment','center', 'VerticalAlignment','middle');
  frame = double(renderFig(f))/255;
  
  % make every other line dark for visualizing fields
  for r=2:2:h
    frame(r,:,:) = frame(r,:,:) * 0.5;
  end
  
  % frame/field recording
  d1920x1080p24{i} = frame;
  d1440x1080p24{i} = imresize(frame, [h w*1440/1920], 'lanczos3');
  d1440x1080i48{i*2-1} = d1440x1080p24{i}(1:2:end,:,:);
  d1440x1080i48{i*2  } = d1440x1080p24{i}(2:2:end,:,:);
end
close(f);

%% Introduce pulldown for PF24
d1440x1080i60 = cell(nOrig,1);
pulldown23 = [0,1, 2,3, 2,5, 4,7, 6,7];
for idx60i=0:10:nOrig*5/4*2-1 % everything's 0-indexed here
  base48i = idx60i*4/5;
  assert(floor(base48i)==base48i);
  for subpart=0:9
    d1440x1080i60{idx60i+1+subpart} = d1440x1080i48{base48i+1+pulldown23(subpart+1)};
  end
end

% DOES 3:2 pulldown
% for idx60i=0:nOrig*5/4*2-1 % everything's 0-indexed here
%   pair24p          = floor(idx60i/5);     % which *frame* pair
%   telecineFieldIdx = mod(idx60i,5);       % 0-4 part of a telecine block
%   topFieldFirst    = mod(pair24p,2)==0;   % bool
%   if topFieldFirst
%     if (telecineFieldIdx < 3)
%       idx48i = pair24p*4 +     mod(telecineFieldIdx,2);
%     else
%       idx48i = pair24p*4 + 2 + 1-(telecineFieldIdx-3);
%     end
%   else
%     if (telecineFieldIdx < 3)
%       idx48i = pair24p*4 +     1-mod(telecineFieldIdx,2);
%     else
%       idx48i = pair24p*4 + 2 + (telecineFieldIdx-3);
%     end
%   end
% 
%   d1440x1080i60{idx60i+1} = d1440x1080i48{idx48i+1};
% end

%% Simulate various codecs
interVideo_video_decode = cell(nOrig*5/4,1);
interVideo_video_deint  = cell(nOrig*5/4,1);
for i=1:length(d1440x1080i60)/2
  reconstructed = d1440x1080i60{i*2-1};
  reconstructed = imresize(reconstructed,[h w*1440/1920],'nearest');
  reconstructed(2:2:end,:,:) = d1440x1080i60{i*2};
  interVideo_video_decode{i} = reconstructed;
  
  reconstructed = d1440x1080i60{i*2-1};
  reconstructed(1:end,:,:) = (reconstructed(1:end,:,:) + d1440x1080i60{i*2})/2;
  interVideo_video_deint{i} = imresize(reconstructed, [h w*1440/1920], 'lanczos3');
end

interVideo_capture = cell(nOrig*5/4,1);
arcSoft = cell(nOrig*5/4,1);
for i=1:length(interVideo_capture)
  frame24p  = floor((i-1)*4/5);
  interVideo_capture{i} = d1440x1080p24{frame24p+1};
  arcSoft{i} = interVideo_capture{i}(1:2:end,:,:);
end

%% Render results for the web
figure(1);clf;
fieldh = h/2;
i60timing = 3;
i48timing = (fieldh+i60timing)*5/4 - fieldh;
p24timing = ((fieldh+i60timing)*5/4)*2 - h;
r30timing = i60timing*2;
f30timing = i60timing*2 + fieldh;
clips = {'d1920x1080p24',           p24timing;
         'd1440x1080p24',           p24timing;
         'd1440x1080i48',           i48timing;
         'd1440x1080i60',           i60timing;
         'interVideo_video_decode', r30timing;
         'interVideo_video_deint',  r30timing;
         'interVideo_capture',      r30timing;
         'arcSoft'                  f30timing; };
nClips = size(clips,1);
for i=1:nClips
  subplot(1,nClips,i); 
  clip = eval(clips{i,1});
  nframes = length(clip);
  fh = size(clip{1},1);
  bh = clips{i,2};
  % we should use a struct instead of eval...
  tiled = imtile(eval(clips{i}), bh);
  fprintf('h:%d b:%d\n',size(tiled,1),bh);
  imshow(tiled); 
  imwrite(imresize(tiled,3,'nearest'), [clips{i} '.png']);
end
fprintf('\n');
