Movie Player
Introduction
OpenBOR plays WebM movies containing VP8 video and optional Vorbis audio. Movies can supply cinematics, animated interfaces, video surfaces, picture-in-picture layouts, or the main content of a module.
The movie system separates playback from drawing. Video and audio decoding run asynchronously on the CPU. The GPU samples the decoded video planes, converts their YUV colors to RGB, and renders the result. Scripts decide when and where to draw the retained frame while the movie channel manages position, pause, speed, repeat, and audio.
Both movie_draw_to_screen() and movie_draw_to_yuv() use GPU rendering. Their difference is the destination: an OpenBOR screen for composition, or the display for direct presentation.
Choosing a drawing destination
| Feature | Screen composition | Direct display |
|---|---|---|
| Function | movie_draw_to_screen(screen, channel) |
movie_draw_to_yuv(channel)
|
| Destination | Main virtual screen or a creator-owned subscreen. | Display window. |
| Rendering | GPU video sampling, YUV-to-RGB conversion, scaling, and drawing. | GPU video sampling, YUV-to-RGB conversion, scaling, and presentation. |
| Placement | Movie dimensions and signed destination offsets, clipped to the screen. | Centered presentation using logical display dimensions and the display's stretch setting. |
| Composition | Combine movies with sprites, text, effects, and other screen content. | Present one movie frame directly. Each call replaces the displayed window content. |
| Image controls | Movie resampling and exact-black protection. Apply drawmethod effects when drawing the resulting screen. | Movie resampling and display brightness/gamma correction. |
| Typical use | Video surfaces, captions, interfaces, multiple views, and layered scenes. | Full-window movie playback controlled directly by a script or scene. |
Drawing uses the channel's retained frame. It does not advance playback or request a separate decode. One retained frame may be drawn repeatedly, sent to several screens, or shown through either drawing function. The renderer reuses its uploaded video planes until playback supplies another frame.
Formats
| Component | Format |
|---|---|
| Container | WebM. Supply the complete module path, including its extension. |
| Video | VP8. Use even frame dimensions of at least 2 by 2 pixels. The video track must use a non-stereoscopic mode. |
| Embedded audio | Optional Vorbis, mono or stereo. The engine's music sample-rate range is 11,025 through 48,000 Hz. |
| Screen destination | 32-bit OpenBOR screen, including screens created with allocscreen().
|
The decoder selects the first video track and first audio track. Unsupported video or audio codecs reject playback when the decoder opens. If initialization of a supported optional Vorbis track fails, video may continue silently with the reason recorded in the engine log.
Loading checks that source data can be opened or cached. Codec and track validation takes place when playback opens asynchronously, so a loaded source can still report a playback-opening error.
Sources, playback, and channels
Three identifiers serve different purposes:
| Item | Returned by | Purpose |
|---|---|---|
| Source ID | movie_load() |
Identifies a reusable streamed file or cached compressed file. |
| Playback object | movie_play() |
Provides property access and playback control for a movie channel. |
| Movie channel index | movie_get_channel_index() |
Selects the movie used by either drawing function. |
Movie channels are numbered 0 through 63. MOVIE_CHANNEL_COUNT provides the channel count, and MOVIE_CHANNEL_AUTO requests automatic selection. Automatic selection takes the lowest inactive channel and reports an error when every channel is active. Selecting an explicit index replaces playback on that channel.
The playback pointer identifies an engine-owned channel object. Its address remains stable while the movie system is running, including when the channel is inactive. Reusing the channel replaces its playback state. Coordinate channel ownership between scripts, and use MOVIE_PROPERTY_ACTIVE to check whether a channel is playing. Release playback with movie_stop(); the engine manages the channel object's storage.
Source storage is separate from the 64-channel playback bank and grows as sources are loaded. Source IDs increase during the session and are not reassigned after unloading. Loading the same path twice creates two source IDs. One source may be played on several channels, with independent timing, presentation settings, and audio routing.
Loading modes
| Constant | Source storage | Use |
|---|---|---|
MOVIE_LOADING_STREAM |
Retains the file path. Each playback opens its own file position and decoder. | Playback that reads its compressed data from the module as needed. This is the default. |
MOVIE_LOADING_CACHE |
Reads the complete compressed file into a shared source buffer. | Reusing a clip or avoiding repeated reads of its compressed data. |
MOVIE_LOADING_AUTO |
Chooses cached or streamed storage using the engine's source-cache policy. | Automatic storage selection. |
Cached storage contains the compressed WebM file. Each playback still owns its decoder, frame queues, position, and controls.
Automatic selection uses an aggregate movie-cache budget of one quarter of total system memory and preserves a reserve of at least one eighth of total memory or 128 MiB, whichever is greater. If a cache allocation cannot be satisfied, automatic mode can use streaming. Explicit cache mode reports a failed load. Open or size-validation failures fail loading. During caching, read errors fail the load instead of selecting streaming.
Script playback
movie_load()
movie_load(path [, loading])Returns an integer source ID. path is the exact module path; OpenBOR does not append an extension. Omitting loading selects MOVIE_LOADING_STREAM.
int source_id = movie_load(
"data/scenes/intro.webm",
openborconstant("MOVIE_LOADING_AUTO")
);Source ID 0 is valid. Invalid paths, empty files, invalid loading modes, and failed source allocations report script errors.
movie_play()
movie_play(source_id [, movie_channel])Returns the playback object for the acquired movie channel. Omit the second argument, pass NULL(), or use MOVIE_CHANNEL_AUTO for automatic selection.
void playback = movie_play(source_id);
int channel = movie_get_channel_index(playback);
// Select a particular movie channel.
void another_playback = movie_play(source_id, 5);Every start restores the channel's default properties. Configure the returned playback object after calling movie_play().
Startup is asynchronous. The channel becomes active before its first frame is necessarily ready. Drawing an active channel during startup can therefore complete without displaying a frame. Decoder-opening errors stop that playback and are reported in the engine log.
movie_stop()
movie_stop(playback);Stops the selected playback and makes its channel available for reuse. The channel's active state, source association, position, and duration are reset. Calling it with a valid inactive channel object leaves that channel inactive.
Decoder teardown can continue asynchronously after the channel becomes inactive. Playback objects remain owned by the engine. Creator-owned destination screens have their own lifetime and can be released with free() when the creator has finished using them.
movie_unload()
movie_unload(source_id);Releases a loaded source after every playback and retiring decoder has finished using it. Invalid or still-referenced sources report a script error. This function returns no status value for polling.
Source unloading requires completed decoder teardown. Decoder teardown may still retain that source after a channel becomes inactive. The script API has no source-readiness query; checking MOVIE_PROPERTY_ACTIVE alone is insufficient for an immediate unload. Keep reusable sources loaded across playbacks. Movie-system shutdown completes decoder teardown and releases remaining sources automatically.
Basic screen-composition example
The following functions use global variables to retain the controller's state across callbacks. Call initialization once, call drawing while the interface is visible, and call cleanup when closing it. The source remains loaded for reuse.
void movie_example_init()
{
int source_id = movie_load(
"data/scenes/intro.webm",
openborconstant("MOVIE_LOADING_AUTO")
);
void playback = movie_play(source_id);
void screen = allocscreen(320, 180);
setglobalvar("movie_example_source", source_id);
setglobalvar("movie_example_playback", playback);
setglobalvar("movie_example_screen", screen);
}
void movie_example_draw()
{
void playback = getglobalvar("movie_example_playback");
void screen = getglobalvar("movie_example_screen");
if(playback == NULL() || screen == NULL()) return;
if(!movie_get_property(
playback, openborconstant("MOVIE_PROPERTY_ACTIVE")
)) return;
int channel = movie_get_channel_index(playback);
clearscreen(screen);
movie_draw_to_screen(screen, channel);
drawstringtoscreen(screen, 6, 6, 0, "PLAYING");
drawscreen(screen, 16, 16, 5000);
}
void movie_example_close()
{
void playback = getglobalvar("movie_example_playback");
void screen = getglobalvar("movie_example_screen");
if(playback != NULL()) movie_stop(playback);
if(screen != NULL()) free(screen);
setglobalvar("movie_example_playback", NULL());
setglobalvar("movie_example_screen", NULL());
}The drawing function uses the common drawmethod when submitting its completed screen through drawscreen(). Keep that shared state appropriate for the intended presentation, as described in Drawmethod. Close the controller after pending draws that reference its screen have finished.
Screen composition
movie_draw_to_screen()
movie_draw_to_screen(screen, movie_channel)The first argument is a screen pointer. The second is an integer movie-channel index. Obtain that index with movie_get_channel_index(playback), read MOVIE_PROPERTY_CHANNEL, or supply the channel explicitly selected at playback start.
The function draws the retained frame immediately into the selected screen. Its opaque movie rectangle is clipped to the destination, and pixels outside that rectangle retain their existing contents. Scripts control clearing, layering, later screen submission, and screen lifetime.
Use this sequence when a movie shares a composition with other graphics:
- Clear or prepare the destination.
- Draw background content.
- Draw the movie with
movie_draw_to_screen(). - Draw captions, sprites, or other overlays.
- Submit the completed screen with
drawscreen().
Multiple movies drawn into one screen follow call order. Later movie rectangles replace earlier content where they overlap. Movie channel numbers do not determine that order.
movie_draw_to_screen() uses the movie's own size, offset, resampling, and black-filter properties. Global drawmethod state does not control this operation. To rotate, curve, blend, or otherwise transform the complete movie image, draw it into a subscreen and apply Drawmethod settings when drawing that screen.
Main screen and subscreens
Use allocscreen(width, height) to create a subscreen. For a drawing hook that operates directly on the main virtual screen:
void destination = openborvariant("vscreen");
movie_draw_to_screen(destination, channel);Direct main-screen drawing participates at the point where the script executes. Subsequent drawing can cover it, and a subsequent clear removes it. Playback retains no destination pointer, so each call can select a different screen.
Both drawing functions require an active channel. Before the first frame is ready, a valid draw can do nothing successfully. Invalid channels, inactive channels, invalid screen destinations, and failed rendering report script errors. The drawing functions return no script value.
Dimensions and offsets
Width and height resolve independently:
| Property value | Screen composition | Direct display |
|---|---|---|
0 |
Use the corresponding destination-screen dimension. | Use the corresponding WebM display dimension. |
MOVIE_SIZE_NATIVE |
Use the corresponding decoded-frame dimension. | Use the corresponding WebM display dimension. |
| Positive explicit value | Set the movie rectangle's dimension in destination pixels. | Set the requested logical display dimension. |
Set MOVIE_PROPERTY_OFFSET_X and MOVIE_PROPERTY_OFFSET_Y to place the movie rectangle within a screen. Offsets are signed. Oversized dimensions combined with negative offsets provide zoom-and-crop layouts; smaller dimensions provide inset placement. Set both dimensions appropriately when preserving the movie's aspect ratio.
For a 640 by 360 movie rectangle centered within a 320 by 180 subscreen:
movie_set_property(playback, openborconstant("MOVIE_PROPERTY_WIDTH"), 640);
movie_set_property(playback, openborconstant("MOVIE_PROPERTY_HEIGHT"), 360);
movie_set_property(playback, openborconstant("MOVIE_PROPERTY_OFFSET_X"), -160);
movie_set_property(playback, openborconstant("MOVIE_PROPERTY_OFFSET_Y"), -90);
movie_draw_to_screen(movie_screen, channel);With width and height both zero, the same playback can fill destinations of different sizes:
movie_set_property(playback, openborconstant("MOVIE_PROPERTY_WIDTH"), 0);
movie_set_property(playback, openborconstant("MOVIE_PROPERTY_HEIGHT"), 0);
movie_set_property(playback, openborconstant("MOVIE_PROPERTY_OFFSET_X"), 0);
movie_set_property(playback, openborconstant("MOVIE_PROPERTY_OFFSET_Y"), 0);
movie_draw_to_screen(large_screen, channel);
movie_draw_to_screen(small_screen, channel);Both calls draw the same retained frame. Changing dimensions or offsets changes its presentation without restarting playback.
Exact-black protection
Screen transparency settings can use RGB 0, 0, 0 as a transparent key when a subscreen is drawn later. Enable black protection when the movie's black areas should remain visible through such a composition:
movie_set_property(
playback, openborconstant("MOVIE_PROPERTY_BLACK_FILTER"), 1
);The GPU first resamples the video planes and converts the result to RGB. It then changes exact output black to RGB 1, 1, 1 when protection is enabled. Other output colors retain their values. This protection is disabled by default and applies to screen composition.
Movie resampling
MOVIE_PROPERTY_RESAMPLE selects the filter used when rendering the decoded video. It applies to screen composition and direct display.
| Constant | Sampling |
|---|---|
DC_RESAMPLE_MOVIE |
Movie default; equivalent to bilinear. |
DC_RESAMPLE_NEAREST |
Nearest-sample reconstruction. |
DC_RESAMPLE_BILINEAR |
Linear reconstruction between neighboring samples. |
DC_RESAMPLE_BICUBIC_CATMULL_ROM |
Catmull-Rom cubic reconstruction. |
DC_RESAMPLE_BICUBIC_MITCHELL |
Mitchell cubic reconstruction. |
DC_RESAMPLE_BICUBIC_BSPLINE |
B-spline cubic reconstruction. |
DC_RESAMPLE_LANCZOS_2 |
Two-lobe Lanczos reconstruction. |
DC_RESAMPLE_LANCZOS_3 |
Three-lobe Lanczos reconstruction. |
movie_set_property(
playback,
openborconstant("MOVIE_PROPERTY_RESAMPLE"),
openborconstant("DC_RESAMPLE_BICUBIC_MITCHELL")
);
int filter = movie_get_property(
playback, openborconstant("MOVIE_PROPERTY_RESAMPLE")
);The GPU reconstructs the Y, U, and V planes at their respective resolutions before converting to RGB. Changing this property takes effect when the frame is drawn again, including while playback is paused. It does not seek or restart decoding.
Movie resampling controls video-to-destination rendering. Drawmethod resampling can separately control a subsequent transformed drawscreen() operation. Final display filtering is another stage applied when presenting the completed game screen. See Drawmethod resampling.
Direct display presentation
movie_draw_to_yuv()
movie_draw_to_yuv(channel);This function renders the retained movie frame directly to the display window and presents it immediately. It clears the window background, centers the movie using the selected logical display dimensions, and applies display brightness and gamma. The display's stretch setting can fill the window.
Logical display dimensions are scaled for the actual window. They are independent of the game screen resolution configured in video.txt; explicit movie width and height are not literal window-pixel extents. Movie X/Y offsets and exact-black protection are screen-composition controls and do not apply here.
Each direct-display call presents a complete movie view. Use screen composition to assemble captions, sprites, several movies, or other overlays into one frame. Direct presentation preserves the normal graphics resources, so ordinary screen rendering and presentation remain available. Stopping a directly presented movie clears its direct-presentation state.
For example, after acquiring an active playback:
movie_set_property(
playback, openborconstant("MOVIE_PROPERTY_WIDTH"),
openborconstant("MOVIE_SIZE_NATIVE")
);
movie_set_property(
playback, openborconstant("MOVIE_PROPERTY_HEIGHT"),
openborconstant("MOVIE_SIZE_NATIVE")
);
// Call when the controller wants to present the retained movie frame.
if(movie_get_property(playback, openborconstant("MOVIE_PROPERTY_ACTIVE")))
{
movie_draw_to_yuv(movie_get_channel_index(playback));
}Position, pause, speed, repeat, interruption, and sound routing use the same playback properties for either destination.
Playback timing and control
Position and duration use milliseconds. The playback clock begins when asynchronous decoder startup is ready. Drawing does not move the clock, and omitting drawing does not pause the movie.
While the movie owns an embedded audio stream, consumed audio provides the playback clock. Video follows that clock, with late frames discarded when necessary to preserve synchronization. Video-only playback uses the movie's own clock. If another audio producer replaces the movie's sound stream, the movie continues from a matching position on its own clock.
Position and seeking
Read MOVIE_PROPERTY_POSITION for the playback position. Write it to request a seek:
movie_set_property(
playback, openborconstant("MOVIE_PROPERTY_POSITION"), 12500
);This requests 12.5 seconds. Seeking reopens decoding asynchronously while retaining the displayed frame until replacement frames are ready. When duration is known, a seek at or beyond its end is clamped to the last available millisecond. MOVIE_PROPERTY_DURATION is read-only and remains zero until duration is known.
Use position changes for rewind controls and other backward navigation. Continuous negative-speed playback is not supported.
Pause
Set MOVIE_PROPERTY_PAUSED to 1 to hold the position and displayed frame and pause the movie's audio. Set it to 0 to resume.
movie_set_property(playback, openborconstant("MOVIE_PROPERTY_PAUSED"), 1);
movie_set_property(playback, openborconstant("MOVIE_PROPERTY_PAUSED"), 0);Speed
MOVIE_PROPERTY_SPEED accepts finite nonnegative values. Normal speed is 1.0. Values below 1 slow playback; values above 1 accelerate it. Positive speed changes also change embedded-audio speed and pitch.
Rates above 16.0 are clamped to 16.0, and accepted rates are rounded to the nearest 0.001. Negative or nonfinite values report an error. Zero holds the position and pauses audio. Returning from zero to a positive rate reopens decoding at the held position.
movie_set_property(playback, openborconstant("MOVIE_PROPERTY_SPEED"), 0.5);
movie_set_property(playback, openborconstant("MOVIE_PROPERTY_SPEED"), 2.0);
movie_set_property(playback, openborconstant("MOVIE_PROPERTY_SPEED"), 1.0);These statements select half speed, double speed, and normal speed respectively; use the statement matching the desired control action.
Repeat and interruption
Set MOVIE_PROPERTY_REPEAT to 1 to reopen playback from the beginning when it reaches its terminal frame. Scripts can implement a custom loop region by observing position and requesting a seek to its beginning.
MOVIE_PROPERTY_INTERRUPT defaults to 1. New Escape or normal movie-skip input stops interruptible playback during the engine update. Set it to 0 when a controller should handle stopping itself:
movie_set_property(playback, openborconstant("MOVIE_PROPERTY_REPEAT"), 1);
movie_set_property(playback, openborconstant("MOVIE_PROPERTY_INTERRUPT"), 0);Embedded audio
Movie channels select playback objects. Sound channels select destinations in the audio mixer. Their indexes are separate: movie channels run from 0 through 63, and sound channels run from 0 through 4095.
Each playback initially selects sound channel 0, the music and media channel. Embedded audio replaces the occupant of its selected sound channel. Ordinary movie_play() leaves unrelated sound channels running, and a movie without audio leaves existing sound playback alone.
movie_set_sound_channel()
movie_set_sound_channel(playback, sound_channel)Routes an active playback's embedded audio to the selected sound channel. Changing the destination reopens decoding asynchronously at the playback position. Selecting the already assigned channel leaves its routing in place. The destination is selected directly, so its existing audio is replaced without priority arbitration.
For simultaneous audible movies, assign separate sound channels immediately after starting each playback:
int source_a = movie_load("data/scenes/feed_a.webm");
int source_b = movie_load("data/scenes/feed_b.webm");
void playback_a = movie_play(source_a, 4);
movie_set_sound_channel(playback_a, 10);
void playback_b = movie_play(source_b, 5);
movie_set_sound_channel(playback_b, 11);MOVIE_PROPERTY_SOUND_CHANNEL reports this routing and is read-only through the property API. Embedded streams also expose applicable controls through the sound-object API. Seek the movie through MOVIE_PROPERTY_POSITION so audio and video remain coordinated. See Audio Overview.
Movie object API
Use openborconstant() for property and loading-mode names. Channel indexes are ordinary integers.
Channel access
| Function | Result |
|---|---|
movie_get_channel_object(channel) |
Engine-owned object for that channel, including an inactive channel. |
movie_get_channel_index(playback) |
Index belonging to a valid playback object. |
movie_get_channel_mask() |
Unsigned 64-bit activity mask with one bit per movie channel. Set bits identify active playback on their corresponding channels. |
For a particular channel, read its active state directly:
void playback = movie_get_channel_object(5);
int active = movie_get_property(
playback, openborconstant("MOVIE_PROPERTY_ACTIVE")
);Property access
movie_get_property(playback, property)
movie_set_property(playback, property, value)The getter returns the property's value. The setter returns no value. Invalid objects, property identifiers, values, and writes to read-only properties report script errors.
The following defaults describe a newly started playback. Stopping resets the channel; its source becomes invalid and position and duration become zero.
| Property | Access | Default | Meaning |
|---|---|---|---|
MOVIE_PROPERTY_ACTIVE |
Read | 1 after start | Whether the channel owns active playback. |
MOVIE_PROPERTY_BLACK_FILTER |
Read/write | 0 | Protect exact output black during screen composition. |
MOVIE_PROPERTY_CHANNEL |
Read | Acquired index | Movie channel index. |
MOVIE_PROPERTY_DURATION |
Read | 0 until known | Unsigned 64-bit duration in milliseconds. |
MOVIE_PROPERTY_HEIGHT |
Read/write | 0 | Height selection; see Dimensions and offsets. |
MOVIE_PROPERTY_INTERRUPT |
Read/write | 1 | Allow normal movie-skip input. |
MOVIE_PROPERTY_OFFSET_X |
Read/write | 0 | Signed horizontal placement within a destination screen. |
MOVIE_PROPERTY_OFFSET_Y |
Read/write | 0 | Signed vertical placement within a destination screen. |
MOVIE_PROPERTY_PAUSED |
Read/write | 0 | Hold the playback clock and owned audio stream. |
MOVIE_PROPERTY_POSITION |
Read/write | 0 | Unsigned 64-bit millisecond position. Writing requests a seek. |
MOVIE_PROPERTY_REPEAT |
Read/write | 0 | Restart from the beginning at the terminal frame. |
MOVIE_PROPERTY_RESAMPLE |
Read/write | DC_RESAMPLE_MOVIE |
Filter used to render decoded video; the movie default is bilinear. |
MOVIE_PROPERTY_SOUND_CHANNEL |
Read | 0 | Embedded-audio destination. Change with movie_set_sound_channel().
|
MOVIE_PROPERTY_SOURCE |
Read | Played source | Source ID associated with playback. |
MOVIE_PROPERTY_SPEED |
Read/write | 1.0 | Forward playback rate, normalized to 0 through 16. |
MOVIE_PROPERTY_WIDTH |
Read/write | 0 | Width selection; see Dimensions and offsets. |
Boolean properties use zero for disabled and nonzero for enabled. Pause, position, speed, and sound-routing changes require active playback. Configure appearance, repeat, and interruption after starting playback because each start restores the defaults.
For a diagnostic listing of the public property map:
movie_get_property(playback, openborconstant("PROPERTY_ACCESS_DUMP"));This writes the property listing to the engine log.
Blocking playback
Use blocking playback when a movie should complete or be interrupted before its calling script or scene continues.
playwebm()
playwebm(path [, noskip])int result = playwebm("data/scenes/intro.webm", 0);noskip defaults to 0, allowing normal skip input. Nonzero disables that input interruption.
| Result | Meaning |
|---|---|
1 |
Movie completed. |
0 |
Playback failed. |
-1 |
Permitted input interrupted playback. |
The function stops existing movie playbacks, opens a streamed source, and presents it directly using the WebM display dimensions. It services input and movie playback while blocking the caller. Width and height use MOVIE_SIZE_NATIVE, playback speed is normal, repeat is disabled, and movie resampling uses its bilinear default.
Movies with embedded audio request exclusive audio playback through sound channel 0. Silent movies allow existing music to continue. Screenshot capture renders the movie through the GPU into a capture screen before reading the completed pixels for the screenshot.
Cleanup completes decoder teardown and releases the temporary source before returning. Normal game rendering remains available after direct movie presentation.
Scene video command
video path [skipone] [noskip]Both optional flags default to zero.
| Setting | Behavior |
|---|---|
skipone 0 |
Interrupting a video skips the remaining scene media. |
skipone 1 |
Interrupting a video skips it and continues with subsequent scene commands. |
noskip 0 |
Normal movie-skip input is allowed. |
noskip 1 |
Movie-skip input is disabled. |
For example, allow skipping just this video while continuing the scene:
video data/scenes/intro.webm 1 0Later silence commands still apply when interruption skips the remaining scene media.
The scene command uses the blocking player. Choose the movie_* functions when a controller needs asynchronous playback, screen composition, seeking, repeat, selectable resampling, or independent movie and sound channels.