Blame view

gen28181ps/main.cpp 1.38 KB
3d2ab595   Hu Chunming   支持gb28181
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
  
  #include <stdlib.h>
  #include "ps.h"
   
  int ReadSourceStream(const char* infile, char **frame_data, int *frame_size)
  {
  	static FILE *fp = NULL; 
  	if (fp == NULL)
  	{
  		// fp = fopen("es_stream.h264", "rb");
          fp = fopen(infile, "rb");
  		if (fp == NULL)
  		{
  			printf("open file es_stream.h264 error\n");
  			return -1;
  		}
  	}
   
  	//文件格式为:
  	//     1    2    3    4         n
  	//|----|----|----|----|---------
  	//|       帧长度       |   数据   
  	do {
  		if (4 != fread(frame_size, 1, 4, fp))
  			break;
  		*frame_data = (char*)malloc(*frame_size);
  		if (*frame_size != fread(*frame_data, 1, *frame_size, fp))
  			break;
  		return 0;
  	} while (0);
  	printf("read file es_stream.h264 eof\n");
  	fclose(fp);
  	fp = NULL;
  	return -2;
  }
   
  int KeyFrame(uint8_t nalu)
  {
  	int nalType = nalu & 0x1f;
  	if (nalType == 0x07 || nalType == 0x05 || nalType == 0x08) 
  		return 1;
  	return 0;
  }
   
  int main0()
  {
      const char* infile = "/mnt/f/fiss/test_data/test.h264";
  	char *frame_data;
  	int frame_size, ps_size;
  	uint32_t ts = 0;
   
  	FILE *fp = fopen("test.ps", "wb");
  	while (0 == ReadSourceStream(infile, &frame_data, &frame_size))
  	{
  		// 文件中只有h264视频,第4个参数固定填1,音频为2
  		ps_size = TransPSFrame(frame_data, frame_size, KeyFrame((uint8_t)frame_data[4]), 1, ts);
  		fwrite(PSFrameBuffer, 1, ps_size, fp);
  		ts += 3600;
  		free(frame_data);
  	}
  	fclose(fp);
  	// getchar();
      return 0;
  }