From 00637026ea05ab028b437b48693ed66be1ed4a9a Mon Sep 17 00:00:00 2001 From: Muaz Ahmad Date: Mon, 27 Nov 2023 15:42:01 +0500 Subject: [PATCH] file reading and queue prepend --- src/input.zig | 62 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 58 insertions(+), 4 deletions(-) diff --git a/src/input.zig b/src/input.zig index 1e8d12a..a7c84a0 100644 --- a/src/input.zig +++ b/src/input.zig @@ -3,8 +3,62 @@ const std = @import("std"); const util = @import("util.zig"); const threads = @import("threads.zig"); -pub fn main_loop(f: std.fs.File, buffs: util.Buffers, thread_mgr: threads.ThreadManager) !void { - _ = thread_mgr; - _ = buffs; - _ = f; +inline fn lum_idxs(i: usize, j: usize) struct { usize, usize, usize } { + return .{ + i / 2, + j / 2, + 2 * (i % 2) + (j % 2), + }; +} + +fn read_lum(f: std.fs.File, source_buff: [][][4]util.Block, target_buff: [][][4]util.BlockQuantized, io_buff: []u8, queue: *util.JobQueue) !void { + const block_h = source_buff.len; + const block_w = source_buff[0].len; + + for (0..block_h * 2) |i| { + _ = try f.read(io_buff); + var io_idx: usize = 0; + for (0..8) |I| { + for (0..block_w * 2) |j| { + const idxs = lum_idxs(i, j); + @memcpy(source_buff[idxs.@"0"][idxs.@"1"][idxs.@"2"][I * 8 .. (I + 1) * 8], io_buff[io_idx .. io_idx + 8]); + io_idx += 8; + if (I == 7) { + try queue.prepend(util.Job{ + .source = &source_buff[idxs.@"0"][idxs.@"1"][idxs.@"2"], + .target = &target_buff[idxs.@"0"][idxs.@"1"][idxs.@"2"], + }); + } + } + } + } +} + +fn read_chrom(f: std.fs.File, source_buff: [][]util.Block, target_buff: [][]util.BlockQuantized, io_buff: []u8, queue: *util.JobQueue) !void { + const block_h = source_buff.len; + const block_w = source_buff[0].len; + + for (0..block_h) |i| { + _ = try f.read(io_buff); + var io_idx: usize = 0; + for (0..8) |I| { + for (0..block_w) |j| { + @memcpy(source_buff[i][j][I * 8 .. (I + 1) * 8], io_buff[io_idx .. io_idx + 8]); + io_idx += 8; + if (I == 7) { + try queue.prepend(util.Job{ + .source = &source_buff[i][j], + .target = &target_buff[i][j], + }); + } + } + } + } +} + +pub fn main_loop(f: std.fs.File, buffs: util.Buffers, thread_mgr: threads.ThreadManager) !void { + try read_lum(f, buffs.Y, buffs.Y_quant, buffs.input_buff, thread_mgr.queue_wrp.queue); + try read_chrom(f, buffs.U, buffs.U_quant, buffs.input_buff[0 .. buffs.input_buff.len / 2], thread_mgr.queue_wrp.queue); + try read_chrom(f, buffs.V, buffs.V_quant, buffs.input_buff[0 .. buffs.input_buff.len / 2], thread_mgr.queue_wrp.queue); + std.time.sleep(10000000); }