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