Statistics
| Branch: | Tag: | Revision:

dvbd / ctools.h @ bfdb7446

History | View | Annotate | Download (9.05 KB)

1
/*
2
 *  mpegtools for the Siemens Fujitsu DVB PCI card
3
 *
4
 * Copyright (C) 2000, 2001 Marcus Metzler 
5
 *            for convergence integrated media GmbH
6
 * 
7
 * This program is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU General Public License
9
 * as published by the Free Software Foundation; either version 2
10
 * of the License, or (at your option) any later version.
11
 * 
12

13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 * 
18

19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22
 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
23
 * 
24

25
 * The author can be reached at marcus@convergence.de, 
26

27
 * the project's page is at http://linuxtv.org/dvb/
28
 */
29

    
30
#include <unistd.h>
31
#include <fcntl.h>
32
#include <stdio.h>
33
#include <stdlib.h>
34
#include <errno.h>
35
#include <netinet/in.h>
36
#include <string.h>
37
#include <sys/types.h>
38
#include <sys/stat.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
        typedef uint8_t u8;
63
        typedef uint16_t u16;
64
        typedef uint32_t u32;
65
        typedef uint64_t u64;
66
        
67
        
68
        enum {PS_STREAM, TS_STREAM, PES_STREAM};
69
        enum {pDUNNO, pPAL, pNTSC};
70

    
71
        u32 trans_pts_dts(u8 *pts);
72

    
73
/*
74
  PES
75
*/
76

    
77
#define PROG_STREAM_MAP  0xBC
78
#ifndef PRIVATE_STREAM1
79
#define PRIVATE_STREAM1  0xBD
80
#endif
81
#define PADDING_STREAM   0xBE
82
#ifndef PRIVATE_STREAM2
83
#define PRIVATE_STREAM2  0xBF
84
#endif
85
#define AUDIO_STREAM_S   0xC0
86
#define AUDIO_STREAM_E   0xDF
87
#define VIDEO_STREAM_S   0xE0
88
#define VIDEO_STREAM_E   0xEF
89
#define ECM_STREAM       0xF0
90
#define EMM_STREAM       0xF1
91
#define DSM_CC_STREAM    0xF2
92
#define ISO13522_STREAM  0xF3
93
#define PROG_STREAM_DIR  0xFF
94

    
95
#define BUFFYSIZE    10*MAX_PLENGTH
96
#define MAX_PTS      8192
97
#define MAX_FRAME    8192
98
#define MAX_PACK_L   4096
99
#define PS_HEADER_L1    14
100
#define PS_HEADER_L2    (PS_HEADER_L1+18)
101
#define MAX_H_SIZE   (PES_H_MIN + PS_HEADER_L1 + 5)
102
#define PES_MIN         7
103
#define PES_H_MIN       9
104

    
105
//flags1
106
#define FLAGS            0x40
107
#define SCRAMBLE_FLAGS   0x30
108
#define PRIORITY_FLAG    0x08
109
#define DATA_ALIGN_FLAG  0x04
110
#define COPYRIGHT_FLAG   0x02
111
#define ORIGINAL_FLAG    0x01
112

    
113
//flags2
114
#define PTS_DTS_FLAGS    0xC0
115
#define ESCR_FLAG        0x20
116
#define ES_RATE_FLAG     0x10
117
#define DSM_TRICK_FLAG   0x08
118
#define ADD_CPY_FLAG     0x04
119
#define PES_CRC_FLAG     0x02
120
#define PES_EXT_FLAG     0x01
121

    
122
//pts_dts flags 
123
#define PTS_ONLY         0x80
124
#define PTS_DTS          0xC0
125

    
126
//private flags
127
#define PRIVATE_DATA     0x80
128
#define HEADER_FIELD     0x40
129
#define PACK_SEQ_CTR     0x20
130
#define P_STD_BUFFER     0x10
131
#define PES_EXT_FLAG2    0x01
132

    
133
#define MPEG1_2_ID       0x40
134
#define STFF_LNGTH_MASK  0x3F
135

    
136

    
137
        typedef struct pes_packet_{
138
                u8 stream_id;
139
                u8 llength[2];
140
                uint32_t length;
141
                u8 flags1;
142
                u8 flags2;
143
                u8 pes_hlength;
144
                u8 pts[5];
145
                u8 dts[5];
146
                u8 escr[6];
147
                u8 es_rate[3];
148
                u8 trick;
149
                u8 add_cpy;
150
                u8 prev_pes_crc[2];
151
                u8 priv_flags;
152
                u8 pes_priv_data[16];
153
                u8 pack_field_length;
154
                u8 *pack_header;
155
                u8 pck_sqnc_cntr;
156
                u8 org_stuff_length;
157
                u8 p_std[2];
158
                u8 pes_ext_lngth;
159
                u8 *pes_ext;
160
                u8 *pes_pckt_data;
161
                int padding;
162
                int mpeg;
163
                int mpeg1_pad;
164
                u8 *mpeg1_headr;
165
                u8 stuffing;
166
        } pes_packet;
167

    
168
        void init_pes(pes_packet *p);
169
        void kill_pes(pes_packet *p);
170
        void setlength_pes(pes_packet *p);
171
        void nlength_pes(pes_packet *p);
172
        int cwrite_pes(u8 *buf, pes_packet *p, long length);
173
        void write_pes(int fd, pes_packet *p);
174
        int read_pes(int f, pes_packet *p);
175
        void cread_pes(char *buf, pes_packet *p);
176

    
177
/*
178
   Transport Stream
179
*/
180

    
181
#define TS_SIZE        188
182
#define TRANS_ERROR    0x80
183
#define PAY_START      0x40
184
#define TRANS_PRIO     0x20
185
#define PID_MASK_HI    0x1F
186
//flags
187
#define TRANS_SCRMBL1  0x80
188
#define TRANS_SCRMBL2  0x40
189
#define ADAPT_FIELD    0x20
190
#define PAYLOAD        0x10
191
#define COUNT_MASK     0x0F
192

    
193
// adaptation flags
194
#define DISCON_IND     0x80
195
#define RAND_ACC_IND   0x40
196
#define ES_PRI_IND     0x20
197
#define PCR_FLAG       0x10
198
#define OPCR_FLAG      0x08
199
#define SPLICE_FLAG    0x04
200
#define TRANS_PRIV     0x02
201
#define ADAP_EXT_FLAG  0x01
202

    
203
// adaptation extension flags
204
#define LTW_FLAG       0x80
205
#define PIECE_RATE     0x40
206
#define SEAM_SPLICE    0x20
207

    
208
        typedef struct  ts_packet_{
209
                u8 pid[2];
210
                u8 flags;
211
                u8 count;
212
                u8 data[184];
213
                u8 adapt_length;
214
                u8 adapt_flags;
215
                u8 pcr[6];
216
                u8 opcr[6];
217
                u8 splice_count;
218
                u8 priv_dat_len;
219
                u8 *priv_dat;
220
                u8 adapt_ext_len;
221
                u8 adapt_eflags;
222
                u8 ltw[2];
223
                u8 piece_rate[3];
224
                u8 dts[5];
225
                int rest;
226
                u8 stuffing;
227
        } ts_packet;
228

    
229
        void init_ts(ts_packet *p);
230
        void kill_ts(ts_packet *p);
231
        unsigned short pid_ts(ts_packet *p);
232
        int cwrite_ts(u8 *buf, ts_packet *p, long length);
233
        void write_ts(int fd, ts_packet *p);
234
        int read_ts(int f, ts_packet *p);
235
        void cread_ts (char *buf, ts_packet *p, long length);
236

    
237

    
238
/*
239
   Program Stream
240
*/
241

    
242
#define PACK_STUFF_MASK  0x07
243

    
244
#define FIXED_FLAG       0x02
245
#define CSPS_FLAG        0x01
246
#define SAUDIO_LOCK_FLAG 0x80
247
#define SVIDEO_LOCK_FLAG 0x40
248

    
249
#define PS_MAX 200
250

    
251
        typedef struct ps_packet_{
252
                u8 scr[6];
253
                u8 mux_rate[3];
254
                u8 stuff_length;
255
                u8 *data;
256
                u8 sheader_llength[2];
257
                int sheader_length;
258
                u8 rate_bound[3];
259
                u8 audio_bound;
260
                u8 video_bound;
261
                u8 reserved;
262
                int npes;
263
                int mpeg;
264
        } ps_packet;
265

    
266
        void init_ps(ps_packet *p);
267
        void kill_ps(ps_packet *p);
268
        void setlength_ps(ps_packet *p);
269
        u32 scr_base_ps(ps_packet *p);
270
        u16 scr_ext_ps(ps_packet *p);
271
        int mux_ps(ps_packet *p);
272
        int rate_ps(ps_packet *p);
273
        int cwrite_ps(u8 *buf, ps_packet *p, long length);
274
        void write_ps(int fd, ps_packet *p);
275
        int read_ps (int f, ps_packet *p);
276
        void cread_ps (char *buf, ps_packet *p, long length);
277

    
278

    
279

    
280
#define MAX_PLENGTH 0xFFFF
281

    
282
        typedef struct sectionstruct {
283
                int  id;
284
                int length;
285
                int found;
286
                u8 payload[4096+3];
287
        } section;
288

    
289

    
290
        typedef u32 tflags;
291
#define MAXFILT 32
292
#define MASKL 16
293
        typedef struct trans_struct {
294
                int found;
295
                u8 packet[188];
296
                u16 pid[MAXFILT];
297
                u8 mask[MAXFILT*MASKL];
298
                u8 filt[MAXFILT*MASKL];
299
                u8 transbuf[MAXFILT*188];
300
                int transcount[MAXFILT];
301
                section sec[MAXFILT];
302
                tflags is_full;
303
                tflags pes_start;
304
                tflags pes_started;
305
                tflags pes;
306
                tflags set;
307
        } trans;
308

    
309

    
310
        void init_trans(trans *p);
311
        int set_trans_filt(trans *p, int filtn, u16 pid, u8 *mask, 
312
                           u8 *filt, int pes);
313

    
314
        void clear_trans_filt(trans *p,int filtn);
315
        int filt_is_set(trans *p, int filtn);
316
        int pes_is_set(trans *p, int filtn);
317
        int pes_is_started(trans *p, int filtn);
318
        int pes_is_start(trans *p, int filtn);
319
        int filt_is_ready(trans *p,int filtn);
320

    
321
        void trans_filt(u8 *buf, int count, trans *p);
322
        void filter(trans *p);
323
        void pes_filter(trans *p, int filtn, int off);
324
        void sec_filter(trans *p, int filtn, int off);
325
        int get_filt_buf(trans *p, int filtn,u8 **buf); 
326
        section *get_filt_sec(trans *p, int filtn); 
327

    
328

    
329
        typedef struct a2pstruct{
330
                int type;
331
                int fd;
332
                int found;
333
                int length;
334
                int headr;
335
                int plength;
336
                u8 cid;
337
                u8 flags;
338
                u8 abuf[MAX_PLENGTH];
339
                int alength;
340
                u8 vbuf[MAX_PLENGTH];
341
                int vlength;
342
                u8 last_av_pts[4];
343
                u8 av_pts[4];
344
                u8 scr[4];
345
                u8 pid0;
346
                u8 pid1;
347
                u8 pidv;
348
                u8 pida;
349
        } a2p;
350

    
351

    
352

    
353
        void get_pespts(u8 *av_pts,u8 *pts);
354
        void init_a2p(a2p *p);
355
        void av_pes_to_pes(u8 *buf,int count, a2p *p);
356
        int w_pesh(u8 id,int length ,u8 *pts, u8 *obuf);
357
        int w_tsh(u8 id,int length ,u8 *pts, u8 *obuf,a2p *p,int startpes);
358
        void pts2pts(u8 *av_pts, u8 *pts);
359
        void write_ps_headr(ps_packet *p,u8 *pts,int fd);
360

    
361
        typedef struct p2t_s{
362
                u8           pes[TS_SIZE];
363
                u8           counter;
364
                long int     pos;
365
                int          frags;
366
                void         (*t_out)(u8 const *buf);
367
        } p2t_t;
368

    
369
        void twrite(u8 const *buf);
370
        void init_p2t(p2t_t *p, void (*fkt)(u8 const *buf));
371
        long int find_pes_header(u8 const *buf, long int length, int *frags);
372
        void pes_to_ts( u8 const *buf, long int length, u16 pid, p2t_t *p);
373
        void p_to_t( u8 const *buf, long int length, u16 pid, u8 *counter, 
374
                    void (*ts_write)(u8 const *));
375

    
376

    
377
        int write_pes_header(u8 id,int length , long PTS, 
378
                             u8 *obuf, int stuffing);
379

    
380
        int write_ps_header(uint8_t *buf, 
381
                            uint32_t   SCR, 
382
                            long  muxr,
383
                            uint8_t    audio_bound,
384
                            uint8_t    fixed,
385
                            uint8_t    CSPS,
386
                            uint8_t    audio_lock,
387
                            uint8_t    video_lock,
388
                            uint8_t    video_bound,
389
                            uint8_t    stream1,
390
                            uint8_t    buffer1_scale,
391
                            uint32_t   buffer1_size,
392
                            uint8_t    stream2,
393
                            uint8_t    buffer2_scale,
394
                            uint32_t   buffer2_size);                    
395
        
396

    
397

    
398

    
399
        void split_mpg(char *name, uint64_t size);
400
        void cut_mpg(char *name, uint64_t size);
401
        int http_open (char *url);
402
#ifdef __cplusplus
403
}
404
#endif                                /* __cplusplus */
405

    
406
#endif /*_CTOOLS_H_*/