-
Notifications
You must be signed in to change notification settings - Fork 22
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
Jpeg files written from captured frames are corrupted #20
Comments
All JPEG files are valid MJPEG frames, but the other way around isn't true. Most cameras will give valid JPEG files when asked to output MJPEG frames, but this isn't true for all cameras. https://lightbits.github.io/v4l2_huffman/ contains simple C code to convert them. |
#include <string.h>
#include <stdint.h>
uint32_t mjpg_to_jpg(uint8_t *mjpg,
uint32_t mjpg_size,
uint8_t *jpg)
{
static uint8_t huffman[] = { 0xff, 0xc4, 0x01, ... , 0xfa };
uint32_t i = 0;
while (!(mjpg[i] == 0xff && mjpg[i+1] == 0xc0))
i++;
memcpy(jpg, mjpg, i);
memcpy(jpg+i, huffman, sizeof(huffman));
memcpy(jpg+i+sizeof(huffman), mjpg+i, mjpg_size-i);
uint32_t jpg_size = mjpg_size+sizeof(huffman);
return jpg_size;
} |
If you can't read C, that puts a (truncated in that snippet) array before the first occurrence of 0xffc0. |
The full array is at https://github.com/lightbits/usbcam/blob/master/mjpg_to_jpg.cpp#L28-L64. |
Also note that libv4l only guarantees RGB24, BGR24, YUV420, and YVU420. For best compatibility, you should use one of those four and convert it to jpeg using image-rs. Untested: use image::RgbImage;
let mut camera = rscam::new("/dev/video0").unwrap();
camera.start(&rscam::Config {
interval: (1, 30), // 30 fps.
resolution: (1280, 720),
format: b"RGB3",
..Default::default()
}).unwrap();
for i in 0..10 {
let frame = camera.capture().unwrap();
let image =
RgbImage::from_vec(frame.resolution.0, frame.resolution.1, Vec::from(&*frame)).unwrap();
image.save(&format!("frame-{}.jpg", i)).unwrap();
} |
Hi @leo60228 I am having a very similar problem. I captured some frames from a tcp stream and they seem corrupted too. I have not been able to identify the manufacturer of the camera but all I know is that it has AV1 line. I tried to follow your method by adding huffman tables to the header but it seemed to have no affect. So I tried looking at the image structure using exiv2 and this is what it outputs
If I'm not wrong it seems like there's already some DHTs in there. Here is the file that I got by cropping from This file is not totally corrupted the background is actually my room's ceiling the only corrupt part are these horizontal patches. And the client application for the webcam displays the video just fine without any artifacts. I would love to have your thoughts on this. Thanks PS: Here's the hex dump of this file.
|
Hi.
I captured some frames and saved it to files.
And I copied the files to local machine and tried to open it by photo viewer, but it said that "The format is not supported.".
By using ImageMagick's imdisplay command, I can see the image with a warning dialog says that some extraneous bytes before marker 0xdxx.
The output of lsusb command is as follows.
My source is as follows.
Binary data of one of the files are as follows.
Jpeg files start at ffd8 and end at ffd9, so I think that just a part of the captured data is written to the file.
(Additionally, it might be strange that a number of 00 bytes and 31 bytes are repeated...)
What should I do about it?
Have I missed any dependencies or procedures?
The text was updated successfully, but these errors were encountered: