VC ++ AVI Video programming

Source: Internet
Author: User
Tags bmp image fread microsoft video

Author: wise fish

Summary: This article analyzes the storage structure of AVI files in detail, introduces a set of Apis provided by Microsoft to operate AVI files, and uses the example code, demonstrate how to synthesize a set of static BMP images into an AVI video file and parse and save an AVI video file as a series of BMP image files.

  Keywords: AVI file BMP image VC

Avi is the abbreviation of audio video interleaved. It is a digital audio and video file format developed by Microsoft to comply with Riff file specifications, it was originally used in Microsoft Video for Windows (VFW for short) environments and is now directly supported by most operating systems such as Windows 95/98 and OS/2. The AVI format allows video and audio to be played in parallel and supports 256-color and RLE compression, but the AVI file is not limited to compression standards. Therefore, the AVI file format is only used as the standard on the control interface, non-compatible. AVI files generated using different compression algorithms must be decompressed to play out. Commonly used Avi playback drivers include Microsoft Video for Windows or Windows 95/98 Video 1 and Intel's Indeo video.

Before introducing the AVI file, let's take a look at the structure of the riff file. AVI files use the riff file structure. Riff (resource Interchange File Format) is a file format defined by Microsoft to manage multimedia data in windows, waveform audio wave, Midi, and digital video Avi are stored in this format. The basic unit for constructing a riff file is called a chunk. Each data block contains three parts,

1. 4-byte data block mark (or data block ID)

2. Data Block Size

3. Data

The entire riff file can be regarded as a data block. Its data block ID is Riff, which is called riff block. Only one riff block is allowed in a riff file. A riff block contains a series of sub-blocks. One of the block IDS is "list", which is called list. A list block can contain a series of sub-blocks, however, all sub-blocks except list blocks cannot contain sub-blocks.

Riff and list blocks have one more data field called form type and list type than normal data blocks, which are composed of the following:

1. 4-byte chunk ID)

2. Data Block Size

3. 4-byte format or list type

4. Data

Next we will look at the structure of the AVI file. AVI files are currently the most complex riff files used. They can simultaneously store audio and video data during synchronization. The format of the AVI riff block is Avi, which contains three child blocks, as described below:

1. Information block. A list block with ID "hdrl" defines the data format of the AVI file.

2. Data Block: A list block with the ID of "movi", containing Avi audio and video sequence data.

3. Index block: the sub-block with the ID of "idxl". It defines the index data of the "movi" list block and is an optional block.

Shows the structure of the AVI file. The following describes the construction of each sub-block of the AVI file.

1. Information block. The information block contains two subblocks: A subblock with ID avih and a list block with ID strl.

Information Block

The content of the "avih" sub-block can be defined by the following structure:

Typedef struct
{
DWORD dwmicrosecperframe; // display the time NS required for each worker, defining the display rate of AVI
DWORD dwmaxbytespersec; // maximum data transmission rate
DWORD dwpaddinggranularity; // The length of the record block must be a multiple of this value, usually 2048
DWORD dwflages; // special attributes of the AVI file, such as whether the index block is included or whether audio/video data is cross-stored
DWORD dwtotalframe; // the total number of workers in the file
DWORD dwinitialframes; // specifies the number of shards required before playback.
DWORD dwstreams; // data stream types contained in the file
DWORD dwsuggestedbuffersize; // recommended buffer size,
// It is usually the sum of the data needed to store a pair of images and synchronize sounds
DWORD dwwidth; // Image Width
DWORD dwheight; // Image Height
DWORD dwreserved [4]; // reserved value
} Mainaviheader;

"Strl" list blocks are used to record AVI data streams. Each Data Stream occupies three sub-blocks in this list. Their IDs are "strh", "STRF ", "strd ";
The "strh" sub-block is defined by the following structure.

Typedef struct
{
Fourcc fcctype; // 4 bytes, which indicates the type of data stream vids indicates the video data stream
// Auds audio data stream
Fourcc fcchandler; // 4-byte, indicating the driver code for data stream Decompression
DWORD dwflags; // data stream attributes
Word wpriority; // playback priority of the data stream
Word wlanguage; // audio language code
DWORD dwinitalframes; // specifies the number of shards required before playback.
DWORD dwscale; // data volume. The video size per region or the audio sampling size
DWORD dwrate; // dwscale/dwrate = number of samples per second
DWORD dwstart; // the position where the stream starts to play, in dwscale.
DWORD dwlength; // data stream data volume, in dwscale
DWORD dwsuggestedbuffersize; // recommended buffer size
DWORD dwquality; // extract the quality parameter. The larger the value, the better the quality.
DWORD dwsamplesize; // audio sampling size
Rect rcframe; // The rectangle occupied by the video image
} Avistreamheader;

The structure of the "STRF" sub-block is determined by the type of the "strh" sub-block. If the strh sub-block is a video data stream, the content of the STRF sub-block is a bimapinfo structure unrelated to the Windows Device, as follows:

Typedef struct tagbitmapinfo
{
Bitmapinfoheader bmiheader;
Rgbquad bmicolors [1]; // color table
} Bitmapinfo;

Typedef struct tagbitmapinfoheader
{
DWORD bisize;
Long biwidth;
Long biheight;
Word biplanes;
Word bibitcount;
DWORD bicompression;
DWORD bisizeimage;
Long bixpelspermeter;
Long biypelspermeter;
DWORD biclrused;
DWORD biclrimportant;
} Bitmapinfoheader;

If the strh sub-block is an audio data stream, the content of the STRF sub-block is a waveformat structure, as shown below:

Typedef struct
{
Word wformattag;
Word nchannels; // number of audio channels
DWORD nsamplespersec; // Sampling Rate
DWORD navgbytespersec; // data volume per second in the wave sound
Word nblockalign; // alignment mark of data blocks
Word bisize; // the size of this structure
} Waveformat

The "strd" sub-block follows the STRF sub-block and stores the parameters used by the compression driver. They do not exist and do not have a fixed structure.

"Strl" list block-defined Avi Data Streams link the data stream header structure in the "hdrl" list block with the data in the "movi" list block in sequence, the first data stream header structure is used for data stream 0, the second is used for data stream 1, and so on.

The data block stores video and audio data streams, and the data can be directly stored in the "movi" list block. Audio and video data in data blocks are stored in different word blocks. The structure is described as follows,

Audio Block
"## WB"
Wave Data Streams
The Dib data is stored in the video sub-block, which can be compressed or uncompressed,
"## DB"
RGB data streams
"# DC"
Compressed image data stream

As you can see, the image data of the AVI file can be compressed or non-compressed. Different encoding formats can also be used for compression. You may have encountered some problems that Avi cannot recognize because of different encoding methods. Without corresponding decoding, you will not be able to recognize video data. There are many Avi encoding methods, such as MPEG2, MPEG4, and DivX.

Index block: The index quickly contains the index of the data block in the file, which can improve the read and write speed of AVI files, including a set of aviindexentry structure data. As shown below, this block is not required and may not exist.

Typedef struct
{
DWORD ckid; // mark the subblock of the record data block
DWORD dwflags; // indicates the attributes of the sub-block referred to by the chid.
DWORD dwchunkoffset; // relative position of the sub-block
DWORD dwchunklength; // sub-block Length
};

Now I believe that you will be very clear about the structure of the AVI file. After introducing the structure of the AVI file, let's take a look at how to read and write the AVI file, to read and write Avi, Microsoft provides a set of APIs with a total of 50 functions. They mainly use two types of Apis: AVI file operations and data stream streams operations.

1. Open and Close files

Avifileopen, avifileaddref, avifilereladdre

2. Read the file information from the file

Avifileinfo can be used to obtain information about the AVI file. This function returns an avifileinfo structure and avifilereaddata can be used to obtain information not obtained by the avifileinfo function. This information may not be contained in the file header, such as the name of the company or individual that owns the file.

3. Write File Information

You can use the avifilewritedata function to write some additional information about the file.

4. Open and Close a stream

Opening a data stream is the same as opening a file. You can use the avifilegetstream function to open a data stream. This function creates a stream interface and stores a handle in the interface.

If you want to operate a single stream of a file, you can use the avistreamopenfromfile function, which integrates avifileopen and avifilegetstream functions.

If you want to operate multiple data streams in the file, you must first avifileopen and then avifilegetstream.

You can use avistreamaddref to add stream interface references.

Use the avistreamrelease function to disable data streams. This function is used to reduce the reference count of streams. It is deleted when the count is reduced to 0.

5. Read data and information from the stream

The avistreaminfo function can obtain some information about the data. This function returns an avistreaminfo structure that contains the data type compression method, recommended buffersize, playback rate, and description.

If the data stream has additional information, you can use the avistreamreaddata function to obtain it. The application allocates a memory and passes it to this function. Then, this function returns the data stream information through the memory. The additional information may include the data stream compression and decompression methods, you can use the avistreamdatasize macro to return the size of the memory block to be applied.

You can use the avistreamreadformat function to obtain the format information of the data stream. This function returns the format information of the data stream through the specified memory. For example, for video streams, this buffer contains a bimapinfo structure. For audio streams, the memory block contains the waveformatex or pcmaveformat structure. You can obtain the buffer size by passing an empty buffer to avistreamreadformat. You can also use the avistreamformatsize macro.

You can use the avistreamread function to return multimedia data. This function copies data to the memory provided by the application. For video streams, this function returns the image watermark. For audio streams, this function returns the audio sample data. You can pass a null buffer to avistreamread to obtain the required buffer size. You can also use the avistreamsamplesize macro to obtain the buffer size.

Some Avi Data Stream handles may need to be prepared before starting the data stream. At this time, we can call the avistreambeginstreaming function to inform the Avi Data Stream handle to apply for allocating some resources it needs. Call the avistreamendstreamming function to release resources.

6. perform operations on compressed video data

If you want to demonstrate how many times a video image is compressed, you can call the avistreamread function to pass the obtained data to the drawdib function to display the image. These functions can display compressed and uncompressed images.

Avifile also provides the avistreamgetframeopen function to obtain uncompressed video streams. This function creates a memory to obtain uncompressed data. You can also use the avistreamgetframe function to extract a single video clip. This function can extract an image from a certain region and return the data in a bimapinfoheader structure. After calling the avistreamgetframe function, you must call the avistreamgetframeclose function to release the resources applied by the previous function.

7. Create a file based on an existing data stream

To create a file that contains multiple data streams, You can integrate multiple data streams and write them into a new file. These data streams can be stored in memory or in another file.

We can use the avisave function to build a file. This function can create a file and write multiple specified data streams to the file in the specified sequence. You can also use the avisavev function to create a new file, this function has the same function as avisave. The main difference is that avisavev uses an array of data streams, while avisave is a single data stream that is saved multiple times.

You can call the avisaveoptions function to display a dialog box that allows you to select the compression mode.

You can specify a callback function when calling the avisave and avisavev functions to display the progress of generating an AVI file. This allows you to cancel generating an AVI file at any time.

You can call the getsavefilenamepreview function to display the saved dialog box and select the saved file name.

Through the avimakefilefromstreams function, we can create a virtual file handle. Other Avi functions can use this virtual file handle to operate on the data stream in the file. After the operation is complete, remember to call avifilerelease for release.

8. Write a data stream to the file

You can use the avifilecreatestream function to create a data stream in a new file or an existing file. This function defines a new data stream based on the avistreaminfo structure and creates an interface for the new data stream to return the pointer to the interface.

Before writing new data, you must specify the stream format information. By using the avistreamsetformat function, you must use the bimapinfo structure to set a video stream, and use waveformat for audio.

Then we can write our multimedia data into the data stream through the avistreamwrite function. This function copies the memory data provided by the application to the specified stream. The default Avi handler writes data to the end of the stream.

If you have additional information to write to the stream, you can call avifilewritedata or avistreamwritedata. Remember to call avistreamrelease after data writing is completed.

9. The Shard location in the data stream

Start shard:

You can use the avistreamstart function to obtain the sample number contained in the First shard. You can also use the avistreaminfo function to obtain this information. The avistreaminfo structure of this function contains dwstart. You can use the avistreamstarttime macro to obtain the first sample.

You can use the avistreamlength function to obtain the stream length. This function returns the number of samples in the stream. You can also use the avistreaminfo function to obtain the information. You can use the avistreamlengthtime macro to obtain the stream length in milliseconds.

In a video stream, a sample corresponds to a sequence of images. Therefore, sometimes there is no video data in these samples. If you call the avistreamread function for data, null may be returned, you can also use avistreamfindsample to find the specified sample by specifying the find_any flag.

Search for key regions

Use the avistreamfindsample function to find the desired sample. Then, you can use the following macro to determine whether the critical samples are used.

Switch between time and sample.

Avistreamsampletotime: this function can convert smaple to milliseconds. For a video, this value indicates the start time of the player.

After learning about the above knowledge, we can understand the structure of the AVI file and how to operate the AVI file. Now we can start programming. We have to do two things:

1. How to synthesize a static BMP bitmap into an AVI video file;

2. How to resolve an uncompressed AVI file into a bitmap.

The sample program interface is as follows:

The following function demonstrates how to save all BMP files in a folder as an AVI file. The first parameter of the function is the AVI file name to be generated, the second parameter is the name of the folder where the BMP file is stored. This function will enumerate all BMP files in the folder and synthesize an AVI file.

Void cbmp2avidlg: avitobmp (cstring stravifilename, cstring strbmp DIR)
{
// Todo: add the control notification handler code here
Avifileinit ();
Pavifile Avi;
Int res = avifileopen (& Avi, stravifilename, of_read, null );
Int n = getlasterror ();
If (res! = Avierr_ OK)
{
// An error occures
If (Avi! = NULL)
Avifilerelease (AVI );
Return;
}
Avifileinfo avi_info;
Avifileinfo (AVI, & avi_info, sizeof (avifileinfo ));
Pavistream pstream;
Res = avifilegetstream (AVI, & pstream, streamtypevideo/* video stream */,
0/* first stream */);
If (res! = Avierr_ OK)
{
If (pstream! = NULL)
Avistreamrelease (pstream );
Avifileexit ();
Return;
}

// Do some task with the stream
Int inumframes;
Int ifirstframe;
Ifirstframe = avistreamstart (pstream );
If (ifirstframe =-1)
{
// Error getteing the frame inside the stream
If (pstream! = NULL)
Avistreamrelease (pstream );
Avifileexit ();
Return;
}
Inumframes = avistreamlength (pstream );
If (inumframes =-1)
{
// Error getteing the number of frames inside the stream
If (pstream! = NULL)
Avistreamrelease (pstream );
Avifileexit ();
Return;
}

// Getting bitmap from Frame
Bitmapinfoheader BiH;
Zeromemory (& BiH, sizeof (bitmapinfoheader ));

BiH. bibitcount = 24; // 24 bit per pixel
BiH. biclrimportant = 0;
BiH. biclrused = 0;
BiH. bicompression = bi_rgb;
BiH. biplanes = 1;
BiH. bisize = 40;
BiH. bixpelspermeter = 0;
BiH. biypelspermeter = 0;
// Calculate total size of rgbquad scanlines (DWORD aligned)
BiH. bisizeimage = (BIH. biwidth * 3) + 3) & 0 xfffc) * BiH. biheight;

Pgetframe pframe;
Pframe = avistreamgetframeopen (pstream, null );

Avistreaminfo streaminfo;
Avistreaminfo (pstream, & streaminfo, sizeof (avistreaminfo ));

// Get the first frame
Bitmapinfoheader bih2;
Long lsize = sizeof (bih2 );
Int Index = 0;
For (INT I = ifirstframe; I <inumframes; I ++)
{
Index = I-ifirstframe;
Byte * pdib = (byte *) avistreamgetframe (pframe, index );//
Avistreamreadformat (pstream, index, & bih2, & lsize );
Bitmapfileheader stfilehdr;

Byte * bits = new byte [bih2.bisizeimage];
Avistreamread (pstream, index, 1, bits, bih2.bisizeimage, null, null );
// Rtlmovememory (bits, pdib + sizeof (bitmapinfoheader), bih2.bisizeimage );

Bih2.biclrused = 0;
Stfilehdr. bfoffbits = sizeof (bitmapfileheader) + sizeof (bitmapinfoheader );
Stfilehdr. bfsize = sizeof (bitmapfileheader );
Stfilehdr. bftype = 0x4d42;

Cstring filename;
Filename. Format ("frame-000005d.bmp", index );
Cstring strtemp = strbmp dir;
Strtemp + = "";
Strtemp + = filename;
File * fp = _ tfopen (strtemp, _ T ("WB "));
Fwrite (& stfilehdr, 1, sizeof (bitmapfileheader), FP );
Fwrite (& bih2, 1, sizeof (bitmapinfoheader), FP );
Int FF = fwrite (bits, 1, bih2.bisizeimage, FP );
Int e = getlasterror ();
Fclose (FP );
/////
Delete bits;
// Createfrompackeddibpointer (pdib, index );
}

Avistreamgetframeclose (pframe );

// Close the stream after finishing the task
If (pstream! = NULL)
Avistreamrelease (pstream );
Avifileexit ();
}

The following function demonstrates how to extract each pixel image from an AVI file and save it as a BMP file. The first parameter of the function is the AVI file name, and the second parameter is the folder where the BMP file is stored.

// Generate Avi
Void cbmp2avidlg: BMP toavi (cstring szaviname, cstring strbmp DIR)
{
Cfilefind finder;
Strbmp dir + = _ T ("/*.*");
Avifileinit ();
Avistreaminfo strhdr;
Pavifile pfile;
Pavistream pS;
Int nframes = 0;
Hresult hr;

Bool bfind = finder. findfile (strbmp DIR );
While (bfind)
{
Bfind = finder. findnextfile ();
If (! Finder. isdots ()&&! Finder. isdirectory ())
{
Cstring STR = finder. getfilepath ();
File * fp = fopen (STR, "rb ");
Bitmapfileheader BMP file HDR;
Bitmapinfoheader BMP infohdr;
Fseek (FP, 0, seek_set );
Fread (& BMP filehdr, sizeof (bitmapfileheader), 1, FP );
Fread (& BMP infohdr, sizeof (bitmapinfoheader), 1, FP );

Byte * tmp_buf = NULL;
If (nframes = 0)
{
Avifileopen (& pfile, szaviname, of_write | of_create, null );
_ Fmemset (& strhdr, 0, sizeof (strhdr ));
Strhdr. fcctype = streamtypevideo; // stream type
Strhdr. fcchandler = 0;
Strhdr. dwscale = 1;
Strhdr. dwrate = 15; // 15 FPS
Strhdr. dwsuggestedbuffersize = BMP infohdr. bisizeimage;
Setrect (& strhdr. rcframe, 0, 0, BMP infohdr. biwidth, BMP infohdr. biheight );

// And create the stream;
HR = avifilecreatestream (pfile, & PS, & strhdr );
// HR = avistreamsetformat (Ps, nframes, & BMP infohdr, sizeof (BMP infohdr ));
}
Tmp_buf = new byte [BMP infohdr. biwidth * BMP infohdr. biheight * 3];
Fread (tmp_buf, 1, BMP infohdr. biwidth * BMP infohdr. biheight * 3, FP );
HR = avistreamsetformat (Ps, nframes, & BMP infohdr, sizeof (BMP infohdr ));
HR = avistreamwrite (Ps, // stream pointer
Nframes, // time of this frame
1, // number to write
(Lpbyte) tmp_buf,
BMP infohdr. bisizeimage, // size of this frame
Aviif_keyframe, // flags ....
Null,
Null );

Nframes ++;
Fclose (FP );
}
}

Avistreamclose (PS );

If (pfile! = NULL)
Avifilerelease (pfile );
Avifileexit ();
}

  Conclusion:

The above code is successfully debugged on the VC 6.0 and Windows XP platforms. You can use these two functions directly in your program. For more detailed code, see the example source code attached with this article. Here I want to point out that, during the mutual conversion between the AVI file and BMP, the video data in AVI is stored with no compressed data, if you want to decompress an AVI file, such as dvsd and MPEG4, you must first use the corresponding decoder to decode the video data, save the decoded data as a BMP file. Now, the introduction to the AVI file is over.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.