You can use the Video Extender to get a shot or frame from a video. Before you can get a shot or frame, you must open the video for shot detection. The Video Extender uses an index to access frames and shots. Before you get a shot or frame, you must create an index for the video.
After a video is opened and an index is created, you can get the next shot or frame in a video or get a specific frame by frame number. The Video Extender can process video clips in MPEG-1 format. If you plan to use a retrieved frame with a program that requires RGB format, you can convert the frame to that format by using a Video Extender API.
Use the DBvOpenFile API to open a video that is stored in a file. The file must be accessible from the client. Use the DBvOpenHandle API to open a video that is stored in a database table. The application must first connect to the database. If the video is stored in a database table, the Video Extender copies the video to a temporary file. The temporary file is located in a directory specified in the client environment variable DB2VIDEOTEMP. Opening a video initializes it for shot detection. The Video Extender sets a pointer to the beginning of the video, that is, frame 0.
When you use either API, you need to point to an area that is used to contain the pointer to the video data structure (DBvIOType). The Video Extender allocates this structure in response to the API call, and uses the structure to store information about the video. The structure also points to the frame data structure (DBvFrameData) that contains the pixel content of the current frame. For a description of these structures see Shot detection data structures. For the DBvOpenFile API, you also need to specify the name of the video file. For the DBvOpenHandle API, you also need to specify the video handle.
For example, the following statement opens a video for shot detection that is stored in a file:
DBvIOType *videoptr; rc=DBvOpenFile ( &videoptr, /* pointer to video structure pointer */ "/employee/video/rsmith.mpg"); /* video file */
The following statement opens a video for shot detection that is stored in a database table:
EXEC SQL BEGIN DECLARE SECTION; char Vid_hdl[251]; EXEC SQL END DECLARE SECTION; DBvIOType *videoptr; EXEC SQL SELECT VIDEO INTO :Vid_hdl FROM EMPLOYEE WHERE NAME="Anita Jones"; rc=DBvOpenHandle( &videoptr, /* pointer to video structure pointer */ vid_hdl); /* video handle*/
The Video Extender uses an index to access frames and shots in a video. You need to create an index for a video before you can get a shot or frame from the video (the MPEG format does not provide an index for frames and shots). The index maps frame numbers to the bit streams that comprise an MPEG-1 video.
You can create an index for a video by using the DBvCreateIndexFromVideo API or the DBvCreateIndex API. However, if you have opened the video for shot detection using the DBvOpenFile API or the DBvOpenHandle API, you do not have to explicitly create an index; the Video Extender will automatically have created an index for you. (See Opening a video for shot detection for information about how to open the video)..
When an index is created (either explicitly or automatically), the DB2 Video Extender attempts to store the index in the same path as the video file. It first attempts to store the index file as fname.ext.idx, where fname is the name of the video file, and ext is the extension of the video file. If that attempt fails, the Video Extender attempts to store the file as fname.idx in the same directory as the video file. If that fails, it attempts to store the index file in the local directory, first as fname.ext.idx, and second as fname.idx.
When the file is opened, the Video Extender looks for the index file in the following order:
For example, if an index is created for a video file named myvideo.mpg, the Video Extender will look first for a writeable index named myvideo.mpg.idx in the same path as the video file.
When you use the DBvCreateIndexFromVideo API, specify the DBvIOType data structure. The Video Extender stores the name of the index file in the structure. For a description of this structure, see Shot detection data structures. For example, the following statement creates an index for a video that has previously been opened for shot detection:
DBvIOType *video; rc=DBvCreateIndexFromVideo( video); /* pointer to video structure */
When you use the DBvCreateIndex API, specify the name of the video file. The Video Extender stores the index in a file (in the same directory in which the video resides). For example, the following statement creates an index for a video file (the file was not previously opened for shot detection):
rc=DBvCreateIndex( "/employee/video/rsmith.mpg"); /* video file */
You can also determine whether an index exists for a video. Use the DBvIsIndex API to check for an index. The API sets a status variable to 0 if no index exists for the video, or 1 if an index exists for the video. For example, the following statement checks for the existence of an index for a video file:
short *status rc=DBvIsIndex( "/employee/video/rsmith.mpg", /* video file */ &status); /* status indicator */
Back up the video index: Back up the video index file in case you need to recover it. The file is located in the directory where the Video Extender is installed.
You can get the current frame in a video. You an also set the current frame to be a particular frame number. Use the DBvGetFrame API to get the current frame in a video. Use the DBvSetFrameNumber API to set the current frame to a particular frame number.
When you use the DBvGetFrame API, specify the video structure. For example, the following statement gets the current frame in a video:
DBvIOType *video; rc=DBvGetFrame( video); /* pointer to video structure */
When you use the DBvSetFrameNumber API, specify the video structure and the number of the frame you want set as the current frame. For example, the following statements set the current frame to frame number 85 and then get the frame:
DBvIOType *video; rc=DBvSetFrameNumber( video, /* pointer to video structure */ 85); /* frame number */ rc=DBvGetFrame( video); /* pointer to video structure */
On output, the DBvSetFrameNumber API resets the currentFrame field in the DBvIOType structure. The DBvGetFrame API puts the pixel content of the frame in the DBvFrameData structure. For a description of these structures, see Shot detection data structures.
Use the DBvDetectShot API to get the next shot in a video. When you use the DBvDetectShot API, you need to point to the following data structures:
You also need to point to a starting frame for the search. The Video Extender will begin its search for the next shot from that point in the video.
As a results of the API, the Video Extender sets a shotDetected flag and points to the starting frame of the next shot and its frame data. If the shotDetected flag is set to 1, a shot has been detected. In this case, the Video Extender:
If the shotDetected flag is set to 0, a shot has not been detected. In this case, the Video Extender returns a code that indicates that the end of the video was reached.
For a description of these structures, see Shot detection data structures.
The following statements, for example, request the next shot in a video:
DBvIOType *video; long start_frame = 1; char shotDetected = 0; DBvShotControl shotCtrl; DBvShotType shot; shotCtrl>method=DETECT_CORRHIST shotCtrl>normalCorrValue=60; shotCtrl>sceneCutSkipXY=1; shotCtrl>CorrHistThresh=10; shotCtrl>DissThresh=10; shotCtrl>DissCacheSize=4; shotCtrl>DissNumCaches=7; shotCtrl>minShotSize=0; rc=DBvDetectShot( video, /* pointer to video structure */ start_frame, /* starting frame for search */ &shotDetected, /* shot detected flag */ /* 1=detected, 0=not detected */ shotCtrl, /* pointer to shot control structure */ &shot); /* pointer to shot type structure */
The content of an MPEG-1 frame is in YUV format, a format that includes information about the luminance pixel plane, Cr pixel plane, and Cb pixel plane of a frame.
If you want to edit the video frame, you may find it convenient to convert the frame from YUV to RGB format. The Video Extender provides the DBvFrameDatato24BitRGB API to convert a retrieved MPEG-1 frame from YUV format to 24-bit RGB format. To use the API, you must first allocate a target buffer.
When you issue the API, you need to point to the target buffer and the frame data that you want to convert. You also need to specify the height and width of the frame. (You can get the data, height, and width of the frame from the DBvIOType structure for the frame.) For example, the following statements convert an MPEG-1 frame to 24-bit RGB format:
char RGB[18000]; DBvIOType *video; DBvFrameData fd; rc=DBvGetNextFrame( video); /* pointer to video structure */ fd=video.fd dx=video.dx dy=video.dy rc=DBvFrameDataTo24BitRGB ( RGB, /* pointer to target buffer */ &fd, /* pointer to frame data */ dx, /* frame width */ dy); /* frame height */
Use the DBvClose API to close a video file that has been opened for shot detection. When you use the API, you specify a pointer to the video structure for the file.
For example, the following statement closes a video file that has been open for shot detection:
DBvIOType *video; rc=DBvClose (video);
The content of a retrieved MPEG-1 frame is in YUV format; this is not a format that is displayable by most image display programs. To display a retrieved video frame, you need to put it in a format that an image display program can understand, such as BMP format. For example, to display an MPEG-1 frame: