1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (C) 2003 Convergence GmbH
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public License
7  * as published by the Free Software Foundation; either version 2.1
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15 
16 #ifndef _DVB_FILTER_H_
17 #define _DVB_FILTER_H_
18 
19 #include <linux/slab.h>
20 
21 #include <media/demux.h>
22 
23 typedef int (dvb_filter_pes2ts_cb_t)(void *, unsigned char *);
24 
25 struct dvb_filter_pes2ts {
26 	unsigned char buf[188];
27 	unsigned char cc;
28 	dvb_filter_pes2ts_cb_t *cb;
29 	void *priv;
30 };
31 
32 void dvb_filter_pes2ts_init(struct dvb_filter_pes2ts *p2ts, unsigned short pid,
33 			    dvb_filter_pes2ts_cb_t *cb, void *priv);
34 
35 int dvb_filter_pes2ts(struct dvb_filter_pes2ts *p2ts, unsigned char *pes,
36 		      int len, int payload_start);
37 
38 #define PROG_STREAM_MAP  0xBC
39 #define PRIVATE_STREAM1  0xBD
40 #define PADDING_STREAM   0xBE
41 #define PRIVATE_STREAM2  0xBF
42 #define AUDIO_STREAM_S   0xC0
43 #define AUDIO_STREAM_E   0xDF
44 #define VIDEO_STREAM_S   0xE0
45 #define VIDEO_STREAM_E   0xEF
46 #define ECM_STREAM       0xF0
47 #define EMM_STREAM       0xF1
48 #define DSM_CC_STREAM    0xF2
49 #define ISO13522_STREAM  0xF3
50 #define PROG_STREAM_DIR  0xFF
51 
52 #define DVB_PICTURE_START    0x00
53 #define DVB_USER_START       0xb2
54 #define DVB_SEQUENCE_HEADER  0xb3
55 #define DVB_SEQUENCE_ERROR   0xb4
56 #define DVB_EXTENSION_START  0xb5
57 #define DVB_SEQUENCE_END     0xb7
58 #define DVB_GOP_START        0xb8
59 #define DVB_EXCEPT_SLICE     0xb0
60 
61 #define SEQUENCE_EXTENSION           0x01
62 #define SEQUENCE_DISPLAY_EXTENSION   0x02
63 #define PICTURE_CODING_EXTENSION     0x08
64 #define QUANT_MATRIX_EXTENSION       0x03
65 #define PICTURE_DISPLAY_EXTENSION    0x07
66 
67 #define I_FRAME 0x01
68 #define B_FRAME 0x02
69 #define P_FRAME 0x03
70 
71 /* Initialize sequence_data */
72 #define INIT_HORIZONTAL_SIZE        720
73 #define INIT_VERTICAL_SIZE          576
74 #define INIT_ASPECT_RATIO          0x02
75 #define INIT_FRAME_RATE            0x03
76 #define INIT_DISP_HORIZONTAL_SIZE   540
77 #define INIT_DISP_VERTICAL_SIZE     576
78 
79 //flags2
80 #define PTS_DTS_FLAGS    0xC0
81 #define ESCR_FLAG        0x20
82 #define ES_RATE_FLAG     0x10
83 #define DSM_TRICK_FLAG   0x08
84 #define ADD_CPY_FLAG     0x04
85 #define PES_CRC_FLAG     0x02
86 #define PES_EXT_FLAG     0x01
87 
88 //pts_dts flags
89 #define PTS_ONLY         0x80
90 #define PTS_DTS          0xC0
91 
92 #define TS_SIZE        188
93 #define TRANS_ERROR    0x80
94 #define PAY_START      0x40
95 #define TRANS_PRIO     0x20
96 #define PID_MASK_HI    0x1F
97 //flags
98 #define TRANS_SCRMBL1  0x80
99 #define TRANS_SCRMBL2  0x40
100 #define ADAPT_FIELD    0x20
101 #define PAYLOAD        0x10
102 #define COUNT_MASK     0x0F
103 
104 // adaptation flags
105 #define DISCON_IND     0x80
106 #define RAND_ACC_IND   0x40
107 #define ES_PRI_IND     0x20
108 #define PCR_FLAG       0x10
109 #define OPCR_FLAG      0x08
110 #define SPLICE_FLAG    0x04
111 #define TRANS_PRIV     0x02
112 #define ADAP_EXT_FLAG  0x01
113 
114 // adaptation extension flags
115 #define LTW_FLAG       0x80
116 #define PIECE_RATE     0x40
117 #define SEAM_SPLICE    0x20
118 
119 #define MAX_PLENGTH 0xFFFF
120 #define MMAX_PLENGTH (256 * MAX_PLENGTH)
121 
122 #ifndef IPACKS
123 #define IPACKS 2048
124 #endif
125 
126 struct ipack {
127 	int size;
128 	int found;
129 	u8 *buf;
130 	u8 cid;
131 	u32 plength;
132 	u8 plen[2];
133 	u8 flag1;
134 	u8 flag2;
135 	u8 hlength;
136 	u8 pts[5];
137 	u16 *pid;
138 	int mpeg;
139 	u8 check;
140 	int which;
141 	int done;
142 	void *data;
143 	void (*func)(u8 *buf,  int size, void *priv);
144 	int count;
145 	int repack_subids;
146 };
147 
148 struct dvb_video_info {
149 	u32 horizontal_size;
150 	u32 vertical_size;
151 	u32 aspect_ratio;
152 	u32 framerate;
153 	u32 video_format;
154 	u32 bit_rate;
155 	u32 comp_bit_rate;
156 	u32 vbv_buffer_size;
157 	s16 vbv_delay;
158 	u32 CSPF;
159 	u32 off;
160 };
161 
162 #define OFF_SIZE 4
163 #define FIRST_FIELD 0
164 #define SECOND_FIELD 1
165 #define VIDEO_FRAME_PICTURE 0x03
166 
167 struct mpg_picture {
168 	int       channel;
169 	struct dvb_video_info vinfo;
170 	u32      *sequence_gop_header;
171 	u32      *picture_header;
172 	s32       time_code;
173 	int       low_delay;
174 	int       closed_gop;
175 	int       broken_link;
176 	int       sequence_header_flag;
177 	int       gop_flag;
178 	int       sequence_end_flag;
179 
180 	u8        profile_and_level;
181 	s32       picture_coding_parameter;
182 	u32       matrix[32];
183 	s8        matrix_change_flag;
184 
185 	u8        picture_header_parameter;
186 	/* bit 0 - 2: bwd f code
187 	 * bit 3    : fpb vector
188 	 * bit 4 - 6: fwd f code
189 	 * bit 7    : fpf vector
190 	 */
191 
192 	int       mpeg1_flag;
193 	int       progressive_sequence;
194 	int       sequence_display_extension_flag;
195 	u32       sequence_header_data;
196 	s16       last_frame_centre_horizontal_offset;
197 	s16       last_frame_centre_vertical_offset;
198 
199 	u32       pts[2]; /* [0] 1st field, [1] 2nd field */
200 	int       top_field_first;
201 	int       repeat_first_field;
202 	int       progressive_frame;
203 	int       bank;
204 	int       forward_bank;
205 	int       backward_bank;
206 	int       compress;
207 	s16       frame_centre_horizontal_offset[OFF_SIZE];
208 		  /* [0-2] 1st field, [3] 2nd field */
209 	s16       frame_centre_vertical_offset[OFF_SIZE];
210 		  /* [0-2] 1st field, [3] 2nd field */
211 	s16       temporal_reference[2];
212 		  /* [0] 1st field, [1] 2nd field */
213 
214 	s8        picture_coding_type[2];
215 		  /* [0] 1st field, [1] 2nd field */
216 	s8        picture_structure[2];
217 		  /* [0] 1st field, [1] 2nd field */
218 	s8        picture_display_extension_flag[2];
219 		  /* [0] 1st field, [1] 2nd field */
220 		  /* picture_display_extenion() 0:no 1:exit*/
221 	s8        pts_flag[2];
222 		  /* [0] 1st field, [1] 2nd field */
223 };
224 
225 struct dvb_audio_info {
226 	int layer;
227 	u32 bit_rate;
228 	u32 frequency;
229 	u32 mode;
230 	u32 mode_extension;
231 	u32 emphasis;
232 	u32 framesize;
233 	u32 off;
234 };
235 
236 int dvb_filter_get_ac3info(u8 *mbuf, int count, struct dvb_audio_info *ai, int pr);
237 
238 #endif
239