File structure

The Swf container is at the root of an SWF file. This container has an 8-byte SwfSignature followed by a compressed SwfPayload. After decompression, the payload yields a Movie with the actual content: a header with global properties and a Tag stream describing how to play the movie.

The general structure of an SWF file can be de described with the following diagram:

      +--------------+---------------------------------------------------------+
 Swf  | SwfSignature |                       SwfPayload                        |
      +--------------+----------------------------+----------------------------+
                                                  |
                                                  | decompress
                                                  |
                                                  v
                     +-------------+-----+-----+-------------------------------+
              Movie  | MovieHeader | Tag | Tag | Tag |  ...  | Tag | EndOfTags |
                     +-------------+-----+-----+-------------------------------+

Swf

The Swf element is the root of the file.

OpenFlash has no type representing the Swf element itself. As it is a container for a Movie, its value is the value of the Movie it contains.

Syntax
Swf :
   {
         SwfSignature(Compression=Simple) SwfPayload(Compression=Simple)
      | SwfSignature(Compression=Deflate) SwfPayload(Compression=Deflate)
      | SwfSignature(Compression=Lzma) SwfPayload(Compression=Lzma)
   }

Swf⁠Signature

The SWF signature is always 8 bytes long. It describes how to interpret the SWF payload.

Syntax
SwfSignature(Compression) :
   CompressionMethod(Compression) SwfVersion UncompressedSize

CompressionMethod(Compression=Simple) :
   0x46 0x57 0x53
CompressionMethod(Compression=Deflate) :
   0x43 0x57 0x53
CompressionMethod(Compression=Lzma) :
   0x5a 0x57 0x53

SwfVersion :
   UINT8

UncompressedSize :
   LE_UINT32

The SWF compression method identifies the file as an SWF file, and how the payload is compressed.

Any other value indicates that the file is not an SWF file.

The SWF version indicates the version used for the file. The interpretation of some tags depends on this version.

The UncompressedSize corresponds to the sum of the size of the SWF signature and of th uncompressed Movie.

Swf⁠Payload

The payload contains the compressed content of the movie. It must be uncompressed first before being parsed. It consists in a header followed by a sequence of tags. Each tag acts as an instruction describing how to play the movie.

Syntax
SwfPayload(Compression=Simple) :
   Movie
SwfPayload(Compression=Deflate) :
   compressWithDeflate(Movie)
SwfPayload(Compression=Lzma) :
   compressWithLzma(Movie)

Movie

Where Swf is a container, Movie is the actual content of the file. It has a header defining global properties and a stream of tags. Each tag represents an update to the runtime movie state, such as defining sprites or running ActionScript bytecode.

Syntax
Movie(SwfVersion) :
   MovieHeader Tag(SwfVersion)* EndOfTags

Movie⁠Header

Syntax
MovieHeader :
   frameSize frameRate frameCount

frameSize :
   Rect

frameRate :
   LE_UFIXED8_P8

frameCount :
   LE_UINT16

Tag

See the Tag index.

Syntax
Tag(SwfVersion) :
   ShortTag | LongTag
ShortTag(SwfVersion) :
   ShortTagHeader(code, tagSize)(code, tagSize) TagBody(code)[size: tagSize]
LongTag(SwfVersion) :
   LongTagHeader(code, tagSize)(code, tagSize) TagBody(code)[size: tagSize]

Tag⁠Header

A tag header defines the tag code and tag body size.

The tag code is an unsigned 10-bit integer identifying the action corresponding to the tag.

The tag body size is the size in bytes of the tag body following this header. It does not count the size of the tag header itself, only of the tag body. The body size is allowed to be 0.

There are two representations for the tag header: the short form (ShortTagHeader) and long form (LongTagHeader). The short form is compat but only allows to describe tags with a body size of 62 bytes or less. The short form is recommended when the tag type allows it and the body size fits.

⚠ Many tag types allow both forms, but not all. In particular bitmap definition tags require a long form header even if the body size is 62 bytes long or shorter.

Syntax
ShortTagHeader(code, bodySize)[where: bodySize < 63] :
   codeAndSize(code, bodySize)

LongTagHeader(code, bodySize) :
   codeAndSize(code, 63) size[value: bodySize]

codeAndSize(code, size)[where: size < 64] :
   LE_UINT16[value: code * 64 + size]

size :
   LE_UINT32

Adobe Reference: Tag format

Tag⁠Body

The body of the tag depends on its type. The type can be derived from the code in the tag header.

Open Flash allows a single type to correspond to multiple codes when the semantics are the same and only the syntax changes.