Macromedia Flash File Format (SWF)

recompiled with RAW HTWL
flash

© Macromedia, Incorporated
600 Townsend Street
San Francisco, California 94103
Tel (415) 252-2000 • Fax (415) 626-0554
Introduction Basic Types Display List Control Tags
Shapes (Examples Shapes) Gradients Buttons
Sprites Fonts and Text Shape Morphing Bitmap
Sounds Actions ActionScripts Reference

Macromedia Flash File Format (SWF) Documentation Contents

Introduction to Macromedia Flash (SWF)
The Macromedia Flash (SWF) Header
Macromedia Flash (SWF) File Structure
Tag Format
Definition and Control Tags
The Dictionary
Processing a Macromedia Flash (SWF) File
File Compression Strategy
Summary

Introduction to Macromedia Flash (SWF)

The Macromedia Flash (SWF) (pronounced 'swiff' ) file format was designed to deliver vector graphics and animation over the Internet. The Macromedia Flash File Format (SWF) was designed as a very efficient delivery format and not as a format for exchanging graphics between graphics editors. It was designed to meet the following goals: Macromedia Flash (SWF) files have the extension .swf and currently for historical reasons the MIME type is application/x-shockwave-flash.

The Macromedia Flash (SWF) Header

All Macromedia Flash (SWF) files begin with the following header:
     SWF File Header     
     Field           Type*           Comment     
     Signature           UI8           Signature byte always ‘F’     
     Signature           UI8           Signature byte always ‘W’     
     Signature           UI8           Signature byte always ‘S’     
     Version           UI8           Single byte file version (e.g. 0x04F for SWF 4)     
     FileLength           UI32           Length of entire file in bytes     
     FrameSize           RECT           Frame size in twips     
     FrameRate           UI16           Frame delay in 8.8 fixed number of frames per second     
     FrameCount           UI16           Total number of frames in movie     
* The types used in this header are defined in the Basic Types section.

The header beings with the three-byte Signature 0x46, 0x57, 0x53 (“FWS”) followed by a one-byte Version number. The version number is not an ASCII character, but an 8-bit number. For example, for SWF 4 the version byte is 0x04, not the ASCII character ‘4’ (0x35).

The FileLength field is the total length of the SWF file including the header. The FrameSize field defines the width and height of the movie. This is stored as a RECT structure.

Note: The RECT structure used for FrameSize always has the Nbits field set to 15.

The FrameRate is the desired playback rate in frames per second. This rate is not guaranteed if the SWF file contains streaming sound data, or the player is running on a slow CPU. The FrameCount is the total number of frames in this SWF movie.

Macromedia Flash (SWF) File Structure

Following the header are a series of tagged data blocks. All tags share a common format, so any program parsing a Macromedia Flash (SWF) file can skip over blocks it does not understand. Data inside the block can point to offsets within the block, but can never point to an offset in another block. This enables tags to be removed, inserted, or modified by tools that process a SWF file.
file
			structure
Macromedia Flash (SWF) File Structure


    SwfJava v0.0: how to create a SWFMovie object    
    new SWFMovieInternal (    
        int version, int width, int height, frameSize, float frameRate,    
        TagEnumeration tags    
    );    

    SwfJava v0.0: how to create a TagEnumeration object    
    final TagCollection tc = new TagCollection();    
    tc.add(tag1);    
    tc.add(tag2);    
    ...    
    final TagEnumeration te = tc.close();    


    XWF v0.0: how to declare a Flash Movie    
    <SWF version="3" width="11000" height="8000" rate="12.0">    
        <tag1 > ... </tag1>    
        <tag2 > ... </tag2>    
        ...    
    </SWF>    


Tag Format

Each tag begins with a tag type and a length. There are both a short and long tags. Short tags are used for blocks with 62 bytes of data or less and large tags can be used for any size block.
     RECORDHEADER (short)     
     Field           Type           Comment     
     Code           UI16           Tag ID and Length     
The high order 10 bits of the Code field is the tag ID. The low order 6 bits of the Code field is the length of the tag in bytes. The tag ID and length can be extracted from the Code field like this:
TagID = Code >> 6;
Length = Code & 0x3f;
If the block is 63 bytes or longer, it is stored in a long tag. The long tag consists of a short tag with a length of 0x3f, followed by a 32-bit length.
     RECORDHEADER (long)     
     Field           Type           Comment     
     Code           UI16           Tag ID and Length of 0x3f     
     Length           UI32           Length of tag     
Note: The low order 6 bits of the Code value in a long tag are all set to 1.


Extension Mechanism

Currently, many useful block types are defined. Tag numbers 0-511 are reserved for future use. Tag numbers 512-1023 are reserved for use by third party applications.

In order to let applications define their own tag types, it is possible to define an AppExtension tag that contains the name of the application functionality being implemented and a tag range that describe what tags will be used by this extension set. The extension sets should be similar to a MIME type where they take the form of the following string:
<company name>/<extension set>
For the entire file, these tag types will be interpreted in the context of the named extension. Applications should be able to remap the tag range for a particular file to avoid conflicts between two extension sets.

The extension must be in the 512-1023 tag type range.

Definition and Control Tags

There are two categories of tag in Macromedia Flash (SWF):

Tag Ordering in Macromedia Flash (SWF)

Generally speaking, tags in a Macromedia Flash (SWF) file can occur in any order. However, there are a few rules that must be observed:
  1. A given tag should only depend on tags that come before it. A tag should never depend on a tag that comes later in the file.
  2. A definition tag that defines a character must occur before any control tag that refers to that character.
  3. Streaming sound tags must be in order. Out of order streaming sound tags will result in the sound being played out of order.
  4. The End tag is always the last tag in the Macromedia Flash (SWF) file.

The Dictionary

The dictionary is a repository of characters that have been defined, and are available for use by Control Tags. The process of building and using the dictionary is as follows:
  1. A definition tag defines some content, such as a shape, font, bitmap or sound.
  2. A unique CharacterId is assigned to the content by the definition tag.
  3. The content is saved in the dictionary under the CharacterId.
  4. A control tag retrieves the content from the dictionary using the CharacterId, and performs some action on the content, such as displaying a shape, or playing a sound
Every definition tag must specify a unique ID. Duplicate IDs are not allowed. Typically, the first CharacterId is 1, the second CharacterId is 2 and so on. Character zero is special and considered a null character.

Control tags are not the only tags that reference the dictionary. Definition tags can use characters from the dictionary to define more complex objects. For example, the DefineButton tag refers to characters to define the four states of the button. The DefineText tag can refer to a font characters to select different fonts in the text.

The diagram below illustrates the interaction between definition tags, control tags and the dictionary:
dictionary
* The Display List is described in a later section.

Processing a Macromedia Flash (SWF) File

The player processes all the tags in a Macromedia Flash (SWF) file until a ShowFrame tag is encountered. At this point, the display list is copied to the screen and the player is idle until it is time to process the next frame. The contents of the first frame are the cumulative effect of performing all the control tag operations before the first ShowFrame tag. The contents of the second frame are the cumulative effect of performing all the control tag operations from the beginning of the file to the second ShowFrame tag, and so on.

File Compression Strategy

Since Macromedia Flash (SWF) files are frequently delivered over a network connection, it is important that they be as compact as possible. There are several techniques that are used to accomplish this. Here are some things to look out for:

Summary

A Macromedia Flash (SWF) file is made up of a header, followed by a number of Tags. There are two types of tags, Definition Tags and Control Tags. Definition Tags define the objects known as characters, which are stored in the Dictionary. Control Tags manipulate characters, and control the flow of the movie.
Introduction Basic Types Display List Control Tags
Shapes (Examples Shapes) Gradients Buttons
Sprites Fonts and Text Shape Morphing Bitmap
Sounds Actions ActionScripts Reference