PDA

View Full Version : Possible to repack h264 stream to h264 with other bitrate?


seemees
21st October 2009, 07:30
I have some interesting question, is it possible to repack h264 stream (say 8000 kbps) to h264 with other bitrate (1500 kbps)?
Problem steps is:
1. We have an file1.mkv with 1 video stream h264 (8000 kbps or same) and some audio streams.
2. We must get file2.mkv with 1 video stream h264 and same audio streams, video from step 1 but with video bitrate 1500 kbps (transcoding).
3. Video in step 2 must be with maximum possible quality and without full repack into 4:2:0 image and back into h264 - we must using all motion vectors and
some other useful info from original h264.

This, thought, saves a big repacking time and gives a small quality penalty.
Tell me, how to complete this task - some algorightm or ready-steady soft?
And of couse, can we do it in the next release of x264.

Example (rough):
1.options in x264 --takestats file1.mkv

works with file1.mkv - and takes info about motion vectors and qp and lambda and mb-tree (and so on). All info recorded into original.stats file.

2.options in x264 --usestats original.stats --bitrate 1200 -o file2.mkv file1.mkv

and this produce file2.mkv with minimum loss quality and fastest speed (no need statistic and lookahead, all intra and scenecut founded, all b-frame is placed into place :-) ).

Sorry if it is not a good idea to ask something like that, but only you can help me.

With best regards, seemees

akupenguin
21st October 2009, 22:32
This will never happen in x264, since it has approximately no code in common with normal encoding.
Also, you're talking about at rather large quality penalty, since motion vectors and other info from 8mbit aren't at all appropriate for 1.5mbit.

Realistically, you could skip the lookahead parts, but nothing macroblock-level. i.e. parse an existing video into a 2pass statsfile, and then run a full 2nd pass from that.

seemees
21st October 2009, 23:34
akupenguin
Thank you for answer. I understand it.
I think that some more info we may use:
1) encoding:
FrameN -> I-Prediction -> Forward transform -> Forward quantization -----> Forward scan ---> Forward entropy coding (CABAC) ----> NAL
-------------------------------- ! ------------------------------------ !
FrameN-1 -> P-B-Prediction -----^ ----------------------------------- V
!--<--Deblock--------<------ Reverse transform <- Reverse quantization

2) decoding

FrameN-1 ----> De-Predict-I --V
^ ------- De-Predict-P-B
FrameN <----Deblock----<---------Reverse transform---<----Reverse quantization---<----Reverse scan<---Reverse entropy coding <---NAL

We see that I-prediction we can use from old stream.
Now I-prediction for P and B-frame we still use too. MB-tree, scenecut and frame sequence already done. And all we must to do - Motion estimation with new
bitrate (and other QP parameters). All other work already done.
Can anyone help me to implement this? Or may be has a ready solution.
With best regards, seemees.

akupenguin
21st October 2009, 23:45
Can't reuse intra prediction: Inter-vs-intra decision depends on bitrate. Blocksize depends on bitrate. And even when they're both intra with the same size, the optimal prediction direction depends on the values of the neighboring pixels, which depends on bitrate.

seemees
22nd October 2009, 01:20
akupenguin
Thanks. I seem to understand.
We I-predict macroblock. Then compare predicted macroblock with original and quant DIFFERENCIES with QP (depends on bitrate). And dequanted DIFF plus predictation used in next prediction (on right-down macroblocks). Ok, New QP changes DIFFERENCIES and all previous macroblocks too (left-and-up blocks).
Chaos... :-) This is a price of feedback h264. Very bad to hear it. But you right. Only first left-top blocks can used from the previous bitrate...
With best regards, seemees

Dark Shikari
22nd October 2009, 01:24
akupenguin
Thanks. I seem to understand.
We I-predict macroblock. Then compare predicted macroblock with original and quant DIFFERENCIES with QP (depends on bitrate). And dequanted DIFF plus predictation used in next prediction (on right-down macroblocks). Ok, New QP changes DIFFERENCIES and all previous macroblocks too (left-and-up blocks).
Chaos... :-) This is a price of feedback h264. Very bad to hear it. But you right. Only first left-top blocks can used from the previous bitrate...
With best regards, seemeesOf course, you could re-use all the modes regardless; it would just be very inefficient, especially if changing the bitrate dramatically.