Skip to content

Rgb packet handling #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed

Conversation

larshg
Copy link
Contributor

@larshg larshg commented Oct 16, 2014

A try at getting a better rgb packet handling with inspiration from hovren's pull request: #72

Its still a work in progress, but I wouldn't mind any feedback on the way 👍

@larshg
Copy link
Contributor Author

larshg commented Oct 28, 2014

Hi @christiankerl

I have tried to unify the pull request #71 #72 and #73 within this one.

The timestamp is only implemented for the rgb parser in this one, but I have made a similar updated request for the depth_packet_parser in #75 .

Feedback would be appreciated.

@@ -36,11 +37,26 @@ namespace libfreenect2
LIBFREENECT2_PACK(struct RawRgbPacket
{
uint32_t sequence;
uint32_t unknown0;
uint32_t magicHeader; // is 'BBBB' equal 0x42424242
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename to magic_header

@larshg larshg force-pushed the rgbPacketHandling branch from a52ded2 to d4c1c84 Compare February 4, 2015 15:29
@xlz
Copy link
Member

xlz commented Feb 7, 2015

First, can you please squash these into a single commit? The second one is cosmetic.

As a matter of fact, a custom hardware accelerated JPEG decoder I'm testing cannot handle garbage after JPEG. So this extra check is actually required for compatibility.

My experiments show your unknown1 is the length of a filler before the footer. Some new specifications:

// starting from JPEG EOI: 0xff 0xd9
// char _pad_0xa5[]; //0-3 bytes alignment of 0xa5
// char _filler[_filler_length] = "ZZZZ...";
LIBFREENECT2_PACK(struct RgbPacketFooter {
    uint32_t magic_header; // is '9999' equal 0x39393939
    uint32_t sequence;
    int32_t unknown1; // _filler_length
[...]

Also, version is shown to be always 0x3f800000 (1.0f). It's likely a float number of certain meaning, but it is probably not representing a version number. The rest of the unknown* fields are also shown to be zeros in all my experiments. Maybe you can rename the unknown* fields to be reserved* because they are not exactly unknown.

I wrote and tested some code to find out the exact size of the JPEG image by searching for its EOI. For your reference. Following the new specifications the code will finish in finite steps and if EOI is not found, returning failure is justified. In my testing it always worked.

@christiankerl
Copy link
Contributor

@xlz would it suffice to compute the correct jpeg_buffer_length using the filler_length?

@larshg can you rename unknown1 accordingly?

// if magic markers match
if (length - i >= sizeof(RgbPacketFooter) && footer->magic_header == 0x39393939 && footer->magic_footer == 0x42424242)
{
memcpy(&fb.data[fb.length], buffer, length);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This copies all remaining data to the fb. I guess it would be more correct to just copy i+sizeof(footer) and copy any remaining data to the new fb after the swap?

@xlz
Copy link
Member

xlz commented Feb 7, 2015

@christiankerl Yes.

Here is my take (edit: updated commit) to compute the exact value of jpeg_buffer_length using filler_length.

Several changes:

  • Return immediate after buffer overflow because the next JPEG packet will be incomplete anyway.
  • Do not scan through every single byte checking for magic bytes. This is much more costly than a memcpy. The current implementation and the lack of reports about buffer overflow and image corruption show it is safe to assume the footer arrives at the very end of a data transfer, and there is no more data after the footer within a data transfer.

@xlz
Copy link
Member

xlz commented Feb 8, 2015


~~Changes from v1:~~
- ~~Corrected size calculation about bytes before JPEG~~
- ~~Tested on Linux~~

~~This is ready for cherry picking.~~

@larshg larshg force-pushed the rgbPacketHandling branch from d4c1c84 to 340b219 Compare February 9, 2015 13:08
@larshg
Copy link
Contributor Author

larshg commented Feb 11, 2015

Nice suggestions for improvements @xlz. I have tried to incorporate most of them - Still lacking to calculate the exact jpeg buffer size with the 0-3 padding bytes, but I'll try look at it tomorrow.

I have removed the for loop, but this requires the assumption that there will be no concatenated images in the USB transfers - which I'm not qualified to determine if it can or will happen? If it does, we will just hit bufferoverflow and reset buffers.

Over 1000 runs in the onDataReceived method, I tried with and without a for loop and I measured difference from 160 ms to 1.6 ms in processing time - so indeed it is more costly, but difference compared to the overall processing is minimal.
But if there is no reason to do it (more secure, removes doubt of the assumption of concatenated images), we might as well remove it?

@xlz
Copy link
Member

xlz commented Feb 11, 2015

In fact you can totally detect the magic bytes in the middle of an image. There is no guarantee these 8 bytes won't appear. If our assumption does not hold, report of buffer overflows will immediately show up.

(It is similar for depth processing which is also doing scanning currently. I'll put on my patch shortly.)

On the side of performance, on ARM platform the CPU is significantly weaker for these kind of memory scanning.

@xlz
Copy link
Member

xlz commented Mar 31, 2015

What's the status of these two stream processor PR #73 #75 ?

larshg added 2 commits April 8, 2015 13:22
inspecting to find magic markers.
copy data in chunks.
Verify by packetlength and sequence of footer and header.
…ed without the next image in same USB buffer.

Better calculation of jpeg buffer size (still missing the 0-3 padding bytes(0xa5)
@larshg
Copy link
Contributor Author

larshg commented May 5, 2015

Closing in favor of #221 .

@larshg larshg closed this May 5, 2015
@larshg larshg deleted the rgbPacketHandling branch May 5, 2015 12:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants