Log in

View Full Version : Implement codec - help required.


DrPhill
30th June 2012, 18:53
Hi all,

I have started a new approach to my ongoing project anf would appreciate pointers to information to help me.

What I am hoping to do is decode mpg, do some video editing, and recode the mpg. I am working in Java (where I am most comfortable). Now, before you say 'this is a ver big task' let me explain.....

I am only intending to decode one particular format - the one produced by my camera. It actually seems very simple so far: One picture per group, one slice per picture, only I frames, no special intraQuantizeMatrix or non-intraQuantise matrix.

I have gleaned all that from the video stream itself - I have the source code for a java mpg player to guide me. It only copes with level one mpg and my video does not work with it. I think it may be leveltwo with paaccketisation, but I think I have identify the 'packet markers'(??) in the stream, though they seem irrelevant to my purposes. I can identify the various start codes and extract information.

I have now drilled down as far as the slice, and need to start reading macroblocks. I am not sure what the code is doing at this point with the macroblock addresses.

So I need a clear explanation of the MPG format at this point - anyone know where I could find one?

I also believe that with I_frames I will not need DCT stuff - that this is for frame types other than I_frame. Am I correct?

I think I can figure out the macroblock --> image stuff, and then the image to RGB32 so that I can do my editing.

Then, on the far side, I will need to know the bare minimum MPG stream content I need for a well formed mpg. Then I can feed my results to a more competent program for serious encoding.

Anybody have pointers to information I will understand and find useful (if those are not mutually exclusive terms here).

Sorry for the long post, and sorry if this is the wrong forum.

All help gratefully received.

Guest
30th June 2012, 23:11
What I am hoping to do is decode mpg, do some video editing, and recode the mpg. I am working in Java (where I am most comfortable). Now, before you say 'this is a ver big task' let me explain..... Your explanation doesn't make it any less of a job.

I have gleaned all that from the video stream itself - I have the source code for a java mpg player to guide me. It only copes with level one mpg and my video does not work with it. I think it may be leveltwo with paaccketisation, but I think I have identify the 'packet markers'(??) in the stream, though they seem irrelevant to my purposes. I can identify the various start codes and extract information. So you need MPEG2 in a transport stream. Why don't you try to port some existing code to Java (if you must, it's a huge undertaking), such as DGMPGDec?

I have now drilled down as far as the slice, and need to start reading macroblocks. I am not sure what the code is doing at this point with the macroblock addresses.

So I need a clear explanation of the MPG format at this point - anyone know where I could find one? Refer to the specs: ISO 13818-1 for transport streams, and ISO 13818-2 for MPEG2 video.

I also believe that with I_frames I will not need DCT stuff - that this is for frame types other than I_frame. Am I correct? Wrong.

I think I can figure out the macroblock --> image stuff, and then the image to RGB32 so that I can do my editing. You know all about IDCT, motion compensation, and all that?

Then, on the far side, I will need to know the bare minimum MPG stream content I need for a well formed mpg. Then I can feed my results to a more competent program for serious encoding. You're going to make an MPEG2 encoder too?

All help gratefully received. Don't do it! Try to make a workflow with existing tools.

DrPhill
1st July 2012, 14:53
Always with the negative waves, Moriarty..... :D

I am sure that you meant your advice kindly, bI learn by doing, neuron2. I have learnt quite a bit so far about (this one) video stream format, though the task does seem a little challenging.

- DCT are no more frightening than FFT (to which they are closely related) and of which I have coded several. I had not realised they were used for compression, thinking rather that they were used for predictive frames. A bit more learnt.

Since I only have I frames, no motion compensation is needed.

I have converted existing decoder to use level two (of the transport rather than mpg?) and it copes well with my videos I now have my source of images to manipulate. The difference between levels one and two turned out to be trivial - the packetisation of the video data (to intersperse it with audio?).

I do not imagine that I will get so lucky as to find a coder written in Java when I need to export my data, but I may be able good stab at a readable format since I now have a reader to test with. rDCT will be the trickiest bit. I probably do not need to use vlc as long as I avoid reserved values - I will let another program figure out maximum compression. Similarly, motion compensation will not be needed as I can write I frames. Still a large task, I admit, but not as large as some I achieved in a professional capacity (solving this kind of challenge is fun - and it made me enough to retire in 15 years professional work).

If the worst comes to the worst then I can output a collection of stills for another program to stitch together, but for now I can do the fun bit....... video manipulation.

kalehrl
1st July 2012, 19:01
Have a look at this Java-based MPEG2 demuxing/editing tool:
http://sourceforge.net/projects/project-x/

DrPhill
2nd July 2012, 16:31
An interesting looking project. Thank you.

DrPhill
6th July 2012, 19:15
OK, so some of this is easy enough. I have implemented and proved the following layers:

RGB32 <==> Y'CrCb
Y'CrCb <==> MacroBlocks
MacroBlocks <==> DCT Blocks
DCT Blocks <==> Quantised Blocks
Quantised Blocks <==> ZigZag blocks

But I am having a little difficulty with the Huffman/LZ77 step. I understand the theory, but do not know which values/codes are used in the standard encoding/decoding table. Do I really need to buy the standards document?

Is there an open source implementation of this portion of the code somewhere that I can read to understand? I can cope with almost any implementation language, but prefer C, C++, or best of all (of course), Java. A source for an implementation may also be optimised in ways that I would struggle to discover.

Working up from the botom of the stack, I have a bitWriting class and can write the MPG stream validly - apart from the picture blocks. I prove this by stealing the picture block code from a real MPG and inserting them to get something playable. eg: I can now code a program to reverse a video and write it to disk in a format that a standard player can render.

Any help or pointers that you can give me would be very welcome. I feel that I am only a couple of days work away from completing the encoder. I could try understanding and reversing the logic from the decoder but that may be a much longer task (weeks, instead of days). And it would not yield an efficient encoding algorithm.

[Remember I am only doing primitive mpg with I-Frames. No fancy motion-compensation or predictive stuff]

I have learnt a lot so far........

DrPhill
19th July 2012, 18:34
OK, thanks for all the help.

Job done, I think this is about as good as I can expect: http://youtu.be/g6n_gmibG10.
It was harder than I expected, but not too hard; the main problem was building the tools to analyse the stream so as to identify the bugs correctly.

I now have encode/decode MPG1 in Java.

I think I will tackle MPG4 this winter when I have some free time......