Description of the 4X Movie (4xm) File Format
by Mike Melanson (melanson@pcisys.net)
v1.0: June 8, 2003


  Copyright (c) 2003 Mike Melanson
  Permission is granted to copy, distribute and/or modify this document
  under the terms of the GNU Free Documentation License, Version 1.2
  or any later version published by the Free Software Foundation;
  with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
  A copy of the license is included in the section entitled "GNU
  Free Documentation License".


Contents
--------
 * Introduction
 * File Format
 * Framerate Calculation
 * References
 * Changelog
 * GNU Free Documentation License


Introduction
------------
The 4xm file format transports video data compressed with 4X
Technologies' proprietary video codec. It also transports audio data
interleaved with the coded video chunks. The format appears to support
multiple audio language tracks, as well. The technology seems to be
primarily intended for use in computer and console games.

The information in this document was obtained by studying a sample of
4xm files.


File Format
-----------
All multi-byte numbers are little endian.

The 4xm file format is quite reminiscent of the Microsoft audio/video
interleaved (AVI) file format. A 4xm file is comprised of a series of
chunks that are identified by human-readable four-character codes
(fourccs). There are two types of chunks. The first type is a LIST chunk
which encapsulates other chunks. A LIST chunk begins with a 12-byte
preamble which is laid out as follows:

  bytes 0-3    fourcc: 'LIST'
  bytes 4-7    length of LIST chunk, not including LIST fourcc or length
               fields, but including the chunk type
  bytes 8-11   chunk type, e.g.: 'HEAD'

A list chunk contains any number of the second type of chunk, which is
laid out as follows:

  bytes 0-3    chunk type fourcc
  bytes 4-7    length of chunk, not including the chunk type or length
  bytes 8..    chunk data

Additionally, a 4xm file begins with the following RIFF header:

  bytes 0-3    fourcc: 'RIFF'
  bytes 4-7    file size, not including RIFF fourcc or file size fields
  bytes 8-11   RIFF sub-type fourcc: '4XMV'

4xm files appear to have the following general structure:

  RIFF header
    LIST-HEAD chunk
      LIST-HNFO chunk
    LIST-TRK_ chunk
      LIST-VTRK chunk
      LIST-STRK chunk
      [..more tracks..]
    LIST-MOVI chunk
      LIST-FRAM chunk
      LIST-FRAM chunk
      LIST-FRAM chunk
      [..more frame tracks..]

A LIST-HNFO chunk is known to contain name, info, and std_ chunks. A
name chunk has the following structure:

  bytes 0-3    fourcc: 'name'
  bytes 4-7    length of name data
  bytes 8..    variable-length DOS/Windows path and filename of 4xm file

An info chunk has the following structure:

  bytes 0-3    fourcc: 'info'
  bytes 4-7    length of info data
  bytes 8..    variable-length info field, apparently the name and
               version of the software used to create the 4xm file;
               example: "Packed with 4xmovie v.4.0.0.2001"

A std_ chunk has the following structure:

  bytes 0-3    fourcc: 'std_'
  bytes 4-7    length of std_ data (always appears to be 8)
  bytes 8-15   unknown

A LIST-TRK_ chunk contains any number of LIST_?TRK chunks (where ? = 'V'
for a video track and 'S' for a sound track) which define the format of
various audio/video tracks within the file. A 4xm file may contain
multiple audio tracks and possibly multiple video tracks.

A LIST_?TRK has typically contains a name chunk and a ?trk format chunk,
where ? = 'v' for a video format chunk and 's' for a sound format
chunk. A name chunk has the following structure:

  bytes 0-3    fourcc: 'name'
  bytes 4-7    length of name data
  bytes 8..    variable-length DOS/Windows path and filename of file
               that was used as the data for this track

A vtrk chunk has the following structure:

  bytes 0-3    fourcc: 'vtrk'
  bytes 4-7    length of vtrk structure (68 or 0x44 bytes)
  bytes 8-35   unknown
  bytes 36-39  video width
  bytes 40-43  video height
  bytes 44-47  video width (again?)
  bytes 48-51  video height (again?)
  bytes 52-75  unknown

A strk chunk has the following structure:

  bytes 0-3    fourcc: 'strk'
  bytes 4-7    length of strk structure (40 or 0x28 bytes)
  bytes 8-11   track number
  bytes 12-15  audio type: 0 = PCM, 1 = 4X IMA ADPCM
  bytes 16-35  unknown
  bytes 36-39  number of audio channels
  bytes 40-43  audio sample rate
  bytes 44-47  audio sample resolution (8 or 16 bits)

A LIST-MOVI chunk contains any number of LIST-FRAM chunks. A LIST-FRAM
chunk contains any number of ?frm chunks (where ? = 'i', 'p', or 'c')
and snd_ chunks. The ifrm, pfrm, and cfrm identifiers refer to the 3
frame types (I, P, and C) in the 4XM video coding method.

For more information regarding the 4XM custom video and audio codecs,
see the links in the references section.


Framerate Calculation
---------------------
One format detail that has not yet been uncovered is framerate data.
However, the audio and video frames appear to be tightly interleaved
such that one video frame has the same duration as the succeeding audio
frame. Thus, the audio frame can be used to determine the duration of
video frames.


References
----------
Website for 4X Technologies, host to 4xm player and samples:
http://www.4xtechnologies.com/

The aformentioned website was unavailable for a time in the beginning of
2003. By the middle of 2003, the domain name appears to have reverted
back to the pool of available names. Some of the public files that were
formerly available from their site (including Win32 player, SDK
documents, and sample files) have been mirrored here:
ftp://ftp.mplayerhq.hu/MPlayer/samples/game-formats/4xm/ 

Description of the 4XM video codec:
      (HTML) http://www.mplayerhq.hu/~michael/4xm.html
      (Text) http://www.mplayerhq.hu/~michael/4xm.txt
(Postscript) http://www.mplayerhq.hu/~michael/4xm.ps
(LyX Source) http://www.mplayerhq.hu/~michael/4xm.lyx

A description of the 4XM IMA ADPCM variant is included in this document:
      (HTML) http://www.pcisys.net/~melanson/codecs/simpleaudio.html
(LyX Source) http://www.pcisys.net/~melanson/codecs/simpleaudio.lyx


Changelog
---------
v1.0: June 8, 2003
- added references to documents that describe the custom video and audio
codecs used in 4xm files
- added reference to archived copy of 4X Tech's public files
- expanded information on strk chunks
- document pushed to v1.0 since enough information has been determined
to create a functional demuxer

v0.2: March 13, 2003
- licensed under GNU Free Documentation License

v0.1: January 1, 2003
- initial release


GNU Free Documentation License
------------------------------
see http://www.gnu.org/licenses/fdl.html