VideoHelp Forum
+ Reply to Thread
Results 1 to 19 of 19
Thread
  1. Member
    Join Date
    Jul 2006
    Location
    Singapore
    Search Comp PM
    Hi to all

    As many of us know, many digital cameras record video in AVI using MJPEG compression - basically a bunch of JPEGs stacked together. Images are compressed *within* frames, presumably by camera hardware. Now for various reasons we may record a video clip in portrait mode and have to rotate the AVI to watch it properly. If we are storing in another format, there are a wide range of tools including VDub. However, a certain amount of recompression is inevitable, with loss of quality.

    In theory it should be possible to split the AVI into separate JPEGs, do a lossless rotate and reassemble the JPEGs with no loss of quality.

    Now, using lavtrans, (part of the mjpegtools suite of software) to split the AVI, the disassembled jpegs are missing Huffman tables (to save space?) and most image processing software will not touch them.

    I managed to modify the source code of sfw2jpg (look for it...) and put in default tables. So now I can view and do a lossless rotate of the JPEGs

    Finally, I tried looking for software that can do a simple reassembly of the JPEGs *without* any recompression. Even mjpegtools makes you convert to YUV first

    Can anyone please help?

    Thanks in advance!
    Quote Quote  
  2. Banned
    Join Date
    Jun 2007
    Location
    UNREACHABLE
    Search Comp PM
    Hi there.

    Please correct me if necessary, but processing the .mjpg video with
    Avisynth ++ encoding the result with ffdshow's lossless .jpg would not
    give you what you're looking for
    Quote Quote  
  3. Originally Posted by Midzuki
    Please correct me if necessary, but processing the .mjpg video with
    Avisynth ++ encoding the result with ffdshow's lossless .jpg would not
    give you what you're looking for
    That may be lossless but the output file will be 10 times bigger.
    Quote Quote  
  4. Since you seem happy to fiddle with source code, you could use Windows' GDI+ API to do the lossless rotation for you. You could also use the AVIFile API to read and write the JPEG frames as a MJPEG stream.
    Quote Quote  
  5. Member
    Join Date
    Jul 2006
    Location
    Singapore
    Search Comp PM
    Hi to all

    Thanks for the responses, the question is rather academic, as I'm already using VDub with rotate and ffdshow mjpg encoder at the max bitrate - which looks good and fast!

    To Johnny, I like to fiddle with code, but not compose it (from scratch that is)
    However, when I have time, I'll look into it.

    I would have thought a simple reassemble of jpegs into mjpeg would be widely available?

    Cheers!
    Quote Quote  
  6. Member
    Join Date
    Jul 2006
    Location
    Singapore
    Search Comp PM
    Hi to all

    I'll post some latest findings before I go into hibernation...Here are some results from searching the 'net.

    It seems that writing code to get an image into any compressed video format using current Windows codecs, the source must be an uncompressed bitmap.

    This makes sense from a general codec point of view, you need a common source from which to process the data.

    So if one wishes to append compressed images to a video file without recompressing, we need to construct our own AVI file from scratch - possible but tedious

    Cheers
    Quote Quote  
  7. Member
    Join Date
    Oct 2001
    Location
    United Kingdom
    Search Comp PM
    Originally Posted by kmlee
    So if one wishes to append compressed images to a video file without recompressing, we need to construct our own AVI file from scratch - possible but tedious
    I'm a programmer who works a lot with video - so I'm quite familiar with JPEG (and MJPEG) and AVI files - I already have code modules for everything you need and might consider knocking up a front end app for you (though it would be pretty quick n dirty - don't expect a work of art!).

    However, ever since I noticed this thread I've been wondering - how the hell do you do a lossless rotate of a JPEG image? (if I knew that then I could have the tool convert AVI->AVI rather than requiring a list of individual JPEG images). AFAIK, rotating the JPEG means rotating the DCT matrix, and I can't see how you would do that except by decoding and re-encoding - a lossy process (through it could use floating point so the losses would be small).

    One thing - my existing code for reading/writing AVIs (bypassing Windows APIs) ignores any audio stream since I don't need audio in my clips. Is this a problem? If so you could always use other tools to demux/remux the audio.
    Quote Quote  
  8. Lossless rotation (and mirroring) of DCT blocks is possible while staying in the DCT domain. The GDI+ API has functions to do it. The only place losses may occur in the compressed image is in quantization changes between the original and new Huffman encoding.

    I would write a DirectShow transform filter to input and output compressed JPEG images. Then it would be a simple matter of using GraphEdit to create a suitable graph to read the existing file, run it through the transform filter and write the new file (mux with the original audio stream if necessary).
    Quote Quote  
  9. Banned
    Join Date
    Jun 2007
    Location
    UNREACHABLE
    Search Comp PM
    airalaMynnhoJ is right.

    http://en.wikipedia.org/wiki/Jpeg#Lossless_editing

    A number of alterations to a JPEG image can be performed losslessly (that is, without recompression and the associated quality loss) as long as the image size is a multiple 1 MCU block (Minimum Coded Unit) (usually 16 pixels in both directions, for 4:2:0).

    Blocks can be rotated in 90 degree increments, flipped in the horizontal, vertical and diagonal axes and moved about in the image. Not all blocks from the original image need to be used in the modified one.

    The top and left of a JPEG image must lie on a block boundary, but the bottom and right need not do so. This limits the possible lossless crop operations, and also what flips and rotates can be performed on an image whose edges do not lie on a block boundary for all channels.

    When using lossless cropping, if the bottom or right side of the crop region is not on a block boundary then the rest of the data from the partially used blocks will still be present in the cropped file and can be recovered relatively easily by anyone with a hex editor and an understanding of the format.

    It is also possible to transform between baseline and progressive formats without any loss of quality, since the only difference is the order in which the coefficients are placed in the file.
    Quote Quote  
  10. Member
    Join Date
    Oct 2001
    Location
    United Kingdom
    Search Comp PM
    Originally Posted by JohnnyMalaria
    Lossless rotation (and mirroring) of DCT blocks is possible while staying in the DCT domain.
    I'm still having a hard time seeing how. The DCT matrix contains coeffs are in the frequency domain, rotation is a spatial operation. I can't see how you can make spatial changes without converting between the two domains (ie. decoding then re-encoding).

    Does anyone actually have any C or Pascal code for a simple 90deg rotate of a simple 8x8 DCT matrix? (obviously if you can do that then you can handle the entire MCU).
    Quote Quote  
  11. Originally Posted by mpack
    Originally Posted by JohnnyMalaria
    Lossless rotation (and mirroring) of DCT blocks is possible while staying in the DCT domain.
    I'm still having a hard time seeing how. The DCT matrix contains coeffs are in the frequency domain, rotation is a spatial operation. I can't see how you can make spatial changes without converting between the two domains (ie. decoding then re-encoding).
    Flipping (diagonally) or rotating a matrix converts frequency domain of the X axis to frequency domain of the Y axis, and vice versa. Then you rearrange the blocks so they're in the right place.
    Quote Quote  
  12. Member thecoalman's Avatar
    Join Date
    Feb 2004
    Location
    Pennsylvania
    Search PM
    Irfanview can do lossless jpg rotation in batch mode, it also supports video but I don't know if that's going to apply to flipping a "video" or even what types of tools it has for video.

    http://www.irfanview.com/
    Quote Quote  
  13. Member
    Join Date
    Oct 2001
    Location
    United Kingdom
    Search Comp PM
    Originally Posted by jagabo
    Flipping (diagonally) or rotating a matrix converts frequency domain of the X axis to frequency domain of the Y axis, and vice versa. Then you rearrange the blocks so they're in the right place.
    Ok, I see where you are going - thanks for that. I still have a problem that the DCT AC coeffs are essentially scale factors for a set of basis functions that do have a preferred orientation/phase... so I can see that flipping about the topleft/botright diagonal would work, but....

    Maybe I should do some experiments (apply FDCT to the same 8x8 matrix at different rotations) to see for myself what the effect is.
    Quote Quote  
  14. In this context, rotation means +/- 90 degrees, not arbitary angles. Since this thread is about horizontal-to-vertical rotation, it's OK.
    Quote Quote  
  15. Member
    Join Date
    Oct 2001
    Location
    United Kingdom
    Search Comp PM
    Originally Posted by JohnnyMalaria
    In this context, rotation means +/- 90 degrees, not arbitary angles.
    Yes, I understood that (at least I understood that the context was rotation by any multiple of 90deg), but thanks anyway. My problem is understanding how you can eg. rotate by 180 deg while keeping left->right and top->bottom basis functions in their normal orientation. Are all the cosine functions symmetrical in an 8 point DCT? I didn't think they were, so maybe that's where I'm going wrong.
    Quote Quote  
  16. After rotating right by 90 degrees the elements are reversed along the (new) X axis. So flip the matrix horizontally:



    Of course, this is equivalent to flipping along the diagonal.

    Then, if this was the top left block, move it to the top right to rotate its overall position.
    Quote Quote  
  17. Member
    Join Date
    Aug 2008
    Location
    United States
    Search Comp PM
    So, has this been solved at all?
    I would LOVE to be able to use a tool to turn my movies around the right way.
    I am recording some scenic driving with a mount that attaches to the windshield (through suction) and ends up screwing into the little camera, holding it upside down as a result.

    @kmlee, can't find your "sfw2jpg" anywhere.
    Is it supposed to take an mjpeg .avi and create a bunch of jpegs, without loss of image quality?

    Thanks,
    a1
    Quote Quote  
  18. Actually, they are saying it can be done -- but the JPEG images must be YUV 4:2:0 subsampled (most JPEG files are) and they cannot use custom Huffman tables. I don't know how common the latter is.
    Quote Quote  



Similar Threads

Visit our sponsor! Try DVDFab and backup Blu-rays!