Skip to content

Commit

Permalink
Merge pull request #19 from YourMJK/fix-crop-flag
Browse files Browse the repository at this point in the history
Fix `objectCroppedFlag` & Add object crop fields reading/writing
  • Loading branch information
MonoS authored Mar 1, 2024
2 parents 1bb2be3 + aa93db3 commit 7bdbc8e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 41 deletions.
81 changes: 44 additions & 37 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ struct t_crop {
uint16_t bottom;
};

enum e_objectFlags : uint8_t {
none = 0x00,
forced = 0x40,
cropped = 0x80
};

enum e_cutMergeFormat : uint8_t {
secut = 0, //"[1000-2000] [3000-4000]" both inclusive
vapoursynth = 1, //"[1000:2001] [3000:4001]" start inclusive, end exclusive
Expand Down Expand Up @@ -106,7 +112,7 @@ struct t_WDS {
struct t_compositionObject {
uint16_t objectID;
uint8_t windowID;
uint8_t objectCroppedFlag;
uint8_t objectCroppedAndForcedFlag;
uint16_t objectHorPos;
uint16_t objectVerPos;
uint16_t objCropHorPos;
Expand Down Expand Up @@ -245,20 +251,21 @@ t_PCS ReadPCS(uint8_t* buffer) {
pcs.paletteID = *(uint8_t*) &buffer[9];
pcs.numCompositionObject = *(uint8_t*) &buffer[10];

size_t bufferStartIdx = 11;
for (int i = 0; i < pcs.numCompositionObject; i++) {
size_t bufferStartIdx = 11 + (size_t)i * 8;

pcs.compositionObject[i].objectID = swapEndianness(*(uint16_t*)&buffer[bufferStartIdx + 0]);
pcs.compositionObject[i].windowID = *(uint8_t*) &buffer[bufferStartIdx + 2];
pcs.compositionObject[i].objectCroppedFlag = *(uint8_t*) &buffer[bufferStartIdx + 3];
pcs.compositionObject[i].objectHorPos = swapEndianness(*(uint16_t*)&buffer[bufferStartIdx + 4]);
pcs.compositionObject[i].objectVerPos = swapEndianness(*(uint16_t*)&buffer[bufferStartIdx + 6]);
/*
pcs.compositionObject.objCropHorPos = swapEndianness(*(uint16_t*)&buffer[bufferStartIdx + 8]);
pcs.compositionObject.objCropVerPos = swapEndianness(*(uint16_t*)&buffer[bufferStartIdx + 10]);
pcs.compositionObject.objCropWidth = swapEndianness(*(uint16_t*)&buffer[bufferStartIdx + 12]);
pcs.compositionObject.objCropHeight = swapEndianness(*(uint16_t*)&buffer[bufferStartIdx + 14]);
*/
pcs.compositionObject[i].objectID = swapEndianness(*(uint16_t*)&buffer[bufferStartIdx + 0]);
pcs.compositionObject[i].windowID = *(uint8_t*) &buffer[bufferStartIdx + 2];
pcs.compositionObject[i].objectCroppedAndForcedFlag = *(uint8_t*) &buffer[bufferStartIdx + 3];
pcs.compositionObject[i].objectHorPos = swapEndianness(*(uint16_t*)&buffer[bufferStartIdx + 4]);
pcs.compositionObject[i].objectVerPos = swapEndianness(*(uint16_t*)&buffer[bufferStartIdx + 6]);
if (pcs.compositionObject[i].objectCroppedAndForcedFlag & e_objectFlags::cropped){
pcs.compositionObject[i].objCropHorPos = swapEndianness(*(uint16_t*)&buffer[bufferStartIdx + 8]);
pcs.compositionObject[i].objCropVerPos = swapEndianness(*(uint16_t*)&buffer[bufferStartIdx + 10]);
pcs.compositionObject[i].objCropWidth = swapEndianness(*(uint16_t*)&buffer[bufferStartIdx + 12]);
pcs.compositionObject[i].objCropHeight = swapEndianness(*(uint16_t*)&buffer[bufferStartIdx + 14]);
bufferStartIdx += 8;
}
bufferStartIdx += 8;
}

return pcs;
Expand All @@ -274,22 +281,22 @@ void WritePCS(t_PCS pcs, uint8_t* buffer) {
*((uint8_t*) (&buffer[9])) = pcs.paletteID;
*((uint8_t*) (&buffer[10])) = pcs.numCompositionObject;

size_t bufferStartIdx = 11;
for (int i = 0; i < pcs.numCompositionObject; i++) {
size_t bufferStartIdx = 11 + (size_t)i * 8;

*((uint16_t*)(&buffer[bufferStartIdx + 0])) = swapEndianness(pcs.compositionObject[i].objectID);
*((uint8_t*) (&buffer[bufferStartIdx + 2])) = pcs.compositionObject[i].windowID;
*((uint8_t*) (&buffer[bufferStartIdx + 3])) = pcs.compositionObject[i].objectCroppedFlag;
*((uint16_t*)(&buffer[bufferStartIdx + 4])) = swapEndianness(pcs.compositionObject[i].objectHorPos);
*((uint16_t*)(&buffer[bufferStartIdx + 6])) = swapEndianness(pcs.compositionObject[i].objectVerPos);
*((uint16_t*)(&buffer[bufferStartIdx + 0])) = swapEndianness(pcs.compositionObject[i].objectID);
*((uint8_t*) (&buffer[bufferStartIdx + 2])) = pcs.compositionObject[i].windowID;
*((uint8_t*) (&buffer[bufferStartIdx + 3])) = pcs.compositionObject[i].objectCroppedAndForcedFlag;
*((uint16_t*)(&buffer[bufferStartIdx + 4])) = swapEndianness(pcs.compositionObject[i].objectHorPos);
*((uint16_t*)(&buffer[bufferStartIdx + 6])) = swapEndianness(pcs.compositionObject[i].objectVerPos);
if (pcs.compositionObject[i].objectCroppedAndForcedFlag & e_objectFlags::cropped){
*((uint16_t*)(&buffer[bufferStartIdx + 8])) = swapEndianness(pcs.compositionObject[i].objCropHorPos);
*((uint16_t*)(&buffer[bufferStartIdx + 10])) = swapEndianness(pcs.compositionObject[i].objCropVerPos);
*((uint16_t*)(&buffer[bufferStartIdx + 12])) = swapEndianness(pcs.compositionObject[i].objCropWidth);
*((uint16_t*)(&buffer[bufferStartIdx + 14])) = swapEndianness(pcs.compositionObject[i].objCropHeight);
bufferStartIdx += 8;
}
bufferStartIdx += 8;
}

/*
*((uint16_t*)(&buffer[bufferStartIdx + 8])) = swapEndianness(pcs.compositionObject.objCropHorPos);
*((uint16_t*)(&buffer[bufferStartIdx + 10])) = swapEndianness(pcs.compositionObject.objCropVerPos);
*((uint16_t*)(&buffer[bufferStartIdx + 12])) = swapEndianness(pcs.compositionObject.objCropWidth);
*((uint16_t*)(&buffer[bufferStartIdx + 14])) = swapEndianness(pcs.compositionObject.objCropHeight);
*/
}

t_PDS ReadPDS(uint8_t* buffer, size_t segment_size) {
Expand Down Expand Up @@ -923,9 +930,13 @@ int main(int32_t argc, char** argv)
std::printf(" + Object ID: %u\n", object.objectID);
std::printf(" + Window ID: %u\n", object.windowID);
std::printf(" + Position: %u,%u\n", object.objectHorPos, object.objectVerPos);
if (object.objectCroppedFlag == 0x40) {
std::printf(" + Object cropped: True\n");
// TODO: Print crop fields
if (object.objectCroppedAndForcedFlag & e_objectFlags::forced) {
std::printf(" + Forced display: True\n");
}
if (object.objectCroppedAndForcedFlag & e_objectFlags::cropped) {
std::printf(" + Cropped: True\n");
std::printf(" + Cropped position: %u,%u\n", object.objCropHorPos, object.objCropVerPos);
std::printf(" + Cropped size: %ux%u\n", object.objCropWidth, object.objCropHeight);
}
}
}
Expand All @@ -944,7 +955,7 @@ int main(int32_t argc, char** argv)
}

for (int i = 0; i < pcs.numCompositionObject; i++) {
if (pcs.compositionObject[i].objectCroppedFlag == 0x40) {
if (pcs.compositionObject[i].objectCroppedAndForcedFlag & e_objectFlags::cropped) {
std::fprintf(stderr, "Object Cropped Flag set at timestamp %s! Implement it!\n", timestampString);
}

Expand Down Expand Up @@ -1062,13 +1073,9 @@ int main(int32_t argc, char** argv)
for (int j = 0; j < pcs.numCompositionObject; j++) {
t_compositionObject *object = &pcs.compositionObject[j];
if (object->windowID != window->windowID) continue;

if (object->objectCroppedFlag == 0x40) {
std::fprintf(stderr, "Object Cropped Flag set at timestamp %s! Crop fields are not supported yet.\n", timestampString);
/*
if (object->objectCroppedAndForcedFlag & e_objectFlags::cropped) {
object->objCropHorPos += clampedDeltaX;
object->objCropVerPos += clampedDeltaY;
*/
}
object->objectHorPos += clampedDeltaX;
object->objectVerPos += clampedDeltaY;
Expand Down
8 changes: 4 additions & 4 deletions sup-parser.ksy
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,16 @@ types:
type: u2
- id: obj_crop_hor_pos
type: u2
if: object_cropped_flag == 0x40
if: object_cropped_flag & 0x80 != 0
- id: obj_crop_ver_pos
type: u2
if: object_cropped_flag == 0x40
if: object_cropped_flag & 0x80 != 0
- id: obj_crop_width
type: u2
if: object_cropped_flag == 0x40
if: object_cropped_flag & 0x80 != 0
- id: obj_crop_height
type: u2
if: object_cropped_flag == 0x40
if: object_cropped_flag & 0x80 != 0

windows_object_type:
seq:
Expand Down

0 comments on commit 7bdbc8e

Please sign in to comment.