1 /*
2  * pes.h: MPEG PES functions for replex
3  *
4  *
5  * Copyright (C) 2003 - 2006
6  *                    Marcus Metzler <mocm@metzlerbros.de>
7  *                    Metzler Brothers Systementwicklung GbR
8  *           (C) 2006 Reel Multimedia
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * General Public License for more details.
20  *
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25  * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
26  *
27  */
28 
29 
30 #ifndef _PES_H_
31 #define _PES_H_
32 
33 #include <inttypes.h>
34 #include "ringbuffer.h"
35 
36 #define PS_HEADER_L1    14
37 #define PS_HEADER_L2    (PS_HEADER_L1+24)
38 #define PES_MIN         7
39 #define PES_H_MIN       9
40 
41 #define SYSTEM_START_CODE_S   0xB9
42 #define SYSTEM_START_CODE_E   0xFF
43 #define PACK_START            0xBA
44 #define SYS_START             0xBB
45 #define PROG_STREAM_MAP       0xBC
46 #define PRIVATE_STREAM1       0xBD
47 #define PADDING_STREAM        0xBE
48 #define PRIVATE_STREAM2       0xBF
49 #define AUDIO_STREAM_S        0xC0
50 #define AUDIO_STREAM_E        0xDF
51 #define VIDEO_STREAM_S        0xE0
52 #define VIDEO_STREAM_E        0xEF
53 #define ECM_STREAM            0xF0
54 #define EMM_STREAM            0xF1
55 #define DSM_CC_STREAM         0xF2
56 #define ISO13522_STREAM       0xF3
57 #define PROG_STREAM_DIR       0xFF
58 
59 //flags2
60 #define PTS_DTS_FLAGS    0xC0
61 #define ESCR_FLAG        0x20
62 #define ES_RATE_FLAG     0x10
63 #define DSM_TRICK_FLAG   0x08
64 #define ADD_CPY_FLAG     0x04
65 #define PES_CRC_FLAG     0x02
66 #define PES_EXT_FLAG     0x01
67 
68 //pts_dts flags
69 #define PTS_ONLY         0x80
70 #define PTS_DTS          0xC0
71 
72 #define MAX_PLENGTH 0xFFFF
73 #define MMAX_PLENGTH (8*MAX_PLENGTH)
74 
75 #define MAX_PTS (0x0000000200000000ULL)
76 #define MAX_PTS2 (300* MAX_PTS)
77 
78 typedef
79 struct ps_packet_{
80 	uint8_t scr[6];
81 	uint8_t mux_rate[3];
82 	uint8_t stuff_length;
83 	uint8_t *data;
84 	uint8_t sheader_llength[2];
85 	int sheader_length;
86 	uint8_t rate_bound[3];
87 	uint8_t audio_bound;
88 	uint8_t video_bound;
89 	uint8_t reserved;
90 	int npes;
91 } ps_packet;
92 
93 
94 typedef
95 struct pes_in_s{
96 	int type;
97 	int found;
98 	int withbuf;
99 	uint8_t *buf;
100 	ringbuffer *rbuf;
101 	uint8_t hbuf[260];
102 	int ini_pos;
103 	uint8_t cid;
104 	uint32_t plength;
105 	uint8_t plen[4];
106 	uint8_t flag1;
107 	uint8_t flag2;
108 	uint8_t hlength;
109 	uint8_t pts[5];
110 	uint8_t dts[5];
111 	int mpeg;
112 	int done;
113 	int which;
114 	void *priv;
115 } pes_in_t;
116 
117 
118 void init_pes_in(pes_in_t *p, int type, ringbuffer *rb, int wi);
119 void get_pes (pes_in_t *p, uint8_t *buf, int count, void (*func)(pes_in_t *p));
120 void printpts(int64_t pts);
121 void printptss(int64_t pts);
122 int64_t ptsdiff(uint64_t pts1, uint64_t pts2);
123 uint64_t uptsdiff(uint64_t pts1, uint64_t pts2);
124 int ptscmp(uint64_t pts1, uint64_t pts2);
125 uint64_t ptsadd(uint64_t pts1, uint64_t pts2);
126 
127 
128 void write_padding_pes( int pack_size, int apidn, int ac3n,
129 			uint64_t SCR, uint64_t muxr, uint8_t *buf);
130 int write_ac3_pes(  int pack_size, int apidn, int ac3n, int n, uint64_t pts,
131 		    uint64_t SCR,
132 		    uint32_t muxr, uint8_t *buf, int *alength, uint8_t ptsdts,
133 		    int nframes,int ac3_off, ringbuffer *ac3rbuffer, int framelength);
134 int bwrite_ac3_pes(  int pack_size, int apidn, int ac3n, int n, uint64_t pts,
135 		    uint64_t SCR,
136 		    uint32_t muxr, uint8_t *buf, int *alength, uint8_t ptsdts,
137 		     int nframes,int ac3_off, uint8_t *ac3rbuffer, int bsize, int framelength);
138 int write_audio_pes(  int pack_size, int apidn, int ac3n, int n, uint64_t pts,
139 		      uint64_t SCR, uint32_t muxr, uint8_t *buf, int *alength,
140 		      uint8_t ptsdts, 	ringbuffer *arbuffer);
141 int bwrite_audio_pes(  int pack_size, int apidn, int ac3n, int n, uint64_t pts,
142 		      uint64_t SCR, uint32_t muxr, uint8_t *buf, int *alength,
143 		       uint8_t ptsdts, uint8_t *arbuffer, int bsize );
144 int write_video_pes( int pack_size, int apidn, int ac3n, uint64_t vpts,
145 		     uint64_t vdts, uint64_t SCR, uint64_t muxr,
146 		     uint8_t *buf, int *vlength,
147 		     uint8_t ptsdts, ringbuffer *vrbuffer);
148 int write_nav_pack(int pack_size, int apidn, int ac3n, uint64_t SCR, uint32_t muxr,
149 		   uint8_t *buf);
150 
ptsdec(uint64_t * pts1,uint64_t pts2)151 static inline void ptsdec(uint64_t *pts1, uint64_t pts2)
152 {
153 	*pts1= uptsdiff(*pts1, pts2);
154 }
155 
ptsinc(uint64_t * pts1,uint64_t pts2)156 static inline void ptsinc(uint64_t *pts1, uint64_t pts2)
157 {
158 	*pts1 = (*pts1 + pts2)%MAX_PTS2;
159 }
160 
161 
162 
163 
164 #endif /*_PES_H_*/
165