leaffei

ps文件解析(纯c解析代码)

参考链接:1. PS流的格式和解析总结 http://www.cnblogs.com/lihaiping/p/4181607.html
     2. TS科普5 PES包解析 https://blog.csdn.net/cabbage2008/article/details/49612011

PES包的解析(本代码主要解析了PTS和DTS, 需结合下图和代码中的PES包的伪代码看):

startcode(24) + streamid(8) + pes_len(16) + {header: flag1(8) + flag2(8, pts标识在这儿) + header_len(8)} + {header_data(header_len, 若前面的flag有数据, 数据就在这儿)} + pes_data(pes_len-3-header_len)

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <string.h>
  4 #include <arpa/inet.h>
  5 
  6 #define TAB44 "    "
  7 #define PRINTF_DEBUG
  8 
  9 #define MAX_PS_STARTCODE_LEN 4
 10 #define MAX_PDTS_LEN 5
 11 #define MAX_ES_NUMS 6
 12 #define MAX_PDTS_STRING_LEN 12
 13 #define MMIN_PS_HEADER_LEN 14
 14 
 15 
 16 #define SCODE_PS_END 0x000001B9
 17 #define SCODE_PS_HEADER 0x000001BA
 18 #define SCODE_PS_SYSTEM_HEADER 0x000001BB
 19 #define SCODE_PS_SYSTEM_MAP_HEADER 0x000001BC
 20 
 21 /**********************************************************************************************************
 22 pack_header() {
 23     pack_start_code                                        32 bits
 24     '01'                                                2 bits
 25     system_clock_reference_base[32..30]                    3 bits
 26     marker_bit                                            1 bit
 27     system_clock_reference_base[29..15]                    15 bits
 28     marker_bit                                            1 bit
 29     system_clock_reference_base[14..0]                    15 bits
 30     marker_bit                                            1 bit
 31     system_clock_reference_extension                    9 bits
 32     marker_bit                                            1 bit
 33     program_mux_rate                                    22 bits
 34     marker_bit                                            1 bit
 35     marker_bit                                            1 bit
 36     reserved                                            5 bit
 37     pack_stuffing_length                                3 bits
 38 
 39     for (i=0; i<pack_stuffing_length; i++){
 40         stuffing_byte                                    8 bits
 41     }
 42 
 43     if (nextbits() == system_header_start_code) {
 44         system_header()
 45     }
 46 }
 47 **********************************************************************************************************/
 48 
 49 
 50 /**********************************************************************************************************
 51 system_header() {
 52     system_header_start_code                            32 bits
 53     header_length                                        16 bits
 54     marker_bit                                            1 bit
 55     rate_bound                                            22 bits
 56     marker_bit                                            1 bit
 57     audio_bound                                            6 bits
 58     fixed_flag                                            1 bit
 59     CSPS_flag                                            1 bit
 60     system_audio_lock_flag                                1 bit
 61     system_video_lock_flag                                1 bit
 62     marker_bit                                            1 bit
 63     vedio_bound                                            5 bits
 64     packet_rate_restriction_flag                        1 bit
 65     reserved_bits                                        7 bits
 66 
 67     while (nextbits() == '1') {
 68         stream_id                                        8 bits
 69         '11'                                            2 bits
 70         P-STD_buffer_bound_scale                        1 bit
 71         P-STD_buffer_size_bound                            13 bits
 72     }
 73 }
 74 **********************************************************************************************************/
 75 
 76 
 77 /**********************************************************************************************************
 78 program_stream_map() {
 79     packet_start_code_prefix             24 bits
 80     map_stream_id                        8 bits
 81     program_stream_map_length            16 bits
 82     current_next_indicator                1 bit
 83     reserved                            2 bits
 84     program_stream_map_version            5 bits
 85     reserved                            7 bits
 86     marker_bit                            1 bit
 87     program_stream_info_length            16 bits
 88 
 89     for (i=0;i<N;i++) {
 90         descriptor()
 91     }
 92 
 93     elementary_stream_map_length        16 bits
 94 
 95     for (i=0;i<N1;i++) {
 96        stream_type                        8 bits
 97        elementary_stream_id                8 bits
 98 
 99        elementary_stream_info_length    16 bits
100 
101        for (i=0;i<N2;i++) {
102            descriptor()
103        }
104     }
105 
106     CRC_32                                32 bits
107 }
108 
109 ** current_next_indicator: 当前下一个指示符字段, 1位字段. 置'1'时表示传送的节目流映射当前是可用的.
110         置'0'时表示传送的节目流映射还不可用, 但它将是下一个生效的表.
111 ** program_stream_map_version: 5位字段, 表示整个节目流映射的版本号. 一旦节目流映射的定义发生变化,
112         该字段将递增1, 并对32取模. 在current_next_indicator为'1'时, 该字段应该是当前适用的节目流映射的版本号;
113         在current_next_indicator为'0'时, 该字段应该是下一个适用的节目流映射的版本号.
114 ** stream_type: 流类型字段, 该字段只能标志包含在PES分组中的基本流且取值不能为0x05.
115                 1. MPEG-4视频流: 0x10;
116                 2. H.264视频流: 0x1B;
117                 3. SVAC视频流: 0x80;
118                 4. G.711音频流: 0x90;
119                 5. G.722.1音频流: 0x92;
120                 6. G.723.1音频流: 0x93;
121                 7. G.729音频流: 0x99;
122                 8. SVAC音频流: 0x9B.
123         因为节目映射流字段只有在关键帧打包的时候, 才会存在, 所以如果要判断PS打包的流编码类型, 就根据这个字段来判断.
124 ** elementary_stream_map_length: 基本流映射长度字段. 指出在该节目流映射中的所有基本流信息的字节长度.
125         它只包括stream_type、elementary_stream_id和elementary_stream_info_length字段.
126 ** elementary_stream_id: 基本流标识字段, 8位字段, 指出该基本流所在PES分组的PES分组标题中stream_id字段的值.
127         这个字段的定义, 其中0x(C0~DF)指音频, 0x(E0~EF)为视频.
128 **********************************************************************************************************/
129 typedef struct t_es_map
130 {
131     unsigned char streamType;
132     unsigned char esId;
133     unsigned short esInfoLen;
134 } T_ES_MAP;
135 
136 typedef struct t_ps_map 
137 {
138     unsigned char curNextInd:1, :2, version:5;
139     
140     unsigned short psInfoLen;
141     unsigned short esStreamMapLen;
142     
143     unsigned int esMapNum;
144     
145     T_ES_MAP esMaps[MAX_ES_NUMS];
146 } T_PS_MAP;
147 
148 /**********************************************************************************************************
149 PES_packet() {
150     packet_start_code_prefix                                24 bits
151     stream_id                                                8 bits
152     PES_packet_length                                        16 bits
153 
154     if (stream_id != program_stream_map 
155         && stream_id != padding_stream
156         && stream_id != private_stream_2
157         && stream_id != ECM
158         && stream_id != EMM
159         && stream_id != program_stream_directory
160         && stream_id !=    DSMCC_stream
161         && stream_id != ITU-T Rec.H.222.1 type E stream) {
162             '10'                                            2 bits
163             PES_scrambling_control                            2 bits
164             PES_priority                                    1 bit
165             data_alignment_indicator                        1 bit
166             copyright                                        1 bit
167             original_or_copy                                1 bit
168             
169             PTS_DTS_flags                                    2 bits
170             ESCR_flag                                        1 bit
171             ES_rate_flag                                    1 bit
172             DSM_trick_mode_flag                                1 bit
173             additional_copy_info_flag                        1 bit
174             PES_CRC_flag                                    1 bit
175             PES_extension_flag                                1 bit
176             
177             PES_header_data_length                            8 bits
178 
179             if (PTS_DTS_flags == '10') {
180                 '0010'                                        4 bits
181                 PTS[32..30]                                    3 bits
182                 marker_bit                                    1 bit
183                 PTS[29..15]                                    15 bits
184                 marker_bit                                    1 bit
185                 PTS[14..0]                                    15 bits
186                 marker_bit                                    1 bit
187             }
188             
189             if (PTS_DTS_flags == '11') {
190                 '0011'                                        4 bits
191                 PTS[32..30]                                    3 bits
192                 marker_bit                                    1 bit
193                 PTS[29..15]                                    15 bits
194                 marker_bit                                    1 bit
195                 PTS[14..0]                                    15 bits
196                 marker_bit                                    1 bit
197                 '0001'                                        4 bits
198                 PTS[32..30]                                    3 bits
199                 marker_bit                                    1 bit
200                 PTS[29..15]                                    15 bits
201                 marker_bit                                    1 bits
202                 PTS[14..0]                                    15 bits
203                 marker_bit                                    1 bit
204             }
205 
206             if (ESCR_flag == '1') {
207                 reserved                                    2 bits
208                 ESCR_base[32..30]                            3 bits
209                 marker_bit                                    1 bit
210                 ESCR_base[29..15]                            15 bits
211                 marker_bit                                    1 bit
212                 ESCR_base[14..0]                            15 bits
213                 marker_bit                                    1 bit
214                 ESCR_extension                                9 bits
215                 marker_bit                                    1 bit
216             }
217 
218             if (ES_rate_flag == '1') {
219                 marker_bit                                    1 bit
220                 ES_rate                                        22 bits
221                 marker_bit                                    1 bit
222             }
223 
224             if (DSM_trick_mode_flag == '1') {
225                 trick_mode_control                            3 bits
226 
227                 if (trick_mode_control == fast_forward) {
228                     field_id                                2 bits
229                     intra_slice_refresh                        1 bits
230                     frequency_truncation                    2 bits
231                 } else if (trick_mode_control == slow_motion) {
232                     rep_cntrl                                5 bits
233                 } else if (trick_mode _control == freeze_frame) {
234                     field_id                                2 bits
235                     reserved                                3 bits
236                 } else if (trick_mode _control == fast_reverse) {
237                     field_id                                2 bits
238                     intra_slice_refresh                        1 bit
239                     frequency_truncation                    2 bits
240                 } else if (trick_mode_control == slow_reverse) {
241                     rep_cntrl                                5 bits
242                 } else {
243                     reserved                                5 bits
244                 }
245             }
246 
247             if (additional_copy_info_flag =='1') {
248                 marker_bit                                    1 bit
249                 additional_copy_info                        7 bits
250             }
251 
252             if (PES_CRC_flag == ‘1’) {
253                 previous_PES_packet_CRC                        16 bits
254             }
255 
256             if (PES_extension_flag == '1') {
257                 PES_private_data_flag                        1 bit
258                 pack_header_field_flag                        1 bit
259                 program_packet_sequence_counter_flag        1 bit
260                 P-STD_buffer_flag                            1 bit
261                 reserved                                    3 bits
262                 PES_extension_flag_2                        1 bit
263 
264                 if (PES_private_data_flag == '1') {
265                     PES_private_data                        128 bits
266                 }
267 
268                 if (pack_header_field_flag == '1') {
269                     pack_field_length                        8 bits
270                     pack_header()
271                 }
272 
273                 if (program_packet_sequence_counter_flag == '1') {
274                     marker_bit                                1 bit
275                     program_packet_sequence_counter            7 bits
276                     marker-bit                                1 bit
277                     MPEG1_MPEG2_indentifier                    1 bit
278                     original_stuff_length                    6 bits
279                 }
280 
281                 if (P-STD_buffer_flag == '1') {
282                     '01'                                    2 bits
283                     P-STD_buffer_scale                        1 bit
284                     P-STD_buffer_size                        13 bits
285                 }
286 
287                 if (PES_extension_flag_2 == '1') {
288                     marker_bit                                1 bit
289                     PES_extension_field_length                7 bits
290 
291                     for (i=0; i<PES_extension_field_length; i++) {
292                         reserved                            8 bits
293                     }
294                 }
295             }
296 
297             for (i=0; i<N1; i++) {
298                 stuffing_byte                                    8 bits
299             }
300             
301             for (i=0; i<N2; i++) {
302                 PES_packet_data_byte                            8 bits
303             }
304     } else if (stream_id == program_stream_map
305                 || stream_id == private_stream_2
306                 || stream_id == ECM
307                 || stream_id == EMM
308                 || stream_id == program_stream_directory
309                 || stream_id == DSMCC_stream
310                 || stream_id == ITU-T Rec. H.222.1 type E stream ) {
311                     for (i=0; i<PES_packet_length; i++) {
312                         PES_packet_data_byte                    8 bits
313                     }
314     } else if (steam_id == padding_stream) {
315         for (i=0; i<PES_packet_length; i++) {
316             padding_byte                                        8 bits
317         }
318     }
319 }
320 
321 ** stream_id:
322         1011 1100        program_stream_map(0xBC)
323         1011 1101        private_stream_1(0xBD)
324         1011 1110        padding_stream(0xBE)
325         1011 1111        private_stream-2(0xBF)
326         110x xxxx        GB/T XXXX.3或GB/T AAAA.3音频流编号xxxx(0xC0~0xDF)
327         1110 xxxx        GB/T XXXX.2或GB/T AAAA.2视频流编号xxxx(0xE0~0xEF)
328         1111 0000        ECM_stream(0xF0)
329         1111 0001        EMM_stream(0xF1)
330         1111 0010        GB/T XXXX.1附录B或GB/T XXXX.6_DSMCC_stream(0xF2)
331         1111 0011        ISO/IEC_13522_stream(0xF3)
332         1111 0100        ITU-T Rec. H.222.1类型A
333         1111 0101        ITU-T Rec. H.222.1类型B
334         1111 0110        ITU-T Rec. H.222.1类型C
335         1111 0111        ITU-T Rec. H.222.1类型D
336         1111 1000        ITU-T Rec. H.222.1类型E
337         1111 1001        ancillary_stream(0xF9)
338         1111 1010…1111 1110        保留数据流
339         1111 1111        program_stream_directory(0xFF)
340         符号x表示值'0'或'1'均被允许且可产生相同的流类型. 流号码由x的取值决定.
341 **********************************************************************************************************/
342 typedef struct t_ps_pes
343 {
344     unsigned char streamId;
345     
346     long long pts;
347     long long dts;
348     
349     unsigned char ptsStr[MAX_PDTS_STRING_LEN+1];
350     unsigned char dtsStr[MAX_PDTS_STRING_LEN+1];
351     
352     unsigned char pesHeaderLen;
353 } T_PS_PES;
354 
355 static void parsePsHeader(unsigned char* const psHeaderData)
356 {
357     
358 }
359 
360 static void parsePsSystemHeader(unsigned char* const psSysHeaderData)
361 {
362 
363 }
364 
365 static void parsePsSystemMapHeader(unsigned char* const psMapHeaderData)
366 {
367     int i = 0;
368     
369     T_PS_MAP psMap = {0};
370     
371     unsigned char *data = NULL;
372     
373     data = psMapHeaderData;
374     
375     memset(&psMap, 0, sizeof(psMap));
376     
377     psMap.curNextInd = (data[0]>>7) & 0x1;
378     psMap.version = data[0] & 0x1f;
379     
380     data += 2;
381     
382     psMap.psInfoLen = (data[0] << 8) | data[1];
383     
384     data += psMap.psInfoLen;
385     
386     psMap.esStreamMapLen = (data[0] << 8) | data[1];
387     
388     psMap.esMapNum = psMap.esStreamMapLen / 4;
389     
390     for (i=0; i<psMap.esMapNum; i++)
391     {
392         if (i == MAX_ES_NUMS)
393         {
394             printf("now just save %d es info!\n", MAX_ES_NUMS);
395             
396             break;
397         }
398         
399         psMap.esMaps[i].streamType = data[0];
400         psMap.esMaps[i].esId = data[1];
401         psMap.esMaps[i].esInfoLen = (data[2] << 8) | data[3];
402         
403         data += (4+psMap.esMaps[i].esInfoLen);
404     }
405     
406 #ifdef PRINTF_DEBUG
407     int mNUm = 0;
408     
409     if (psMap.esMapNum > MAX_ES_NUMS)
410     {
411         mNUm = MAX_ES_NUMS;
412     }
413     
414     for (i=0; i<mNUm; i++)
415     {
416         printf("%s%sstreamNum: %d, streamType: %d, esId: %d\n",  TAB44, TAB44, i, psMap.esMaps[i].streamType, psMap.esMaps[i].esId);
417     }
418 #endif
419 }
420 
421 static void getPdts(unsigned char *pdtsData, long long *pdts, unsigned char *pdtsString)
422 {
423     int hour = 0;
424     int minute = 0;
425     int second = 0;
426     int msecond = 0;
427     
428     long long pts = 0;
429     long long pts2Ms = 0;
430 
431     unsigned char ptsStr[MAX_PDTS_STRING_LEN+1] = {0};
432     
433     /* 5个字节转33位的值 */
434     pts = (((pdtsData[0]>>1) & 0x7) << 30) | (pdtsData[1] << 22) | (((pdtsData[2]>>1) & 0x7f) << 15) | (pdtsData[3] << 7) | (pdtsData[4]>>1 & 0x7f);
435     
436     /* 90KHz, 1000ms/90 */
437     pts2Ms = pts/90;
438     
439     hour = pts2Ms/(60*60*1000);
440     minute = (pts2Ms - hour * (60*60*1000)) / (60*1000);
441     second = (pts2Ms - hour * (60*60*1000) - minute * (60*1000)) / 1000;
442     msecond = pts2Ms - hour * (60*60*1000) - minute * (60*1000) - second * 1000;
443     
444     sprintf(ptsStr, "%02d:%02d:%02d:%03d", hour, minute, second, msecond);
445     
446     ptsStr[MAX_PDTS_STRING_LEN] = '\0';
447     
448     memcpy(pdtsString, ptsStr, MAX_PDTS_STRING_LEN);
449     
450     *pdts = pts;
451 }
452 
453 /*********************************************************************************
454     startcode(24) + streamid(8) + pes_len(16) + {header: flag1(8) + flag2(8, pts标识在这儿) + header_len(8)} + {header_data(header_len, 若前面的flag有数据, 数据就在这儿)} + pes_data(pes_len-3-header_len)
455 **********************************************************************************/
456 static void parsePes(const unsigned char streamId, unsigned char* const pesData, const unsigned short pesLen)
457 {
458     unsigned char pts_dts_flag;
459     
460     static int audioNum = 0;
461     static int videoNum = 0;
462     static int privateNum = 0;
463     static int paddingNum = 0;
464     
465     unsigned char *data = NULL;
466     
467     unsigned char pts[MAX_PDTS_LEN+1] = {0};
468     unsigned char dts[MAX_PDTS_LEN+1] = {0};
469 
470     T_PS_PES psPes = {0};
471     
472     data = pesData;
473     
474     memset(&psPes, 0x0, sizeof(psPes));
475     
476     psPes.streamId = streamId;
477     
478     if (((streamId>=0xC0) && (streamId<=0xDF)) || ((streamId>=0xE0) && (streamId<=0xEF)))
479     {
480         pts_dts_flag = data[1]>>6 & 0x3;
481 
482         psPes.pesHeaderLen = data[2];
483         
484         data += 3;
485         
486         switch (pts_dts_flag)
487         {
488             case 0: /* 00, no pts, dts */
489                 break;
490             
491             case 2: /* 10, only pts*/
492                 memset(pts, 0x0, sizeof(pts));
493 
494                 memcpy(pts, data, MAX_PDTS_LEN);
495                 
496                 getPdts(pts, &psPes.pts, psPes.ptsStr);
497                 
498                 break;
499                 
500             case 3: /* 11 pts & dts*/
501                 memset(pts, 0x0, sizeof(pts));
502                 memset(dts, 0x0, sizeof(dts));
503 
504                 memcpy(pts, data, MAX_PDTS_LEN);
505                 memcpy(dts, data+MAX_PDTS_LEN, MAX_PDTS_LEN);
506                 
507                 getPdts(pts, &psPes.pts, psPes.ptsStr);
508                 getPdts(dts, &psPes.dts, psPes.dtsStr);
509 
510                 break;
511                 
512             default:
513                 break;    
514         }
515     }
516     
517     
518 #ifdef PRINTF_DEBUG
519     if ((streamId>=0xC0) && (streamId<=0xDF))
520     {
521         audioNum++;
522         
523         printf("%s%spes, Audio[%d], streamId: 0x%02X(%d), pesLength: %d, pesHeaderLen: %d", TAB44, TAB44, audioNum, streamId, streamId, pesLen, psPes.pesHeaderLen);
524         
525         if (2 == pts_dts_flag)
526         {
527             printf(", pts: %s(%lld)", psPes.ptsStr, psPes.pts);
528         }
529 
530         if (3 == pts_dts_flag)
531         {
532             printf(", pts: %s(%lld), dts: %s(%lld)", psPes.ptsStr, psPes.pts, psPes.dtsStr, psPes.dts);
533         }
534 
535         printf("\n");
536     }
537     else if ((streamId>=0xE0) && (streamId<=0xEF))
538     {
539         videoNum++;
540 
541         printf("%s%spes, Video[%d], streamId: 0x%02X(%d), pesLength: %d, pesHeaderLen: %d", TAB44, TAB44, videoNum, streamId, streamId, pesLen, psPes.pesHeaderLen);
542 
543         if (2 == pts_dts_flag)
544         {
545             printf(", pts: %s(%lld)", psPes.ptsStr, psPes.pts);
546         }
547         
548         if (3 == pts_dts_flag)
549         {
550             printf(", pts: %s(%lld), dts: %s(%lld)", psPes.ptsStr, psPes.pts, psPes.dtsStr, psPes.dts);
551         }
552         
553         printf("\n");
554     }
555     else if ((streamId==0xBD) || (streamId==0xBF))
556     {
557         privateNum++;
558         
559         printf("%s%spes, private[%d], streamId: 0x%02X(%d), pesLength: %d\n", TAB44, TAB44, privateNum, streamId, streamId, pesLen);
560     }
561     else if (streamId==0xBE)
562     {
563         paddingNum++;
564         
565         printf("%s%spes, padding[%d], streamId: 0x%02X(%d), pesLength: %d\n", TAB44, TAB44, privateNum, streamId, streamId, pesLen);
566     }
567     else
568     {
569         printf("%s%spes, streamId: 0x%02X(%d), pesLength: %d\n", TAB44, TAB44, streamId, streamId, pesLen);
570     }
571 #endif
572 }
573 
574 int main(int argc, char *argv[])
575 {
576     int readLen = 0;
577     int pack_stuffing_length = 0;
578     
579     unsigned int startCode = 0;
580     
581     unsigned short pesPacketLen = 0;
582     unsigned short psSystemHeaderLen = 0;
583     unsigned short psSystemMapHeaderLen = 0;
584     
585     unsigned char pesStreamId = 0;
586     
587     unsigned char sCodeData[MAX_PS_STARTCODE_LEN+1] = {0};
588     unsigned char psData[MMIN_PS_HEADER_LEN-3] = {0};
589     
590     unsigned char *pesData = NULL;
591 
592     FILE *fp = NULL;
593 
594     if (2 != argc)
595     {
596         printf("Usage: flvparse **.mpg\n");
597 
598         return -1;
599     }
600 
601     fp = fopen(argv[1], "rb");
602     if (!fp)
603     {
604         printf("open file[%s] error!\n", argv[1]);
605 
606         return -1;
607     }
608 
609     while (1)
610     {
611         readLen = fread(&startCode, MAX_PS_STARTCODE_LEN, 1, fp);
612         if (1 != readLen)
613         {
614             break;
615         }
616         
617         startCode = ntohl(startCode);
618 
619 #ifdef PRINTF_DEBUG
620         if (SCODE_PS_HEADER == startCode)
621         {
622             printf("+startCode: 0x%08X\n", startCode);
623         }
624         else
625         {
626             printf("%s+startCode: 0x%08X\n", TAB44, startCode);
627         }
628 #endif
629 
630         if ((0 != (startCode>>24 & 0xff)) && (0 != (startCode>>16 & 0xff)) && (1 != (startCode>>8 & 0xff)))
631         {
632             return -1;
633         }
634         
635         switch (startCode)
636         {
637             case SCODE_PS_HEADER:
638                 memset(psData, 0x0, sizeof(psData));
639                 
640                 readLen = fread(psData, 1, MMIN_PS_HEADER_LEN-4, fp);
641                 if ((MMIN_PS_HEADER_LEN-4) != readLen)
642                 {
643                     fclose(fp);
644                     
645                     return 0;
646                 }
647                 
648                 pack_stuffing_length = psData[MMIN_PS_HEADER_LEN-5] & 0x7;
649 
650                 fseek(fp, pack_stuffing_length, SEEK_CUR);
651                 
652                 break;
653 
654             case SCODE_PS_SYSTEM_HEADER:
655                 if (1 != fread(&psSystemHeaderLen, 2, 1, fp))
656                 {
657                     fclose(fp);
658 
659                     return 0;
660                 }
661                 
662                 psSystemHeaderLen = ntohs(psSystemHeaderLen);
663 
664                 fseek(fp, psSystemHeaderLen, SEEK_CUR);
665 
666                 break;
667 
668             case SCODE_PS_SYSTEM_MAP_HEADER:
669                 if (1 != fread(&psSystemMapHeaderLen, 2, 1, fp))
670                 {
671                     fclose(fp);
672 
673                     return 0;
674                 }
675                 
676                 psSystemMapHeaderLen = ntohs(psSystemMapHeaderLen);
677 
678                 pesData = (unsigned char*)malloc(psSystemMapHeaderLen);
679                 if (pesData)
680                 {
681                     memset(pesData, 0x0, pesPacketLen);
682                     
683                     if (psSystemMapHeaderLen != fread(pesData, 1, psSystemMapHeaderLen, fp))
684                     {
685                         fclose(fp);
686 
687                         return 0;
688                     }
689                     
690                     parsePsSystemMapHeader(pesData);
691                     
692                     free(pesData);
693                     
694                     pesData = NULL;
695                 }
696                 
697                 break;
698                 
699             case SCODE_PS_END:
700 #ifdef PRINTF_DEBUG
701                 printf("ps is end!\n");
702 #endif
703                 
704                 return 0;
705                 
706                 break;
707 
708             /* pes pcaket */
709             default:
710                 pesStreamId = startCode & 0xff;
711                 
712                 if (1 != fread(&pesPacketLen, 2, 1, fp))
713                 {
714                     fclose(fp);
715 
716                     return 0;
717                 }
718                 
719                 pesPacketLen = ntohs(pesPacketLen);
720                 
721                 pesData = (unsigned char*)malloc(pesPacketLen);
722                 if (pesData)
723                 {
724                     memset(pesData, 0x0, pesPacketLen);
725                     
726                     if (pesPacketLen != fread(pesData, 1, pesPacketLen, fp))
727                     {
728                         fclose(fp);
729 
730                         return 0;
731                     }
732                     
733                     parsePes(pesStreamId, pesData, pesPacketLen);
734                     
735                     free(pesData);
736                     
737                     pesData = NULL;
738                 }
739                 
740                 break;
741         }
742     }
743 
744     fclose(fp);
745     
746     return 0;
747 }
View Code

   最后如果您觉得本篇对您有帮助,可以打赏下,谢谢!!!

posted on 2019-03-07 09:31  leaffei  阅读(1264)  评论(0编辑  收藏  举报

导航