自己生成MJPG格式AVI文件

描述


注:本文是作者以前发表在其个人博客,现在发布到“聚丰开发”专栏




现在电子产品经常需要使用到视频,于是对视频技术的需求也日益高涨,通常做法是采用现成的硬件或软件方案来实现对视频数据的处理,要说掌握这部分算法的人并不多,大多数工程师没有时间和精力去消化吸收这些算法。


        MJPG是最简单的视频文件格式,存储时是将图像用JPG格式一帧一帧的保存起来,回放的时候按照规定的时间间隔一帧一帧的回放。用这种处理方法算法相对比较简单,如果对图像要求不高可以达到20~30倍的压缩率,对于单片机来说不失为一种可行性较高的备选方案。


        以前有个项目需要支持MJPG文件,为此我写了一段PC程序进行代码验证,同时生成测试用MJPG文件。具体做法是预先做好10张JPG图片,每张图片都有VGA和QVGA两种尺寸,然后按输入参数生成相应的MJPG文件。比如输入选择VGA/10帧,生成的MJPG文件第一秒为第一张VGA图片重复10次,依此类推。


        程序生成的MJPG用电脑播放,如果能正常播放,就说明代码对文件格式的处理基本正确。这里程序每一帧都对应有自己的JPG数据,实际上可以不这么做,MJPG文件有音频和视频流的播放序号,如果相邻的视频帧内容相同,是可以将播放序号指向同一张JPG图像数据的。




每一帧自己有独立的JPG图像数据
播放序号: JPG01 JPG02 JPG03 ... JPG09 JPG10 JPG11
JPG01_Data:
...(第一张图片内容)...
JPG02_Data:
...(第一张图片内容)...
...
JPG09_Data:
...(第一张图片内容)...
JPG10_Data:
...(第一张图片内容)...
JPG11_Data:
...(第二张图片内容)...

相邻帧相同利用同一张JPG数据
播放序号: JPG01 JPG01 JPG01 ... JPG01 JPG01 JPG02
JPG01_Data:
...(第一张图片内容)...
JPG02_Data:
...(第二张图片内容)...
...
JPG10_Data:
...(第十张图片内容)...

注意代码只是用于临时工具,没有详细注释进行说明,代码书写也存在许多不规范的地方。



-------------------------参考代码---------------------------

#include "string.h"
#include "stdio.h"

/*unsigned char mMJPEG_Header[] =
{
 0x52,0x49,0x46,0x46, //0x0000 "RIFF"
 0x00,0x00,0x00,0x00, //0x0004 length of data, need update it
 0x41,0x56,0x49,0x20, //0x0008 RIFF form identifier "AVI "

 

 0x4C,0x49,0x53,0x54, //0x000C List identifier "LIST"
 0x46,0x01,0x00,0x00, //0x0010 Length of list(326 bytes)
 0x68,0x64,0x72,0x6C, //0x0014 List type identifier "hdrl"

 

 0x61,0x76,0x69,0x68, //0x0018 Chunk identifier "avih"
 0x38,0x00,0x00,0x00, //0x001C Length of chunk(56 bytes)
 0x00,0x00,0x00,0x00, //0x0020 Number of us per fer frame(=1000000/FrameRate), need update it
 0x00,0x00,0x00,0x00, //0x0024 Max bytes per second(ignored)
 0x00,0x00,0x00,0x00, //0x0028 Padding granularity(ignored)
 0x10,0x00,0x01,0x00,//0x00,0x00,0x00,0x00, //0x002C Flags
 0x00,0x00,0x00,0x00, //0x0030 Total number of frame, need update it
 0x00,0x00,0x00,0x00, //0x0034 Intial frame number for frames
 0x02,0x00,0x00,0x00, //0x0038 Number of streams(2)
 0x00,0x00,0x10,0x00,//0x00,0x00,0x00,0x00, //0x003C Suggested buffer size(ignored)
 0x00,0x00,0x00,0x00, //0x0040 Width of video, need update it
 0x00,0x00,0x00,0x00, //0x0044 Height of video, need update it
 0x00,0x00,0x00,0x00, //0x0048 Reserved
 0x00,0x00,0x00,0x00, //0x004C Reserved
 0x00,0x00,0x00,0x00, //0x0050 Reserved
 0x00,0x00,0x00,0x00, //0x0054 Reserved

 

 0x4C,0x49,0x53,0x54, //0x0058 List identifier "LIST"
 0x94,0x00,0x00,0x00, //0x005C Length of list(148 bytes)
 0x73,0x74,0x72,0x6C, //0x0060 List type identifier"strl"

 

 0x73,0x74,0x72,0x68, //0x0064 Chunk identifier "strh"
 0x38,0x00,0x00,0x00, //0x0068 Length of chunk(56 bytes)
 0x76,0x69,0x64,0x73, //0x006C Stream type identifier "vids"
 0x4D,0x4A,0x50,0x47, //0x0070 Stream handler identifier "MJPG"
 0x00,0x00,0x00,0x00, //0x0074 Flags
 0x00,0x00,    //0x0078 Priority(ignored)
 0x00,0x00,    //0x007A Language(ignored)
 0x00,0x00,0x00,0x00, //0x007C Initial frame number(usually is 0)
 0x01,0x00,0x00,0x00, //0x0080 Scale(usually is 1)
 0x40,0x42,0x0f,0x00,//0x00,0x00,0x00,0x00, //0x0084 Frame rate, need update it
 0x00,0x00,0x00,0x00, //0x0088 Star(usually is 0)
 0x00,0x00,0x00,0x00, //0x008C Length(usually is total numbers of frames), need update it
 0x00,0x00,0x10,0x00,//0x00,0x00,0x00,0x00, //0x0090 Suggested buffer size(ignored)
 0x10,0x27,0x00,0x00,//0x00,0x00,0x00,0x00, //0x0094 Quality(ignored)
 0x00,0x00,0x00,0x00, //0x0098 Sample size(ignored)
 0x00,0x00,    //0x009C Frame upper left X coordinate
 0x00,0x00,    //0x009E Frame upper left Y coordinate
 0x00,0x00,    //0x00A0 Frame lower right X coordinate, need update it
 0x00,0x00,    //0x00A2 Frame lower right Y coordinate, need update it

 

 0x73,0x74,0x72,0x66, //0x00A4 Chunk identifier "strf"
 0x48,0x00,0x00,0x00, //0x00A8 Length of chunk(72 bytes)
 0x28,0x00,0x00,0x00,//0x48,0x00,0x00,0x00, //0x00AC Length of this chunk(72 bytes)
 0x00,0x00,0x00,0x00, //0x00B0 Width of video, need update it
 0x00,0x00,0x00,0x00, //0x00B4 Height of video, need update it
 0x01,0x00,    //0x00B8 Number of planes(1)
 0x18,0x00,//0x10,0x00,    //0x00BA Bits per pixel(16)
 0x4D,0x4A,0x50,0x47, //0x00BC Stream handler identifier "MJPG"
 0x00,0x00,0x00,0x00, //0x00C0 Raw image size, need update it
 0x00,0x00,0x00,0x00, //0x00C4 X pixels per meter(ignored)
 0x00,0x00,0x00,0x00, //0x00C8 Y pixels pre meter(ignored)
 0x00,0x00,0x00,0x00, //0x00CC Number of colors used(ignored)
 0x00,0x00,0x00,0x00, //0x00D0 Number of colors important(ignored)
 0x00,0x00,0x00,0x00, //0x00D4 DV audio channel 0 auxillary source(ignored)
 0x00,0x00,0x00,0x00, //0x00D8 DV audio channel 0 auxillary control(ignored)
 0x00,0x00,0x00,0x00, //0x00DC DV audio channel 1 auxillary source(ignored)
 0x00,0x00,0x00,0x00, //0x00E0 DV audio channel 1 auxillary control(ignored)
 0x00,0x00,0x00,0x00, //0x00E4 DV video auxillary source(ignored)
 0x00,0x00,0x00,0x00, //0x00E8 DV video auxillary control(ignored)
 0x00,0x00,0x00,0x00, //0x00EC Reserved
 0x00,0x00,0x00,0x00, //0x00F0 Reserved

 

 0x4C,0x49,0x53,0x54, //0x00F4 List identifier "LIST"
 0x62,0x00,0x00,0x00, //0x00F8 Length of list(98 bytes)
 0x73,0x74,0x72,0x6C, //0x00FC List type identifier "strl"

 

 0x73,0x74,0x72,0x68, //0x0100 Chunk indentifier "strh"
 0x38,0x00,0x00,0x00, //0x0104 Length of chunk(56 bytes)
 0x61,0x75,0x64,0x73, //0x0108 Stream type identifier "auds"
 0x00,0x00,0x00,0x00,//0x20,0x20,0x20,0x20, //0x010C Stream handler identifier
 0x00,0x00,0x00,0x00, //0x0110 Flags
 0x00,0x00,    //0x0114 Priority(ignored)
 0x00,0x00,    //0x0116 Language(ignored)
 0x00,0x00,0x00,0x00, //0x0118 Initial frame number(usually is 0)
 0x01,0x00,0x00,0x00, //0x011C Scale(usually is 1)
 0x40,0x1F,0x00,0x00, //0x0120 Sampling rate(8k)
 0x00,0x00,0x00,0x00, //0x0124 Start(usually is 0)
 0x00,0x00,0x00,0x00, //0x0128 Length(number of audio data segments), need update it
 0x00,0x00,0x10,0x00,//0x00,0x00,0x00,0x00, //0x012C Suggested buffer size(ignored)
 0x10,0x27,0x00,0x00,//0x00,0x00,0x00,0x00, //0x0130 Quality(ignored)
 0x01,0x00,0x00,0x00,//0x00,0x00,0x00,0x00, //0x0134 Sample size(ignored)
 0x00,0x00,    //0x0138 Frame upper left X coordinate(ignored)
 0x00,0x00,    //0x013A Frame upper left Y coordinate(ignored)
 0x00,0x00,    //0x013C Frame lower right X coordinate(ignored)
 0x00,0x00,    //0x013E Frame lower right Y coordinate(ignored)
 
 0x73,0x74,0x72,0x66, //0x0140 Chunk identifier "strf"
 0x12,0x00,0x00,0x00, //0x0144 Length of chunk(18 bytes)
 0x01,0x00,    //0x0148 Format tag(PCM is 1)
 0x01,0x00,    //0x014A Number of audio channels(1)
 0x40,0x1F,0x00,0x00, //0x014C Sampling rate(8k)
 0x40,0x1F,0x00,0x00, //0x0150 Sampling rate x Number of audio channels
 0x01,0x00,//0x00,0x00,    //0x0154 Block alignment(ignored)
 0x08,0x00,    //0x0156 Bits per sample(8)
 0x00,0x00,    //0x0158 Size of extra information(0)

 

 //0x4C,0x49,0x53,0x54, //0x015A List identifier "LIST"
 //0x00,0x00,0x00,0x00, //0x015E Length of list, need update it
 //0x6D,0x6F,0x76,0x69  //0x0162 List type identifier "movi"
};*/


 

unsigned char mMJPEG_Header[]={
0x52, 0x49, 0x46, 0x46, 0x34, 0x06, 0x1c, 0x00, 0x41, 0x56, 0x49, 0x20, 0x4c, 0x49, 0x53, 0x54,
0x46, 0x01, 0x00, 0x00, 0x68, 0x64, 0x72, 0x6c, 0x61, 0x76, 0x69, 0x68, 0x38, 0x00, 0x00, 0x00,
0x6a, 0x04, 0x01, 0x00, 0x4a, 0x65, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x01, 0x00,
0x68, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x26, 0x48, 0x00, 0x00,
0x40, 0x01, 0x00, 0x00, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4c, 0x49, 0x53, 0x54, 0x74, 0x00, 0x00, 0x00,
0x73, 0x74, 0x72, 0x6c, 0x73, 0x74, 0x72, 0x68, 0x38, 0x00, 0x00, 0x00, 0x76, 0x69, 0x64, 0x73,
0x6d, 0x6a, 0x70, 0x67, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x6a, 0x04, 0x01, 0x00, 0x40, 0x42, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x68, 0x00, 0x00, 0x00,
0x26, 0x48, 0x00, 0x00, 0x10, 0x27, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x40, 0x01, 0xf0, 0x00, 0x73, 0x74, 0x72, 0x66, 0x28, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00,
0x40, 0x01, 0x00, 0x00, 0xf0, 0x00, 0x00, 0x00, 0x01, 0x00, 0x18, 0x00, 0x4d, 0x4a, 0x50, 0x47,
0x00, 0x84, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x4c, 0x49, 0x53, 0x54, 0x5c, 0x00, 0x00, 0x00, 0x73, 0x74, 0x72, 0x6c,
0x73, 0x74, 0x72, 0x68, 0x38, 0x00, 0x00, 0x00, 0x61, 0x75, 0x64, 0x73, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x10, 0x2b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x92, 0x2a, 0x01, 0x00, 0x10, 0x2b, 0x00, 0x00,
0x10, 0x27, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x73, 0x74, 0x72, 0x66, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x10, 0x2b, 0x00, 0x00,
0x10, 0x2b, 0x00, 0x00, 0x01, 0x00, 0x08, 0x00, 0x49, 0x44, 0x49, 0x54, 0x1a, 0x00, 0x00, 0x00,
0x54, 0x48, 0x55, 0x20, 0x4e, 0x4f, 0x56, 0x20, 0x30, 0x36, 0x20, 0x31, 0x36, 0x3a, 0x34, 0x32,
0x3a, 0x32, 0x36, 0x20, 0x32, 0x30, 0x30, 0x38, 0x0a, 0x00, 0x4c, 0x49, 0x53, 0x54, 0x18, 0x00,
0x00, 0x00, 0x49, 0x4e, 0x46, 0x4f, 0x49, 0x53, 0x46, 0x54, 0x0c, 0x00, 0x00, 0x00, 0x43, 0x61,
0x6e, 0x6f, 0x6e, 0x4d, 0x56, 0x49, 0x30, 0x32, 0x00, 0x00, 0x4a, 0x55, 0x4e, 0x4b, 0x7e, 0x06,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x4c, 0x49, 0x53, 0x54, 0x3c, 0xf7, 0x1b, 0x00, 0x6d, 0x6f, 0x76, 0x69};

 

void make_qvga_mjpeg()
{
 unsigned long mFrameRate,mFrameTime,i,j,k,mTemp;
 unsigned long mTargetFileLength,mSourceFileLength;
 unsigned long mOffset,mSize;
 unsigned char sFileName[256],ch;
 FILE *fp1,*fp2,*fp3,*fp4;

 

 mFrameRate=0;
 printf("\r\nBegin make QVGA MJPG file\r\n");
 while((mFrameRate<5)||(mframerate>30))
 {
  printf("Please input frame rate(5~30):");
  scanf("%d",&mFrameRate);
 }
 printf("Please input target filename:");
 scanf("%s",sFileName);

 

 mFrameTime=1000000/mFrameRate;

 

 mMJPEG_Header[0x20]=(unsigned char)mFrameTime&0xFF;
 mMJPEG_Header[0x21]=(unsigned char)(mFrameTime>>8)&0xFF;
 mMJPEG_Header[0x22]=(unsigned char)(mFrameTime>>16)&0xFF;
 mMJPEG_Header[0x23]=(unsigned char)(mFrameTime>>24)&0xFF;

 

 mMJPEG_Header[0x30]=(unsigned char)(mFrameRate*10)&0xFF;
 mMJPEG_Header[0x31]=(unsigned char)((mFrameRate*10)>>8)&0xFF;
 mMJPEG_Header[0x32]=(unsigned char)((mFrameRate*10)>>16)&0xFF;
 mMJPEG_Header[0x33]=(unsigned char)((mFrameRate*10)>>24)&0xFF;

 

 mMJPEG_Header[0x40]=0x40;
 mMJPEG_Header[0x41]=0x01;
 mMJPEG_Header[0x42]=0x00;
 mMJPEG_Header[0x43]=0x00;

 

 mMJPEG_Header[0x44]=0xF0;
 mMJPEG_Header[0x45]=0x00;
 mMJPEG_Header[0x46]=0x00;
 mMJPEG_Header[0x47]=0x00;

 

 mMJPEG_Header[0x80]=(unsigned char)mFrameTime&0xFF;
 mMJPEG_Header[0x81]=(unsigned char)(mFrameTime>>8)&0xFF;
 mMJPEG_Header[0x82]=(unsigned char)(mFrameTime>>16)&0xFF;
 mMJPEG_Header[0x83]=(unsigned char)(mFrameTime>>24)&0xFF;

 

 //mMJPEG_Header[0x84]=(unsigned char)mFrameRate&0xFF;
 //mMJPEG_Header[0x85]=(unsigned char)(mFrameRate>>8)&0xFF;
 //mMJPEG_Header[0x86]=(unsigned char)(mFrameRate>>16)&0xFF;
 //mMJPEG_Header[0x87]=(unsigned char)(mFrameRate>>24)&0xFF;

 

 mMJPEG_Header[0x8C]=(unsigned char)(mFrameRate*10)&0xFF;
 mMJPEG_Header[0x8D]=(unsigned char)((mFrameRate*10)>>8)&0xFF;
 mMJPEG_Header[0x8E]=(unsigned char)((mFrameRate*10)>>16)&0xFF;
 mMJPEG_Header[0x8F]=(unsigned char)((mFrameRate*10)>>24)&0xFF;

 

 mMJPEG_Header[0xA0]=0x40;
 mMJPEG_Header[0xA1]=0x01;
 mMJPEG_Header[0xA2]=0xF0;
 mMJPEG_Header[0xA3]=0x00;

 

 mMJPEG_Header[0xB0]=0x40;
 mMJPEG_Header[0xB1]=0x01;
 mMJPEG_Header[0xB2]=0x00;
 mMJPEG_Header[0xB3]=0x00;

 

 mMJPEG_Header[0xB4]=0xF0;
 mMJPEG_Header[0xB5]=0x00;
 mMJPEG_Header[0xB6]=0x00;
 mMJPEG_Header[0xB7]=0x00;

 

 //mMJPEG_Header[0xBA]=0x18; //must set as 24
 //mMJPEG_Header[0xBB]=0x00;

 

 mMJPEG_Header[0xC0]=0x00;
 mMJPEG_Header[0xC1]=0x58;
 mMJPEG_Header[0xC2]=0x02;
 mMJPEG_Header[0xC3]=0x00;

 

 mMJPEG_Header[0x120-0x20]=0x40;
 mMJPEG_Header[0x121-0x20]=0x1F;
 mMJPEG_Header[0x122-0x20]=0x00;
 mMJPEG_Header[0x123-0x20]=0x00;

 

 mMJPEG_Header[0x128-0x20]=(unsigned char)(8000*10)&0xFF;
 mMJPEG_Header[0x129-0x20]=(unsigned char)((8000*10)>>8)&0xFF;
 mMJPEG_Header[0x12A-0x20]=(unsigned char)((8000*10)>>16)&0xFF;
 mMJPEG_Header[0x12B-0x20]=(unsigned char)((8000*10)>>24)&0xFF;

 

 mMJPEG_Header[0x12C-0x20]=0x40;
 mMJPEG_Header[0x12D-0x20]=0x1F;
 mMJPEG_Header[0x12E-0x20]=0x00;
 mMJPEG_Header[0x12F-0x20]=0x00;

 

 mMJPEG_Header[0x14C-0x20]=0x40;
 mMJPEG_Header[0x14D-0x20]=0x1F;
 mMJPEG_Header[0x14E-0x20]=0x00;
 mMJPEG_Header[0x14F-0x20]=0x00;

 

 mMJPEG_Header[0x150-0x20]=0x40;
 mMJPEG_Header[0x151-0x20]=0x1f;
 mMJPEG_Header[0x152-0x20]=0x00;
 mMJPEG_Header[0x153-0x20]=0x00;

 

 /*mMJPEG_Header[0x120]=0x10;
 mMJPEG_Header[0x121]=0x2B;
 mMJPEG_Header[0x122]=0x00;
 mMJPEG_Header[0x123]=0x00;

 

 mMJPEG_Header[0x128]=(unsigned char)(8000*10)&0xFF;
 mMJPEG_Header[0x129]=(unsigned char)((8000*10)>>8)&0xFF;
 mMJPEG_Header[0x12A]=(unsigned char)((8000*10)>>16)&0xFF;
 mMJPEG_Header[0x12B]=(unsigned char)((8000*10)>>24)&0xFF;

 

 mMJPEG_Header[0x12C]=0x10;
 mMJPEG_Header[0x12D]=0x2B;
 mMJPEG_Header[0x12E]=0x00;
 mMJPEG_Header[0x12F]=0x00;

 

 mMJPEG_Header[0x14C]=0x10;
 mMJPEG_Header[0x14D]=0x2B;
 mMJPEG_Header[0x14E]=0x00;
 mMJPEG_Header[0x14F]=0x00;

 

 mMJPEG_Header[0x150]=0x10;
 mMJPEG_Header[0x151]=0x2B;
 mMJPEG_Header[0x152]=0x00;
 mMJPEG_Header[0x153]=0x00;*/

 

 mMJPEG_Header[0x3C]=0x00;
 mMJPEG_Header[0x3D]=0x00;
 mMJPEG_Header[0x3E]=0x10;
 mMJPEG_Header[0x3F]=0x00;

 

 mMJPEG_Header[0x90]=0x00;
 mMJPEG_Header[0x91]=0x00;
 mMJPEG_Header[0x92]=0x10;
 mMJPEG_Header[0x93]=0x00;

 


 fp1=fopen(sFileName,"wb+");
 if(fp1==NULL)
 {
  printf("Target file open error!\r\n");
  fcloseall();
  return;
 }
 fp3=fopen("idx_tmp.bin","wb+");
 if(fp3==NULL)
 {
  printf("Target file open error!\r\n");
  fcloseall();
  return;
 }
 fp4=fopen("10.pcm","rb+");
 if(fp4==NULL)
 {
  printf("Source file open error!\r\n");
  fcloseall();
  return;
 }

 

 fwrite(mMJPEG_Header,1,sizeof(mMJPEG_Header),fp1);

 

//----------------
   /*ch='L';
   fwrite(&ch,1,1,fp3);
   ch='I';
   fwrite(&ch,1,1,fp3);
   ch='S';
   fwrite(&ch,1,1,fp3);
   ch='T';
   fwrite(&ch,1,1,fp3);
   ch=0;
   fwrite(&ch,1,1,fp3);
   ch=0;
   fwrite(&ch,1,1,fp3);
   ch=0;
   fwrite(&ch,1,1,fp3);
   ch=0;
   fwrite(&ch,1,1,fp3);
   ch='m';
   fwrite(&ch,1,1,fp3);
   ch='o';
   fwrite(&ch,1,1,fp3);
   ch='v';
   fwrite(&ch,1,1,fp3);
   ch='i';
   fwrite(&ch,1,1,fp3);*/
//-------------

 

 ch='i';
 fwrite(&ch,1,1,fp3);
 ch='d';
 fwrite(&ch,1,1,fp3);
 ch='x';
 fwrite(&ch,1,1,fp3);
 ch='1';
 fwrite(&ch,1,1,fp3);
 mTemp=0;
 fwrite(&mTemp,1,4,fp3);
 mOffset=4;

 

 for(i=0;i<10;i++)
 {
  switch(i)
  {
  case 0:
   printf("0b.jpg ");
   fp2=fopen("0b.jpg","rb+");
   break;
  case 1:
   printf("1b.jpg ");
   fclose(fp2);
   fp2=fopen("1b.jpg","rb+");
   break;
  case 2:
   printf("2b.jpg ");
   fclose(fp2);
   fp2=fopen("2b.jpg","rb+");
   break;
  case 3:
   printf("3b.jpg ");
   fclose(fp2);
   fp2=fopen("3b.jpg","rb+");
   break;
  case 4:
   printf("4b.jpg ");
   fclose(fp2);
   fp2=fopen("4b.jpg","rb");
   break;
  case 5:
   printf("5b.jpg ");
   fclose(fp2);
   fp2=fopen("5b.jpg","rb+");
   break;
  case 6:
   printf("6b.jpg ");
   fclose(fp2);
   fp2=fopen("6b.jpg","rb+");
   break;
  case 7:
   printf("7b.jpg ");
   fclose(fp2);
   fp2=fopen("7b.jpg","rb+");
   break;
  case 8:
   printf("8b.jpg ");
   fclose(fp2);
   fp2=fopen("8b.jpg","rb+");
   break;
  default:
   printf("9b.jpg ");
   fclose(fp2);
   fp2=fopen("9b.jpg","rb+");
   break;
  }
  if(fp2==NULL)
  {
   printf("Source file open error!\r\n");
   fcloseall();
   return;
  }
  //mSourceFileLength=(unsigned long)filelength(fp2);
  fseek(fp2,0,SEEK_END);
  mSourceFileLength=(unsigned long)ftell(fp2);

 


  for(j=0;j  {
   //video
   ch='0';
   fwrite(&ch,1,1,fp3);
   ch='0';
   fwrite(&ch,1,1,fp3);
   ch='d';
   fwrite(&ch,1,1,fp3);
   ch='c';
   fwrite(&ch,1,1,fp3);
   mTemp=0x00000010;
   fwrite(&mTemp,1,4,fp3);
   fwrite(&mOffset,1,4,fp3);
   mSize=mSourceFileLength;
   if(mSize&1)
   {
    //mSize++;
   }
   fwrite(&mSize,1,4,fp3);
   mOffset+=(mSize+8);

 


   ch='0';
   fwrite(&ch,1,1,fp1);
   ch='0';
   fwrite(&ch,1,1,fp1);
   ch='d';
   fwrite(&ch,1,1,fp1);
   ch='c';
   fwrite(&ch,1,1,fp1);
   mSize=mSourceFileLength;
   if(mSize&1)
   {
    //mSize++;
   }
   fwrite(&mSize,1,4,fp1);
   fseek(fp2,0,SEEK_SET);
   for(k=0;k   {
    if((k&0xFF)==0)
    {
     printf(".");
    }
    fread(&ch,1,1,fp2);
    fwrite(&ch,1,1,fp1);
   }
   if(mSourceFileLength&1)
   {
    ch=0; //double bytes align
    //fwrite(&ch,1,1,fp1);
   }

 

   //audio
   ch='0';
   fwrite(&ch,1,1,fp3);
   ch='1';
   fwrite(&ch,1,1,fp3);
   ch='w';
   fwrite(&ch,1,1,fp3);
   ch='b';
   fwrite(&ch,1,1,fp3);
   mTemp=0x00000010;
   fwrite(&mTemp,1,4,fp3);
   fwrite(&mOffset,1,4,fp3);
   mSize=8000/mFrameRate;
   if(mSize&1)
   {
    //mSize++; //double bytes align
   }
   fwrite(&mSize,1,4,fp3);
   mOffset+=(mSize+8);

 

   k=ftell(fp1);
   ch='0';
   fwrite(&ch,1,1,fp1);
   ch='1';
   fwrite(&ch,1,1,fp1);
   ch='w';
   fwrite(&ch,1,1,fp1);
   ch='b';
   fwrite(&ch,1,1,fp1);
   mSize=8000/mFrameRate;
   if(mSize&1)
   {
    //mSize++; //double bytes align
   }
   fwrite(&mSize,1,4,fp1);
   ch=0;
   for(k=0;k   {
    if((k&0xFF)==0)
    {
     printf(".");
    }
    //ch++;
    fread(&ch,1,1,fp4);
    fwrite(&ch,1,1,fp1);
   }
  }
 }
 mTargetFileLength=ftell(fp1);
 if(mTargetFileLength&1)
 {
  ch=0;
  fwrite(&ch,1,1,fp1);
 }
 mSize=ftell(fp3);
 mSize-=8;
 fseek(fp3,4,SEEK_SET);
 fwrite(&mSize,1,4,fp3);

 

 mTargetFileLength=ftell(fp1);
 mTargetFileLength-=0x808;
 fseek(fp1,0x0804,SEEK_SET);
 fwrite(&mTargetFileLength,1,4,fp1);
 //mTargetFileLength=ftell(fp1);
 //mTargetFileLength-=(sizeof(mMJPEG_Header)+8);
 //fseek(fp1,sizeof(mMJPEG_Header)+4,SEEK_SET);
 //fwrite(&mTargetFileLength,1,4,fp1);

 

 fseek(fp1,0,SEEK_END);
 mSize+=8;
 fseek(fp3,0,SEEK_SET);
 for(i=0;i {
  fread(&ch,1,1,fp3);
  fwrite(&ch,1,1,fp1);
 }

 

 mTargetFileLength=ftell(fp1);
 fseek(fp1,0x0004,SEEK_SET);
 mTargetFileLength-=8;
 fwrite(&mTargetFileLength,1,4,fp1);

 

 printf("\r\n");
 fcloseall();
}

 

void make_vga_mjpeg()
{
 unsigned long mFrameRate,mFrameTime,i,j,k,mTemp;
 unsigned long mTargetFileLength,mSourceFileLength;
 unsigned long mOffset,mSize;
 unsigned char sFileName[256],ch;
 FILE *fp1,*fp2,*fp3,*fp4;

 

 mFrameRate=0;
 printf("\r\nBegin make VGA MJPG file\r\n");
 while((mFrameRate<5)||(mframerate>30))
 {
  printf("Please input frame rate(5~30):");
  scanf("%d",&mFrameRate);
 }
 printf("Please input target filename:");
 scanf("%s",sFileName);

 

 mFrameTime=1000000/mFrameRate;

 

 mMJPEG_Header[0x20]=(unsigned char)mFrameTime&0xFF;
 mMJPEG_Header[0x21]=(unsigned char)(mFrameTime>>8)&0xFF;
 mMJPEG_Header[0x22]=(unsigned char)(mFrameTime>>16)&0xFF;
 mMJPEG_Header[0x23]=(unsigned char)(mFrameTime>>24)&0xFF;

 

 mMJPEG_Header[0x30]=(unsigned char)(mFrameRate*10)&0xFF;
 mMJPEG_Header[0x31]=(unsigned char)((mFrameRate*10)>>8)&0xFF;
 mMJPEG_Header[0x32]=(unsigned char)((mFrameRate*10)>>16)&0xFF;
 mMJPEG_Header[0x33]=(unsigned char)((mFrameRate*10)>>24)&0xFF;

 

 mMJPEG_Header[0x40]=0x80;
 mMJPEG_Header[0x41]=0x02;
 mMJPEG_Header[0x42]=0x00;
 mMJPEG_Header[0x43]=0x00;

 

 mMJPEG_Header[0x44]=0xE0;
 mMJPEG_Header[0x45]=0x01;
 mMJPEG_Header[0x46]=0x00;
 mMJPEG_Header[0x47]=0x00;

 

 mMJPEG_Header[0x80]=(unsigned char)mFrameTime&0xFF;
 mMJPEG_Header[0x81]=(unsigned char)(mFrameTime>>8)&0xFF;
 mMJPEG_Header[0x82]=(unsigned char)(mFrameTime>>16)&0xFF;
 mMJPEG_Header[0x83]=(unsigned char)(mFrameTime>>24)&0xFF;

 

 //mMJPEG_Header[0x84]=(unsigned char)mFrameRate&0xFF;
 //mMJPEG_Header[0x85]=(unsigned char)(mFrameRate>>8)&0xFF;
 //mMJPEG_Header[0x86]=(unsigned char)(mFrameRate>>16)&0xFF;
 //mMJPEG_Header[0x87]=(unsigned char)(mFrameRate>>24)&0xFF;

 

 mMJPEG_Header[0x8C]=(unsigned char)(mFrameRate*10)&0xFF;
 mMJPEG_Header[0x8D]=(unsigned char)((mFrameRate*10)>>8)&0xFF;
 mMJPEG_Header[0x8E]=(unsigned char)((mFrameRate*10)>>16)&0xFF;
 mMJPEG_Header[0x8F]=(unsigned char)((mFrameRate*10)>>24)&0xFF;

 

 mMJPEG_Header[0xA0]=0x80;
 mMJPEG_Header[0xA1]=0x02;
 mMJPEG_Header[0xA2]=0xE0;
 mMJPEG_Header[0xA3]=0x01;

 

 mMJPEG_Header[0xB0]=0x80;
 mMJPEG_Header[0xB1]=0x02;
 mMJPEG_Header[0xB2]=0x00;
 mMJPEG_Header[0xB3]=0x00;

 

 mMJPEG_Header[0xB4]=0xE0;
 mMJPEG_Header[0xB5]=0x01;
 mMJPEG_Header[0xB6]=0x00;
 mMJPEG_Header[0xB7]=0x00;

 

 //mMJPEG_Header[0xBA]=0x18; //must set as 24
 //mMJPEG_Header[0xBB]=0x00;

 

 mMJPEG_Header[0xC0]=0x00;
 mMJPEG_Header[0xC1]=0x60;
 mMJPEG_Header[0xC2]=0x09;
 mMJPEG_Header[0xC3]=0x00;

 

 mMJPEG_Header[0x120-0x20]=0x40;
 mMJPEG_Header[0x121-0x20]=0x1F;
 mMJPEG_Header[0x122-0x20]=0x00;
 mMJPEG_Header[0x123-0x20]=0x00;

 

 mMJPEG_Header[0x128-0x20]=(unsigned char)(8000*10)&0xFF;
 mMJPEG_Header[0x129-0x20]=(unsigned char)((8000*10)>>8)&0xFF;
 mMJPEG_Header[0x12A-0x20]=(unsigned char)((8000*10)>>16)&0xFF;
 mMJPEG_Header[0x12B-0x20]=(unsigned char)((8000*10)>>24)&0xFF;

 

 mMJPEG_Header[0x12C-0x20]=0x40;
 mMJPEG_Header[0x12D-0x20]=0x1F;
 mMJPEG_Header[0x12E-0x20]=0x00;
 mMJPEG_Header[0x12F-0x20]=0x00;

 

 mMJPEG_Header[0x14C-0x20]=0x40;
 mMJPEG_Header[0x14D-0x20]=0x1F;
 mMJPEG_Header[0x14E-0x20]=0x00;
 mMJPEG_Header[0x14F-0x20]=0x00;

 

 mMJPEG_Header[0x150-0x20]=0x40;
 mMJPEG_Header[0x151-0x20]=0x1F;
 mMJPEG_Header[0x152-0x20]=0x00;
 mMJPEG_Header[0x153-0x20]=0x00;

 

 /*mMJPEG_Header[0x120]=0x10;
 mMJPEG_Header[0x121]=0x2B;
 mMJPEG_Header[0x122]=0x00;
 mMJPEG_Header[0x123]=0x00;

 

 mMJPEG_Header[0x128]=(unsigned char)(8000*10)&0xFF;
 mMJPEG_Header[0x129]=(unsigned char)((8000*10)>>8)&0xFF;
 mMJPEG_Header[0x12A]=(unsigned char)((8000*10)>>16)&0xFF;
 mMJPEG_Header[0x12B]=(unsigned char)((8000*10)>>24)&0xFF;

 

 mMJPEG_Header[0x12C]=0x10;
 mMJPEG_Header[0x12D]=0x2B;
 mMJPEG_Header[0x12E]=0x00;
 mMJPEG_Header[0x12F]=0x00;

 

 mMJPEG_Header[0x14C]=0x10;
 mMJPEG_Header[0x14D]=0x2B;
 mMJPEG_Header[0x14E]=0x00;
 mMJPEG_Header[0x14F]=0x00;

 

 mMJPEG_Header[0x150]=0x10;
 mMJPEG_Header[0x151]=0x2B;
 mMJPEG_Header[0x152]=0x00;
 mMJPEG_Header[0x153]=0x00;*/

 

 mMJPEG_Header[0x3C]=0x00;
 mMJPEG_Header[0x3D]=0x00;
 mMJPEG_Header[0x3E]=0x10;
 mMJPEG_Header[0x3F]=0x00;

 

 mMJPEG_Header[0x90]=0x00;
 mMJPEG_Header[0x91]=0x00;
 mMJPEG_Header[0x92]=0x10;
 mMJPEG_Header[0x93]=0x00;

 

 fp1=fopen(sFileName,"wb+");
 if(fp1==NULL)
 {
  printf("Target file open error!\r\n");
  fcloseall();
  return;
 }
 fp3=fopen("idx_tmp.bin","wb+");
 if(fp3==NULL)
 {
  printf("Target file open error!\r\n");
  fcloseall();
  return;
 }
 fp4=fopen("10.pcm","rb+");
 if(fp4==NULL)
 {
  printf("Source file open error!\r\n");
  fcloseall();
  return;
 }

 

 fwrite(mMJPEG_Header,1,sizeof(mMJPEG_Header),fp1);


 

 

//----------------
   /*ch='L';
   fwrite(&ch,1,1,fp3);
   ch='I';
   fwrite(&ch,1,1,fp3);
   ch='S';
   fwrite(&ch,1,1,fp3);
   ch='T';
   fwrite(&ch,1,1,fp3);
   ch=0;
   fwrite(&ch,1,1,fp3);
   ch=0;
   fwrite(&ch,1,1,fp3);
   ch=0;
   fwrite(&ch,1,1,fp3);
   ch=0;
   fwrite(&ch,1,1,fp3);
   ch='m';
   fwrite(&ch,1,1,fp3);
   ch='o';
   fwrite(&ch,1,1,fp3);
   ch='v';
   fwrite(&ch,1,1,fp3);
   ch='i';
   fwrite(&ch,1,1,fp3);*/
//-------------

 

 ch='i';
 fwrite(&ch,1,1,fp3);
 ch='d';
 fwrite(&ch,1,1,fp3);
 ch='x';
 fwrite(&ch,1,1,fp3);
 ch='1';
 fwrite(&ch,1,1,fp3);
 mTemp=0;
 fwrite(&mTemp,1,4,fp3);
 mOffset=4;

 

 for(i=0;i<10;i++)
 {
  switch(i)
  {
  case 0:
   printf("0a.jpg ");
   fp2=fopen("0a.jpg","rb+");
   break;
  case 1:
   printf("1a.jpg ");
   fclose(fp2);
   fp2=fopen("1a.jpg","rb+");
   break;
  case 2:
   printf("2a.jpg ");
   fclose(fp2);
   fp2=fopen("2a.jpg","rb+");
   break;
  case 3:
   printf("3a.jpg ");
   fclose(fp2);
   fp2=fopen("3a.jpg","rb+");
   break;
  case 4:
   printf("4a.jpg ");
   fclose(fp2);
   fp2=fopen("4a.jpg","rb");
   break;
  case 5:
   printf("5a.jpg ");
   fclose(fp2);
   fp2=fopen("5a.jpg","rb+");
   break;
  case 6:
   printf("6a.jpg ");
   fclose(fp2);
   fp2=fopen("6a.jpg","rb+");
   break;
  case 7:
   printf("7a.jpg ");
   fclose(fp2);
   fp2=fopen("7a.jpg","rb+");
   break;
  case 8:
   printf("8a.jpg ");
   fclose(fp2);
   fp2=fopen("8a.jpg","rb+");
   break;
  default:
   printf("9a.jpg ");
   fclose(fp2);
   fp2=fopen("9a.jpg","rb+");
   break;
  }
  if(fp2==NULL)
  {
   printf("Source file open error!\r\n");
   fcloseall();
   return;
  }
  //mSourceFileLength=(unsigned long)filelength(fp2);
  fseek(fp2,0,SEEK_END);
  mSourceFileLength=(unsigned long)ftell(fp2);

 


  for(j=0;j  {
   //video
   ch='0';
   fwrite(&ch,1,1,fp3);
   ch='0';
   fwrite(&ch,1,1,fp3);
   ch='d';
   fwrite(&ch,1,1,fp3);
   ch='c';
   fwrite(&ch,1,1,fp3);
   mTemp=0x00000010;
   fwrite(&mTemp,1,4,fp3);
   fwrite(&mOffset,1,4,fp3);
   mSize=mSourceFileLength;
   if(mSize&1)
   {
    //mSize++;
   }
   fwrite(&mSize,1,4,fp3);
   mOffset+=(mSize+8);

 


   ch='0';
   fwrite(&ch,1,1,fp1);
   ch='0';
   fwrite(&ch,1,1,fp1);
   ch='d';
   fwrite(&ch,1,1,fp1);
   ch='c';
   fwrite(&ch,1,1,fp1);
   mSize=mSourceFileLength;
   if(mSize&1)
   {
    //mSize++;
   }
   fwrite(&mSize,1,4,fp1);
   fseek(fp2,0,SEEK_SET);
   for(k=0;k   {
    if((k&0xFF)==0)
    {
     printf(".");
    }
    fread(&ch,1,1,fp2);
    fwrite(&ch,1,1,fp1);
   }
   if(mSourceFileLength&1)
   {
    ch=0; //double bytes align
    //fwrite(&ch,1,1,fp1);
   }

 

   //audio
   ch='0';
   fwrite(&ch,1,1,fp3);
   ch='1';
   fwrite(&ch,1,1,fp3);
   ch='w';
   fwrite(&ch,1,1,fp3);
   ch='b';
   fwrite(&ch,1,1,fp3);
   mTemp=0x00000010;
   fwrite(&mTemp,1,4,fp3);
   fwrite(&mOffset,1,4,fp3);
   mSize=8000/mFrameRate;
   if(mSize&1)
   {
    //mSize++; //double bytes align
   }
   fwrite(&mSize,1,4,fp3);
   mOffset+=(mSize+8);

 

   k=ftell(fp1);
   ch='0';
   fwrite(&ch,1,1,fp1);
   ch='1';
   fwrite(&ch,1,1,fp1);
   ch='w';
   fwrite(&ch,1,1,fp1);
   ch='b';
   fwrite(&ch,1,1,fp1);
   mSize=8000/mFrameRate;
   if(mSize&1)
   {
    //mSize++; //double bytes align
   }
   fwrite(&mSize,1,4,fp1);
   ch=0;
   for(k=0;k   {
    if((k&0xFF)==0)
    {
     printf(".");
    }
    //ch++;
    fread(&ch,1,1,fp4);
    fwrite(&ch,1,1,fp1);
   }
  }
 }
 mTargetFileLength=ftell(fp1);
 if(mTargetFileLength&1)
 {
  ch=0;
  fwrite(&ch,1,1,fp1);
 }
 mSize=ftell(fp3);
 mSize-=8;
 fseek(fp3,4,SEEK_SET);
 fwrite(&mSize,1,4,fp3);


 

 mTargetFileLength=ftell(fp1);
 mTargetFileLength-=0x808;
 fseek(fp1,0x0804,SEEK_SET);
 fwrite(&mTargetFileLength,1,4,fp1);
 //mTargetFileLength=ftell(fp1);
 //mTargetFileLength-=(sizeof(mMJPEG_Header)+8);
 //fseek(fp1,sizeof(mMJPEG_Header)+4,SEEK_SET);
 //fwrite(&mTargetFileLength,1,4,fp1);

 

 fseek(fp1,0,SEEK_END);
 mSize+=8;
 fseek(fp3,0,SEEK_SET);
 for(i=0;i {
  fread(&ch,1,1,fp3);
  fwrite(&ch,1,1,fp1);
 }

 

 mTargetFileLength=ftell(fp1);
 fseek(fp1,0x0004,SEEK_SET);
 mTargetFileLength-=8;
 fwrite(&mTargetFileLength,1,4,fp1);

 

 printf("\r\n");
 fcloseall();
}

 

main()
{
 char ch;
 do
 {
  printf("\r\nSelect image size(1 QVGA, 2 VGA):");
  //scanf("%c",&ch);
  ch=getch();
  printf("%c",ch);
 }while((ch!='1')&&(ch!='2'));

 

 if(ch=='1')
 {
  make_qvga_mjpeg();
 }
 else
 {
  make_vga_mjpeg();
 }
 printf("\r\nPress any key to Exit");
 getch();

 

}

 

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分