]> git.vomp.tv Git - vompserver.git/blob - libdvbmpeg/ctools.h
Initial import
[vompserver.git] / libdvbmpeg / ctools.h
1 /*
2  *  dvb-mpegtools for the Siemens Fujitsu DVB PCI card
3  *
4  * Copyright (C) 2000, 2001 Marcus Metzler 
5  *            for convergence integrated media GmbH
6  * Copyright (C) 2002, 2003 Marcus Metzler 
7  * 
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  * 
13
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  * 
19
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23  * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
24  * 
25
26  * The author can be reached at mocm@metzlerbros.de
27
28  */
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <errno.h>
33 #include <unistd.h>
34 #include <netinet/in.h>
35 #include <string.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <fcntl.h>
39 #include <sys/ioctl.h>
40 #include <libgen.h>
41 #include <stdint.h>
42 #include <netdb.h>
43 #include <sys/param.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
47
48
49 #include "ringbuffy.h"
50 #include "transform.h"
51
52 #ifndef _CTOOLS_H_
53 #define _CTOOLS_H_
54
55 #define VIDEO_MODE_PAL          0
56 #define VIDEO_MODE_NTSC         1
57
58 #ifdef __cplusplus
59 extern "C" {
60 #endif                          /* __cplusplus */
61         
62         enum {PS_STREAM, TS_STREAM, PES_STREAM};
63         enum {pDUNNO, pPAL, pNTSC};
64         
65         uint64_t trans_pts_dts(uint8_t *pts);
66
67 /*
68   PES
69 */
70
71 #define PROG_STREAM_MAP  0xBC
72 #ifndef PRIVATE_STREAM1
73 #define PRIVATE_STREAM1  0xBD
74 #endif
75 #define PADDING_STREAM   0xBE
76 #ifndef PRIVATE_STREAM2
77 #define PRIVATE_STREAM2  0xBF
78 #endif
79 #define AUDIO_STREAM_S   0xC0
80 #define AUDIO_STREAM_E   0xDF
81 #define VIDEO_STREAM_S   0xE0
82 #define VIDEO_STREAM_E   0xEF
83 #define ECM_STREAM       0xF0
84 #define EMM_STREAM       0xF1
85 #define DSM_CC_STREAM    0xF2
86 #define ISO13522_STREAM  0xF3
87 #define PROG_STREAM_DIR  0xFF
88
89 #define BUFFYSIZE    10*MAX_PLENGTH
90 #define MAX_PTS      8192
91 #define MAX_FRAME    8192
92 #define MAX_PACK_L   4096
93 #define PS_HEADER_L1    14
94 #define PS_HEADER_L2    (PS_HEADER_L1+18)
95 #define MAX_H_SIZE   (PES_H_MIN + PS_HEADER_L1 + 5)
96 #define PES_MIN         7
97 #define PES_H_MIN       9
98
99 //flags1
100 #define FLAGS            0x40
101 #define SCRAMBLE_FLAGS   0x30
102 #define PRIORITY_FLAG    0x08
103 #define DATA_ALIGN_FLAG  0x04
104 #define COPYRIGHT_FLAG   0x02
105 #define ORIGINAL_FLAG    0x01
106
107 //flags2
108 #define PTS_DTS_FLAGS    0xC0
109 #define ESCR_FLAG        0x20
110 #define ES_RATE_FLAG     0x10
111 #define DSM_TRICK_FLAG   0x08
112 #define ADD_CPY_FLAG     0x04
113 #define PES_CRC_FLAG     0x02
114 #define PES_EXT_FLAG     0x01
115
116 //pts_dts flags 
117 #define PTS_ONLY         0x80
118 #define PTS_DTS          0xC0
119
120 //private flags
121 #define PRIVATE_DATA     0x80
122 #define HEADER_FIELD     0x40
123 #define PACK_SEQ_CTR     0x20
124 #define P_STD_BUFFER     0x10
125 #define PES_EXT_FLAG2    0x01
126
127 #define MPEG1_2_ID       0x40
128 #define STFF_LNGTH_MASK  0x3F
129
130
131         typedef struct pes_packet_{
132                 uint8_t stream_id;
133                 uint8_t llength[2];
134                 uint32_t length;
135                 uint8_t flags1;
136                 uint8_t flags2;
137                 uint8_t pes_hlength;
138                 uint8_t pts[5];
139                 uint8_t dts[5];
140                 uint8_t escr[6];
141                 uint8_t es_rate[3];
142                 uint8_t trick;
143                 uint8_t add_cpy;
144                 uint8_t prev_pes_crc[2];
145                 uint8_t priv_flags;
146                 uint8_t pes_priv_data[16];
147                 uint8_t pack_field_length;
148                 uint8_t *pack_header;
149                 uint8_t pck_sqnc_cntr;
150                 uint8_t org_stuff_length;
151                 uint8_t p_std[2];
152                 uint8_t pes_ext_lngth;
153                 uint8_t *pes_ext;
154                 uint8_t *pes_pckt_data;
155                 int padding;
156                 int mpeg;
157                 int mpeg1_pad;
158                 uint8_t *mpeg1_headr;
159                 uint8_t stuffing;
160         } pes_packet;
161
162         void init_pes(pes_packet *p);
163         void kill_pes(pes_packet *p);
164         void setlength_pes(pes_packet *p);
165         void nlength_pes(pes_packet *p);
166         int cwrite_pes(uint8_t *buf, pes_packet *p, long length);
167         void write_pes(int fd, pes_packet *p);
168         int read_pes(int f, pes_packet *p);
169         void cread_pes(char *buf, pes_packet *p);
170
171 /*
172    Transport Stream
173 */
174
175 #define TS_SIZE        188
176 #define TRANS_ERROR    0x80
177 #define PAY_START      0x40
178 #define TRANS_PRIO     0x20
179 #define PID_MASK_HI    0x1F
180 //flags
181 #define TRANS_SCRMBL1  0x80
182 #define TRANS_SCRMBL2  0x40
183 #define ADAPT_FIELD    0x20
184 #define PAYLOAD        0x10
185 #define COUNT_MASK     0x0F
186
187 // adaptation flags
188 #define DISCON_IND     0x80
189 #define RAND_ACC_IND   0x40
190 #define ES_PRI_IND     0x20
191 #define PCR_FLAG       0x10
192 #define OPCR_FLAG      0x08
193 #define SPLICE_FLAG    0x04
194 #define TRANS_PRIV     0x02
195 #define ADAP_EXT_FLAG  0x01
196
197 // adaptation extension flags
198 #define LTW_FLAG       0x80
199 #define PIECE_RATE     0x40
200 #define SEAM_SPLICE    0x20
201
202         typedef struct  ts_packet_{
203                 uint8_t pid[2];
204                 uint8_t flags;
205                 uint8_t count;
206                 uint8_t data[184];
207                 uint8_t adapt_length;
208                 uint8_t adapt_flags;
209                 uint8_t pcr[6];
210                 uint8_t opcr[6];
211                 uint8_t splice_count;
212                 uint8_t priv_dat_len;
213                 uint8_t *priv_dat;
214                 uint8_t adapt_ext_len;
215                 uint8_t adapt_eflags;
216                 uint8_t ltw[2];
217                 uint8_t piece_rate[3];
218                 uint8_t dts[5];
219                 int rest;
220                 uint8_t stuffing;
221         } ts_packet;
222
223         void init_ts(ts_packet *p);
224         void kill_ts(ts_packet *p);
225         unsigned short pid_ts(ts_packet *p);
226         int cwrite_ts(uint8_t *buf, ts_packet *p, long length);
227         void write_ts(int fd, ts_packet *p);
228         int read_ts(int f, ts_packet *p);
229         void cread_ts (char *buf, ts_packet *p, long length);
230
231
232 /*
233    Program Stream
234 */
235
236 #define PACK_STUFF_MASK  0x07
237
238 #define FIXED_FLAG       0x02
239 #define CSPS_FLAG        0x01
240 #define SAUDIO_LOCK_FLAG 0x80
241 #define SVIDEO_LOCK_FLAG 0x40
242
243 #define PS_MAX 200
244
245         typedef struct ps_packet_{
246                 uint8_t scr[6];
247                 uint8_t mux_rate[3];
248                 uint8_t stuff_length;
249                 uint8_t *data;
250                 uint8_t sheader_llength[2];
251                 int sheader_length;
252                 uint8_t rate_bound[3];
253                 uint8_t audio_bound;
254                 uint8_t video_bound;
255                 uint8_t reserved;
256                 int npes;
257                 int mpeg;
258         } ps_packet;
259
260         void init_ps(ps_packet *p);
261         void kill_ps(ps_packet *p);
262         void setlength_ps(ps_packet *p);
263         uint32_t scr_base_ps(ps_packet *p);
264         uint16_t scr_ext_ps(ps_packet *p);
265         int mux_ps(ps_packet *p);
266         int rate_ps(ps_packet *p);
267         int cwrite_ps(uint8_t *buf, ps_packet *p, long length);
268         void write_ps(int fd, ps_packet *p);
269         int read_ps (int f, ps_packet *p);
270         void cread_ps (char *buf, ps_packet *p, long length);
271
272
273
274 #define MAX_PLENGTH 0xFFFF
275
276         typedef struct sectionstruct {
277                 int  id;
278                 int length;
279                 int found;
280                 uint8_t payload[4096+3];
281         } section;
282
283
284         typedef uint32_t tflags;
285 #define MAXFILT 32
286 #define MASKL 16
287         typedef struct trans_struct {
288                 int found;
289                 uint8_t packet[188];
290                 uint16_t pid[MAXFILT];
291                 uint8_t mask[MAXFILT*MASKL];
292                 uint8_t filt[MAXFILT*MASKL];
293                 uint8_t transbuf[MAXFILT*188];
294                 int transcount[MAXFILT];
295                 section sec[MAXFILT];
296                 tflags is_full;
297                 tflags pes_start;
298                 tflags pes_started;
299                 tflags pes;
300                 tflags set;
301         } trans;
302
303
304         void init_trans(trans *p);
305         int set_trans_filt(trans *p, int filtn, uint16_t pid, uint8_t *mask, 
306                            uint8_t *filt, int pes);
307
308         void clear_trans_filt(trans *p,int filtn);
309         int filt_is_set(trans *p, int filtn);
310         int pes_is_set(trans *p, int filtn);
311         int pes_is_started(trans *p, int filtn);
312         int pes_is_start(trans *p, int filtn);
313         int filt_is_ready(trans *p,int filtn);
314
315         void trans_filt(uint8_t *buf, int count, trans *p);
316         void tfilter(trans *p);
317         void pes_filter(trans *p, int filtn, int off);
318         void sec_filter(trans *p, int filtn, int off);
319         int get_filt_buf(trans *p, int filtn,uint8_t **buf); 
320         section *get_filt_sec(trans *p, int filtn); 
321
322
323         typedef struct a2pstruct{
324                 int type;
325                 int fd;
326                 int found;
327                 int length;
328                 int headr;
329                 int plength;
330                 uint8_t cid;
331                 uint8_t flags;
332                 uint8_t abuf[MAX_PLENGTH];
333                 int alength;
334                 uint8_t vbuf[MAX_PLENGTH];
335                 int vlength;
336                 uint8_t last_av_pts[4];
337                 uint8_t av_pts[4];
338                 uint8_t scr[4];
339                 uint8_t pid0;
340                 uint8_t pid1;
341                 uint8_t pidv;
342                 uint8_t pida;
343         } a2p;
344
345
346
347         void get_pespts(uint8_t *av_pts,uint8_t *pts);
348         void init_a2p(a2p *p);
349         void av_pes_to_pes(uint8_t *buf,int count, a2p *p);
350         int w_pesh(uint8_t id,int length ,uint8_t *pts, uint8_t *obuf);
351         int w_tsh(uint8_t id,int length ,uint8_t *pts, uint8_t *obuf,a2p *p,int startpes);
352         void pts2pts(uint8_t *av_pts, uint8_t *pts);
353         void write_ps_headr(ps_packet *p,uint8_t *pts,int fd);
354
355         typedef struct p2t_s{
356                 uint8_t           pes[TS_SIZE];
357                 uint8_t           counter;
358                 long int     pos;
359                 int          frags;
360                 void         (*t_out)(uint8_t const *buf);
361         } p2t_t;
362
363         void twrite(uint8_t const *buf);
364         void init_p2t(p2t_t *p, void (*fkt)(uint8_t const *buf));
365         long int find_pes_header(uint8_t const *buf, long int length, int *frags);
366         void pes_to_ts( uint8_t const *buf, long int length, uint16_t pid, p2t_t *p);
367         void p_to_t( uint8_t const *buf, long int length, uint16_t pid, 
368                      uint8_t *counter, void (*ts_write)(uint8_t const *));
369
370
371         int write_pes_header(uint8_t id,int length , long PTS, 
372                              uint8_t *obuf, int stuffing);
373
374         int write_ps_header(uint8_t *buf, 
375                             uint32_t   SCR, 
376                             long  muxr,
377                             uint8_t    audio_bound,
378                             uint8_t    fixed,
379                             uint8_t    CSPS,
380                             uint8_t    audio_lock,
381                             uint8_t    video_lock,
382                             uint8_t    video_bound,
383                             uint8_t    stream1,
384                             uint8_t    buffer1_scale,
385                             uint32_t   buffer1_size,
386                             uint8_t    stream2,
387                             uint8_t    buffer2_scale,
388                             uint32_t   buffer2_size);                    
389         
390
391         int seek_mpg_start(uint8_t *buf, int size);
392
393
394         void split_mpg(char *name, uint64_t size);
395         void cut_mpg(char *name, uint64_t size);
396         int http_open (char *url);
397         ssize_t save_read(int fd, void *buf, size_t count);
398
399         const char * strerrno(void);
400 #ifdef __cplusplus
401 }
402 #endif                          /* __cplusplus */
403
404 #endif /*_CTOOLS_H_*/