From bccb1d61907fea45c5e84b29499989f7cee104a5 Mon Sep 17 00:00:00 2001 From: Fox Caminiti Date: Thu, 4 Aug 2022 12:33:54 -0400 Subject: loaded bitmap wrap buffer seems stable --- ffmpeg_backend.cpp | 63 ++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 52 insertions(+), 11 deletions(-) (limited to 'ffmpeg_backend.cpp') diff --git a/ffmpeg_backend.cpp b/ffmpeg_backend.cpp index dfc1241..1f920ba 100644 --- a/ffmpeg_backend.cpp +++ b/ffmpeg_backend.cpp @@ -272,40 +272,81 @@ bool32 AV_LoadVideoFrame(source *Source, layer_bitmap_info *BitmapInfo, memory * uint16 BytesPerPixel = Source->Info.BytesPerPixel; int32 Pitch = Width*BytesPerPixel; - // probably should rename this memory_table *Table = &Memory->Slot[B_LoadedBitmaps]; + + // First check whether we'd run over the buffer at the current + // position, and reset it if so. uint64 Size = Bitmap_CalcTotalBytes(Width, Height, BytesPerPixel); if (Table->CurrentPosition + Size > Table->Size) { Table->CurrentPosition = 0; Table->PointerIndex = 0; } cached_bitmap *Bitmap = &Memory->Bitmap[Table->PointerIndex]; - if (Table->PointerIndex < Table->NumberOfPointers) { + + // Next, if there's a pointer in front of the current position, + // check whether it's far away enough so that the size fits. + bool32 BS = true; + if (Bitmap->Data) { + uint64 BytesBetween = (uint8 *)Bitmap->Data - ((uint8 *)Table->Address + Table->CurrentPosition); + if (BytesBetween > Size) { + int16 StopAt = Table->NumberOfPointers - 1; + while (StopAt > Table->PointerIndex - 1) { + Memory->Bitmap[StopAt + 1] = Memory->Bitmap[StopAt]; + StopAt--; + } + Table->NumberOfPointers++; + BS = false; + } + } + // If it doesn't fit, then we need to dereference the pointers + // until we have enough space. + if ((Table->PointerIndex < Table->NumberOfPointers) && BS) + { bool32 Avail = false; - void *DataStart = Memory->Bitmap[Table->PointerIndex].Data; + void *AddressStart = (void *)((uint8 *)Table->Address + Table->CurrentPosition); + uint32 Amount = 0; while(!Avail) { - void *Data2 = Memory->Bitmap[Table->PointerIndex+1].Data; - uint64 BytesBetween = (uint8 *)Data2 - (uint8 *)DataStart; - if (BytesBetween < Size) { - Memory->Bitmap[Table->PointerIndex+1].SourceOwner = NULL; - Table->PointerIndex++; + // Bail out if we're on the last index, as we don't need to do anything else. + if (Table->PointerIndex != Table->NumberOfPointers - 1) { + void *Data2 = Memory->Bitmap[Table->PointerIndex+1].Data; + uint64 BytesBetween = (uint8 *)Data2 - (uint8 *)AddressStart; + if (BytesBetween < Size) { + int16 StopAt = Table->PointerIndex; + while (StopAt < Table->NumberOfPointers - 1) { + Memory->Bitmap[StopAt] = Memory->Bitmap[StopAt + 1]; + StopAt++; + } + Amount++; + Table->NumberOfPointers--; + if (Amount > 2) { + Amount += 0; + } + } else { + Avail = true; + } } else { Avail = true; } - Table->NumberOfPointers--; } } - Table->NumberOfPointers++; + // Increment the total number of pointers if the bitmap is + // empty, i.e. we're at the first iteration of the buffer loop. + if (!Bitmap->Data) { + Table->NumberOfPointers++; + } Bitmap->Data = AllocateMemory(Memory, Size, B_LoadedBitmaps); if (!Bitmap->Data) { Assert(0); } Bitmap->SourceOwner = Source; Bitmap->Frame = FrameToSeek; - Bitmap->Index = Table->PointerIndex; Table->PointerIndex++; void *Buffer = Bitmap->Data; + // No two pointers on the table should hold the same data + // address or be empty. + Debug_Memory_Assert_Cohesion(Memory, Table); + int out_linesize[4] = { Pitch, Pitch, Pitch, Pitch }; uint8 *dst_data[4] = { (uint8 *)Buffer, (uint8 *)Buffer + Width*Height*BytesPerPixel, (uint8 *)Buffer + Width*Height*BytesPerPixel*2, (uint8 *)Buffer + Width*Height*BytesPerPixel*3 }; -- cgit v1.2.3