Reading

# Reading a file

Most users simply want read access for a player toy or something. A good place to start is before opening the file, making sure it is Quicktime with quicktime_check_sig().

`quicktime_check_sig("path");`

This returns 1 if it looks like a Quicktime file or 0 if it doesn't. Then you can open the file as described in opening.html.

Next get the number of tracks for each media type in the file:

`int quicktime_video_tracks(quicktime_t *file);
int quicktime_audio_tracks(quicktime_t *file);`

While Quicktime can store multiple video tracks, the audio track count is a bit more complicated. Usually you'll only encounter a single audio track. Inside the audio track is a variable number of channels. To get the channel count call:

`int quicktime_track_channels(quicktime_t *file, int track);`

With the track parameter set to track 0. Many routines require a track parameter to specify the track to operate on. Tracks are always numbered from 0 to the total number of tracks - 1 for the particular media type.

Audio tracks are numbered from 0 to the total number of audio tracks - 1. But like I said, you'll probably never encounter an audio track higher than 0. Other routines you might find useful for getting audio information are:

`long quicktime_sample_rate(quicktime_t *file, int track);
long quicktime_audio_length(quicktime_t *file, int track);
`

quicktime_audio_length gives you the total number of samples. The sample rate is samples per second.

Routines you'll never use unless you want to write a codec are:

`char* quicktime_audio_compressor(quicktime_t *file, int track);
int quicktime_audio_bits(quicktime_t *file, int track);
`

The audio compressor call returns a 4 byte array identifying the data compression of the track. These identifiers are 4 alphanumeric characters which go along with one of the #defines in quicktime.h. The bits parameter returns the number of bits in a sample.

The most interesting contents of a Quicktime file are of course the video tracks. Quicktime stores multiple video tracks.

The available video information for each video track is:

`long quicktime_video_length(quicktime_t *file, int track);
int quicktime_video_width(quicktime_t *file, int track);
int quicktime_video_height(quicktime_t *file, int track);
float quicktime_frame_rate(quicktime_t *file, int track);
long quicktime_frame_size(quicktime_t *file, long frame, int track);
int quicktime_video_depth(quicktime_t *file, int track);
`

Tracks are numbered 0 to the total number of tracks - 1. The video length is in frames. The width and height are in pixels. The frame rate is in frames per second. Depth returns the total number of bits per pixel. These are not the number of bits per pixel on disk but the number of bits per pixel in the decompressed data returned by one of the video decoding routines. The only two values Quicktime for Linux returns are 24 and 32 and the 32 bit depth is only supported in PNG and RGB format. There's no reason to use 16 or 8. A depth of 24 means a video row from a decoding routine is in RGBRGBRGB packed bytes. A depth of 32 means video data is in RGBARGBARGBA packed bytes with alpha channels.

To get a four byte compressor type for the track issue:

`char* quicktime_video_compressor(quicktime_t *file, int track);
`

Unless you get a really nihilistic file for reading, you can safely assume the encoding scheme for track 0 of any file is the same for all tracks of that media type.

# Decoding video

The library decodes compressed video frames into rows of unsigned RGB bytes or RGBA bytes, depending on the depth. Then it gives you an array of row pointers. First issue

`int quicktime_supported_video(quicktime_t *file, int track);
`

to find out if the data for the track can be decoded by the library. This returns 1 if it is and 0 if it isn't supported.

Then use

`int quicktime_decode_video(quicktime_t *file, unsigned char **row_pointers, int track);
`

to decompress a frame at the current position of the track into **row_pointers and advance the current position. The array of rows must have enough space allocated for the entire frame. For each row that's width * depth / 8 bytes. For more about positioning go to positioning

There are other routines for reading compressed data and chunks, but unless you want to write a codec, you'd better focus on more important things.

# Decoding audio

For reading audio, first use:

`int quicktime_supported_audio(quicktime_t *file, int track);
`

To determine if the audio can be decompressed by the library. This returns 1 if it is and 0 if it isn't supported. Then use

`int quicktime_decode_audio(quicktime_t *file, QUICKTIME_INT16 *output_i, float *output_f, long samples, int channel);
`

To read a buffer's worth of samples for a single channel starting at the current position in the track. Notice this command takes a channel argument not a track argument. The channel argument is automatically converted into a track and channel. Positioning information is automatically taken from the appropriate track and advanced for all the channels in the track.

Notice the QUICKTIME_INT16* and float* parameters. This call can either return a buffer of int16 samples or float samples. The argument for the data format you want should be passed a preallocated buffer big enough to contain the sample range while the undesired format should be passed NULL. For a buffer of float samples you would say

`result = quicktime_decode_audio(file, NULL, output_f, samples, channel);
`

For a buffer of signed int16 samples you would say

`result = quicktime_decode_audio(file, output_i, NULL, samples, channel);
`

The data format you don't want should be passed a NULL. The decoder automatically fills the appropriate buffer. Floating point samples are from -1 to 0 to 1.

When you're done reading, call `quicktime_close(quicktime_t *file);
`

# Reading raw video

`long quicktime_read_frame(quicktime_t *file, unsigned char *video_buffer, int track);`

quicktime_read_frame reads one frame worth of raw data from your current position on the specified video track and returns the number of bytes in the frame. You have to make sure the buffer is big enough for the frame. A return value of 0 means error.

`long quicktime_frame_size(quicktime_t *file, long frame, int track);`

gives up the number of bytes in the specified frame in the specified track even if you haven't read the frame yet. Frame numbers start on 0.

Now some of you are going to want to read frames directly from a file descriptor using another library like libjpeg or something. To read a frame directly start by calling

`int quicktime_read_frame_init(quicktime_t *file, int track);`

to initialize the input. Then read your raw, compressed data from a file descriptor given by

`FILE* quicktime_get_fd(quicktime_t *file);`

End the frame read operation by calling

`int quicktime_read_frame_end(quicktime_t *file, int track);`

You can get the file descriptor any time the file is opened, not just when reading or writing, but you must call the init and end routines to read a frame.

# Reading raw audio

These commands are good for reading raw sample data. They should only be used for codecs not supported in the library and only work for interleaved, linear PCM data.

`long quicktime_read_audio(quicktime_t *file, char *audio_buffer, long samples, int track);`

quicktime_read_audio requires a number of samples of raw audio data to read. Then it reads that corresponding number of bytes on the specified track and returns the equivalent number of bytes read or 0 if error. The data read is PCM audio data of interleaved channels depending on the format of the track. Be aware that Quicktime for Linux tries to guess the number of bytes by the codec type, so attempts to read most nonlinear codecs will crash.