2 * dvb-mpegtools for the Siemens Fujitsu DVB PCI card
4 * Copyright (C) 2000, 2001 Marcus Metzler
5 * for convergence integrated media GmbH
6 * Copyright (C) 2002, 2003 Marcus Metzler
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.
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.
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
26 * The author can be reached at mocm@metzlerbros.de
34 #include <netinet/in.h>
36 #include <sys/types.h>
39 #include <sys/ioctl.h>
43 #include <sys/param.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
49 #include "ringbuffy.h"
50 #include "transform.h"
55 #define VIDEO_MODE_PAL 0
56 #define VIDEO_MODE_NTSC 1
60 #endif /* __cplusplus */
62 enum {PS_STREAM, TS_STREAM, PES_STREAM};
63 enum {pDUNNO, pPAL, pNTSC};
65 uint64_t trans_pts_dts(uint8_t *pts);
71 #define PROG_STREAM_MAP 0xBC
72 #ifndef PRIVATE_STREAM1
73 #define PRIVATE_STREAM1 0xBD
75 #define PADDING_STREAM 0xBE
76 #ifndef PRIVATE_STREAM2
77 #define PRIVATE_STREAM2 0xBF
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
89 #define BUFFYSIZE 10*MAX_PLENGTH
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)
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
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
117 #define PTS_ONLY 0x80
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
127 #define MPEG1_2_ID 0x40
128 #define STFF_LNGTH_MASK 0x3F
131 typedef struct pes_packet_{
144 uint8_t prev_pes_crc[2];
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;
152 uint8_t pes_ext_lngth;
154 uint8_t *pes_pckt_data;
158 uint8_t *mpeg1_headr;
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);
176 #define TRANS_ERROR 0x80
177 #define PAY_START 0x40
178 #define TRANS_PRIO 0x20
179 #define PID_MASK_HI 0x1F
181 #define TRANS_SCRMBL1 0x80
182 #define TRANS_SCRMBL2 0x40
183 #define ADAPT_FIELD 0x20
185 #define COUNT_MASK 0x0F
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
197 // adaptation extension flags
198 #define LTW_FLAG 0x80
199 #define PIECE_RATE 0x40
200 #define SEAM_SPLICE 0x20
202 typedef struct ts_packet_{
207 uint8_t adapt_length;
211 uint8_t splice_count;
212 uint8_t priv_dat_len;
214 uint8_t adapt_ext_len;
215 uint8_t adapt_eflags;
217 uint8_t piece_rate[3];
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);
236 #define PACK_STUFF_MASK 0x07
238 #define FIXED_FLAG 0x02
239 #define CSPS_FLAG 0x01
240 #define SAUDIO_LOCK_FLAG 0x80
241 #define SVIDEO_LOCK_FLAG 0x40
245 typedef struct ps_packet_{
248 uint8_t stuff_length;
250 uint8_t sheader_llength[2];
252 uint8_t rate_bound[3];
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);
274 #define MAX_PLENGTH 0xFFFF
276 typedef struct sectionstruct {
280 uint8_t payload[4096+3];
284 typedef uint32_t tflags;
287 typedef struct trans_struct {
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];
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);
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);
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);
323 typedef struct a2pstruct{
332 uint8_t abuf[MAX_PLENGTH];
334 uint8_t vbuf[MAX_PLENGTH];
336 uint8_t last_av_pts[4];
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);
355 typedef struct p2t_s{
356 uint8_t pes[TS_SIZE];
360 void (*t_out)(uint8_t const *buf);
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 *));
371 int write_pes_header(uint8_t id,int length , long PTS,
372 uint8_t *obuf, int stuffing);
374 int write_ps_header(uint8_t *buf,
384 uint8_t buffer1_scale,
385 uint32_t buffer1_size,
387 uint8_t buffer2_scale,
388 uint32_t buffer2_size);
391 int seek_mpg_start(uint8_t *buf, int size);
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);
399 const char * strerrno(void);
402 #endif /* __cplusplus */
404 #endif /*_CTOOLS_H_*/