1.      概述现在很多智能手机都支持多媒体功能,特别是音频和视频播放功能,而3GP文件格式是手机端普遍支持的视频文件格式。   目前很多手机都支持h263视频编码格式的视频文件播放,还有些手机支持h264。音频文件格式普遍支持amr,有些手机还支持AAC音频格式。   鉴于凤凰卫视节目主要以新闻类为主,音频压缩采用AMR标准,视频压缩采用H263和H264标准。   编号 文件名 文件长度 时长 1 news_h263.3gp 1.38MB 01:21.90 2 zbc1217_h263.3gp 1.92MB 02:37.98 3 zbc1217_h264.3gp 2.44MB 02:37.98         2. 3GP文件结构分析示例 AtomicParsley是一个开源的多媒体文件结构分析器。下面是用该程序分析的两个3GP文件结构: news_h263.3gp Atom ftyp @ 0 of size: 28, ends @ 28 Atom mdat @ 28 of size: 65, ends @ 93 Atom mdat @ 93 of size: 130856, ends @ 130949 Atom mdat @ 130949 of size: 1312119, ends @ 1443068 Atom moov @ 1443068 of size: 7136, ends @ 1450204      Atom mvhd @ 1443076 of size: 108, ends @ 1443184      Atom trak @ 1443184 of size: 1546, ends @ 1444730          Atom tkhd @ 1443192 of size: 92, ends @ 1443284          Atom udta @ 1443284 of size: 48, ends @ 1443332              Atom uuid=484d496e-6354-7261-636b-496e666f2020 @ 1443292 of size: 40, ends @ 1443332          Atom mdia @ 1443332 of size: 1398, ends @ 1444730              Atom mdhd @ 1443340 of size: 32, ends @ 1443372              Atom hdlr @ 1443372 of size: 37, ends @ 1443409              Atom minf @ 1443409 of size: 1321, ends @ 1444730                  Atom smhd @ 1443417 of size: 16, ends @ 1443433                  Atom dinf @ 1443433 of size: 36, ends @ 1443469                      Atom dref @ 1443441 of size: 28, ends @ 1443469                  Atom stbl @ 1443469 of size: 1261, ends @ 1444730                      Atom stts @ 1443477 of size: 24, ends @ 1443501                      Atom stsd @ 1443501 of size: 69, ends @ 1443570                          Atom samr @ 1443517 of size: 53, ends @ 1443570                              Atom damr @ 1443553 of size: 17, ends @ 1443570                      Atom stsz @ 1443570 of size: 1112, ends @ 1444682                      Atom stsc @ 1444682 of size: 28, ends @ 1444710                      Atom stco @ 1444710 of size: 20, ends @ 1444730      Atom trak @ 1444730 of size: 5474, ends @ 1450204          Atom tkhd @ 1444738 of size: 92, ends @ 1444830          Atom tref @ 1444830 of size: 20, ends @ 1444850              Atom sync @ 1444838 of size: 12, ends @ 1444850          Atom udta @ 1444850 of size: 48, ends @ 1444898              Atom uuid=484d496e-6354-7261-636b-496e666f2020 @ 1444858 of size: 40, ends @ 1444898          Atom mdia @ 1444898 of size: 5306, ends @ 1450204              Atom mdhd @ 1444906 of size: 32, ends @ 1444938              Atom hdlr @ 1444938 of size: 37, ends @ 1444975              Atom minf @ 1444975 of size: 5229, ends @ 1450204                  Atom vmhd @ 1444983 of size: 20, ends @ 1445003                  Atom dinf @ 1445003 of size: 36, ends @ 1445039                      Atom dref @ 1445011 of size: 28, ends @ 1445039                  Atom stbl @ 1445039 of size: 5165, ends @ 1450204                      Atom stts @ 1445047 of size: 24, ends @ 1445071                      Atom stsd @ 1445071 of size: 117, ends @ 1445188                          Atom s263 @ 1445087 of size: 101, ends @ 1445188                              Atom d263 @ 1445173 of size: 15, ends @ 1445188                      Atom stsz @ 1445188 of size: 4920, ends @ 1450108                      Atom stss @ 1450108 of size: 48, ends @ 1450156                      Atom stsc @ 1450156 of size: 28, ends @ 1450184                      Atom stco @ 1450184 of size: 20, ends @ 1450204 ------------------------------------------------------ Total size: 1450204 bytes; 46 atoms total. AtomicParsley version: 0.9.0 (utf16) Media data: 1442975 bytes; 7229 bytes all other atoms (0.498% atom overhead). Total free atom space: 0 bytes; 0.000% waste. ------------------------------------------------------ Movie duration: 81.900 seconds (01:21.90) - 140.95* kbp/sec bitrate (*=approximate) Low-level details. Total tracks: 2 Trk Type Handler                    Kind Lang Bytes 1    soun soun                       samr eng   130848   Encoder: NXTR      12.78* kbp/s 81.900 sec AMR Narrow-Band. Modes: 0, 1, 2, 3, 4, 5, 6, 7, 8. Encoder vendor code: NXTR 2    vide vide                       s263 eng   1312111   Encoder: NXTR      128.41* kbp/s 81.748 sec H.263 Baseline Profile, Level 10. Encoder vendor code: NXTR      该3GP文件结构如下: zbc1217_h263.3gp Atom ftyp @ 0 of size: 24, ends @ 24 Atom mdat @ 24 of size: 1989236, ends @ 1989260 Atom moov @ 1989260 of size: 14951, ends @ 2004211      Atom mvhd @ 1989268 of size: 108, ends @ 1989376      Atom iods @ 1989376 of size: 33, ends @ 1989409      Atom udta @ 1989409 of size: 1069, ends @ 1990478          Atom rtng [eng] @ 1989417 of size: 29, ends @ 1989446          Atom uuid=f78caa0c-36be-4ce9-88d2-03c256dabeb2 @ 1989446 of size: 1032, ends @ 1990478      Atom trak @ 1990478 of size: 9978, ends @ 2000456          Atom tkhd @ 1990486 of size: 92, ends @ 1990578          Atom mdia @ 1990578 of size: 9842, ends @ 2000420              Atom mdhd @ 1990586 of size: 32, ends @ 1990618              Atom hdlr @ 1990618 of size: 33, ends @ 1990651              Atom minf @ 1990651 of size: 9769, ends @ 2000420                  Atom vmhd @ 1990659 of size: 20, ends @ 1990679                  Atom dinf @ 1990679 of size: 36, ends @ 1990715                      Atom dref @ 1990687 of size: 28, ends @ 1990715                  Atom stbl @ 1990715 of size: 9705, ends @ 2000420                      Atom stsd @ 1990723 of size: 117, ends @ 1990840                          Atom s263 @ 1990739 of size: 101, ends @ 1990840                              Atom d263 @ 1990825 of size: 15, ends @ 1990840                      Atom stts @ 1990840 of size: 32, ends @ 1990872                      Atom stsc @ 1990872 of size: 1900, ends @ 1992772                      Atom stsz @ 1992772 of size: 6300, ends @ 1999072                      Atom stco @ 1999072 of size: 644, ends @ 1999716                      Atom stss @ 1999716 of size: 704, ends @ 2000420          Atom udta @ 2000420 of size: 36, ends @ 2000456              Atom uuid=f78caa0c-36be-4ce9-87d2-03c256dabeb2 @ 2000428 of size: 28, ends @ 2000456      Atom trak @ 2000456 of size: 2934, ends @ 2003390          Atom tkhd @ 2000464 of size: 92, ends @ 2000556          Atom mdia @ 2000556 of size: 2798, ends @ 2003354              Atom mdhd @ 2000564 of size: 32, ends @ 2000596              Atom hdlr @ 2000596 of size: 33, ends @ 2000629              Atom minf @ 2000629 of size: 2725, ends @ 2003354                  Atom smhd @ 2000637 of size: 16, ends @ 2000653                  Atom dinf @ 2000653 of size: 36, ends @ 2000689                      Atom dref @ 2000661 of size: 28, ends @ 2000689                  Atom stbl @ 2000689 of size: 2665, ends @ 2003354                      Atom stsd @ 2000697 of size: 69, ends @ 2000766                          Atom samr @ 2000713 of size: 53, ends @ 2000766                              Atom damr @ 2000749 of size: 17, ends @ 2000766                      Atom stts @ 2000766 of size: 24, ends @ 2000790                      Atom stsc @ 2000790 of size: 1900, ends @ 2002690                      Atom stsz @ 2002690 of size: 20, ends @ 2002710                      Atom stco @ 2002710 of size: 644, ends @ 2003354          Atom udta @ 2003354 of size: 36, ends @ 2003390              Atom uuid=f78caa0c-36be-4ce9-87d2-03c256dabeb2 @ 2003362 of size: 28, ends @ 2003390      Atom trak @ 2003390 of size: 420, ends @ 2003810          Atom tkhd @ 2003398 of size: 92, ends @ 2003490          Atom mdia @ 2003490 of size: 296, ends @ 2003786              Atom mdhd @ 2003498 of size: 32, ends @ 2003530              Atom hdlr @ 2003530 of size: 33, ends @ 2003563              Atom minf @ 2003563 of size: 223, ends @ 2003786                  Atom nmhd @ 2003571 of size: 12, ends @ 2003583                  Atom dinf @ 2003583 of size: 36, ends @ 2003619                      Atom dref @ 2003591 of size: 28, ends @ 2003619                  Atom stbl @ 2003619 of size: 167, ends @ 2003786                      Atom stsd @ 2003627 of size: 67, ends @ 2003694                          Atom mp4s @ 2003643 of size: 51, ends @ 2003694                              Atom esds @ 2003659 of size: 35, ends @ 2003694                      Atom stts @ 2003694 of size: 24, ends @ 2003718                      Atom stsc @ 2003718 of size: 28, ends @ 2003746                      Atom stsz @ 2003746 of size: 20, ends @ 2003766                      Atom stco @ 2003766 of size: 20, ends @ 2003786          Atom tref @ 2003786 of size: 24, ends @ 2003810              Atom mpod @ 2003794 of size: 16, ends @ 2003810      Atom trak @ 2003810 of size: 401, ends @ 2004211          Atom tkhd @ 2003818 of size: 92, ends @ 2003910          Atom mdia @ 2003910 of size: 301, ends @ 2004211              Atom mdhd @ 2003918 of size: 32, ends @ 2003950              Atom hdlr @ 2003950 of size: 33, ends @ 2003983              Atom minf @ 2003983 of size: 228, ends @ 2004211                  Atom nmhd @ 2003991 of size: 12, ends @ 2004003                  Atom dinf @ 2004003 of size: 36, ends @ 2004039                      Atom dref @ 2004011 of size: 28, ends @ 2004039                  Atom stbl @ 2004039 of size: 172, ends @ 2004211                      Atom stsd @ 2004047 of size: 72, ends @ 2004119                          Atom mp4s @ 2004063 of size: 56, ends @ 2004119                              Atom esds @ 2004079 of size: 40, ends @ 2004119                      Atom stts @ 2004119 of size: 24, ends @ 2004143                      Atom stsc @ 2004143 of size: 28, ends @ 2004171                      Atom stsz @ 2004171 of size: 20, ends @ 2004191                      Atom stco @ 2004191 of size: 20, ends @ 2004211 ------------------------------------------------------ Total size: 2004211 bytes; 82 atoms total. AtomicParsley version: 0.9.0 (utf16) Media data: 1989236 bytes; 14975 bytes all other atoms (0.747% atom overhead). Total free atom space: 0 bytes; 0.000% waste. ------------------------------------------------------ Movie duration: 156.980 seconds (02:36.98) - 101.38* kbp/sec bitrate (*=approximate) Low-level details. Total tracks: 4 Trk Type Handler                    Kind Lang Bytes 1    vide [none listed]              s263 und   1738018   Encoder: ViVi      88.58* kbp/s 156.960 sec H.263 Baseline Profile, Level 20. Encoder vendor code: ViVi 2    soun [none listed]              samr und   251168   Encoder: ViVi      12.80* kbp/s 156.980 sec AMR Narrow-Band. Modes: 7. Encoder vendor code: ViVi 3    odsm [none listed]              mp4s und   18 4    sdsm [none listed]              mp4s und   24   该3GP文件结构如下:     其实3GP文件就是一个对象(box)容器,对象还允许包含子对象。   对象格式如下: struct { UINT32          size;       // 对象大小 char                     type;      // 对象类型 void*             data;      // 对象数据(属性),大小为(size-8)}; 《ISO base media file format》说明不同box对象的结构定义细节。   3. 3GP文件对象实例分析 l         ftype File Type(该对象仅有一个)记录该文件使用的规范和版本等信息。     属性 大小 值 说明 size 4 0x0000001c = 28   ftyp 4 ftyp   major_brand 4 3gp4 主类型 minor_version 4   最小版本号 compatible_brands[] string 3gp4 | 3gp5 | 3g2a 兼容类型     l         mdat Media Data mdat 存放媒体数据(media data)。一般包括文字、音频和视频等。这些mdat的顺序不固定。   当前mdat对象包含的是纯文字。     大小 值 其他说明 size 4 0x00000041 = 65   mdat 4       string     这个box可以忽略。     l         mdat 音频数据块       大小 值 其他说明 size 4 0x0001FF28 = 130856   mdat 4       32*N 3C打头 每个音频帧32字节   amr音频数据以32字节一帧,每帧20毫秒。所以根据amr数据包大小就能估算出语音的时间长度。 (130856 – 8) / 32 = 4089 * 20 = 81780 ms = 817.80秒 = 01:21.90 这个时间与播放器上显示的时间基本相同。注意:这里真实的视频数据起始位置为0x000005D。在音频trak里面的stso对象里面的chunk_offset记录的就是这个值。   在RFC3267中有如下描述: The sampling frequency used in AMR is 8000 Hz and the speech encoding is performed on 20 ms speech frames. Therefore, each encoded AMR speech frame represents 160 samples of the original speech.   AMR语音采样频率为8Khz,每帧对应20ms。   l         mdat 视频数据块       大小 值 其他说明 size 4 0x004946F1 = 4802289   mdat 4       4   H263视频数据实际视频数据大小:4802289 – 8 = 4802281   《H263协议中文版》第8页 5.1图象层中描述:每帧图象的数据包含一个图象头,并紧跟着块组数据,最后是一个end-of-sequence码和填塞位。图象开始码 (PSC) (22 bits) PSC是一个22比特的字。它的值是0000 0000 0000 0000 1000 00- (00 00 80 00 …z)时域参考 (TR) (8 bits)  00-00 0000 00- 类型信息 (PTYPE) (13 bits)  00-10 0000 1000 000-0 位6-8:010 :表示图像为QCIF。位9 :图象编码类型,“0” INTRA (I-picture),“1” INTER (P-picture)。关键帧标志位,0:关键帧,1:p帧。   l         moov Movie Box 该对象保留了3GP视频的metadata,即该3GP的全部描述信息。这个对象一般在文件开始或者末尾。下面详细分析moov的各个子对象。   l         mvhd Movie Head Box     大小 值 其他说明 size 4 0x0000006C = 108   mvhd 4     version 1 0   flag 3 0   creation_time 4     modification_time 4     timescale 4 0x000003E8 = 1000 时间缩放因子 duration 4 0x00013FEC = 81900 = 81.9s 持续时间 rate 4 0x00010000 1.0(即全速播放) 播放比率 volume 2 0x0100 1.0 (音量) 音量 reserved     保留 reserved     保留 matrix     视频转换矩阵 pre_defined     预定义(全0) next_track_ID 4 0x000000CA = 202 下一个track_id   trak(音频) Track Box   l         tkhd Track Header Box 定义了该track的属性。     大小 值 其他说明 size 4 0x0000005C = 92   tkhd 4     version 1 0   flag 3 1   creation_time 4     modification_time 4     track_id 4 0x00000065 = 101   reserved 4 全0   duration 4 0x00013FEC = 81900 = 81.9s 音频播放时间 reserved 8     layer 2     alternate_group 2     volume 2 0x0100 如果track是音频则为0x0100,视频则为0x0000。 reserved 2 0   matrix 4 * 9   视频转换矩阵 width 4 0 音频的w和h为0 height 4 0     注意:如果track是音频volume则为0x0100,视频volume则为0x0000。     l         mdhd Media Header Box     大小 值 其他说明 size 4 32   mdhd 4     version 1     flag 3     creation_time 4     modification_time 4     timescale 4 0x00001F40 = 8000 时间缩放因子 duration 4 0x0009FF60 = 655200/8000=81.9 音频播放时长 pad 1bit     language 15bit 0x15c7 ISO-639-2/T language code   pre_defined 2       l         hdlr Handler Reference Box     大小 值 其他说明 size 4 0x00000025 = 37   hdlr 4     version 1     flag 3     pre_defined 4 0   handler_type 4 soun vide:Video  track soun:Audio  track reserved 4 * 3 0   handler_type string (4+1) soun 以’/0’结束   l         stbl Sample Table Box     l         stts Decoding Time to Sample Box     大小 值 其他说明 size 4 0x00000018 = 24   stts 4     version 1     flag 3     entry_count 4 1   sample_count 4 0x00000111 = 273   sample_delta 4 0x00000960 = 2400             说明:该amr音频数据里面有273个sample,每个sample的时间为2400,而音频的时间因子为8000(见5.2.3.1 mdhd的timescale),所以每个sample的时长 = 2400 / 8000s = 0.3s。   l         stsd Sample Description Box     大小 值 其他说明 size 4 0x00000045 = 69   stsd 4     version 1     flag 3     entry_count 4 1   AudioSampleEntry         l         samr amr 音频   size 4 0x35 = 53   samr 4     reserved 6 0   data_reference_index 2 0x0001   reserved 4 * 2 0   channelcount 2 0x0002 = 2 (1:单声道 2:双声道) samplesize 2 0x0010 = 16   pre_defined 2 0   reserved 2 0   samplerate 4 0x1F400000 = (14F0)<<16 0x1F40 = 8000 时间因子左移16位。声音采样频率(hz)         size 4 0x11 = 17   damr 4     NXTR ... 9               l         stsz Sample Size Boxes     大小 值 其他说明 size 4 0x00000458 = 1112   stsz 4     version 1     flag 3     sample_size 4 0   sample_count 4 0x00000111 = 273           entry_size 4 0x000001E0 = 480 每个sample大小   4 0x000001E0 = 480     4 0x000001E0 = 480       ...     说明:在(5.2.3.3.3.1 stts)中说明了有1001个sample,这里的sample_count=1001,每个sample数据块的大小为480,(480 / 32)*20 = 15*20 = 0.3s。这里包含了每个sample的大小,都为480字节。   l         stsc Sample To Chunk Box   大小 值 其他说明 size 4 0x0000001C = 28   stsc 4     version 1     flag 3     entry_count 4 1   first_chunk 4 1   samples_per_chunk 4 0X0111 = 273   sample_description_index 4 1     l         stco Chunk Offset Box   大小 值 其他说明 size 4 0x00000014 = 20   stco 4     version 1     flag 3     entry_count 4 1   chunk_offset 4 0x65 = 101       trak(视频) Track Box   l         tkhd Track Header Box 定义了该track的属性。     大小 值 其他说明 size 4 0x0000005C = 92   tkhd 4     version 1 0   flag 3 1   creation_time 4     modification_time 4     track_id 4 0x000000C9 = 201   reserved 4 全0   duration 4 0x00013F54 = 81748 = 81.7s 视频播放时间 reserved 8     layer 2     alternate_group 2     volume 2 0x0000 如果track是音频则为0x0100,视频则为0x0000。 reserved 2 0   matrix 4 * 9   视频转换矩阵 width 4 0x01400000<<16 = 0x0140=320   height 4 0x00F00000<<16 = 0x00F0=240       l         vmhd Video Media Header Box   大小 值 其他说明 size 4 0x00000014 = 20   wmhd 4     version 1     flag 3     graphicsmode 2 0   opcolor 2 * 3 {0,0,0}     l          stts Sync Sample Box   大小 值 其他说明 size 4 0x00000018 = 24   stts 4     version 1     flag 3     entry_count 4 1   sample_count 4 0x000004C9 = 1225   sample_delta 4 0x00001776 = 6006     说明:该H263视频数据里面有1225个sample,每个sample的时间为6006,而视频的时间因子为90000(见5.3.3.1 mdhd的timescale),所以每个sample的时长 = 6006 / 90000s = 0.0667s。 1225*(6006 / 90000 ) = (1225*6006)/90000 = 81.7s   l          stsd Sample Description Box   大小 值 其他说明 size 4 0x00000075 = 117   stsd 4 stsd   version 1 0   flag 3 0   entry_count 4 1   VideoSampleEntry                   l         S263 263视频 size 4 0x65 = 101   s263 4     reserved 6     data_reference_index 2 0x0001   pre_defined 2 0   reserved 2 0   pre_defined 4 * 3     width 2 0xb0 = 176 视频宽度 height 2 0x90 = 144 视频高度 horizresolution 4 0x00480000; // 72 dpi   vertresolution 4 0x00480000; // 72 dpi   reserved 4     frame_count 2 1   compressorname 32 string[32] 全0 depth 2 0x0018   pre_defined 2 -1   size 4 15   d263 4     NXTR ... 7       l         stsz Sample Size Boxes     大小 值 其他说明 size 4 0x1338   stsz 4     version 1     flag 3     sample_size 4 0   sample_count 4 0x04C9 = 1225           entry_size 4 0x0000297 = 663     4 0x068C = 1676     4 0x028D = 653       ...   说明:总共1225个sample即有1225帧,sample_count后面紧跟的是每个视频帧的数据大小,可以看出每个帧的大小都不一样,这是因为视频帧的类型不同,有些帧是I帧,有些是P帧,有些是B帧。   MPEG图像编码包含3个成分:I帧,P帧和B帧。MPEG编码过程中,一些图像压缩成I帧,一些压缩成P帧,另一些压缩成B帧。I帧压缩可以得到6;1的压缩比而不产生任何可觉察的模糊现象。I帧压缩的同时使用P帧压缩,可以达到更高的压缩比而无可觉察的模糊现象。B帧压缩可以达到200:1的压缩比,其文件尺寸一般为I帧压缩尺寸的15%,不到P帧压缩尺寸的一半。I帧压缩去掉图像的空间冗余度,P帧和B帧去掉时间冗余度,下文将进一步解释。   I帧压缩采用基准帧模式,只提供帧内压缩,即把帧图像压缩到I帧时,仅仅考虑了帧内的图像。I帧压缩不能除去帧间冗余度。帧内压缩基于离散余弦变换(DCT),类似于JPEG和H.261图像中使用DCT的压缩标准。   P帧采用预测编码,利用相邻帧的一般统计信息进行预测。也就是说,它考虑运动特性,提供帧间编码。P帧预测当前帧与前面最近的I帧或P帧的差别。   B帧为双向帧间编码。它从前面和后面的I帧或P帧中提取数据。B帧基于当前帧与前一帧和后一帧图像之间的差别进行压缩。   MEPG数据流开始时对CCIR-601规定的SIF分辨率的未压缩数字图像进行抽样。SIF分辨率,对于NTSC制,就是亮度信号为352*240各像素,每个色度信号都为176*120个象素。各信号都是每秒30帧。MPEG压缩器决定了当前帧以I帧,P帧还是B帧。帧确定之后就采用DCT变换,对结果进行量化,舍入,行程编码即变长编码。编码后的典型图像帧序为:IBBPBBPBBPBBIBBPBBPBBPBBI…   B帧和P帧要求计算机有更强的功能。有些压缩器不能产生B帧或者连P帧也不能产生,则图像的压缩结果将有很明显的间断。   H263采用了与MPEG相似的技术。 l         stco Chunk Offset Box     大小 值 其他说明 size 4 0x00000014 = 20   stco 4     version 1     flag 3     entry_count 4 1   chunk_offset 4 0x0001FF8D   说明:chunk_offset定义的是视频数据的起始位置。