Log in

View Full Version : Resume Support for x264: Draft Specification


Isochroma
25th October 2006, 02:23
The following is a highly condensed summary of the full tentative specification, whose complete paper with example file is available here: x264 Resume (http://isochroma.com/Testfiles/Misc/x2R/x2R.htm)

The Current Scenario

The x264 encoder is an excellent choice for video compression. Depending on the parameters supplied via commandline or GUI, the encoder can compress video at a wide range of speeds. The best-quality configuration is capable of using large quantities of CPU cycles per frame.

On even the fastest machines, video sequences can take long periods of time to compress to completion using the highest quality settings (exhaustive motion search, RDO, etc.) A recent 256,576-frame video compression run using best settings was to complete in just over seven days. At about noon on the third day, the encoding machine (an Athlon XP 2500+) suffered a write-induced crash (hard drive corruption) from a second unrelated process performing routine network transfer.

After such an interruption, the output file was in an unusable state; the user is forced to delete the file and restart encoding from the first frame.

x264 encoding can be interrupted by at least three classes of events:

1. Unplanned system shutdown/restart: crashes, hard locks, etc. caused by hardware or software

2. Third party (including OS) software crashes that affect x264 or cmd.exe, or require manual reboot to resume other functions.

3. Unexpected accidental or volitionary termination of the x264 process by:

3.1. Window Close

3.2. CTRL-C/CTRL-Break

3.3. Task Manager's End Process/End Process Tree

3.4. Running Software: other processes request termination using API calls or the TaskKill executeable.

Due to write-induced crashing which occurs on average every three days, the machine will likely never complete a compression run of this particular video and is thus unusable for the purpose.

--------

The Proposal

It is proposed that to overcome the difficulties encountered in maintaining a stable environment for the x264 encoder during long encoding runs, a resume feature be implemented.

This resume feature has some desireable characteristics, and the costs are low:

1. Resumption of compression can occur in all cases (1-3) listed above.

2. Encoding normally resumes at the last keyframe successfully written to completion in the output stream.

3. If the recovery file is damaged, encoding resumes at the keyframe before the last keyframe.

4. The recovery file is small (<100k), and need only be updated at each keyframe.

5. The resume feature uses very little CPU, and only periodically at each keyframe.

6. The resume feature's activation is optional and predicated on the appearance of a currently-unused switch in the commandline.

The meat of the idea is a recovery file, written in plaintext and having an optional extension of .x2r. This recovery file contains various things, among them a header with the x264 version used, commandline passed which initiated the reference encoding session, and one or two FRS (Frame Recovery Segments), depending on the stage of encoding.

User X, who wishes to use the resume feature specifies the new -z filename.txt or -z filename.x2r argument anywhere in the commandline:

x264.exe (parameters) -z filename.x2r

Encoding begins, and when the encoder finishes the first keyframe, it creates filename.x2r. This file allows the encoding to resume/recover at a future point in time, when x264 is restarted.

At some point in the encoding process, our happy User X is now sad because his computer crashed / virus wiped it out, or his mom made him shut it down before bed :) Due to premature termination, the x264 process produced an incomplete .264/MKV/MP4 file, which has no header and would likely be difficult to parse, not to mention play.

But this time, luck is on his side! The next morning after brushing his teeth and eating a nourishing breakfast, he sits down at the box and brings up a cmd window. Into it he types this line:

x264.exe -zr filename.x2r

The magical -zr parameter tells x264 to ignore anything else on the line and begin a resume process by first opening filename.x2r. In filename.x2r the process will find first a header (initial scan starts from the beginning) which provides certain information:

---- CUT ----

See full paper (http://isochroma.com/Testfiles/Misc/x2R/x2R.htm) for the remaining juicy details.

akupenguin
25th October 2006, 03:57
quick notes:
The only feasible place to stop is just _before_ a keyframe. The keyframe itself needs to be encoded by the copy of x264 doing the recovering.
A physical file offset is sufficient to continue writing a .264 file, or mkv with some modification, but does not work for mp4.
I won't comment on the "Other data which x264 needs to continue compression" part until you find a volunteer to actually implement it.

bkman
25th October 2006, 07:18
Oh man, this may seem an ironic coincidence, but I've just had a crash after 30 hours of encoding.

Akupenguin, do you know how I might salvage a usable stream so far that I can splice with the rest after I encode it? The output format used was MP4 (though it doesn't seem a valid MP4 file at the moment).

(obviously I voted in the affirmative for this proposal!)

GodofaGap
25th October 2006, 08:41
Making some modifications to ELDER might be a not so difficult way to have resume/recovery support. It will not be as granular, though.

I have voted I won't use the feature, since I basically won't let my encodings run over several days.

Kopernikus
25th October 2006, 10:13
Use sane settings. Exhaustive search is not better than umhex.

xyloy
25th October 2006, 12:42
Use sane settings. Exhaustive search is not better than umhex.
I agree.
Also, you can use turbo for the first pass(--subme 1 --me dia --partitions none) 5 ref. frames for "normal" movies(8 for anime/cartoon), and RDO Level 1(subme 6) instead of RDO Level 2(subme 7).
The output quality will be as good(unless you are a bionicman :P ) and the encoding process much faster.

Still, a sort of resume feature would be great when the PC crashed during the night. ^^

Thunderbolt8
25th October 2006, 14:23
since my cpu is quite slow and crashes might happen due to older hardware, I would really look forward to see such a feature to be implented, if possible. for both, crash resume and convenience.

Mc Onyx
25th October 2006, 14:50
Use Matroska, because few times i interrupted the encoding in 2pass 2. pass the file was playable and it was only partially done. Then you could resume the encoding at the last playable keyframe. When i used MP4 partially encoded file was not playable.

bond
25th October 2006, 19:13
writing fragmented mp4 files should be possible with gpac already

i dont get why resume shouldnt work with normal mp4? obviously a not to the end encoded mp4 will not work when the headers are written at the end (as its the case now), tough why should this problem also exist with resumed writing to mp4?

pyrates
26th October 2006, 22:01
This is just what is needed. Right now I'm spending 4-5 days doing the 2nd pass of a 1080p encode and if power goes out or there is a crash I gotta start over. By the way, don't ever use the asus overclock utility, if it crashes, x264 stops using cpu cycles and crashes in about an hour. And I got an amd athlon 64 x2 4600+. Course I use the filter hybridfupp in avisynth, and that is very cpu intensive. But gives great results.