actually run on ds lol

This commit is contained in:
sillysagiri 2024-08-10 10:43:28 +07:00
parent eade12482c
commit e102d7c198
6 changed files with 426 additions and 142 deletions

5
.gitignore vendored
View File

@ -7,9 +7,8 @@
.cache .cache
/nitrofs/image.bin /nitrofs/*.bin
/nitrofs/music.raw /nitrofs/*.raw
/nitrofs/palette.bin
compile_commands.json compile_commands.json
*.nds *.nds

View File

@ -29,8 +29,8 @@ SDIMAGE := image.bin
# Source code paths # Source code paths
# ----------------- # -----------------
SOURCEDIRS := src libs/FastLZ/fastlz.c libs/RaylibDS libs/lwrb/lwrb/src/lwrb SOURCEDIRS := src libs/FastLZ/fastlz.c libs/RaylibDS
INCLUDEDIRS := src libs/FastLZ libs/RaylibDS libs/lwrb/lwrb/src/include INCLUDEDIRS := src libs/FastLZ libs/RaylibDS
GFXDIRS := GFXDIRS :=
BINDIRS := BINDIRS :=
AUDIODIRS := AUDIODIRS :=

View File

@ -1,15 +1,27 @@
#!/bin/sh #!/bin/sh
palette="palette.png" video1="video.mp4"
video="kiss2x.mp4" palette1="palette1.png"
# output="kiss2x_out.mp4" # output1="out_badapple.mp4"
output="out/out_%d.bmp" output1="out1/out_%d.bmp"
filters="scale=256x192:flags=lanczos:force_original_aspect_ratio=decrease,pad=256:192:-1:-1:color=black" video2="2d.mp4"
palette2="palette2.png"
output2="out2/out_%d.bmp"
# output="output.mp4"
filters="scale=256x192:flags=lanczos:force_original_aspect_ratio=decrease,pad=256:192:-1:-1:color=black,fps=30"
# video # video
ffmpeg -i $video -vf "$filters,palettegen=max_colors=256:reserve_transparent=0:stats_mode=diff" -y $palette ffmpeg -i $video1 -vf "$filters,palettegen=max_colors=256:reserve_transparent=0:stats_mode=diff" -y $palette1
ffmpeg -i $video -i $palette -filter_complex "$filters[x];[x][1:v]paletteuse=dither=none" -y $output ffmpeg -i $video1 -i $palette1 -filter_complex "$filters[x];[x][1:v]paletteuse=dither=none" -y $output1
# audio # audio
ffmpeg -i $video -f s16le -vn -ac 1 -ar 22050 -y music.raw ffmpeg -i $video1 -f s16le -vn -ac 2 -ar 22050 -y music1.raw
# video
# ffmpeg -i $video2 -vf "$filters,palettegen=max_colors=256:reserve_transparent=0:stats_mode=diff" -y $palette2
# ffmpeg -i $video2 -i $palette2 -filter_complex "$filters[x];[x][1:v]paletteuse=dither=none" -y $output2
# # audio
# ffmpeg -i $video2 -f s16le -vn -ac 2 -ar 22050 -y music2.raw

View File

@ -332,7 +332,7 @@ unsigned char *LZS_Fast(unsigned char *raw_buffer, size_t raw_len, size_t *new_l
unsigned int r, s, len_tmp, i; unsigned int r, s, len_tmp, i;
unsigned char mask; unsigned char mask;
lzs_vram = LZS_VFAST & 0xF; // lzs_vram = LZS_VFAST & 0xF;
pak_len = 4 + raw_len + ((raw_len + 7) / 8); pak_len = 4 + raw_len + ((raw_len + 7) / 8);
pak_buffer = Memory(pak_len, sizeof(char)); pak_buffer = Memory(pak_len, sizeof(char));

View File

@ -23,7 +23,7 @@ extern "C" unsigned char *LZS_Code(unsigned char *raw_buffer, size_t raw_len, si
#define PALETTE_SIZE 256 #define PALETTE_SIZE 256
#define FRAME_SIZE 256*192 #define FRAME_SIZE 256*192
#define CHUNK_SIZE 1 #define CHUNK_SIZE 8
void Frame_RAWtoRGB15(unsigned char *frame, unsigned char *dest, uint16_t *palette_buffer); void Frame_RAWtoRGB15(unsigned char *frame, unsigned char *dest, uint16_t *palette_buffer);
@ -68,7 +68,7 @@ void Convert(std::string frame_path, std::string palette_path, std::string outpu
uint32_t compress_size = 0; uint32_t compress_size = 0;
uint32_t compress_size_total = 0; uint32_t compress_size_total = 0;
size_t compress_size_biggest = 0; size_t compress_size_biggest = 0;
uint8_t compress_buffer[FRAME_SIZE*CHUNK_SIZE*2]; uint8_t compress_buffer[162000];
std::ofstream file_out; std::ofstream file_out;
@ -110,6 +110,28 @@ void Convert(std::string frame_path, std::string palette_path, std::string outpu
std::stringstream ss; std::stringstream ss;
file_out.open(output_path + output_basename + "_img.bin", std::ios::binary); file_out.open(output_path + output_basename + "_img.bin", std::ios::binary);
// ########## PER FRAME ENCODER ##########
// for (int i=0; i<frame_total; i++)
// {
// ss.str("");
// ss << frame_path << "out_" << i+1 << ".bmp";
// int frame_w, frame_h, frame_n;
// unsigned char *frame_raw = stbi_load(ss.str().c_str(), &frame_w, &frame_h, &frame_n, 3);
// Frame_RAWtoRGB15(frame_raw, frame_buffer, palette_buffer);
// compress_size = Compress(type, frame_buffer, FRAME_SIZE, compress_buffer);
// if ((compress_size) > compress_size_biggest) compress_size_biggest = compress_size;
// compress_size_total += compress_size;
// file_out.write(reinterpret_cast<const char*>(&compress_size), sizeof(uint32_t));
// file_out.write(reinterpret_cast<const char*>(compress_buffer), compress_size);
// printf("write %i bytes (%i/%i)\n", compress_size, i+1, frame_total);
// }
// ########################################
// Calculate the number of complete iterations needed // Calculate the number of complete iterations needed
int numIterations = frame_total / CHUNK_SIZE; int numIterations = frame_total / CHUNK_SIZE;
@ -186,7 +208,7 @@ int main()
{ {
// Convert(ASSETS_PATH "out1/", ASSETS_PATH "palette1.png", "../nitrofs/", "data_fastlz", 1); // Convert(ASSETS_PATH "out1/", ASSETS_PATH "palette1.png", "../nitrofs/", "data_fastlz", 1);
// Convert(ASSETS_PATH "out1/", ASSETS_PATH "palette1.png", "../nitrofs/", "data_rle", 2); // Convert(ASSETS_PATH "out1/", ASSETS_PATH "palette1.png", "../nitrofs/", "data_rle", 2);
Convert(ASSETS_PATH "out1/", ASSETS_PATH "palette1.png", "../nitrofs/", "data_lzss", 3); Convert(ASSETS_PATH "out1/", ASSETS_PATH "palette1.png", "../nitrofs/", "main", 3);
// Convert(ASSETS_PATH "out2/", ASSETS_PATH "palette2.png", "../nitrofs/", "sub"); // Convert(ASSETS_PATH "out2/", ASSETS_PATH "palette2.png", "../nitrofs/", "sub");
} }

View File

@ -1,58 +1,187 @@
#include "fastlz.h" #include "fastlz.h"
#include "filesystem.h" #include "filesystem.h"
#include "mm_types.h"
#include "nds/arm9/background.h" #include "nds/arm9/background.h"
#include "nds/arm9/cache.h"
#include "nds/arm9/console.h" #include "nds/arm9/console.h"
#include "nds/arm9/input.h"
#include "nds/arm9/sassert.h"
#include "nds/cothread.h" #include "nds/cothread.h"
#include "nds/decompress.h"
#include "nds/dma.h" #include "nds/dma.h"
#include "nds/interrupts.h"
#include "nds/timers.h" #include "nds/timers.h"
#include "raylibds.hpp" #include "raylibds.hpp"
#include <cstddef>
#include <cstdint> #include <cstdint>
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include <nds.h> #include <nds.h>
#include <maxmod9.h> #include <maxmod9.h>
#include <sys/_types.h>
#define PALETTE_SIZE 256 #define PALETTE_SIZE 256
#define FRAME_SIZE 256*192 #define FRAME_SIZE 256*192
// TODO: hardcode fix // TODO: hardcode fix
#define CHUNK_SIZE 10 #define CHUNK_SIZE 8
#define QUEUE_SIZE 6
#define FRAMERATE 30 #define FRAMERATE 30
#define VIDEO_TOTALFRAME 827 #define VIDEO_TOTALFRAME 6584
#define MMSTREAM_BUF_SIZE 800 #define VIDEO_RBUF_LEN CHUNK_SIZE*6
#define MUSIC_BUFFER_SIZE 16000 #define VIDEO_RBUF_BUFFER FRAME_SIZE
#define VIDEO_TEMP_SIZE 96100
#define MMSTREAM_BUF_SIZE 16*200
#define MUSIC_RBUF_SIZE 96000*2
void wait_forever(const char* msg); void wait_forever(const char* msg);
void LoadNextChunk(); bool LoadNextChunk(unsigned char *dest);
void TimerCallback(); void TimerCallback();
void VBLCallback(); void VBLCallback();
void FrameStep(); void FrameStep();
int ThreadEntrypoint(void *arg); int ThreadMusic(void *arg);
int ThreadVideo(void *arg); int ThreadVideo(void *arg);
mm_word on_stream_request( mm_word length, mm_addr dest, mm_stream_formats format ); mm_word on_stream_request( mm_word length, mm_addr dest, mm_stream_formats format );
volatile bool doDraw = true;
volatile bool isPause = false;
volatile int noframe = 0;
void *sassert_malloc(size_t size)
{
void *p = malloc(size);
sassert(p != nullptr, "Memory allocation failed %i", size);
return p;
}
struct RingBuffer { struct RingBuffer {
char *buffer = nullptr; unsigned char *buffer = nullptr;
int head;
int tail;
size_t buffer_size;
size_t size;
size_t count;
RingBuffer(size_t rb_len, size_t buffer_len, unsigned char * buffer_ptr)
{
buffer = buffer_ptr;
buffer_size = buffer_len;
size = rb_len;
head = 0;
tail = 0;
count = 0;
Raylib::nocashMessageFormat("per block %i", buffer_size/size);
}
unsigned char* Get()
{
if (count > 0)
{
count--;
unsigned char* ptr = &buffer[head*(buffer_size/size)];
head = (head+1) % size;
return ptr;
}
return nullptr;
}
unsigned char* Write()
{
if (count < size)
{
count++;
unsigned char* ptr = &buffer[tail*(buffer_size/size)];
tail = (tail+1) % size;
return ptr;
}
return nullptr;
}
unsigned char* Manual_Read()
{
unsigned char* ptr = &buffer[head*(buffer_size/size)];
return ptr;
}
unsigned char* Manual_Write()
{
unsigned char* ptr = &buffer[tail*(buffer_size/size)];
return ptr;
}
void Manual_Skip(int total)
{
count += total;
tail = (tail+total) % size;
}
void Manual_Advance(int total)
{
count -= total;
head = (head+total) % size;
}
size_t getFree()
{
return size-count;
}
};
struct Rbuf_Music {
unsigned char *buffer = nullptr;
int head; int head;
int tail; int tail;
int size; int size;
int count; int count;
RingBuffer(size_t size_t) Rbuf_Music(size_t buffer_size, unsigned char* buffer_ptr)
{ {
buffer = (char*)malloc(size_t); buffer = buffer_ptr;
size = size_t; size = buffer_size;
head = 0; head = 0;
tail = 0; tail = 0;
count = 0; count = 0;
} }
~RingBuffer() void Skip(int total)
{ {
free(buffer); count -= total;
head = (head+total) % size;
}
void Advance(int total)
{
count += total;
tail = (tail+total) % size;
}
int LinearReadSize()
{
return size - head;
}
int LinearWriteSize()
{
return size - tail;
}
size_t GetFree()
{
return size-count;
}
unsigned char *GetHead()
{
return &buffer[head];
}
unsigned char *GetTail()
{
return &buffer[tail];
} }
int read(char *dest, size_t len) int read(char *dest, size_t len)
@ -64,13 +193,10 @@ struct RingBuffer {
if (len < linear_len) if (len < linear_len)
memcpy(dest, &buffer[head], len); memcpy(dest, &buffer[head], len);
// swiCopy(&buffer[head], dest, len);
else else
{ {
memcpy(dest, &buffer[head], linear_len); memcpy(dest, &buffer[head], linear_len);
memcpy(&dest[linear_len], buffer, len-linear_len); memcpy(&dest[linear_len], buffer, len-linear_len);
// swiCopy(&buffer[head], dest, linear_len);
// swiCopy(buffer, &dest[linear_len], len-linear_len);
} }
head = (head+len) % size; head = (head+len) % size;
@ -90,13 +216,10 @@ struct RingBuffer {
if (len < linear_len) if (len < linear_len)
memcpy(&buffer[tail], data, len); memcpy(&buffer[tail], data, len);
// swiCopy(data, &buffer[tail], len);
else else
{ {
memcpy(&buffer[tail], data, linear_len); memcpy(&buffer[tail], data, linear_len);
memcpy(buffer, &data[linear_len], len-linear_len); memcpy(buffer, &data[linear_len], len-linear_len);
// swiCopy(data, &buffer[tail], linear_len);
// swiCopy(&data[linear_len], buffer, len-linear_len);
} }
tail = (tail+len) % size; tail = (tail+len) % size;
@ -110,24 +233,25 @@ struct RingBuffer {
int ptr_background; int ptr_background;
int ptr_subbackground; int ptr_subbackground;
FILE *file_video = nullptr;
FILE *file_music = nullptr; FILE *file_music = nullptr;
int file_music_len; int file_music_len;
int file_video_len;
int framecounter = 0; FILE *file_videomain = nullptr;
int file_videomain_len;
RingBuffer music_rb(MUSIC_BUFFER_SIZE); RingBuffer *videomain_rb;
void *music_btemp; unsigned char *frame_preload_temp;
unsigned char *frame_read_temp;
RingBuffer video_rb(FRAME_SIZE*CHUNK_SIZE*QUEUE_SIZE); Rbuf_Music *music_rb;
void *video_btemp; unsigned char *music_buffer_temp;
void *video_decompress;
void *video_compress;
u32 timer_video_start = 0; uint32_t preload_video_size = 0;
u32 timer_video_end = 0; uint32_t next_video_size = 0;
u32 timer_video_avg = 0;
volatile int timer_start = 0;
volatile int timer_avg = 0;
int main(void) { int main(void) {
@ -141,49 +265,57 @@ int main(void) {
consoleDemoInit(); consoleDemoInit();
consoleDebugInit(DebugDevice_NOCASH); consoleDebugInit(DebugDevice_NOCASH);
video_decompress = malloc(FRAME_SIZE*CHUNK_SIZE);
video_compress = malloc(FRAME_SIZE*CHUNK_SIZE);
video_btemp = malloc(FRAME_SIZE);
music_btemp = malloc(MUSIC_BUFFER_SIZE);
bool nitrofs = nitroFSInit(NULL); bool nitrofs = nitroFSInit(NULL);
sassert(nitrofs, "error nitrofs"); sassert(nitrofs, "nitrofs init failed");
file_video = fopen("nitro:/image.bin", "rb"); frame_preload_temp = (unsigned char*)sassert_malloc(VIDEO_TEMP_SIZE);
sassert(file_video != nullptr, "failed to load image.bin"); music_buffer_temp = (unsigned char*)sassert_malloc(MUSIC_RBUF_SIZE);
fseek(file_video, 0, SEEK_END);
file_video_len = ftell(file_video);
fseek(file_video, 0, SEEK_SET);
file_music = fopen("nitro:/music.raw", "rb"); videomain_rb = new RingBuffer(VIDEO_RBUF_LEN, VIDEO_RBUF_LEN*VIDEO_RBUF_BUFFER, (unsigned char*)sassert_malloc(VIDEO_RBUF_LEN*VIDEO_RBUF_BUFFER));
music_rb = new Rbuf_Music(MUSIC_RBUF_SIZE, (unsigned char*)sassert_malloc(MUSIC_RBUF_SIZE));
memset(music_rb->buffer, 0, MUSIC_RBUF_SIZE);
file_videomain = fopen("nitro:/main_img.bin", "rb");
sassert(file_videomain != nullptr, "failed to load image.bin");
fseek(file_videomain, 0, SEEK_END);
file_videomain_len = ftell(file_videomain);
fseek(file_videomain, 0, SEEK_SET);
file_music = fopen("nitro:/music1.raw", "rb");
sassert(file_music != nullptr, "failed to load music.raw"); sassert(file_music != nullptr, "failed to load music.raw");
fseek(file_music, 0, SEEK_END); fseek(file_music, 0, SEEK_END);
file_music_len = ftell(file_music); file_music_len = ftell(file_music);
fseek(file_music, 0, SEEK_SET); fseek(file_music, 0, SEEK_SET);
int pal_len;
unsigned char* pal = Raylib::LoadFile("nitro:/palette.bin", pal_len);
ptr_background = bgInit(3, BgType_Bmp8, BgSize_B8_256x256, 0, 0); ptr_background = bgInit(3, BgType_Bmp8, BgSize_B8_256x256, 0, 0);
ptr_subbackground = bgInitSub(3, BgType_Bmp8, BgSize_B8_256x256, VRAM_C_SUB_BG, 0); // ptr_subbackground = bgInitSub(3, BgType_Bmp8, BgSize_B8_256x256, VRAM_C_SUB_BG, 0);
DC_FlushRange(pal, pal_len); int palmain_len;
dmaCopy(pal, BG_PALETTE, pal_len); unsigned char* palmain = Raylib::LoadFile("nitro:/main_pal.bin", palmain_len);
dmaCopy(pal, BG_PALETTE_SUB, pal_len);
free(pal); DC_FlushRange(palmain, palmain_len);
dmaCopy(palmain, BG_PALETTE, palmain_len);
Raylib::nocashMessageFormat("preload video"); Raylib::UnloadFile(palmain);
while(video_rb.size-video_rb.count >= FRAME_SIZE*CHUNK_SIZE) {
LoadNextChunk(); {
video_rb.write((char*)video_decompress, FRAME_SIZE*CHUNK_SIZE); Raylib::nocashMessageFormat("preload video main");
do {
unsigned char *ptr = videomain_rb->Manual_Write();
LoadNextChunk(ptr);
videomain_rb->Manual_Skip(CHUNK_SIZE);
} while(videomain_rb->count+CHUNK_SIZE < videomain_rb->size);
Raylib::nocashMessageFormat("preload done");
} }
{ {
Raylib::nocashMessageFormat("preload music"); Raylib::nocashMessageFormat("preload audio");
int len = music_rb.size - music_rb.count; int len = music_rb->GetFree();
fread(music_btemp, sizeof(char), len, file_music); fread(music_rb->GetTail(), len, 1, file_music);
music_rb.write((char*)music_btemp, len); music_rb->Advance(len);
Raylib::nocashMessageFormat("preload done");
} }
mm_ds_system sys; mm_ds_system sys;
@ -191,42 +323,73 @@ int main(void) {
sys.samp_count = 0; sys.samp_count = 0;
sys.mem_bank = 0; sys.mem_bank = 0;
sys.fifo_channel = FIFO_MAXMOD; sys.fifo_channel = FIFO_MAXMOD;
mmInit( &sys ); mmInit(&sys);
mm_stream mystream; mm_stream mystream;
mystream.sampling_rate = 22050; mystream.sampling_rate = 22050;
mystream.buffer_length = MMSTREAM_BUF_SIZE; mystream.buffer_length = MMSTREAM_BUF_SIZE;
mystream.callback = on_stream_request; mystream.callback = on_stream_request;
mystream.format = MM_STREAM_16BIT_MONO; mystream.format = MM_STREAM_16BIT_STEREO;
mystream.timer = MM_TIMER2; mystream.timer = MM_TIMER1;
mystream.manual = false; mystream.manual = false;
timerStart(3, ClockDivider_1024, TIMER_FREQ_1024(FRAMERATE), TimerCallback); timerStart(0, ClockDivider_1024, TIMER_FREQ_1024(FRAMERATE), TimerCallback);
cothread_create(ThreadEntrypoint, NULL, 0, COTHREAD_DETACHED); irqSet(IRQ_VBLANK, []{
cothread_create(ThreadVideo, NULL, 0, COTHREAD_DETACHED); printf("\033[3;2H\033[Kno frame: %i\n", noframe);
printf("\033[4;2H\033[Kvideo buffer: %i frames\n", videomain_rb->count);
printf("\033[5;2H\033[Kmusic buffer: %i bytes\n", music_rb->count);
// static int counter = 0;
// counter++;
// if (counter % 30 == 0)
// printf("\033[6;2H\033[Kavg decode time: %ims\n", timerTicks2msec(timer_avg/30));
scanKeys();
if (keysUp() & KEY_A) isPause = !isPause;
if (keysUp() & KEY_B) FrameStep();
if (keysHeld() & KEY_Y) FrameStep();
});
cothread_create(ThreadMusic, NULL, 0, COTHREAD_DETACHED);
// cothread_create(ThreadVideo, NULL, 0, COTHREAD_DETACHED);
// cpuStartTiming(1);
mmStreamOpen( &mystream ); mmStreamOpen( &mystream );
cpuStartTiming(0);
while(1) { while(1) {
cothread_yield_irq(IRQ_VBLANK); cothread_yield();
scanKeys(); // swiWaitForVBlank();
// cothread_yield_irq(IRQ_VBLANK);
// scanKeys();
if (keysUp() & KEY_A) break;
printf("\033[4;2H\033[Kvideo buffer: %i", video_rb.count); // if (keysUp() & KEY_A) FrameStep();
printf("\033[5;2H\033[Kmusic buffer: %i", music_rb.count);
printf("\033[6;2H\033[Kprogress: %0.f%%", ((float)framecounter/VIDEO_TOTALFRAME)*100);
static int frame = 0; if (!isPause)
frame++; {
if (file_videomain)
{
unsigned char *ptr = videomain_rb->Manual_Write();
if (videomain_rb->count+CHUNK_SIZE < videomain_rb->size)
{
// timer_start = cpuGetTiming();
LoadNextChunk(ptr);
videomain_rb->Manual_Skip(CHUNK_SIZE);
// timer_avg = cpuGetTiming() - timer_start;
}
}
if (ftell(file_videomain) == file_videomain_len)
{
fclose(file_videomain);
file_videomain = nullptr;
// return 0;
}
}
if (frame % 30 == 0)
{
printf("\033[8;2H\033[Kavg decoding time: %" PRIu32 "ms", timerTicks2msec(timer_video_avg/30));
timer_video_avg = 0;
}
} }
return 0; return 0;
@ -234,49 +397,79 @@ int main(void) {
int ThreadVideo(void *arg) int ThreadVideo(void *arg)
{ {
while(1) { // while(1) {
cothread_yield(); // if (videomain_rb->getFree() > (sizeof(uint32_t)+preload_video_size) && videomain_rb->getFree() > videomain_rb->size/2)
// {
// // Raylib::nocashMessageFormat("video thread %i %i", videomain_rb->getFree(), preload_video_size+4);
if (file_video != nullptr && video_rb.size-video_rb.count >= FRAME_SIZE*CHUNK_SIZE) // // if (comutex_try_acquire(&mutex))
{ // // {
timer_video_start = cpuGetTiming(); // bool preload;
// do {
// preload = LoadNextChunk(file_videomain);
// } while(preload);
// // comutex_release(&mutex);
// // }
// }
LoadNextChunk(); // if (ftell(file_videomain) == file_videomain_len)
video_rb.write((char*)video_decompress, FRAME_SIZE*CHUNK_SIZE); // {
// fclose(file_videomain);
// file_videomain = nullptr;
// return 0;
// }
// cothread_yield();
// }
timer_video_end = cpuGetTiming(); // // Raylib::nocashMessageFormat("video thread exit");
timer_video_avg += timer_video_end - timer_video_start; // return 0;
if (ftell(file_video) == file_video_len)
{
fclose(file_video);
file_video = nullptr;
}
}
}
return 0;
} }
int ThreadEntrypoint(void *arg) int ThreadMusic(void *arg)
{ {
while(1) { while(1) {
cothread_yield(); cothread_yield();
if (file_music != nullptr && music_rb.count < MUSIC_BUFFER_SIZE/2) if (file_music != nullptr && music_rb->count < music_rb->size*0.35)
{ {
int len = music_rb.size - music_rb.count; int free = music_rb->GetFree();
int linear = music_rb->LinearWriteSize();
if ((file_music_len - ftell(file_music)) < len) // memset(music_buffer_temp, 0, MUSIC_RBUF_SIZE);
// fread(music_buffer_temp, free, 1, file_music);
if (free < linear)
{ {
memset(music_btemp, 0, MUSIC_BUFFER_SIZE); // memcpy(music_rb->GetTail(), music_buffer_temp, free);
fread(music_btemp, sizeof(char), (file_music_len - ftell(file_music)), file_music); fread(music_rb->GetTail(), 1, free, file_music);
fclose(file_music); }
file_music = nullptr; else
} else {
fread(music_btemp, sizeof(char), len, file_music); // memcpy(music_rb->GetTail(), music_buffer_temp, linear);
// memcpy(music_rb->buffer, music_buffer_temp, free-linear);
fread(music_rb->GetTail(), 1, linear, file_music);
fread(music_rb->buffer, 1, free-linear, file_music);
}
music_rb.write((char*)music_btemp, len); music_rb->Advance(free);
}
if (ftell(file_music) >= file_music_len)
{
fclose(file_music);
file_music = nullptr;
Raylib::nocashMessageFormat("music close!");
}
static bool closeOnce = false;
if (!closeOnce && file_music == nullptr && music_rb->count <= 0)
{
mmStreamClose();
Raylib::nocashMessageFormat("stream close!");
closeOnce = true;
} }
} }
@ -285,26 +478,55 @@ int ThreadEntrypoint(void *arg)
void FrameStep() void FrameStep()
{ {
if (video_rb.count) if (videomain_rb->count == 0 && !file_videomain) return;
if (videomain_rb->count)
{ {
video_rb.read((char*)video_btemp, FRAME_SIZE); // if (comutex_try_acquire(&mutex))
dmaCopyAsynch(video_btemp, bgGetGfxPtr(ptr_background), FRAME_SIZE); // {
framecounter++; // videomain_rb->Get();
} // videomain_rb->read((unsigned char*)&next_video_size, sizeof(uint32_t));
unsigned char *dest = videomain_rb->Get();
// Raylib::nocashMessageFormat("frame step start %p %i", dest, videomain_rb->count);
// videomain_rb->read(frame_read_temp, next_video_size);
// DC_FlushRange(dest, VIDEO_RBUF_BUFFER);
// decompress(dest, bgGetGfxPtr(ptr_background), DecompressType::LZ77Vram);
dmaCopyAsynch(dest, bgGetGfxPtr(ptr_background), FRAME_SIZE);
// dmaCopyAsynch(frame_temp, bgGetGfxPtr(ptr_background), FRAME_SIZE);
// comutex_release(&mutex);
// Raylib::nocashMessageFormat("frame step end");
} else {
printf("no frame!!!!!\n");
noframe++;
}
// }
} }
void TimerCallback() void TimerCallback()
{ {
FrameStep(); FrameStep();
doDraw = false;
// printf("\033[4;2H\033[Kvideo buffer: %i", videomain_rb->count/(FRAME_SIZE));
// printf("\033[5;2H\033[Kmusic buffer: %i", music_rb->count/(MMSTREAM_BUF_SIZE*4));
} }
void LoadNextChunk() bool LoadNextChunk(unsigned char *dest)
{ {
uint32_t compress_size; if (!dest) return false;
fread(&compress_size, sizeof(uint32_t), 1, file_video); // Raylib::nocashMessageFormat("load next start, %p", dest);
fread(video_compress, sizeof(uint8_t), compress_size, file_video); // videomain_rb->write((unsigned char*)&preload_video_size, sizeof(uint32_t));
fastlz_decompress(video_compress, compress_size, video_decompress, FRAME_SIZE*CHUNK_SIZE);
fread(&preload_video_size, sizeof(uint32_t), 1, file_videomain);
fread(frame_preload_temp, sizeof(uint8_t), preload_video_size, file_videomain);
decompress(frame_preload_temp, dest, DecompressType::LZ77);
// fastlz_decompress(frame_preload_temp, preload_video_size, dest, FRAME_SIZE*CHUNK_SIZE);
return true;
} }
mm_word on_stream_request( mm_word length, mm_addr dest, mm_stream_formats format ) { mm_word on_stream_request( mm_word length, mm_addr dest, mm_stream_formats format ) {
@ -316,13 +538,42 @@ mm_word on_stream_request( mm_word length, mm_addr dest, mm_stream_formats forma
case MM_STREAM_16BIT_STEREO: samplesize = 4; break; case MM_STREAM_16BIT_STEREO: samplesize = 4; break;
} }
int len = length*samplesize; int len = length*samplesize;
int linear = music_rb->LinearWriteSize();
if (music_rb.count >= len) // Raylib::nocashMessageFormat("%i", len);
// if (ftell(file_music) >= file_music_len) mmStreamClose();
// fread(music_buffer_temp, 1, len, file_music);
// memcpy(dest, music_buffer_temp, len);
// return length;
if (len < linear)
{ {
music_rb.read((char*)dest, len); memcpy(dest, music_rb->GetHead(), len);
music_rb->Skip(len);
return length;
}
else
{
// Raylib::nocashMessageFormat("linear %i", linear);
memcpy(dest, music_rb->GetHead(), linear);
// memcpy((char*)dest+linear, music_rb->buffer, len-linear);
music_rb->Skip(linear);
return linear/samplesize;
} }
if (music_rb.count < len && file_music == nullptr) mmStreamClose();
return length; // if (len < linear)
// {
// memcpy(dest, music_rb->GetHead(), len);
// }
// else
// {
// // Raylib::nocashMessageFormat("linear %i", linear);
// memcpy(dest, music_rb->GetHead(), linear);
// memcpy((char*)dest+linear, music_rb->buffer, len-linear);
// }
// music_rb->Skip(len);
// return length;
} }