Chapter 32. Using the Movie Library Sample Programs

A comprehensive set of sample programs is provided with the Movie Library. You can use sample programs to learn about the Movie Library or as skeleton code for building your own application. This chapter describes the Movie Library sample programs.

About the Sample Programs

Sample programs demonstrating how to use the Movie Library for creating, editing, and playing movies and displaying their parameters are included in the /usr/people/4Dgifts/examples/libmovie directory. Sample movies, somersault.mv and sampleQT.mv are also provided.

The sample programs are organized into directories according to purpose; most programs reside in a directory having the same name:

common 

contains source code to a helper function called glxhelper, which creates an X window suitable for GL drawing—it uses no libmovie functions but is used by the simplemovie, manymovie, and moviescreen sample programs

createmovie 

is a movie-making application with a command-line interface

createmovieqt 

is a movie-making application with a command-line interface for making QuickTime movies

editmovie 

is a movie-editing application with a command-line interface for editing QuickTime movies

manymovie 

is a movie-playing application that allows several movies to play at the same time

misc 

contains four programs:

  • aud-to-movie, which adds (or replaces) a movie audio track

  • img-to-movie, which converts a sequenced image file into a movie

  • mvinfo, which displays the parameters of a movie file

  • simplemovie, which provides a keyboard interface for playing a movie

miscqt 

contains two programs:

  • mvinfoqt, which displays the parameters of a movie file

  • simplemovieqt, which provides a keyboard interface for playing a QuickTime movie

The Movie Library sample programs have been put through an extensive code review and testing process by the engineers who created the Movie Library. The sample programs demonstrate how to write solid code with the Movie Library and provide many basic movie application features that you may want to incorporate into your own code.

Some of the guidelines followed in creating these sample programs:

  • global functions are put in header files to increase modularity

  • forward declarations are provided for local functions

  • variables are lowercase

  • symbols are uppercase

  • functions begin with lowercase and have uppercase letters at word breaks

  • extensive comments appear both at the beginning of each program or module and in the body of the code

Figure 32-1 shows the format of the introductory comments that appear in all the Movie Library sample programs. Introductory comments document the name of the file, its usage, a brief description of what the program does, and a complete listing of the functions used in the program.

Figure 32-1. Comments in Movie Library Sample Programs: createmovie.c++


Figure 32-2 shows the body of createmovie.c++, showing the modularity of this program—main() contains only 5 lines that call other modules.

Figure 32-2. Modularity of Movie Library Sample Programs: createmovie.c++


Creating Movies

This section describes two sample programs that demonstrate how to use the Movie Library to create movies:

createmovie 

which creates a movie from any combination of image files, audio files, or other movie files, letting you specify the compression scheme, loop mode, frame rate and size, and the output movie filename from the command line

img-to-movie 

which creates a movie file from an image file

createmovie [-c compression] [-l loopMode] [-r frameRate] [-s xsize,ysize]
[-p paramtype, userParam, userParamVal] [-o outMovie] file[…]

where:

-c compression 

specifies the image compression scheme, where you enter one of the following for compression:

jpeg

DM_IMAGE_JPEG

mvc1 (default)

DM_IMAGE_MVC1

mvc2

DM_IMAGE_MVC2

none

DM_IMAGE_UNCOMPRESSED

rle

DM_IMAGE_RLE

8rgb

DM_IMAGE_UNCOMPRESSED


-l loopMode 

specifies the loop mode, where you enter one of the following for loopMode: once, loop, or swing

-r frameRate 

specifies the frame rate in frames per second

If one or more movie files are included in the file source material, outmovie will have the same frame rate as the first movie file on the command line; otherwise, if the frame rate is not set explicitly, the default is 15.0 frames per second.

-s xsize,ysize 

specifies the frame size, where:
xsize is the horizontal dimension
ysize is the vertical dimension

The default frame size is that of the first file on the command line containing frame size information, which might be either an image or a movie file.

You can set the frame size explicitly, which enlarges or reduces the images as required. Aspect ratios are preserved when scaling images, so that the resulting images might be letter-boxed if the aspect ratio of the source image is different from the aspect ratio specified by the given xsize and ysize.

-p paramtype, userParam, userParamVal 


adds a user-defined parameter of type paramtype, named userParam, whose value is userParamVal

-o outmovie 

specifies the name of the output movie

file 

includes one or more image, audio, or movie files

You can also use createmovie to convert a movie file from one compression scheme to another, by creating a new movie in the specified compression scheme from the old movie.

createmovie contains the following files:

createmovieArgs.c++

contains command line processing functions, access to user preferences, and creation and access to the filenames entered on the command line

createmovieArgs.h

is the external interface to createmovieArgs.c

createmovieConvert.c

is used only in conjunction with the Silicon Graphics QuickTime Compressor Library to convert between QuickTime and rgb image data

createmovieConvert.h

is the external interface to createmovieqtConvert.c++

createmovieFiles.c++

puts files into a new movie

createmovieFiles.h

has external declarations for createmovieFiles.c++

createmovieInit.c++

initializes a new movie file

createmovieInit.h

has external declarations for createmovieInit.c++

createmovieResize.c++

performs filtering operations on image frames

createmovieResize.h

has external declarations for createmovieResize.c++

Figure 32-3 shows a call graph for createmovie. The call graph shows the overall structure of the program, indicating which modules call which functions. Some calls are omitted for the sake of clarity.

Figure 32-3. Call Graph for createmovie


Creating a Movie from a Sequence of Images

The img-to-movie.c program in the /usr/people/4Dgifts/examples/libmovie/misc directory creates a movie file from an image file. The image file can have a single image, in which case the movie file has only a single frame, or a sequence of images. The resulting movie file has a frame rate of 15.0 frames per second and uses MVC1 compression.

To run the program, enter:

img-to-movie imagefile newmoviefile

where:

imagefile 

is the name of the image file you want to make into a movie

newmoviefile 

is the name of the new movie

Adding or Replacing a Movie Audio Track

The aud-to-movie.c program in the /usr/people/4Dgifts/examples/libmovie/misc directory adds an audio track to a silent movie or replaces the movie's existing audio track. The audio track can be an AIFF file or any audio file that is readable by libaudiofile.

To run the program, enter:

aud-to-movie audiofile moviefile

where:

audiofile 

is the name of the audio track you want to put in the movie

moviefile 

is the name of the movie

Editing Movies

The /usr/people/4Dgifts/examples/libmovie/editmovie directory contains editmovie.c, a simple command line program for editing movies, and its associated files. The editing operations available are insert, delete, and paste. Only one editing operation can be selected at a time.

To run the program, enter:

editmovie -e editMovie,trackType,firstEditFrame,numFrames
[-d] [-s sourceMovie,firstSrcFrame [-i] [-p] [-m] [-o outMovie]

where:

-e 

is followed by four arguments:

editMovie

the name of the movie to be edited

 

trackType

the type of track to edit, either image or audio

 

firstEditFrame

the first frame to be edited

 

numFrames

the number of frames to be edited


-d 

deletes frames from editMovie

-s 

specifies both the source movie and first frame to copy

-i 

inserts frames from sourceMovie into editMovie

-p 

pastes frames from sourceMovie into editMovie

-m 

performs editing on a memory-resident copy of the movie

-o 

optimizes the edited movie for playback and places it in outMovie

editmovie has the following requirements:

  • numFrames must be > 0

  • firstSrcFrame must be ≥ 0

  • when inserting (-i) or pasting (-p), the -s option must be supplied in order to specify the movie to copy from and the first frame to copy

  • when inserting (-i) or pasting (-p), the sum of numFrames and firstSrcFrame must not exceed the last frame number in sourceMovie.

  • when deleting (-d), the sum of numFrames and firstEditFrame must not exceed the last frame number in editMovie

editmovie contains the following files:

editmovie.c 

a simple command line movie editor

editmovieArgs.c 

a command-line-parsing module for editmovie

editmovieArgs.h 

declarations for external functions in editmovieArgs.c

editmovieEdit.c 

contains editTheMovie(), which is the only external function, and its supporting functions

editmovieEdit.h 

external interface to editmovieEdit.c, which actually performs the editing for editmovie

Figure 32-4 shows the call graph for editmovie.c

Figure 32-4. Call Graph for editmovie.c


Displaying Movie Parameters

mvinfo displays information about a movie, including its image track and audio track (if present) parameters. To run the program, enter:
mvinfo moviefile

Playing Movies

This section describes three sample programs that play movies:

simplemovie 

implements a keyboard interface for playing movies

manymovie 

plays up to four movies simultaneously

moviescreen 

implements a movie-based screen-saver application

Creating a Simple Keyboard Interface for Playing Movies

simplemovie has a keyboard interface for playing a movie. To run the program, enter:
simplemovie moviefile

The keyboard commands are:

<3> 

loop 3 times

<b> or <B> 

play backward

<e> or <E> 

play every frame

<f> or <F> 

play fast

<h> or <H> 

play slow

<l> or <L> 

toggle loop state

<m> or <M> 

toggle audio muting

<p> or <P> 

play the movie

<q> or <Q> 

quit simplemovie

<r> or <R> 

rewind the movie

<s> or <S> 

stop playback

Playing Multiple Movies

manymovie is a command line program for playing up to 4 movies simultaneously. The movies can have different frame sizes.


Note: The 4 movie limitation is not related to the Movie Library; rather, it is the result of the very simple layout scheme used in manymovie for placing all movies in one window. 

To run the program, enter:

manymovie [-one] moviefile1 [moviefile2…]

By default, each of the movies appears in a separate window. When each movie is played in a separate window, the keyboard commands apply only to the current window, except for the quit command, which always exits from manymovie.

To play all movies in a single window, use the -one command line option. If the -one option is used, all keyboard commands apply simultaneously to all movies.

manymovie uses the following keyboard interface:

<l> or <L> 

changes the looping state

<m> or <M> 

toggles audio muting

<p> or <P> 

plays the movie

<q> or <Q> 

quits manymovie

<r> or <R> 

rewinds to the beginning of the movie

<s> or <S> 

stops the movie

manymovie contains the following files:

manymovie.c

is the main program, which plays several movies at once.

 

manymovieArgs.c

contains the code for processing command-line arguments, creating and accessing the movie list, and recording the number of movie

 

manymovieArgs.h

contains the external interface to the command-line argument processing code

 

manymovieEvents.c

contains X and Movie event handling code

 

manymovieEvents.h

is the external interface to manymovieEvents.c and handles X and Movie events for manymovie

manymovieWin.c

contains code for creating X windows suitable for playing movies

 

manymovieWin.h

is the external interface to manymovieWin.c, creates X windows suitable for GL rendering for all the movies, opens the movies and the windows, provides access to the X Display


Creating a Movie Screensaver Application

moviescreen is a screensaver application that plays movies. moviescreen does not save screens by itself; rather, it is designed to run under haven(1), a wrapper for IRIS GL-based screensavers, which is available on Silicon Graphics computers. Normally, the screen turns black and the movie begins playing silently, slowly drifting around the screen as it plays.

To run the program as a screensaver under haven(1), enter:

haven [-n |-o] [< moviescreen [-f] [-s] [-v volume] [-z zoom] [-l loopmode] moviefile1 [moviefile2…]>|-k]

where:

-f 

enables fullscreen playback

-s 

turns on sound for the movie (by default, screensaver movies play silently)

-v volume 

sets the playback volume if the -s option is used

Enter a value from 0 to 255 for volume.

-z zoom 

zooms the movie larger for playback

Enter an integer value for zoom. Zooming takes effect only if the -f option, fullscreen playback, is not used.

-l loopmode 

sets the loop mode.

To loop continuously (default), set loopmode to 0. To swing, set it to 1.

moviescreen contains the following files:

moviescreen.c

is the main program, which uses a movie as a screensaver

 

moviescreenArgs.c

contains code for helper functions used by moviescreen for accessing/manipulating data entered by the user via command-line arguments; maintains this information as static variables that are restricted to this module

moviescreenArgs.h

contains functions for processing the command- line arguments and accessing variables set therefrom, including movie names

 

moviescreenEvents.c

contains code to start, perform, and end screen saving, depending on reception of XEvents

 

moviescreenEvents.h

contains code to wait for an XEvent and initiate or terminate screen saving as appropriate

 

moviescreenGl.c

contains code used by moviescreen for determining the movie window position and erasing the movie window as it moves via GL drawing

moviescreenGl.h

contains functions for controlling positioning and erasing (via GL drawing) of the movie window

 

moviescreenWin.c

contains code for creating and accessing an X window suitable for GL rendering that is used by moviescreen

 

moviescreenWin.h

contains functions to create and access a mixed- model GL window

Figure 32-5 shows the call graph for moviescreen.c

Figure 32-5. Call Graph for moviescreen.c


Using the SMPTE Time Code Sample Application

Included with the Movie Library sample code is a group of utility routines in a sample application for handling SMPTE time codes. These routines work with the movie frame that is closest to the given time code rather than providing exact time code precision. Time code type is indicated by the timetype argument, which supports the four basic types of time codes listed in Table 32-1.

Table 32-1. SMPTE Time Code Types

Time Code Type

Meaning

MV_TIME_SMPTE_24

24 frames/second (motion pictures)

MV_TIME_SMPTE_25

25 frames/second (PAL video)

MV_TIME_SMPTE_30

30 frames/second (NTSC)

MV_TIME_SMPTE_D30

30 frame/second drop (NTSC 29.97)



Note: Currently, only MV_TIME_SMPTE_30 is supported.


Converting a SMPTE Time Code String to a Frame Number

To access the frame closest to a given SMPTE code, pass the SMPTE code as a string to mvStringToFrame(), which converts the given string to a frame number in the specified movie. Its function prototype is:

extern DMstatus mvStringToFrame ( MVid        movieid,
                                  char        *timestring,
                                  MVtimetype  timetype,
                                  MVframe     *framereturn )

where:

timestring 

is the SMPTE time code string

timetype 

is the SMPTE code type

framereturn 

is a pointer to the frame number that is to be returned

Specify the time code string in the format "HH:MM:SS:FF" where HH signifies two digits that represent hours, and similarly, MM minutes, SS seconds, and FF the frame count for the current second. The converter assumes that incomplete time strings are specified from the least-significant unit up; that is, a value of "02" means SMPTE frame code 02 of the current second. "03:02" means second 03, frame 02 of the current minute, and so on. The frame number is between 0 and one less than the total number of frames in the movie.

Converting a Frame Number to a SMPTE Time Code String

To obtain a SMTPE-style time code string for a given frame number, call mvFrameToString() with the frame number for which you need the SMPTE code. Its function prototype is:

extern DMstatus mvFrameToString ( MVid        movieid,
                                  MVframe     frame,
                                  MVtimetype  timetype,
                                  char        *timereturn )

where:

frame 

is the frame number for which you want to get the time code

timetype 

is the SMPTE code type

timereturn 

is a pointer into which the time code string is returned

The SMPTE code is returned as a string that lists the hour, minute, second, and frame count for the given frame number.

Converting a Time Specification to a Frame Number

To convert a fully formed time code to the nearest corresponding frame in the specified movie, call mvTimeToFrame(). Its function prototype is:

extern DMstatus mvTimeToFrame ( MVid        movieid,
                                int         hour,
                                int         minute,
                                int         second,
                                MVframe     framecnt,
                                MVtimetype  timetype,
                                MVframe     *framereturn)

where:

hour 

is the hour field of the time code

minute 

is the minute field of the time code

second 

is the second field of the time code

framecnt 

is the frame field of the time code

timetype 

is a time type from Table 32-1

framereturn 

is a pointer into which the frame number is returned

Converting a Frame Number to a Time Code

To obtain a time code corresponding to a particular frame, call mvFrameToTime() with the index of the frame for which you want the time code. Its function prototype is:

DMstatus mvFrameToTime    ( MVid        movieid,
                            MVframe     frame,
                            MVtimetype  timetype,
                            int         *hourreturn,
                            int         *minutereturn,
                            int         *secondreturn,
                            MVframe     *framecntreturn)

where:

frame 

is the frame number for which you want to get the time code

timetype 

is a time type from Table 32-1

hourreturn 

is a pointer into which the time code hour is returned

minutereturn 

is a pointer into which the time code minutes are returned

secondreturn 

is a pointer into which the time code seconds are returned

framecntreturn 

is a pointer into which the time code frame is returned