Flash File Format SDK


Macromedia

Flash File Format SDK

ă Macromedia, Incorporated

600 Townsend Street

San Francisco, California 94103

Tel (415) 252-2000Fax (415) 626-0554

 

 

 


SWF Format Documents

Introduction

Basic Types

Display List

Control Tags

Actions

Shapes

Gradients

Morph Shapes

Bitmaps

Fonts and Text

Sounds

Buttons

Sprites

Using the Flash File Format SDK

Appendix A: Flash Uncovered

 


Introduction to SWF

 

The SWF (pronounced 'swiff' ) file format was designed to deliver vector graphics and animation over the Internet.  The SWF file format 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:

 

·         On-screen Display—The format is primarily intended for on-screen display and so it supports anti-aliasing, fast rendering to a bitmap of any color format, animation and interactive buttons.

·         Extensibility—The format is a tagged format, so the format can be evolved with new features while maintaining backward compatibility with older players.

·         Network Delivery—The files can be delivered over a network with limited and unpredictable bandwidth. The files are compressed to be small and support incremental rendering through streaming.   SWF is a binary format and is not human readable like HTML.  SWF uses techniques such as bit-packing and structures with optional fields, to minimize file size.

·         Simplicity—The format is simple so that the player is small and easily ported. Also, the player depends upon only a very limited set of operating system functionality.

·         File Independence—Files can be displayed without any dependence on external resources such as fonts.

·         Scalability—Different computers have different monitor resolutions and bit depths. Files work well on limited hardware, while taking advantage of more expensive hardware when it is available.

·         Speed—The files are designed to be rendered at a high quality very quickly.

 

SWF files have the extension .swf and currently for historical reasons the MIME type is application/x-shockwave-flash.

 

The SWF Header

 

All 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. 0x04 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.

SWF File Structure

 

Following the header are a series of tagged data blocks.  All tags share a common format, so any program parsing a 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. 

 

 

 

 


SWF File Structure

 

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 SWF:

 

·         Definition Tags are used to define the content of the SWF movie – the shapes, text, bitmaps and sounds that are used in the movie.  Each definition tag assigns a unique ID called a character to the content it defines.  The player then stores the character is a repository called the dictionary.

 

·         Control Tags are used to manipulate characters in the dictionary, and control the flow of the movie.  For example, control tags can be used to place characters on the screen, start sounds, and to tell the player to go to another frame in the movie.

 

Tag Ordering in SWF

Generally speaking, tags in a 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 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:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


* The Display List is described in a later section.

                   

 

Processing a SWF File

The player processes all the tags in a 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 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:

·         ReuseThe structure of the character dictionary makes it very easy to reuse elements in a SWF file. For example, a shape, button, sound, font, or bitmap can be stored in a file once and referenced many times.

·         CompressionShapes are compressed using a very efficient delta encoding scheme, often the first coordinate of a line is assumed to be the last coordinate of the previous one.  Distances are also often expressed relative to the last position.

·         Default valuesSome structures like matrices and color transforms have common fields that are used more often than others. For example, for a matrix, the most common field is the translation field. Scaling and rotation are less common. Therefore if the scaling field is not present, it is assumed to be 100%. If the rotation field is not present, it is assumed that there is no rotation. This use of default values helps to minimize file sizes.

·         Change EncodingAs a rule, SWF files only store the changes between states. This is reflected in shape data structures and in the place/move/remove model used by the display list.

·         Shape Data StructureThe shape data structure uses a unique structure to minimize the size of shapes and to render anti-aliased shapes very efficiently on the screen.

 

Summary

A Flash 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.