6 Capture and Compress Example





The example video capture and compression program captures and compresses video frames from one of the SunVideo card's input ports and saves the compressed frames to a file (Figure 6-1). This example program (rtvc_capture_movie) does not display the movie while it is being captured. The techniques used in this program are typical of those that might be used in a program that was part of a movie-making application.

The example can be used to capture a complete movie. Or, you can use it to capture and compress a single frame.

    Figure 6-1 Video Capture Program

Running the Video Capture and Compress Example

The sources for the video capture and compression example are installed in the directory:

/opt/SUNWits/Graphics-sw/xil/examples/rtvc_capture_movie

Use the provided Makefile to compile the program.

After building the program, you can display its usage information by typing the program name and an argument that the program doesn't use. For example:

% rtvc_capture_movie -@

With just the -o <filename'> argument, the application captures video from composite port 1, compresses the video using CellB compression, and sends the compressed bitstream to filename.

Note - The rtvc_capture_movie program does not append a "compressor type" extension to "filename."

Table 6-1 describes the rtvc_capture_movie command's arguments. Arguments without values are switches. For example, -E is a switch.

    Table 6-1 Arguments for the rtvc_capture_movie Program

--------------------------------------------------------------------------------
Argument Description Default --------------------------------------------------------------------------------
                                                                   
-o <filename'>           The name of the file into which to         None - a 
                        store the compressed video.                filename is 
                                                                   required
                                                                   
-C <compression_type'>   The type of compression. Valid             CellB
                        compressor names are CellB, Jpeg,                         
                        or Mpeg1.                                                 
                                                                   
-I <port_number'>        The number of the SunVideo card's          Composite 
                        input port to which your video             port 1
                        device is connected (0 for S-Video, 1                     
                        or 2 for composite).                                      
                                                                   
-f <number_frames        The number of frames to capture.           100
                        Set to 1 to capture a single frame.                       
                                                                   
-s <shrink_factor'>      Shrinks the captured image by the          2
                        integer value of                                          
                        <shrink_factor> (1, 2, 3, 4, 5, ...).                       
                        A shrink factor of 0 turns off                            
                        shrinking, which is equivalent to a                       
                        shrink factor of 1.                                       
                                                                                  
                        For NTSC, the default shrink factor                       
                        of 2 delivers SIF images of 320x240                       
                        pixel images. A shrink factor of 4                        
                        delivers smaller Quarter SIF (QSIF)                       
                        images of 160x120 pixels.                                 
                                                                   
-i <skip_frames          Number of frames to skip between           0
                        captures. For example, for NTSC,                          
                        skipping one frame captures frames                        
                        at a rate of 15 fps.                                      
                                                                   
-D <devname'>            Name of the SunVideo card. See the         /dev/rtvc0
                        section "A Comment About Moving                           
                        SunVideo Cards Among Slots" on                            
                        page 19 for more information about                        
                        SunVideo device names.                                    
                                                                   
-E                      Exit on first XIL error.                   Keep running 
                                                                   and try to 
                                                                   recover

--------------------------------------------------------------------------------

Example rtvc_capture_movie Commands

Capture 100 frames from composite port 2, compressing with CellB (the default), and store the compressed frames in the file mymovie.CellB:

% rtvc_capture_movie -I 2 -o mymovie.CellB

Capture a single frame, compress it with JPEG, and store the compressed frame in the file mymovie.Jpeg:

% rtvc_capture_movie -f 1 -C Jpeg -o mymovie.Jpeg

About the rtvc_capture_movie Video Capture and Compression Code

The video capture and compression program uses many of the same programming elements as the video capture and display program described in the previous chapter. These common code segments are shaded in the list that follows. However, because this example program performs compression, it includes additional code to create a Compressed Image Sequence (CIS) and to capture and compress the image.

Examples of the code that performs the last three operations are described in the next three sections.

Creating a Compressed Image Sequence

The following example shows how to create a CIS with the xil_cis_create() function. A CIS is a container that holds compressed images for a particular type of compression. Therefore, the CIS type must match the compression type.

----------------------------------------
XilSystemState state; XilCis cis; char *cis_type = "CellB"; . . cis = xil_cis_create(state, cis_type); ----------------------------------------

Capturing and Compressing an Image

Use the xil_compress() function to capture and compress an image. (When connected to a device image, the xil_compress() function automatically performs a capture operation.)

The xil_compress() function checks to see if the proper compression software (in this case CellB) is loaded into the SunVideo card's compression engine and, if necessary, automatically downloads the proper software. The function then writes the compressed image to a CIS.

The code that follows is an example of using the function to compress an image named "scaled_image" into a CIS. The CIS must have been initialized to hold CellB data by the xil_cis_create() function as shown in the previous example.

----------------------------------------------------
XilSystemState state; XilCis cis; XilImage rtvc_image, scaled_image; . . if (doscale) { xil_scale(rtvc_image, scaled_image, "nearest", 1.0 / (float) doscale, 1.0 / (float) doscale); } else scaled_image = rtvc_image; xil_compress(scaled_image, cis); xil_cis_sync(cis); xil_toss(scaled_image); ----------------------------------------------------

In the example SunVideo programs, the xil_toss() and xil_cis_sync() functions are used to help improve performance.

The xil_toss() function tells the XIL library that the contents of an image will not be referenced again. In the absence of the xil_toss() call, the XIL library could perform some operations that an application programmer knows are not necessary.

In order to execute multiple operations as a molecule, XIL defers the operations until they are needed. Thus, the capture occurs when the deferred molecule or atom is executed, not when xil_scale() is called.

The xil_cis_sync() function forces any outstanding calls to xil_compress() to complete. This forces the actual capture and compress to occur immediately instead of being deferred as part of a molecule.

For more information about deferred execution, refer to the "Acceleration in XIL Programs" chapter in the XIL Programmer's Guide.

Writing the Compressed Image to a File

The rtvc_capture_movie program uses the following code to write the contents of the CIS to a file. (The scale and compress operations are also included.) Refer to the chapter "Compressing and Decompressing Sequences of Images" in the XIL Programmer's Guide for more information about writing compressed images to a file.

------------------------------------------------------------------
df = fopen(movie_file, "w"); if (df == NULL) { fprintf(stderr, "failed to open output movie file.\n"); xil_close(state); exit(1); } for (image_count = frame_count; image_count 0; image_count--) { if (doscale) { xil_scale(rtvc_image, scaled_image, "nearest", 1.0 / (float) doscale, 1.0 / (float) doscale); } else scaled_image = rtvc_image; xil_compress(scaled_image, cis); xil_cis_sync(cis); xil_toss(scaled_image); write_frames( df, cis ); /* get initial time after first capture (this is to avoid */ /* including board initialization time in frames per second */ /* reporting) */ if (image_count == frame_count) gettimeofday(&t0, NULL); } /* done, empty buffer if any frames remain */ xil_cis_flush(cis); write_frames(df, cis); ------------------------------------------------------------------

Playing Movies

You can use the XIL movie player program to play a movie that you've captured and saved to a file with the rtvc_capture_movie example program. The movie player example (xilcis_example) is described in the XIL Programmer's Guide. The source (and Makefile) for the program is provided in the SUNWxilh package.

Table 6-2 describes the xilcis_example program's arguments. Arguments without values are switches. For example, -c is a switch.

    Table 6-2 Arguments for the xilcis_example Program

--------------------------------------------------------------------------------
Argument Description Default --------------------------------------------------------------------------------
                                                           
-c                   Cell file                             If one of these 
                                                           three switches is 
-cb                  CellB file                            not specified, 
                                                           display a JPEG 
-m                   MPEG-1 file                           file
                                                           
-s <width <height'>    Width and height of CellB image in    None
                     pixels (required only if the -cb                             
                     argument is used).                                           
                                                           
-i <filename'>        Name of compressed file to display.   Display the JPEG 
                     The compression type must match       file earth.jpg
                     the -c, -cb, or -m argument.                                 

--------------------------------------------------------------------------------

Example Movie Player Commands

Show the earth.jpg JPEG single frame:

% xilcis_example

Play a CellB movie stored in the file mymovie.CellB:

% xilcis_example -cb -s 320 240 -i mymovie.CellB

(To play a CellB movie, you must include the width and height because it is not included in the compressed bytestream.)

Play a MPEG-1 movie stored in the file mymovie.Mpeg1:

% xilcis_example -m -i mymovie.Mpeg1

Playing rtvc_capture_movie Movies with the SunVideo Program

You can use the SunVideo demonstration program to play back movies that you create with the rtvc_capture_movie example program. The SunVideo program will play back JPEG and MPEG-1 movies directly; these movie files must have either a .Jpeg or .Mpeg1 extension. However, the SunVideo program requires a .VCR file in order to play back a CellB movie. The ASCII .VCR file contains the information about a CellB movie's width and height, which is not included in the movie's bytestream.

You can use a text editor to create a .VCR file. An example of the .VCR file format is shown on page 33.