Apple Developer Connection
Member Login Log In | Not a Member? Contact ADC

< Previous PageNext Page > Hide TOC

Sample Atoms

QuickTime stores media data in samples. A sample is a single element in a sequence of time-ordered data. Samples are stored in the media, and they may have varying durations.

Samples are stored in a series of chunks in a media. Chunks are a collection of data samples in a media that allow optimized data access. A chunk may contain one or more samples. Chunks in a media may have different sizes, and the individual samples within a chunk may have different sizes from one another, as shown in Figure 2-25.


Figure 2-25  Samples in a media

Samples in a media

One way to describe a sample is to use a sample table atom. The sample table atom acts as a storehouse of information about the samples and contains a number of different types of atoms. The various atoms contain information that allows the media handler to parse the samples in the proper order. This approach enforces an ordering of the samples without requiring that the sample data be stored sequentially with respect to movie time in the actual data stream.

The next section discusses the sample table atom. Subsequent sections discuss each of the atoms that may reside in a sample table atom.

In this section:

Sample Table Atoms
Sample Description Atoms
Time-to-Sample Atoms
Sync Sample Atoms
Sample-to-Chunk Atoms
Sample Size Atoms
Chunk Offset Atoms
Using Sample Atoms


Sample Table Atoms

The sample table atom contains information for converting from media time to sample number to sample location. This atom also indicates how to interpret the sample (for example, whether to decompress the video data and, if so, how). This section describes the format and content of the sample table atom.

The sample table atom has an atom type of 'stbl'. It can contain the sample description atom, the time-to-sample atom, the sync sample atom, the sample-to-chunk atom, the sample size atom, the chunk offset atom, and the shadow sync atom.

The sample table atom contains all the time and data indexing of the media samples in a track. Using tables, it is possible to locate samples in time, determine their type, and determine their size, container, and offset into that container.

If the track that contains the sample table atom references no data, then the sample table atom does not need to contain any child atoms (not a very useful media track).

If the track that the sample table atom is contained in does reference data, then the following child atoms are required: sample description, sample size, sample to chunk, and chunk offset. All of the subtables of the sample table use the same total sample count.

The sample description atom must contain at least one entry. A sample description atom is required because it contains the data reference index field that indicates which data reference atom to use to retrieve the media samples. Without the sample description, it is not possible to determine where the media samples are stored. The sync sample atom is optional. If the sync sample atom is not present, all samples are implicitly sync samples.

Figure 2-26 shows the layout of the sample table atom.


Figure 2-26  The layout of a sample table atom

The layout of a sample table atom

The sample table atom contains the following data elements.

Size

A 32-bit integer that specifies the number of bytes in this sample table atom.

Type

A 32-bit integer that identifies the atom type; this field must be set to 'stbl'.

Sample description atom

See “Sample Description Atoms.”

Time-to-sample atom

See “Time-to-Sample Atoms.”

Sync sample atom

See “Sync Sample Atoms.”

Sample-to-chunk atom

See “Sample-to-Chunk Atoms.”

Sample size atom

See “Sample Size Atoms.”

Chunk offset atom

See “Chunk Offset Atoms.”

Shadow sync atom

Reserved for future use.

Sample Description Atoms

The sample description atom stores information that allows you to decode samples in the media. The data stored in the sample description varies, depending on the media type. For example, in the case of video media, the sample descriptions are image description structures. The sample description information for each media type is explained in “Media Data Atom Types”

Figure 2-27 shows the layout of the sample description atom.


Figure 2-27  The layout of a sample description atom

The layout of a sample description atom

The sample description atom has an atom type of 'stsd'. The sample description atom contains a table of sample descriptions. A media may have one or more sample descriptions, depending upon the number of different encoding schemes used in the media and on the number of files used to store the data. The sample-to-chunk atom identifies the sample description for each sample in the media by specifying the index into this table for the appropriate description (see “Sample-to-Chunk Atoms”).

The sample description atom contains the following data elements.

Size

A 32-bit integer that specifies the number of bytes in this sample description atom.

Type

A 32-bit integer that identifies the atom type; this field must be set to 'stsd'.

Version

A 1-byte specification of the version of this sample description atom.

Flags

A 3-byte space for sample description flags. Set this field to 0.

Number of entries

A 32-bit integer containing the number of sample descriptions that follow.

Sample description table

An array of sample descriptions. For details, see “General Structure of a Sample Description.”

General Structure of a Sample Description

While the exact format of the sample description varies by media type, the first four fields of every sample description are the same.

Sample description size

A 32-bit integer indicating the number of bytes in the sample description.

Data format

A 32-bit integer indicating the format of the stored data. This depends on the media type, but is usually either the compression format or the media type.

Reserved

Six bytes that must be set to 0.

Data reference index

A 16-bit integer that contains the index of the data reference to use to retrieve data associated with samples that use this sample description. Data references are stored in data reference atoms.

These four fields may be followed by additional data specific to the media type and data format. See “Media Data Atom Types” for additional details regarding specific media types and media formats.

Time-to-Sample Atoms

Time-to-sample atoms store duration information for a media’s samples, providing a mapping from a time in a media to the corresponding data sample. The time-to-sample atom has an atom type of 'stts'.

You can determine the appropriate sample for any time in a media by examining the time-to-sample atom table, which is contained in the time-to-sample atom.

The atom contains a compact version of a table that allows indexing from time to sample number. Other tables provide sample sizes and pointers from the sample number. Each entry in the table gives the number of consecutive samples with the same time delta, and the delta of those samples. By adding the deltas, a complete time-to-sample map can be built.

The atom contains time deltas: DT(n+1) = DT(n) + STTS(n) where STTS(n) is the (uncompressed) table entry for sample n and DT is the display time for sample (n). The sample entries are ordered by time stamps; therefore, the deltas are all nonnegative. The DT axis has a zero origin; DT(i) = SUM (for j=0 to i-1 of delta(j)), and the sum of all deltas gives the length of the media in the track (not mapped to the overall time scale, and not considering any edit list). The edit list atom provides the initial DT value if it is nonempty (nonzero).

Figure 2-28 shows the layout of the time-to-sample atom.


Figure 2-28  The layout of a time-to-sample atom

The layout of a time-to-sample atom

The time-to-sample atom contains the following data elements.

Size

A 32-bit integer that specifies the number of bytes in this time-to-sample atom.

Type

A 32-bit integer that identifies the atom type; this field must be set to 'stts'.

Version

A 1-byte specification of the version of this time-to-sample atom.

Flags

A 3-byte space for time-to-sample flags. Set this field to 0.

Number of entries

A 32-bit integer containing the count of entries in the time-to-sample table.

Time-to-sample table

A table that defines the duration of each sample in the media. Each table entry contains a count field and a duration field. The structure of the time-to-sample table is shown in Figure 2-29.


Figure 2-29  The layout of a time-to-sample table entry

The layout of a time-to-sample table entry

You define a time-to-sample table entry by specifying these fields:

Sample count

A 32-bit integer that specifies the number of consecutive samples that have the same duration.

Sample duration

A 32-bit integer that specifies the duration of each sample.

Entries in the table describe samples according to their order in the media and their duration. If consecutive samples have the same duration, a single table entry can be used to define more than one sample. In these cases, the count field indicates the number of consecutive samples that have the same duration. For example, if a video media has a constant frame rate, this table would have one entry and the count would be equal to the number of samples.

Figure 2-30 presents an example of a time-to-sample table that is based on the chunked media data shown in Figure 2-25. That data stream contains a total of nine samples that correspond in count and duration to the entries of the table shown here. Even though samples 4, 5, and 6 are in the same chunk, sample 4 has a duration of 3, and samples 5 and 6 have a duration of 2.


Figure 2-30  An example of a time-to-sample table

An example of a time-to-sample table

Sync Sample Atoms

The sync sample atom identifies the key frames in the media. In a media that contains compressed data, key frames define starting points for portions of a temporally compressed sequence. The key frame is self-contained—that is, it is independent of preceding frames. Subsequent frames may depend on the key frame.

The sync sample atom provides a compact marking of the random access points within a stream. The table is arranged in strictly increasing order of sample number. If this table is not present, every sample is implicitly a random access point.

Sync sample atoms have an atom type of 'stss'. The sync sample atom contains a table of sample numbers. Each entry in the table identifies a sample that is a key frame for the media. If no sync sample atom exists, then all the samples are key frames.

Figure 2-31 shows the layout of a sync sample atom.


Figure 2-31  The layout of a sync sample atom

The layout of a sync sample atom

The sync sample atom contains the following data elements.

Size

A 32-bit integer that specifies the number of bytes in this sync sample atom.

Type

A 32-bit integer that identifies the atom type; this field must be set to 'stss'.

Version

A 1-byte specification of the version of this sync sample atom.

Flags

A 3-byte space for sync sample flags. Set this field to 0.

Number of entries

A 32-bit integer containing the count of entries in the sync sample table.

Sync sample table

A table of sample numbers; each sample number corresponds to a key frame. Figure 2-32 shows the layout of the sync sample table.


Figure 2-32  The layout of a sync sample table

The layout of a sync sample table

Sample-to-Chunk Atoms

As samples are added to a media, they are collected into chunks that allow optimized data access. A chunk contains one or more samples. Chunks in a media may have different sizes, and the samples within a chunk may have different sizes. The sample-to-chunk atom stores chunk information for the samples in a media.

Sample-to-chunk atoms have an atom type of 'stsc'. The sample-to-chunk atom contains a table that maps samples to chunks in the media data stream. By examining the sample-to-chunk atom, you can determine the chunk that contains a specific sample.

Figure 2-33 shows the layout of the sample-to-chunk atom.


Figure 2-33  The layout of a sample-to-chunk atom

The layout of a sample-to-chunk atom

The sample-to-chunk atom contains the following data elements.

Size

A 32-bit integer that specifies the number of bytes in this sample-to-chunk atom.

Type

A 32-bit integer that identifies the atom type; this field must be set to 'stsc'.

Version

A 1-byte specification of the version of this sample-to-chunk atom.

Flags

A 3-byte space for sample-to-chunk flags. Set this field to 0.

Number of entries

A 32-bit integer containing the count of entries in the sample-to-chunk table.

Sample-to-chunk table

A table that maps samples to chunks. Figure 2-34 shows the structure of an entry in a sample-to-chunk table. Each sample-to-chunk atom contains such a table, which identifies the chunk for each sample in a media. Each entry in the table contains a first chunk field, a samples per chunk field, and a sample description ID field. From this information, you can ascertain where samples reside in the media data.


Figure 2-34  The layout of a sample-to-chunk table entry

The layout of a sample-to-chunk table entry

You define a sample-to-chunk table entry by specifying the following data elements.

First chunk

The first chunk number using this table entry.

Samples per chunk

The number of samples in each chunk.

Sample description ID

The identification number associated with the sample description for the sample. For details on sample description atoms, see “Sample Description Atoms.”

Figure 2-35 shows an example of a sample-to-chunk table that is based on the data stream shown in Figure 2-25.


Figure 2-35  An example of a sample-to-chunk table

An example of a sample-to-chunk table

Each table entry corresponds to a set of consecutive chunks, each of which contains the same number of samples. Furthermore, each of the samples in these chunks must use the same sample description. Whenever the number of samples per chunk or the sample description changes, you must create a new table entry. If all the chunks have the same number of samples per chunk and use the same sample description, this table has one entry.

Sample Size Atoms

You use sample size atoms to specify the size of each sample in the media. Sample size atoms have an atom type of 'stsz'.

The sample size atom contains the sample count and a table giving the size of each sample. This allows the media data itself to be unframed. The total number of samples in the media is always indicated in the sample count. If the default size is indicated, then no table follows.

Figure 2-36 shows the layout of the sample size atom.


Figure 2-36  The layout of a sample size atom

The layout of a sample size atom

The sample size atom contains the following data elements.

Size

A 32-bit integer that specifies the number of bytes in this sample size atom.

Type

A 32-bit integer that identifies the atom type; this field must be set to 'stsz'.

Version

A 1-byte specification of the version of this sample size atom.

Flags

A 3-byte space for sample size flags. Set this field to 0.

Sample size

A 32-bit integer specifying the sample size. If all the samples are the same size, this field contains that size value. If this field is set to 0, then the samples have different sizes, and those sizes are stored in the sample size table.

Number of entries

A 32-bit integer containing the count of entries in the sample size table.

Sample size table

A table containing the sample size information. The sample size table contains an entry for every sample in the media’s data stream. Each table entry contains a size field. The size field contains the size, in bytes, of the sample in question. The table is indexed by sample number—the first entry corresponds to the first sample, the second entry is for the second sample, and so on.

Figure 2-37 shows a sample size table.


Figure 2-37  An example of a sample size table

An example of a sample size table

Chunk Offset Atoms

Chunk offset atoms identify the location of each chunk of data in the media’s data stream. Chunk offset atoms have an atom type of 'stco'.

The chunk-offset table gives the index of each chunk into the containing file. There are two variants, permitting the use of 32-bit or 64-bit offsets. The latter is useful when managing very large movies. Only one of these variants occurs in any single instance of a sample table atom.

Note that offsets are file offsets, not the offset into any atom within the file (for example, a 'mdat' atom). This permits referring to media data in files without any atom structure. However, be careful when constructing a self-contained QuickTime file with its metadata (movie atom) at the front because the size of the movie atom affects the chunk offsets to the media data.

Note: The sample table atom can contain a 64-bit chunk offset atom (STChunkOffset64AID = 'co64'). When this atom appears, it is used in place of the original chunk offset atom, which can contain only 32-bit offsets. When QuickTime writes movie files, it uses the 64-bit chunk offset atom only if there are chunks that use the high 32-bits of the chunk offset. Otherwise, the original 32-bit chunk offset atom is used to ensure compatibility with previous versions of QuickTime.

Figure 2-38 shows the layout of a chunk offset atom.


Figure 2-38  The layout of a chunk offset atom

The layout of a chunk offset atom

The chunk offset atom contains the following data elements.

Size

A 32-bit integer that specifies the number of bytes in this chunk offset atom.

Type

A 32-bit integer that identifies the atom type; this field must be set to 'stco'.

Version

A 1-byte specification of the version of this chunk offset atom.

Flags

A 3-byte space for chunk offset flags. Set this field to 0.

Number of entries

A 32-bit integer containing the count of entries in the chunk offset table.

Chunk offset table

A chunk offset table consisting of an array of offset values. There is one table entry for each chunk in the media. The offset contains the byte offset from the beginning of the data stream to the chunk. The table is indexed by chunk number—the first table entry corresponds to the first chunk, the second table entry is for the second chunk, and so on.

Figure 2-39 shows an example of a chunk offset table.


Figure 2-39  An example of a chunk offset table

An example of a chunk offset table

Using Sample Atoms

This section presents examples using the atoms just described. These examples are intended to help you understand the relationships between these atoms.

The first section, “Finding a Sample,” describes the steps that the video media handler uses to find the sample that contains the media data for a particular time in a media. The second section, “Finding a Key Frame,” describes the steps that the video media handler uses to find an appropriate key frame for a specific time in a movie.

Finding a Sample

When QuickTime displays a movie or track, it “tells” the appropriate media handler to access the media data for a particular time. The media handler must correctly interpret the data stream to retrieve the requested data. In the case of video media, the media handler traverses several atoms to find the location and size of a sample for a given media time.

The media handler performs the following steps:

  1. Determines the time in the media time coordinate system.

  2. Examines the time-to-sample atom to determine the sample number that contains the data for the specified time.

  3. Scans the sample-to-chunk atom to discover which chunk contains the sample in question.

  4. Extracts the offset to the chunk from the chunk offset atom.

  5. Finds the offset within the chunk and the sample’s size by using the sample size atom.

Finding a Key Frame

Finding a key frame for a specified time in a movie is slightly more complicated than finding a sample for a specified time. The media handler must use the sync sample atom and the time-to-sample atom together in order to find a key frame.

The media handler performs the following steps:

  1. Examines the time-to-sample atom to determine the sample number that contains the data for the specified time.

  2. Scans the sync sample atom to find the key frame that precedes the sample number chosen in step 1.

  3. Scans the sample-to-chunk atom to discover which chunk contains the key frame.

  4. Extracts the offset to the chunk from the chunk offset atom.

  5. Finds the offset within the chunk and the sample’s size by using the sample size atom.



< Previous PageNext Page > Hide TOC


Last updated: 2007-09-04




Did this document help you?
Yes: Tell us what works for you.

It’s good, but: Report typos, inaccuracies, and so forth.

It wasn’t helpful: Tell us what would have helped.
Get information on Apple products.
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Copyright © 2007 Apple Inc.
All rights reserved. | Terms of use | Privacy Notice