0
|
1 #include <stdlib.h>
|
|
2 #include <string.h>
|
|
3
|
|
4 #include "arib_std_b25.h"
|
|
5 #include "arib_std_b25_error_code.h"
|
|
6 #include "multi2.h"
|
|
7 #include "ts_common_types.h"
|
|
8 #include "ts_section_parser.h"
|
|
9
|
|
10 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
11 inner structures
|
|
12 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
|
13 typedef struct {
|
|
14 int32_t pid;
|
|
15 int32_t type;
|
|
16 void *prev;
|
|
17 void *next;
|
|
18 } TS_STREAM_ELEM;
|
|
19
|
|
20 typedef struct {
|
|
21 TS_STREAM_ELEM *head;
|
|
22 TS_STREAM_ELEM *tail;
|
|
23 int32_t count;
|
|
24 } TS_STREAM_LIST;
|
|
25
|
|
26 typedef struct {
|
|
27
|
|
28 uint8_t *pool;
|
|
29 uint8_t *head;
|
|
30 uint8_t *tail;
|
|
31 int32_t max;
|
|
32
|
|
33 } TS_WORK_BUFFER;
|
|
34
|
|
35 typedef struct {
|
|
36
|
|
37 int32_t phase;
|
|
38
|
|
39 int32_t program_number;
|
|
40
|
|
41 int32_t pmt_pid;
|
|
42 TS_SECTION_PARSER *pmt;
|
|
43
|
|
44 int32_t pcr_pid;
|
|
45
|
|
46 TS_STREAM_LIST streams;
|
|
47 TS_STREAM_LIST old_strm;
|
|
48
|
|
49 } TS_PROGRAM;
|
|
50
|
|
51 typedef struct {
|
|
52
|
|
53 int32_t ref;
|
|
54 int32_t phase;
|
|
55
|
|
56 int32_t locked;
|
|
57
|
|
58 int32_t ecm_pid;
|
|
59 TS_SECTION_PARSER *ecm;
|
|
60
|
|
61 MULTI2 *m2;
|
|
62
|
|
63 int32_t unpurchased;
|
|
64 int32_t last_error;
|
|
65
|
|
66 void *prev;
|
|
67 void *next;
|
|
68
|
|
69 } DECRYPTOR_ELEM;
|
|
70
|
|
71 typedef struct {
|
|
72 DECRYPTOR_ELEM *head;
|
|
73 DECRYPTOR_ELEM *tail;
|
|
74 int32_t count;
|
|
75 } DECRYPTOR_LIST;
|
|
76
|
|
77 typedef struct {
|
|
78 uint32_t ref;
|
|
79 uint32_t type;
|
|
80 int64_t normal_packet;
|
|
81 int64_t undecrypted;
|
|
82 void *target;
|
|
83 } PID_MAP;
|
|
84
|
|
85 typedef struct {
|
|
86
|
|
87 int32_t multi2_round;
|
|
88 int32_t strip;
|
|
89 int32_t emm_proc_on;
|
|
90
|
|
91 int32_t unit_size;
|
|
92
|
|
93 int32_t sbuf_offset;
|
|
94
|
|
95 TS_SECTION_PARSER *pat;
|
|
96 TS_SECTION_PARSER *cat;
|
|
97
|
|
98 TS_STREAM_LIST strm_pool;
|
|
99
|
|
100 int32_t p_count;
|
|
101 TS_PROGRAM *program;
|
|
102
|
|
103 DECRYPTOR_LIST decrypt;
|
|
104
|
|
105 PID_MAP map[0x2000];
|
|
106
|
|
107 B_CAS_CARD *bcas;
|
|
108 B_CAS_ID casid;
|
3
|
109 int32_t ca_system_id;
|
0
|
110
|
|
111 int32_t emm_pid;
|
|
112 TS_SECTION_PARSER *emm;
|
|
113
|
|
114 TS_WORK_BUFFER sbuf;
|
|
115 TS_WORK_BUFFER dbuf;
|
|
116
|
|
117 } ARIB_STD_B25_PRIVATE_DATA;
|
|
118
|
|
119 typedef struct {
|
|
120 int64_t card_id;
|
|
121 int32_t associated_information_length;
|
|
122 int32_t protocol_number;
|
|
123 int32_t broadcaster_group_id;
|
|
124 int32_t update_number;
|
|
125 int32_t expiration_date;
|
|
126 } EMM_FIXED_PART;
|
|
127
|
|
128 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
129 constant values
|
|
130 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
|
131 enum TS_STREAM_TYPE {
|
|
132 TS_STREAM_TYPE_11172_2_VIDEO = 0x01,
|
|
133 TS_STREAM_TYPE_13818_2_VIDEO = 0x02,
|
|
134 TS_STREAM_TYPE_11172_3_AUDIO = 0x03,
|
|
135 TS_STREAM_TYPE_13818_3_AUDIO = 0x04,
|
|
136 TS_STREAM_TYPE_13818_1_PRIVATE_SECTIONS = 0x05,
|
|
137 TS_STREAM_TYPE_13818_1_PES_PRIVATE_DATA = 0x06,
|
|
138 TS_STREAM_TYPE_13522_MHEG = 0x07,
|
|
139 TS_STREAM_TYPE_13818_1_DSM_CC = 0x08,
|
|
140 TS_STREAM_TYPE_H_222_1 = 0x09,
|
|
141 TS_STREAM_TYPE_13818_6_TYPE_A = 0x0a,
|
|
142 TS_STREAM_TYPE_13818_6_TYPE_B = 0x0b,
|
|
143 TS_STREAM_TYPE_13818_6_TYPE_C = 0x0c,
|
|
144 TS_STREAM_TYPE_13818_6_TYPE_D = 0x0d,
|
|
145 TS_STREAM_TYPE_13818_1_AUX = 0x0e,
|
|
146 TS_STREAM_TYPE_13818_7_AUDIO_ADTS = 0x0f,
|
|
147 TS_STREAM_TYPE_14496_2_VISUAL = 0x10,
|
|
148 TS_STREAM_TYPE_14496_3_AUDIO_LATM = 0x11,
|
|
149 TS_STREAM_TYPE_14496_1_PES_SL_PACKET = 0x12,
|
|
150 TS_STREAM_TYPE_14496_1_SECTIONS_SL_PACKET = 0x13,
|
|
151 TS_STREAM_TYPE_13818_6_SYNC_DWLOAD_PROTCOL = 0x14,
|
|
152 };
|
|
153
|
|
154 enum TS_SECTION_ID {
|
|
155 TS_SECTION_ID_PROGRAM_ASSOCIATION = 0x00,
|
|
156 TS_SECTION_ID_CONDITIONAL_ACCESS = 0x01,
|
|
157 TS_SECTION_ID_PROGRAM_MAP = 0x02,
|
|
158 TS_SECTION_ID_DESCRIPTION = 0x03,
|
|
159 TS_SECTION_ID_14496_SCENE_DESCRIPTION = 0x04,
|
|
160 TS_SECTION_ID_14496_OBJECT_DESCRIPTION = 0x05,
|
|
161
|
|
162 /* ARIB STD-B10 stuff */
|
|
163 TS_SECTION_ID_DSM_CC_HEAD = 0x3a,
|
|
164 TS_SECTION_ID_DSM_CC_TAIL = 0x3f,
|
|
165 TS_SECTION_ID_NIT_ACTUAL = 0x40,
|
|
166 TS_SECTION_ID_NIT_OTHER = 0x41,
|
|
167 TS_SECTION_ID_SDT_ACTUAL = 0x42,
|
|
168 TS_SECTION_ID_SDT_OTHER = 0x46,
|
|
169 TS_SECTION_ID_BAT = 0x4a,
|
|
170 TS_SECTION_ID_EIT_ACTUAL_CURRENT = 0x4e,
|
|
171 TS_SECTION_ID_EIT_OTHER_CURRENT = 0x4f,
|
|
172 TS_SECTION_ID_EIT_ACTUAL_SCHEDULE_HEAD = 0x50,
|
|
173 TS_SECTION_ID_EIT_ACTUAL_SCHEDULE_TAIL = 0x5f,
|
|
174 TS_SECTION_ID_EIT_OTHER_SCHEDULE_HEAD = 0x60,
|
|
175 TS_SECTION_ID_EIT_OTHER_SCHEDULE_TAIL = 0x6f,
|
|
176 TS_SECTION_ID_TDT = 0x70,
|
|
177 TS_SECTION_ID_RST = 0x71,
|
|
178 TS_SECTION_ID_ST = 0x72,
|
|
179 TS_SECTION_ID_TOT = 0x73,
|
|
180 TS_SECTION_ID_AIT = 0x74,
|
|
181 TS_SECTION_ID_DIT = 0x7e,
|
|
182 TS_SECTION_ID_SIT = 0x7f,
|
|
183 TS_SECTION_ID_ECM_S = 0x82,
|
|
184 TS_SECTION_ID_ECM = 0x83,
|
|
185 TS_SECTION_ID_EMM_S = 0x84,
|
|
186 TS_SECTION_ID_EMM_MESSAGE = 0x85,
|
|
187 TS_SECTION_ID_DCT = 0xc0,
|
|
188 TS_SECTION_ID_DLT = 0xc1,
|
|
189 TS_SECTION_ID_PCAT = 0xc2,
|
|
190 TS_SECTION_ID_SDTT = 0xc3,
|
|
191 TS_SECTION_ID_BIT = 0xc4,
|
|
192 TS_SECTION_ID_NBIT_BODY = 0xc5,
|
|
193 TS_SECTION_ID_NBIT_REFERENCE = 0xc6,
|
|
194 TS_SECTION_ID_LDT = 0xc7,
|
|
195 TS_SECTION_ID_CDT = 0xc8,
|
|
196 TS_SECTION_ID_LIT = 0xd0,
|
|
197 TS_SECTION_ID_ERT = 0xd1,
|
|
198 TS_SECTION_ID_ITT = 0xd2,
|
|
199 };
|
|
200
|
|
201 enum TS_DESCRIPTOR_TAG {
|
|
202 TS_DESCRIPTOR_TAG_VIDEO_STREAM = 0x02,
|
|
203 TS_DESCRIPTOR_TAG_AUDIO_STREAM = 0x03,
|
|
204 TS_DESCRIPTOR_TAG_HIERARCHY = 0x04,
|
|
205 TS_DESCRIPTOR_TAG_REGISTRATION = 0x05,
|
|
206 TS_DESCRIPTOR_TAG_DATA_STREAM_ALIGNMENT = 0x06,
|
|
207 TS_DESCRIPTOR_TAG_TARGET_BACKGROUND_GRID = 0x07,
|
|
208 TS_DESCRIPTOR_TAG_VIDEO_WINDOW = 0x08,
|
|
209 TS_DESCRIPTOR_TAG_CA = 0x09,
|
|
210 TS_DESCRIPTOR_TAG_ISO_639_LANGUAGE = 0x0a,
|
|
211 TS_DESCRIPTOR_TAG_SYSTEM_CLOCK = 0x0b,
|
|
212 TS_DESCRIPTOR_TAG_MULTIPLEX_BUF_UTILIZ = 0x0c,
|
|
213 TS_DESCRIPTOR_TAG_COPYRIGHT = 0x0d,
|
|
214 TS_DESCRIPTOR_TAG_MAXIMUM_BITRATE = 0x0e,
|
|
215 TS_DESCRIPTOR_TAG_PRIVATE_DATA_INDICATOR = 0x0f,
|
|
216 TS_DESCRIPTOR_TAG_SMOOTHING_BUFFER = 0x10,
|
|
217 TS_DESCRIPTOR_TAG_STD = 0x11,
|
|
218 TS_DESCRIPTOR_TAG_IBP = 0x12,
|
|
219 TS_DESCRIPTOR_TAG_MPEG4_VIDEO = 0x1b,
|
|
220 TS_DESCRIPTOR_TAG_MPEG4_AUDIO = 0x1c,
|
|
221 TS_DESCRIPTOR_TAG_IOD = 0x1d,
|
|
222 TS_DESCRIPTOR_TAG_SL = 0x1e,
|
|
223 TS_DESCRIPTOR_TAG_FMC = 0x1f,
|
|
224 TS_DESCRIPTOR_TAG_EXTERNAL_ES_ID = 0x20,
|
|
225 TS_DESCRIPTOR_TAG_MUXCODE = 0x21,
|
|
226 TS_DESCRIPTOR_TAG_FMX_BUFFER_SIZE = 0x22,
|
|
227 TS_DESCRIPTOR_TAG_MULTIPLEX_BUFFER = 0x23,
|
|
228 TS_DESCRIPTOR_TAG_AVC_VIDEO = 0x28,
|
|
229 TS_DESCRIPTOR_TAG_AVC_TIMING_HRD = 0x2a,
|
|
230
|
|
231 /* ARIB STD-B10 stuff */
|
|
232 TS_DESCRIPTOR_TAG_NETWORK_NAME = 0x40,
|
|
233 TS_DESCRIPTOR_TAG_SERVICE_LIST = 0x41,
|
|
234 TS_DESCRIPTOR_TAG_STUFFING = 0x42,
|
|
235 TS_DESCRIPTOR_TAG_SATELLITE_DELIVERY_SYS = 0x43,
|
|
236 TS_DESCRIPTOR_TAG_CABLE_DISTRIBUTION = 0x44,
|
|
237 TS_DESCRIPTOR_TAG_BOUNQUET_NAME = 0x47,
|
|
238 TS_DESCRIPTOR_TAG_SERVICE = 0x48,
|
|
239 TS_DESCRIPTOR_TAG_COUNTRY_AVAILABILITY = 0x49,
|
|
240 TS_DESCRIPTOR_TAG_LINKAGE = 0x4a,
|
|
241 TS_DESCRIPTOR_TAG_NVOD_REFERENCE = 0x4b,
|
|
242 TS_DESCRIPTOR_TAG_TIME_SHIFTED_SERVICE = 0x4c,
|
|
243 TS_DESCRIPTOR_TAG_SHORT_EVENT = 0x4d,
|
|
244 TS_DESCRIPTOR_TAG_EXTENDED_EVENT = 0x4e,
|
|
245 TS_DESCRIPTOR_TAG_TIME_SHIFTED_EVENT = 0x4f,
|
|
246 TS_DESCRIPTOR_TAG_COMPONENT = 0x50,
|
|
247 TS_DESCRIPTOR_TAG_MOSAIC = 0x51,
|
|
248 TS_DESCRIPTOR_TAG_STREAM_IDENTIFIER = 0x52,
|
|
249 TS_DESCRIPTOR_TAG_CA_IDENTIFIER = 0x53,
|
|
250 TS_DESCRIPTOR_TAG_CONTENT = 0x54,
|
|
251 TS_DESCRIPTOR_TAG_PARENTAL_RATING = 0x55,
|
|
252 TS_DESCRIPTOR_TAG_LOCAL_TIME_OFFSET = 0x58,
|
|
253 TS_DESCRIPTOR_TAG_PARTIAL_TRANSPORT_STREAM = 0x63,
|
|
254 TS_DESCRIPTOR_TAG_HIERARCHICAL_TRANSMISSION = 0xc0,
|
|
255 TS_DESCRIPTOR_TAG_DIGITAL_COPY_CONTROL = 0xc1,
|
|
256 TS_DESCRIPTOR_TAG_NETWORK_IDENTIFICATION = 0xc2,
|
|
257 TS_DESCRIPTOR_TAG_PARTIAL_TRANSPORT_TIME = 0xc3,
|
|
258 TS_DESCRIPTOR_TAG_AUDIO_COMPONENT = 0xc4,
|
|
259 TS_DESCRIPTOR_TAG_HYPERLINK = 0xc5,
|
|
260 TS_DESCRIPTOR_TAG_TARGET_REGION = 0xc6,
|
|
261 TS_DESCRIPTOR_TAG_DATA_COTENT = 0xc7,
|
|
262 TS_DESCRIPTOR_TAG_VIDEO_DECODE_CONTROL = 0xc8,
|
|
263 TS_DESCRIPTOR_TAG_DOWNLOAD_CONTENT = 0xc9,
|
|
264 TS_DESCRIPTOR_TAG_CA_EMM_TS = 0xca,
|
|
265 TS_DESCRIPTOR_TAG_CA_CONTRACT_INFORMATION = 0xcb,
|
|
266 TS_DESCRIPTOR_TAG_CA_SERVICE = 0xcc,
|
|
267 TS_DESCRIPTOR_TAG_TS_INFORMATION = 0xcd,
|
|
268 TS_DESCRIPTOR_TAG_EXTENDED_BROADCASTER = 0xce,
|
|
269 TS_DESCRIPTOR_TAG_LOGO_TRANSMISSION = 0xcf,
|
|
270 TS_DESCRIPTOR_TAG_BASIC_LOCAL_EVENT = 0xd0,
|
|
271 TS_DESCRIPTOR_TAG_REFERENCE = 0xd1,
|
|
272 TS_DESCRIPTOR_TAG_NODE_RELATION = 0xd2,
|
|
273 TS_DESCRIPTOR_TAG_SHORT_NODE_INFORMATION = 0xd3,
|
|
274 TS_DESCRIPTOR_TAG_STC_REFERENCE = 0xd4,
|
|
275 TS_DESCRIPTOR_TAG_SERIES = 0xd5,
|
|
276 TS_DESCRIPTOR_TAG_EVENT_GROUP = 0xd6,
|
|
277 TS_DESCRIPTOR_TAG_SI_PARAMETER = 0xd7,
|
|
278 TS_DESCRIPTOR_TAG_BROADCASTER_NAME = 0xd8,
|
|
279 TS_DESCRIPTOR_TAG_COMPONENT_GROUP = 0xd9,
|
|
280 TS_DESCRIPTOR_TAG_SI_PRIME_TS = 0xda,
|
|
281 TS_DESCRIPTOR_TAG_BOARD_INFORMATION = 0xdb,
|
|
282 TS_DESCRIPTOR_TAG_LDT_LINKAGE = 0xdc,
|
|
283 TS_DESCRIPTOR_TAG_CONNECTED_TRANSMISSION = 0xdd,
|
|
284 TS_DESCRIPTOR_TAG_CONTENT_AVAILABILITY = 0xde,
|
|
285 TS_DESCRIPTOR_TAG_VALUE_EXTENSION = 0xdf,
|
|
286 TS_DESCRIPTOR_TAG_SERVICE_GROUP = 0xe0,
|
|
287 TS_DESCRIPTOR_TAG_CARUSEL_COMPOSITE = 0xf7,
|
|
288 TS_DESCRIPTOR_TAG_CONDITIONAL_PLAYBACK = 0xf8,
|
|
289 TS_DESCRIPTOR_TAG_CABLE_TS_DIVISSION = 0xf9,
|
|
290 TS_DESCRIPTOR_TAG_TERRESTRIAL_DELIVERY_SYS = 0xfa,
|
|
291 TS_DESCRIPTOR_TAG_PARTIAL_RECEPTION = 0xfb,
|
|
292 TS_DESCRIPTOR_TAG_EMERGENCY_INFOMATION = 0xfc,
|
|
293 TS_DESCRIPTOR_TAG_DATA_COMPONENT = 0xfd,
|
|
294 TS_DESCRIPTOR_TAG_SYSTEM_MANAGEMENT = 0xfe,
|
|
295 };
|
|
296
|
|
297 enum PID_MAP_TYPE {
|
|
298 PID_MAP_TYPE_UNKNOWN = 0x0000,
|
|
299 PID_MAP_TYPE_PAT = 0x0100,
|
|
300 PID_MAP_TYPE_PMT = 0x0200,
|
|
301 PID_MAP_TYPE_NIT = 0x0300,
|
|
302 PID_MAP_TYPE_PCR = 0x0400,
|
|
303 PID_MAP_TYPE_ECM = 0x0500,
|
|
304 PID_MAP_TYPE_EMM = 0x0600,
|
|
305 PID_MAP_TYPE_EIT = 0x0700,
|
|
306 PID_MAP_TYPE_CAT = 0x0800,
|
|
307 PID_MAP_TYPE_OTHER = 0xff00,
|
|
308 };
|
|
309
|
|
310 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
311 function prottypes (interface method)
|
|
312 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
|
313 static void release_arib_std_b25(void *std_b25);
|
|
314 static int set_multi2_round_arib_std_b25(void *std_b25, int32_t round);
|
|
315 static int set_strip_arib_std_b25(void *std_b25, int32_t strip);
|
|
316 static int set_emm_proc_arib_std_b25(void *std_b25, int32_t on);
|
|
317 static int set_b_cas_card_arib_std_b25(void *std_b25, B_CAS_CARD *bcas);
|
|
318 static int reset_arib_std_b25(void *std_b25);
|
|
319 static int flush_arib_std_b25(void *std_b25);
|
|
320 static int put_arib_std_b25(void *std_b25, ARIB_STD_B25_BUFFER *buf);
|
|
321 static int get_arib_std_b25(void *std_b25, ARIB_STD_B25_BUFFER *buf);
|
|
322 static int get_program_count_arib_std_b25(void *std_b25);
|
|
323 static int get_program_info_arib_std_b25(void *std_b25, ARIB_STD_B25_PROGRAM_INFO *info, int idx);
|
|
324
|
|
325 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
326 global function implementation
|
|
327 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
|
328 ARIB_STD_B25 *create_arib_std_b25()
|
|
329 {
|
|
330 int n;
|
|
331
|
|
332 ARIB_STD_B25 *r;
|
|
333 ARIB_STD_B25_PRIVATE_DATA *prv;
|
|
334
|
|
335 n = sizeof(ARIB_STD_B25_PRIVATE_DATA);
|
|
336 n += sizeof(ARIB_STD_B25);
|
|
337
|
|
338 prv = (ARIB_STD_B25_PRIVATE_DATA *)calloc(1, n);
|
|
339 if(prv == NULL){
|
|
340 return NULL;
|
|
341 }
|
|
342
|
|
343 prv->multi2_round = 4;
|
|
344
|
|
345 r = (ARIB_STD_B25 *)(prv+1);
|
|
346 r->private_data = prv;
|
|
347
|
|
348 r->release = release_arib_std_b25;
|
|
349 r->set_multi2_round = set_multi2_round_arib_std_b25;
|
|
350 r->set_strip = set_strip_arib_std_b25;
|
|
351 r->set_emm_proc = set_emm_proc_arib_std_b25;
|
|
352 r->set_b_cas_card = set_b_cas_card_arib_std_b25;
|
|
353 r->reset = reset_arib_std_b25;
|
|
354 r->flush = flush_arib_std_b25;
|
|
355 r->put = put_arib_std_b25;
|
|
356 r->get = get_arib_std_b25;
|
|
357 r->get_program_count = get_program_count_arib_std_b25;
|
|
358 r->get_program_info = get_program_info_arib_std_b25;
|
|
359
|
|
360 return r;
|
|
361 }
|
|
362
|
|
363 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
364 function prottypes (private method)
|
|
365 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
|
366 static ARIB_STD_B25_PRIVATE_DATA *private_data(void *std_b25);
|
|
367 static void teardown(ARIB_STD_B25_PRIVATE_DATA *prv);
|
|
368 static int select_unit_size(ARIB_STD_B25_PRIVATE_DATA *prv);
|
|
369 static int find_pat(ARIB_STD_B25_PRIVATE_DATA *prv);
|
|
370 static int proc_pat(ARIB_STD_B25_PRIVATE_DATA *prv);
|
|
371 static int check_pmt_complete(ARIB_STD_B25_PRIVATE_DATA *prv);
|
|
372 static int find_pmt(ARIB_STD_B25_PRIVATE_DATA *prv);
|
|
373 static int proc_pmt(ARIB_STD_B25_PRIVATE_DATA *prv, TS_PROGRAM *pgrm);
|
3
|
374 static int32_t find_ca_descriptor_pid(uint8_t *head, uint8_t *tail, int32_t ca_system_id);
|
0
|
375 static int32_t add_ecm_stream(ARIB_STD_B25_PRIVATE_DATA *prv, TS_STREAM_LIST *list, int32_t ecm_pid);
|
|
376 static int check_ecm_complete(ARIB_STD_B25_PRIVATE_DATA *prv);
|
|
377 static int find_ecm(ARIB_STD_B25_PRIVATE_DATA *prv);
|
|
378 static int proc_ecm(DECRYPTOR_ELEM *dec, B_CAS_CARD *bcas, int32_t multi2_round);
|
|
379 static int proc_arib_std_b25(ARIB_STD_B25_PRIVATE_DATA *prv);
|
|
380
|
|
381 static int proc_cat(ARIB_STD_B25_PRIVATE_DATA *prv);
|
|
382 static int proc_emm(ARIB_STD_B25_PRIVATE_DATA *prv);
|
|
383
|
|
384 static void release_program(ARIB_STD_B25_PRIVATE_DATA *prv, TS_PROGRAM *pgrm);
|
|
385
|
|
386 static void unref_stream(ARIB_STD_B25_PRIVATE_DATA *prv, int32_t pid);
|
|
387
|
|
388 static DECRYPTOR_ELEM *set_decryptor(ARIB_STD_B25_PRIVATE_DATA *prv, int32_t pid);
|
|
389 static void remove_decryptor(ARIB_STD_B25_PRIVATE_DATA *prv, DECRYPTOR_ELEM *dec);
|
|
390 static DECRYPTOR_ELEM *select_active_decryptor(DECRYPTOR_ELEM *a, DECRYPTOR_ELEM *b, int32_t pid);
|
|
391 static void bind_stream_decryptor(ARIB_STD_B25_PRIVATE_DATA *prv, int32_t pid, DECRYPTOR_ELEM *dec);
|
|
392 static void unlock_all_decryptor(ARIB_STD_B25_PRIVATE_DATA *prv);
|
|
393
|
|
394 static TS_STREAM_ELEM *get_stream_list_head(TS_STREAM_LIST *list);
|
|
395 static TS_STREAM_ELEM *find_stream_list_elem(TS_STREAM_LIST *list, int32_t pid);
|
|
396 static TS_STREAM_ELEM *create_stream_elem(int32_t pid, int32_t type);
|
|
397 static void put_stream_list_tail(TS_STREAM_LIST *list, TS_STREAM_ELEM *elem);
|
|
398 static void clear_stream_list(TS_STREAM_LIST *list);
|
|
399
|
|
400 static int reserve_work_buffer(TS_WORK_BUFFER *buf, int32_t size);
|
|
401 static int append_work_buffer(TS_WORK_BUFFER *buf, uint8_t *data, int32_t size);
|
|
402 static void reset_work_buffer(TS_WORK_BUFFER *buf);
|
|
403 static void release_work_buffer(TS_WORK_BUFFER *buf);
|
|
404
|
|
405 static void extract_ts_header(TS_HEADER *dst, uint8_t *src);
|
|
406 static void extract_emm_fixed_part(EMM_FIXED_PART *dst, uint8_t *src);
|
|
407
|
|
408 static uint8_t *resync(uint8_t *head, uint8_t *tail, int32_t unit);
|
|
409 static uint8_t *resync_force(uint8_t *head, uint8_t *tail, int32_t unit);
|
|
410
|
56
|
411 /* static uint32_t crc32(uint8_t *head, uint8_t *tail); */
|
54
|
412
|
0
|
413 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
414 interface method implementation
|
|
415 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
|
416 static void release_arib_std_b25(void *std_b25)
|
|
417 {
|
|
418 ARIB_STD_B25_PRIVATE_DATA *prv;
|
|
419
|
|
420 prv = private_data(std_b25);
|
|
421 if(prv == NULL){
|
|
422 return;
|
|
423 }
|
|
424
|
|
425 teardown(prv);
|
|
426 free(prv);
|
|
427 }
|
|
428
|
|
429 static int set_multi2_round_arib_std_b25(void *std_b25, int32_t round)
|
|
430 {
|
|
431 ARIB_STD_B25_PRIVATE_DATA *prv;
|
|
432
|
|
433 prv = private_data(std_b25);
|
|
434 if(prv == NULL){
|
|
435 return ARIB_STD_B25_ERROR_INVALID_PARAM;
|
|
436 }
|
|
437
|
|
438 prv->multi2_round = round;
|
|
439
|
|
440 return 0;
|
|
441 }
|
|
442
|
|
443 static int set_strip_arib_std_b25(void *std_b25, int32_t strip)
|
|
444 {
|
|
445 ARIB_STD_B25_PRIVATE_DATA *prv;
|
|
446
|
|
447 prv = private_data(std_b25);
|
|
448 if(prv == NULL){
|
|
449 return ARIB_STD_B25_ERROR_INVALID_PARAM;
|
|
450 }
|
|
451
|
|
452 prv->strip = strip;
|
|
453
|
|
454 return 0;
|
|
455 }
|
|
456
|
|
457 static int set_emm_proc_arib_std_b25(void *std_b25, int32_t on)
|
|
458 {
|
|
459 ARIB_STD_B25_PRIVATE_DATA *prv;
|
|
460
|
|
461 prv = private_data(std_b25);
|
|
462 if(prv == NULL){
|
|
463 return ARIB_STD_B25_ERROR_INVALID_PARAM;
|
|
464 }
|
|
465
|
|
466 prv->emm_proc_on = on;
|
|
467
|
|
468 return 0;
|
|
469 }
|
|
470
|
|
471 static int set_b_cas_card_arib_std_b25(void *std_b25, B_CAS_CARD *bcas)
|
|
472 {
|
|
473 int n;
|
3
|
474 B_CAS_INIT_STATUS is;
|
0
|
475 ARIB_STD_B25_PRIVATE_DATA *prv;
|
|
476
|
|
477 prv = private_data(std_b25);
|
|
478 if(prv == NULL){
|
|
479 return ARIB_STD_B25_ERROR_INVALID_PARAM;
|
|
480 }
|
|
481
|
|
482 prv->bcas = bcas;
|
|
483 if(prv->bcas != NULL){
|
3
|
484 n = prv->bcas->get_init_status(bcas, &is);
|
|
485 if(n < 0){
|
|
486 return ARIB_STD_B25_ERROR_INVALID_B_CAS_STATUS;
|
|
487 }
|
|
488 prv->ca_system_id = is.ca_system_id;
|
0
|
489 n = prv->bcas->get_id(prv->bcas, &(prv->casid));
|
|
490 if(n < 0){
|
|
491 return ARIB_STD_B25_ERROR_INVALID_B_CAS_STATUS;
|
|
492 }
|
|
493 }
|
|
494
|
|
495 return 0;
|
|
496 }
|
|
497
|
|
498 static int reset_arib_std_b25(void *std_b25)
|
|
499 {
|
|
500 ARIB_STD_B25_PRIVATE_DATA *prv;
|
|
501
|
|
502 prv = private_data(std_b25);
|
|
503 if(prv == NULL){
|
|
504 return ARIB_STD_B25_ERROR_INVALID_PARAM;
|
|
505 }
|
|
506
|
|
507 teardown(prv);
|
|
508
|
|
509 return 0;
|
|
510 }
|
|
511
|
|
512 static int flush_arib_std_b25(void *std_b25)
|
|
513 {
|
|
514 int r;
|
|
515 int m,n;
|
|
516
|
|
517 int32_t crypt;
|
|
518 int32_t unit;
|
|
519 int32_t pid;
|
|
520
|
|
521 uint8_t *p;
|
|
522 uint8_t *curr;
|
|
523 uint8_t *tail;
|
|
524
|
|
525 TS_HEADER hdr;
|
|
526 DECRYPTOR_ELEM *dec;
|
|
527 TS_PROGRAM *pgrm;
|
|
528
|
|
529 ARIB_STD_B25_PRIVATE_DATA *prv;
|
|
530
|
|
531 prv = private_data(std_b25);
|
|
532 if(prv == NULL){
|
|
533 return ARIB_STD_B25_ERROR_INVALID_PARAM;
|
|
534 }
|
|
535
|
|
536 if(prv->unit_size < 188){
|
|
537 r = select_unit_size(prv);
|
|
538 if(r < 0){
|
|
539 return r;
|
|
540 }
|
|
541 }
|
|
542
|
|
543 r = proc_arib_std_b25(prv);
|
|
544 if(r < 0){
|
|
545 return r;
|
|
546 }
|
|
547
|
|
548 unit = prv->unit_size;
|
|
549 curr = prv->sbuf.head;
|
|
550 tail = prv->sbuf.tail;
|
|
551
|
|
552 m = prv->dbuf.tail - prv->dbuf.head;
|
|
553 n = tail - curr;
|
|
554 if(!reserve_work_buffer(&(prv->dbuf), m+n)){
|
|
555 return ARIB_STD_B25_ERROR_NO_ENOUGH_MEMORY;
|
|
556 }
|
|
557
|
|
558 r = 0;
|
|
559
|
|
560 while( (curr+188) <= tail ){
|
|
561
|
|
562 if(curr[0] != 0x47){
|
|
563 p = resync_force(curr, tail, unit);
|
|
564 if(p == NULL){
|
|
565 goto LAST;
|
|
566 }
|
|
567 curr = p;
|
|
568 }
|
|
569
|
|
570 extract_ts_header(&hdr, curr);
|
|
571 crypt = hdr.transport_scrambling_control;
|
|
572 pid = hdr.pid;
|
54
|
573
|
|
574 if(hdr.transport_error_indicator != 0){
|
|
575 /* bit error - append output buffer without parsing */
|
|
576 if(!append_work_buffer(&(prv->dbuf), curr, 188)){
|
|
577 r = ARIB_STD_B25_ERROR_NO_ENOUGH_MEMORY;
|
|
578 goto LAST;
|
|
579 }
|
|
580 goto NEXT;
|
|
581 }
|
0
|
582
|
|
583 if( (pid == 0x1fff) && (prv->strip) ){
|
|
584 goto NEXT;
|
|
585 }
|
|
586
|
|
587 p = curr+4;
|
|
588 if(hdr.adaptation_field_control & 0x02){
|
|
589 p += (p[0]+1);
|
|
590 }
|
|
591 n = 188 - (p-curr);
|
|
592 if( (n < 1) && ((n < 0) || (hdr.adaptation_field_control & 0x01)) ){
|
|
593 /* broken packet */
|
|
594 curr += 1;
|
|
595 continue;
|
|
596 }
|
|
597
|
|
598 if( (crypt != 0) &&
|
|
599 (hdr.adaptation_field_control & 0x01) ){
|
|
600
|
|
601 if(prv->map[pid].type == PID_MAP_TYPE_OTHER){
|
|
602 dec = (DECRYPTOR_ELEM *)(prv->map[pid].target);
|
54
|
603 }else if( (prv->map[pid].type == 0) &&
|
|
604 (prv->decrypt.count == 1) ){
|
|
605 dec = prv->decrypt.head;
|
0
|
606 }else{
|
|
607 dec = NULL;
|
|
608 }
|
|
609
|
|
610 if( (dec != NULL) && (dec->m2 != NULL) ){
|
|
611 m = dec->m2->decrypt(dec->m2, crypt, p, n);
|
|
612 if(m < 0){
|
|
613 r = ARIB_STD_B25_ERROR_DECRYPT_FAILURE;
|
|
614 goto LAST;
|
|
615 }
|
|
616 curr[3] &= 0x3f;
|
|
617 prv->map[pid].normal_packet += 1;
|
|
618 }else{
|
|
619 prv->map[pid].undecrypted += 1;
|
|
620 }
|
|
621 }else{
|
|
622 prv->map[pid].normal_packet += 1;
|
|
623 }
|
|
624
|
|
625 if(!append_work_buffer(&(prv->dbuf), curr, 188)){
|
|
626 r = ARIB_STD_B25_ERROR_NO_ENOUGH_MEMORY;
|
|
627 goto LAST;
|
|
628 }
|
|
629
|
|
630 if(prv->map[pid].type == PID_MAP_TYPE_ECM){
|
|
631 dec = (DECRYPTOR_ELEM *)(prv->map[pid].target);
|
|
632 if( (dec == NULL) || (dec->ecm == NULL) ){
|
|
633 /* this code will never execute */
|
|
634 r = ARIB_STD_B25_ERROR_ECM_PARSE_FAILURE;
|
|
635 goto LAST;
|
|
636 }
|
|
637 m = dec->ecm->put(dec->ecm, &hdr, p, n);
|
|
638 if(m < 0){
|
|
639 r = ARIB_STD_B25_ERROR_ECM_PARSE_FAILURE;
|
|
640 goto LAST;
|
|
641 }
|
|
642 m = dec->ecm->get_count(dec->ecm);
|
|
643 if(m < 0){
|
|
644 r = ARIB_STD_B25_ERROR_ECM_PARSE_FAILURE;
|
|
645 goto LAST;
|
|
646 }
|
|
647 if(m == 0){
|
|
648 goto NEXT;
|
|
649 }
|
|
650 r = proc_ecm(dec, prv->bcas, prv->multi2_round);
|
|
651 if(r < 0){
|
|
652 goto LAST;
|
|
653 }
|
|
654 }else if(prv->map[pid].type == PID_MAP_TYPE_PMT){
|
|
655 pgrm = (TS_PROGRAM *)(prv->map[pid].target);
|
|
656 if( (pgrm == NULL) || (pgrm->pmt == NULL) ){
|
|
657 /* this code will never execute */
|
|
658 r = ARIB_STD_B25_ERROR_PMT_PARSE_FAILURE;
|
|
659 goto LAST;
|
|
660 }
|
|
661 m = pgrm->pmt->put(pgrm->pmt, &hdr, p, n);
|
|
662 if(m < 0){
|
|
663 r = ARIB_STD_B25_ERROR_PMT_PARSE_FAILURE;
|
|
664 goto LAST;
|
|
665 }
|
|
666 m = pgrm->pmt->get_count(pgrm->pmt);
|
|
667 if(m < 0){
|
|
668 r = ARIB_STD_B25_ERROR_PMT_PARSE_FAILURE;
|
|
669 goto LAST;
|
|
670 }
|
|
671 if(m == 0){
|
|
672 goto NEXT;
|
|
673 }
|
|
674 r = proc_pmt(prv, pgrm);
|
|
675 if(r < 0){
|
|
676 goto LAST;
|
|
677 }
|
|
678 }else if(prv->map[pid].type == PID_MAP_TYPE_EMM){
|
|
679 if( prv->emm_proc_on == 0){
|
|
680 goto NEXT;
|
|
681 }
|
|
682 if( prv->emm == NULL ){
|
|
683 prv->emm = create_ts_section_parser();
|
|
684 if(prv->emm == NULL){
|
|
685 r = ARIB_STD_B25_ERROR_EMM_PARSE_FAILURE;
|
|
686 goto LAST;
|
|
687 }
|
|
688 }
|
|
689 m = prv->emm->put(prv->emm, &hdr, p, n);
|
|
690 if(m < 0){
|
|
691 r = ARIB_STD_B25_ERROR_EMM_PARSE_FAILURE;
|
|
692 goto LAST;
|
|
693 }
|
|
694 m = prv->emm->get_count(prv->emm);
|
|
695 if(m < 0){
|
|
696 r = ARIB_STD_B25_ERROR_EMM_PARSE_FAILURE;
|
|
697 goto LAST;
|
|
698 }
|
|
699 if(m == 0){
|
|
700 goto NEXT;
|
|
701 }
|
|
702 r = proc_emm(prv);
|
|
703 if(r < 0){
|
|
704 goto LAST;
|
|
705 }
|
|
706 }else if(pid == 0x0001){
|
|
707 if( prv->cat == NULL ){
|
|
708 prv->cat = create_ts_section_parser();
|
|
709 if(prv->cat == NULL){
|
|
710 r = ARIB_STD_B25_ERROR_NO_ENOUGH_MEMORY;
|
|
711 goto LAST;
|
|
712 }
|
|
713 }
|
|
714 m = prv->cat->put(prv->cat, &hdr, p, n);
|
|
715 if(m < 0){
|
|
716 r = ARIB_STD_B25_ERROR_CAT_PARSE_FAILURE;
|
|
717 goto LAST;
|
|
718 }
|
|
719 m = prv->cat->get_count(prv->cat);
|
|
720 if(m < 0){
|
|
721 r = ARIB_STD_B25_ERROR_CAT_PARSE_FAILURE;
|
|
722 goto LAST;
|
|
723 }
|
|
724 if(m == 0){
|
|
725 goto NEXT;
|
|
726 }
|
|
727 r = proc_cat(prv);
|
|
728 if(r < 0){
|
|
729 goto LAST;
|
|
730 }
|
|
731 }else if(pid == 0x0000){
|
|
732 if( prv->pat == NULL ){
|
|
733 prv->pat = create_ts_section_parser();
|
|
734 if(prv->pat == NULL){
|
|
735 r = ARIB_STD_B25_ERROR_NO_ENOUGH_MEMORY;
|
|
736 goto LAST;
|
|
737 }
|
|
738 }
|
|
739 m = prv->pat->put(prv->pat, &hdr, p, n);
|
|
740 if(m < 0){
|
|
741 r = ARIB_STD_B25_ERROR_PAT_PARSE_FAILURE;
|
|
742 goto LAST;
|
|
743 }
|
|
744 m = prv->pat->get_count(prv->pat);
|
|
745 if(m < 0){
|
|
746 r = ARIB_STD_B25_ERROR_PAT_PARSE_FAILURE;
|
|
747 goto LAST;
|
|
748 }
|
|
749 if(m == 0){
|
|
750 goto NEXT;
|
|
751 }
|
|
752 r = proc_pat(prv);
|
|
753 if(r < 0){
|
|
754 goto LAST;
|
|
755 }
|
|
756 }
|
|
757
|
|
758 NEXT:
|
|
759 curr += unit;
|
|
760 }
|
|
761
|
|
762 LAST:
|
|
763
|
|
764 m = curr - prv->sbuf.head;
|
|
765 n = tail - curr;
|
|
766 if( (n < 1024) || (m > (prv->sbuf.max/2) ) ){
|
|
767 p = prv->sbuf.pool;
|
|
768 memcpy(p, curr, n);
|
|
769 prv->sbuf.head = p;
|
|
770 prv->sbuf.tail = p+n;
|
|
771 }else{
|
|
772 prv->sbuf.head = curr;
|
|
773 }
|
|
774
|
|
775 return r;
|
|
776 }
|
|
777
|
|
778 static int put_arib_std_b25(void *std_b25, ARIB_STD_B25_BUFFER *buf)
|
|
779 {
|
|
780 int32_t n;
|
|
781
|
|
782 ARIB_STD_B25_PRIVATE_DATA *prv;
|
|
783
|
|
784 prv = private_data(std_b25);
|
|
785 if( (prv == NULL) || (buf == NULL) ){
|
|
786 return ARIB_STD_B25_ERROR_INVALID_PARAM;
|
|
787 }
|
|
788
|
|
789 if(!append_work_buffer(&(prv->sbuf), buf->data, buf->size)){
|
|
790 return ARIB_STD_B25_ERROR_NO_ENOUGH_MEMORY;
|
|
791 }
|
|
792
|
|
793 if(prv->unit_size < 188){
|
|
794 n = select_unit_size(prv);
|
|
795 if(n < 0){
|
|
796 return n;
|
|
797 }
|
|
798 if(prv->unit_size < 188){
|
|
799 /* need more data */
|
|
800 return 0;
|
|
801 }
|
|
802 }
|
|
803
|
|
804 if(prv->p_count < 1){
|
|
805 n = find_pat(prv);
|
|
806 if(n < 0){
|
|
807 return n;
|
|
808 }
|
|
809 if(prv->p_count < 1){
|
|
810 if(prv->sbuf_offset < (16*1024*1024)){
|
|
811 /* need more data */
|
|
812 return 0;
|
|
813 }else{
|
|
814 /* exceed sbuf limit */
|
|
815 return ARIB_STD_B25_ERROR_NO_PAT_IN_HEAD_16M;
|
|
816 }
|
|
817 }
|
|
818 prv->sbuf_offset = 0;
|
|
819 }
|
|
820
|
|
821 if(!check_pmt_complete(prv)){
|
|
822 n = find_pmt(prv);
|
|
823 if(n < 0){
|
|
824 return n;
|
|
825 }
|
|
826 if(!check_pmt_complete(prv)){
|
|
827 if(prv->sbuf_offset < (32*1024*1024)){
|
|
828 /* need more data */
|
|
829 return 0;
|
|
830 }else{
|
|
831 /* exceed sbuf limit */
|
|
832 return ARIB_STD_B25_ERROR_NO_PMT_IN_HEAD_32M;
|
|
833 }
|
|
834 }
|
|
835 prv->sbuf_offset = 0;
|
|
836 }
|
|
837
|
|
838 if(!check_ecm_complete(prv)){
|
|
839 n = find_ecm(prv);
|
|
840 if(n < 0){
|
|
841 return n;
|
|
842 }
|
|
843 if(!check_ecm_complete(prv)){
|
|
844 if(prv->sbuf_offset < (32*1024*1024)){
|
|
845 /* need more data */
|
|
846 return 0;
|
|
847 }else{
|
|
848 /* exceed sbuf limit */
|
|
849 return ARIB_STD_B25_ERROR_NO_ECM_IN_HEAD_32M;
|
|
850 }
|
|
851 }
|
|
852 prv->sbuf_offset = 0;
|
|
853 }
|
|
854
|
|
855 return proc_arib_std_b25(prv);
|
|
856 }
|
|
857
|
|
858 static int get_arib_std_b25(void *std_b25, ARIB_STD_B25_BUFFER *buf)
|
|
859 {
|
|
860 ARIB_STD_B25_PRIVATE_DATA *prv;
|
|
861 prv = private_data(std_b25);
|
|
862 if( (prv == NULL) || (buf == NULL) ){
|
|
863 return ARIB_STD_B25_ERROR_INVALID_PARAM;
|
|
864 }
|
|
865
|
|
866 buf->data = prv->dbuf.head;
|
|
867 buf->size = prv->dbuf.tail - prv->dbuf.head;
|
|
868
|
|
869 reset_work_buffer(&(prv->dbuf));
|
|
870
|
|
871 return 0;
|
|
872 }
|
|
873
|
|
874 static int get_program_count_arib_std_b25(void *std_b25)
|
|
875 {
|
|
876 ARIB_STD_B25_PRIVATE_DATA *prv;
|
|
877
|
|
878 prv = private_data(std_b25);
|
|
879 if(prv == NULL){
|
|
880 return ARIB_STD_B25_ERROR_INVALID_PARAM;
|
|
881 }
|
|
882
|
|
883 return prv->p_count;
|
|
884 }
|
|
885
|
|
886 static int get_program_info_arib_std_b25(void *std_b25, ARIB_STD_B25_PROGRAM_INFO *info, int idx)
|
|
887 {
|
|
888 ARIB_STD_B25_PRIVATE_DATA *prv;
|
|
889
|
|
890 TS_PROGRAM *pgrm;
|
|
891
|
|
892 TS_STREAM_ELEM *strm;
|
|
893 DECRYPTOR_ELEM *dec;
|
|
894
|
|
895 int32_t pid;
|
|
896
|
|
897 prv = private_data(std_b25);
|
|
898 if( (prv == NULL) || (info == NULL) || (idx < 0) || (idx >= prv->p_count) ){
|
|
899 return ARIB_STD_B25_ERROR_INVALID_PARAM;
|
|
900 }
|
|
901
|
|
902 pgrm = prv->program + idx;
|
|
903
|
|
904 memset(info, 0, sizeof(ARIB_STD_B25_PROGRAM_INFO));
|
|
905
|
|
906 info->program_number = pgrm->program_number;
|
|
907
|
|
908 pid = pgrm->pmt_pid;
|
|
909 info->total_packet_count += prv->map[pid].normal_packet;
|
|
910 info->total_packet_count += prv->map[pid].undecrypted;
|
|
911 info->undecrypted_packet_count += prv->map[pid].undecrypted;
|
|
912
|
|
913 pid = pgrm->pcr_pid;
|
|
914 if( (pid != 0) && (pid != 0x1fff) ){
|
|
915 info->total_packet_count += prv->map[pid].normal_packet;
|
|
916 info->total_packet_count += prv->map[pid].undecrypted;
|
|
917 info->undecrypted_packet_count += prv->map[pid].undecrypted;
|
|
918 }
|
|
919
|
|
920 strm = pgrm->streams.head;
|
|
921 while(strm != NULL){
|
|
922 pid = strm->pid;
|
|
923 if(prv->map[pid].type == PID_MAP_TYPE_ECM){
|
|
924 dec = (DECRYPTOR_ELEM *)(prv->map[pid].target);
|
|
925 info->ecm_unpurchased_count += dec->unpurchased;
|
|
926 info->last_ecm_error_code = dec->last_error;
|
|
927 }
|
|
928 info->total_packet_count += prv->map[pid].normal_packet;
|
|
929 info->total_packet_count += prv->map[pid].undecrypted;
|
|
930 info->undecrypted_packet_count += prv->map[pid].undecrypted;
|
|
931 strm = (TS_STREAM_ELEM *)(strm->next);
|
|
932 }
|
|
933
|
|
934 return 0;
|
|
935 }
|
|
936
|
|
937 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
938 private method implementation
|
|
939 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
|
940 static ARIB_STD_B25_PRIVATE_DATA *private_data(void *std_b25)
|
|
941 {
|
|
942 ARIB_STD_B25 *p;
|
|
943 ARIB_STD_B25_PRIVATE_DATA *r;
|
|
944
|
|
945 p = (ARIB_STD_B25 *)std_b25;
|
|
946 if(p == NULL){
|
|
947 return NULL;
|
|
948 }
|
|
949
|
|
950 r = (ARIB_STD_B25_PRIVATE_DATA *)p->private_data;
|
|
951 if( ((void *)(r+1)) != ((void *)p) ){
|
|
952 return NULL;
|
|
953 }
|
|
954
|
|
955 return r;
|
|
956 }
|
|
957
|
|
958 static void teardown(ARIB_STD_B25_PRIVATE_DATA *prv)
|
|
959 {
|
|
960 int i;
|
|
961
|
|
962 prv->unit_size = 0;
|
|
963 prv->sbuf_offset = 0;
|
|
964
|
|
965 if(prv->pat != NULL){
|
|
966 prv->pat->release(prv->pat);
|
|
967 prv->pat = NULL;
|
|
968 }
|
|
969 if(prv->cat != NULL){
|
|
970 prv->cat->release(prv->cat);
|
|
971 prv->cat = NULL;
|
|
972 }
|
|
973
|
|
974 if(prv->program != NULL){
|
|
975 for(i=0;i<prv->p_count;i++){
|
|
976 release_program(prv, prv->program+i);
|
|
977 }
|
|
978 free(prv->program);
|
|
979 prv->program = NULL;
|
|
980 }
|
|
981 prv->p_count = 0;
|
|
982
|
|
983 clear_stream_list(&(prv->strm_pool));
|
|
984
|
|
985 while(prv->decrypt.head != NULL){
|
|
986 remove_decryptor(prv, prv->decrypt.head);
|
|
987 }
|
|
988
|
|
989 memset(prv->map, 0, sizeof(prv->map));
|
|
990
|
|
991 prv->emm_pid = 0;
|
|
992 if(prv->emm != NULL){
|
|
993 prv->emm->release(prv->emm);
|
|
994 prv->emm = NULL;
|
|
995 }
|
|
996
|
|
997 release_work_buffer(&(prv->sbuf));
|
|
998 release_work_buffer(&(prv->dbuf));
|
|
999 }
|
|
1000
|
|
1001 static int select_unit_size(ARIB_STD_B25_PRIVATE_DATA *prv)
|
|
1002 {
|
|
1003 int i;
|
|
1004 int m,n,w;
|
3
|
1005 int count[320-188];
|
0
|
1006
|
|
1007 unsigned char *head;
|
|
1008 unsigned char *buf;
|
|
1009 unsigned char *tail;
|
|
1010
|
|
1011 head = prv->sbuf.head;
|
|
1012 tail = prv->sbuf.tail;
|
54
|
1013
|
0
|
1014 buf = head;
|
|
1015 memset(count, 0, sizeof(count));
|
|
1016
|
3
|
1017 // 1st step, count up 0x47 interval
|
|
1018 while( (buf+188) < tail ){
|
|
1019 if(buf[0] != 0x47){
|
|
1020 buf += 1;
|
|
1021 continue;
|
0
|
1022 }
|
3
|
1023 m = 320;
|
|
1024 if( buf+m > tail ){
|
|
1025 m = tail-buf;
|
|
1026 }
|
|
1027 for(i=188;i<m;i++){
|
|
1028 if(buf[i] == 0x47){
|
|
1029 count[i-188] += 1;
|
0
|
1030 }
|
|
1031 }
|
|
1032 buf += 1;
|
|
1033 }
|
|
1034
|
3
|
1035 // 2nd step, select maximum appeared interval
|
0
|
1036 m = 0;
|
|
1037 n = 0;
|
|
1038 for(i=188;i<320;i++){
|
3
|
1039 if(m < count[i-188]){
|
|
1040 m = count[i-188];
|
0
|
1041 n = i;
|
|
1042 }
|
|
1043 }
|
|
1044
|
3
|
1045 // 3rd step, verify unit_size
|
0
|
1046 w = m*n;
|
3
|
1047 if( (m < 8) || ((w+3*n) < (tail-head)) ){
|
0
|
1048 return ARIB_STD_B25_ERROR_NON_TS_INPUT_STREAM;
|
|
1049 }
|
|
1050
|
|
1051 prv->unit_size = n;
|
|
1052
|
|
1053 return 0;
|
|
1054 }
|
|
1055
|
|
1056 static int find_pat(ARIB_STD_B25_PRIVATE_DATA *prv)
|
|
1057 {
|
|
1058 int r;
|
|
1059 int n,size;
|
|
1060
|
|
1061 int32_t unit;
|
|
1062
|
|
1063 uint8_t *p;
|
|
1064 uint8_t *curr;
|
|
1065 uint8_t *tail;
|
|
1066
|
|
1067 TS_HEADER hdr;
|
|
1068
|
|
1069 r = 0;
|
|
1070 unit = prv->unit_size;
|
|
1071 curr = prv->sbuf.head + prv->sbuf_offset;
|
|
1072 tail = prv->sbuf.tail;
|
|
1073
|
|
1074 while( (curr+unit) < tail ){
|
|
1075 if( (curr[0] != 0x47) || (curr[unit] != 0x47) ){
|
|
1076 p = resync(curr, tail, unit);
|
|
1077 if(p == NULL){
|
|
1078 goto LAST;
|
|
1079 }
|
|
1080 curr = p;
|
|
1081 }
|
|
1082 extract_ts_header(&hdr, curr);
|
|
1083 if(hdr.pid == 0x0000){
|
|
1084
|
|
1085 p = curr+4;
|
|
1086 if(hdr.adaptation_field_control & 0x02){
|
|
1087 p += (p[0]+1);
|
|
1088 }
|
|
1089 size = 188 - (p-curr);
|
|
1090 if(size < 1){
|
|
1091 goto NEXT;
|
|
1092 }
|
|
1093
|
|
1094 if(prv->pat == NULL){
|
|
1095 prv->pat = create_ts_section_parser();
|
|
1096 if(prv->pat == NULL){
|
|
1097 return ARIB_STD_B25_ERROR_NO_ENOUGH_MEMORY;
|
|
1098 }
|
|
1099 }
|
|
1100
|
|
1101 n = prv->pat->put(prv->pat, &hdr, p, size);
|
|
1102 if(n < 0){
|
|
1103 r = ARIB_STD_B25_ERROR_PAT_PARSE_FAILURE;
|
|
1104 curr += unit;
|
|
1105 goto LAST;
|
|
1106 }
|
|
1107 n = prv->pat->get_count(prv->pat);
|
|
1108 if(n < 0){
|
|
1109 r = ARIB_STD_B25_ERROR_PAT_PARSE_FAILURE;
|
|
1110 curr += unit;
|
|
1111 goto LAST;
|
|
1112 }
|
|
1113 if(n > 0){
|
|
1114 curr += unit;
|
|
1115 goto LAST;
|
|
1116 }
|
|
1117 }
|
|
1118 NEXT:
|
|
1119 curr += unit;
|
|
1120 }
|
|
1121
|
|
1122 LAST:
|
|
1123 prv->sbuf_offset = curr - prv->sbuf.head;
|
|
1124
|
|
1125 if( (prv->pat != NULL) && (prv->pat->get_count(prv->pat) > 0) ){
|
|
1126 r = proc_pat(prv);
|
|
1127 }
|
|
1128
|
|
1129 return r;
|
|
1130 }
|
|
1131
|
|
1132 static int proc_pat(ARIB_STD_B25_PRIVATE_DATA *prv)
|
|
1133 {
|
|
1134 int r;
|
|
1135 int i,n;
|
|
1136 int len;
|
|
1137 int count;
|
|
1138
|
|
1139 int32_t program_number;
|
|
1140 int32_t pid;
|
|
1141
|
|
1142 uint8_t *head;
|
|
1143 uint8_t *tail;
|
|
1144
|
|
1145 TS_PROGRAM *work;
|
|
1146 TS_SECTION sect;
|
|
1147
|
|
1148 r = 0;
|
|
1149 memset(§, 0, sizeof(sect));
|
|
1150
|
|
1151 n = prv->pat->get(prv->pat, §);
|
|
1152 if(n < 0){
|
|
1153 r = ARIB_STD_B25_ERROR_PAT_PARSE_FAILURE;
|
|
1154 goto LAST;
|
|
1155 }
|
|
1156
|
|
1157 if(sect.hdr.table_id != TS_SECTION_ID_PROGRAM_ASSOCIATION){
|
|
1158 r = ARIB_STD_B25_WARN_TS_SECTION_ID_MISSMATCH;
|
|
1159 goto LAST;
|
|
1160 }
|
|
1161
|
|
1162 len = (sect.tail - sect.data) - 4;
|
|
1163
|
|
1164 count = len / 4;
|
|
1165 work = (TS_PROGRAM *)calloc(count, sizeof(TS_PROGRAM));
|
|
1166 if(work == NULL){
|
|
1167 r = ARIB_STD_B25_ERROR_NO_ENOUGH_MEMORY;
|
|
1168 goto LAST;
|
|
1169 }
|
|
1170
|
|
1171 if(prv->program != NULL){
|
|
1172 for(i=0;i<prv->p_count;i++){
|
|
1173 release_program(prv, prv->program+i);
|
|
1174 }
|
|
1175 free(prv->program);
|
|
1176 prv->program = NULL;
|
|
1177 }
|
|
1178 prv->p_count = 0;
|
|
1179 memset(&(prv->map), 0, sizeof(prv->map));
|
|
1180
|
|
1181 head = sect.data;
|
|
1182 tail = sect.tail-4;
|
|
1183
|
|
1184 i = 0;
|
|
1185 while( (head+4) <= tail ){
|
|
1186 program_number = ((head[0] << 8) | head[1]);
|
|
1187 pid = ((head[2] << 8) | head[3]) & 0x1fff;
|
|
1188 if(program_number != 0){
|
|
1189 work[i].program_number = program_number;
|
|
1190 work[i].pmt_pid = pid;
|
|
1191 work[i].pmt = create_ts_section_parser();
|
|
1192 if(work[i].pmt == NULL){
|
|
1193 r = ARIB_STD_B25_ERROR_NO_ENOUGH_MEMORY;
|
|
1194 break;
|
|
1195 }
|
|
1196 prv->map[pid].type = PID_MAP_TYPE_PMT;
|
|
1197 prv->map[pid].target = work+i;
|
|
1198 i += 1;
|
|
1199 }
|
|
1200 head += 4;
|
|
1201 }
|
|
1202
|
|
1203 prv->program = work;
|
|
1204 prv->p_count = i;
|
|
1205
|
|
1206 prv->map[0x0000].ref = 1;
|
|
1207 prv->map[0x0000].type = PID_MAP_TYPE_PAT;
|
|
1208 prv->map[0x0000].target = NULL;
|
|
1209
|
|
1210 LAST:
|
|
1211 if(sect.raw != NULL){
|
|
1212 n = prv->pat->ret(prv->pat, §);
|
|
1213 if( (n < 0) && (r == 0) ){
|
|
1214 r = ARIB_STD_B25_ERROR_PAT_PARSE_FAILURE;
|
|
1215 }
|
|
1216 }
|
|
1217
|
|
1218 return r;
|
|
1219 }
|
|
1220
|
|
1221 static int check_pmt_complete(ARIB_STD_B25_PRIVATE_DATA *prv)
|
|
1222 {
|
|
1223 int i,n;
|
|
1224 int num[3];
|
|
1225
|
|
1226 memset(num, 0, sizeof(num));
|
|
1227
|
|
1228 for(i=0;i<prv->p_count;i++){
|
|
1229 n = prv->program[i].phase;
|
|
1230 if(n < 0){
|
|
1231 n = 0;
|
|
1232 }else if(n > 2){
|
|
1233 n = 2;
|
|
1234 }
|
|
1235 num[n] += 1;
|
|
1236 }
|
|
1237
|
|
1238 if(num[2] > 0){
|
|
1239 return 1;
|
|
1240 }
|
|
1241
|
|
1242 if(num[0] > 0){
|
|
1243 return 0;
|
|
1244 }
|
|
1245
|
|
1246 return 1;
|
|
1247 }
|
|
1248
|
|
1249 static int find_pmt(ARIB_STD_B25_PRIVATE_DATA *prv)
|
|
1250 {
|
|
1251 int r;
|
|
1252 int n,size;
|
|
1253
|
|
1254 int32_t unit;
|
|
1255
|
|
1256 uint8_t *p;
|
|
1257 uint8_t *curr;
|
|
1258 uint8_t *tail;
|
|
1259
|
|
1260 TS_HEADER hdr;
|
|
1261 TS_PROGRAM *pgrm;
|
|
1262
|
|
1263 r = 0;
|
|
1264 unit = prv->unit_size;
|
|
1265 curr = prv->sbuf.head + prv->sbuf_offset;
|
|
1266 tail = prv->sbuf.tail;
|
|
1267
|
|
1268 while( (curr+unit) < tail ){
|
|
1269
|
|
1270 if( (curr[0] != 0x47) || (curr[unit] != 0x47) ){
|
|
1271 p = resync(curr, tail, unit);
|
|
1272 if(p == NULL){
|
|
1273 goto LAST;
|
|
1274 }
|
|
1275 curr = p;
|
|
1276 }
|
|
1277
|
|
1278 extract_ts_header(&hdr, curr);
|
|
1279
|
|
1280 if(prv->map[hdr.pid].type != PID_MAP_TYPE_PMT){
|
|
1281 goto NEXT;
|
|
1282 }
|
|
1283 pgrm = (TS_PROGRAM *)(prv->map[hdr.pid].target);
|
|
1284 if(pgrm == NULL){
|
|
1285 goto NEXT;
|
|
1286 }
|
|
1287
|
|
1288 if(pgrm->phase == 0){
|
|
1289
|
|
1290 p = curr + 4;
|
|
1291 if(hdr.adaptation_field_control & 0x02){
|
|
1292 p += (p[0]+1);
|
|
1293 }
|
|
1294 size = 188 - (p-curr);
|
|
1295 if(size < 1){
|
|
1296 goto NEXT;
|
|
1297 }
|
|
1298
|
|
1299 if(pgrm->pmt == NULL){
|
|
1300 /* this code will never execute */
|
|
1301 r = ARIB_STD_B25_ERROR_PMT_PARSE_FAILURE;
|
|
1302 curr += unit;
|
|
1303 goto LAST;
|
|
1304 }
|
|
1305
|
|
1306 n = pgrm->pmt->put(pgrm->pmt, &hdr, p, size);
|
|
1307 if(n < 0){
|
|
1308 r = ARIB_STD_B25_ERROR_PMT_PARSE_FAILURE;
|
|
1309 curr += unit;
|
|
1310 goto LAST;
|
|
1311 }
|
|
1312 n = pgrm->pmt->get_count(pgrm->pmt);
|
|
1313 if(n < 0){
|
|
1314 r =ARIB_STD_B25_ERROR_PMT_PARSE_FAILURE;
|
|
1315 curr += unit;
|
|
1316 goto LAST;
|
|
1317 }
|
|
1318 if(n == 0){
|
|
1319 goto NEXT;
|
|
1320 }
|
|
1321 r = proc_pmt(prv, pgrm);
|
|
1322 if(r < 0){
|
|
1323 curr += unit;
|
|
1324 goto LAST;
|
|
1325 }
|
|
1326 if(r > 0){
|
|
1327 /* broken or unexpected section data */
|
|
1328 goto NEXT;
|
|
1329 }
|
|
1330 pgrm->phase = 1;
|
|
1331 if(check_pmt_complete(prv)){
|
|
1332 curr += unit;
|
|
1333 goto LAST;
|
|
1334 }
|
|
1335 }else{
|
|
1336 pgrm->phase = 2;
|
|
1337 curr += unit;
|
|
1338 goto LAST;
|
|
1339 }
|
|
1340
|
|
1341 NEXT:
|
|
1342 curr += unit;
|
|
1343 }
|
|
1344
|
|
1345 LAST:
|
|
1346 prv->sbuf_offset = curr - prv->sbuf.head;
|
|
1347
|
|
1348 return r;
|
|
1349 }
|
|
1350
|
|
1351 static int proc_pmt(ARIB_STD_B25_PRIVATE_DATA *prv, TS_PROGRAM *pgrm)
|
|
1352 {
|
|
1353 int r;
|
|
1354
|
|
1355 int n;
|
|
1356 int length;
|
|
1357
|
|
1358 uint8_t *head;
|
|
1359 uint8_t *tail;
|
|
1360
|
|
1361 int32_t ecm_pid;
|
|
1362 int32_t pid;
|
|
1363 int32_t type;
|
|
1364
|
|
1365 TS_SECTION sect;
|
|
1366
|
|
1367 DECRYPTOR_ELEM *dec[2];
|
|
1368 DECRYPTOR_ELEM *dw;
|
|
1369
|
|
1370 TS_STREAM_ELEM *strm;
|
|
1371
|
|
1372 r = 0;
|
|
1373 dec[0] = NULL;
|
|
1374 memset(§, 0, sizeof(sect));
|
|
1375
|
|
1376 n = pgrm->pmt->get(pgrm->pmt, §);
|
|
1377 if(n < 0){
|
|
1378 r = ARIB_STD_B25_ERROR_PMT_PARSE_FAILURE;
|
|
1379 goto LAST;
|
|
1380 }
|
|
1381 if(sect.hdr.table_id != TS_SECTION_ID_PROGRAM_MAP){
|
|
1382 r = ARIB_STD_B25_WARN_TS_SECTION_ID_MISSMATCH;
|
|
1383 goto LAST;
|
|
1384 }
|
|
1385
|
|
1386 head = sect.data;
|
|
1387 tail = sect.tail-4;
|
|
1388
|
|
1389 pgrm->pcr_pid = ((head[0] << 8) | head[1]) & 0x1fff;
|
|
1390 length = ((head[2] << 8) | head[3]) & 0x0fff;
|
|
1391 head += 4;
|
|
1392 if(head+length > tail){
|
|
1393 r = ARIB_STD_B25_WARN_BROKEN_TS_SECTION;
|
|
1394 goto LAST;
|
|
1395 }
|
|
1396
|
|
1397 /* find major ecm_pid and regist decryptor */
|
3
|
1398 ecm_pid = find_ca_descriptor_pid(head, head+length, prv->ca_system_id);
|
0
|
1399 if( (ecm_pid != 0) && (ecm_pid != 0x1fff) ){
|
|
1400 dec[0] = set_decryptor(prv, ecm_pid);
|
|
1401 if(dec[0] == NULL){
|
|
1402 r = ARIB_STD_B25_ERROR_NO_ENOUGH_MEMORY;
|
|
1403 goto LAST;
|
|
1404 }
|
|
1405 dec[0]->ref += 1;
|
|
1406 }
|
|
1407 head += length;
|
|
1408
|
|
1409 /* unref old stream entries */
|
|
1410 while( (strm = get_stream_list_head(&(pgrm->old_strm))) != NULL ){
|
|
1411 unref_stream(prv, strm->pid);
|
|
1412 memset(strm, 0, sizeof(TS_STREAM_ELEM));
|
|
1413 put_stream_list_tail(&(prv->strm_pool), strm);
|
|
1414 }
|
|
1415
|
|
1416 /* save current streams */
|
|
1417 memcpy(&(pgrm->old_strm), &(pgrm->streams), sizeof(TS_STREAM_LIST));
|
|
1418 memset(&(pgrm->streams), 0, sizeof(TS_STREAM_LIST));
|
|
1419
|
|
1420 /* add current stream entries */
|
|
1421 if( (ecm_pid != 0) && (ecm_pid != 0x1fff) ){
|
|
1422 if(!add_ecm_stream(prv, &(pgrm->streams), ecm_pid)){
|
|
1423 r = ARIB_STD_B25_ERROR_NO_ENOUGH_MEMORY;
|
|
1424 goto LAST;
|
|
1425 }
|
|
1426 }
|
|
1427
|
|
1428 while( head+4 < tail ){
|
|
1429
|
|
1430 type = head[0];
|
|
1431 pid = ((head[1] << 8) | head[2]) & 0x1fff;
|
|
1432 length = ((head[3] << 8) | head[4]) & 0x0fff;
|
|
1433 head += 5;
|
3
|
1434 ecm_pid = find_ca_descriptor_pid(head, head+length, prv->ca_system_id);
|
0
|
1435 head += length;
|
|
1436
|
|
1437 if( (ecm_pid != 0) && (ecm_pid != 0x1fff) ){
|
|
1438 dec[1] = set_decryptor(prv, ecm_pid);
|
|
1439 if(dec[1] == NULL){
|
|
1440 r = ARIB_STD_B25_ERROR_NO_ENOUGH_MEMORY;
|
|
1441 goto LAST;
|
|
1442 }
|
|
1443 if(!add_ecm_stream(prv, &(pgrm->streams), ecm_pid)){
|
|
1444 r = ARIB_STD_B25_ERROR_NO_ENOUGH_MEMORY;
|
|
1445 goto LAST;
|
|
1446 }
|
|
1447 }else{
|
|
1448 dec[1] = NULL;
|
|
1449 }
|
|
1450
|
|
1451 strm = get_stream_list_head(&(prv->strm_pool));
|
|
1452 if( strm == NULL ){
|
|
1453 strm = create_stream_elem(pid, type);
|
|
1454 if(strm == NULL){
|
|
1455 r = ARIB_STD_B25_ERROR_NO_ENOUGH_MEMORY;
|
|
1456 goto LAST;
|
|
1457 }
|
|
1458 }else{
|
|
1459 strm->pid = pid;
|
|
1460 strm->type = type;
|
|
1461 }
|
|
1462
|
|
1463 prv->map[pid].type = PID_MAP_TYPE_OTHER;
|
|
1464 prv->map[pid].ref += 1;
|
|
1465
|
|
1466 dw = select_active_decryptor(dec[0], dec[1], ecm_pid);
|
|
1467 bind_stream_decryptor(prv, pid, dw);
|
|
1468
|
|
1469 put_stream_list_tail(&(pgrm->streams), strm);
|
|
1470 }
|
|
1471
|
|
1472 LAST:
|
|
1473 if( dec[0] != NULL ){
|
|
1474 dec[0]->ref -= 1;
|
|
1475 if( dec[0]->ref < 1 ){
|
|
1476 remove_decryptor(prv, dec[0]);
|
|
1477 dec[0] = NULL;
|
|
1478 }
|
|
1479 }
|
|
1480
|
|
1481 if(sect.raw != NULL){
|
|
1482 n = pgrm->pmt->ret(pgrm->pmt, §);
|
|
1483 if( (n < 0) && (r == 0) ){
|
|
1484 return ARIB_STD_B25_ERROR_PMT_PARSE_FAILURE;
|
|
1485 }
|
|
1486 }
|
|
1487
|
|
1488 return 0;
|
|
1489 }
|
|
1490
|
3
|
1491 static int32_t find_ca_descriptor_pid(uint8_t *head, uint8_t *tail, int32_t ca_system_id)
|
0
|
1492 {
|
|
1493 uint32_t ca_pid;
|
|
1494 uint32_t ca_sys_id;
|
|
1495
|
|
1496 uint32_t tag;
|
|
1497 uint32_t len;
|
|
1498
|
|
1499 while(head+1 < tail){
|
|
1500 tag = head[0];
|
|
1501 len = head[1];
|
|
1502 head += 2;
|
|
1503 if( (tag == 0x09) && /* CA_descriptor */
|
|
1504 (len >= 4) &&
|
|
1505 (head+len <= tail) ){
|
|
1506 ca_sys_id = ((head[0] << 8) | head[1]);
|
|
1507 ca_pid = ((head[2] << 8) | head[3]) & 0x1fff;
|
3
|
1508 if(ca_sys_id == ca_system_id){
|
|
1509 return ca_pid;
|
|
1510 }
|
0
|
1511 }
|
|
1512 head += len;
|
|
1513 }
|
|
1514
|
|
1515 return 0;
|
|
1516 }
|
|
1517
|
|
1518 static int add_ecm_stream(ARIB_STD_B25_PRIVATE_DATA *prv, TS_STREAM_LIST *list, int32_t ecm_pid)
|
|
1519 {
|
|
1520 TS_STREAM_ELEM *strm;
|
|
1521
|
|
1522 strm = find_stream_list_elem(list, ecm_pid);
|
|
1523 if(strm != NULL){
|
|
1524 // ECM is already registered
|
|
1525 return 1;
|
|
1526 }
|
|
1527
|
|
1528 strm = get_stream_list_head(&(prv->strm_pool));
|
|
1529 if(strm == NULL){
|
|
1530 strm = create_stream_elem(ecm_pid, PID_MAP_TYPE_ECM);
|
|
1531 if(strm == NULL){
|
|
1532 return 0;
|
|
1533 }
|
|
1534 }else{
|
|
1535 strm->pid = ecm_pid;
|
|
1536 strm->type = PID_MAP_TYPE_ECM;
|
|
1537 }
|
|
1538
|
|
1539 put_stream_list_tail(list, strm);
|
|
1540 prv->map[ecm_pid].ref += 1;
|
|
1541
|
|
1542 return 1;
|
|
1543 }
|
|
1544
|
|
1545 static int check_ecm_complete(ARIB_STD_B25_PRIVATE_DATA *prv)
|
|
1546 {
|
|
1547 int n,num[3];
|
|
1548 DECRYPTOR_ELEM *e;
|
|
1549
|
|
1550 memset(num, 0, sizeof(num));
|
|
1551
|
|
1552 e = prv->decrypt.head;
|
|
1553 while( e != NULL ){
|
|
1554 n = e->phase;
|
|
1555 if(n < 0){
|
|
1556 n = 0;
|
|
1557 }else if(n > 2){
|
|
1558 n = 2;
|
|
1559 }
|
|
1560 num[n] += 1;
|
|
1561 e = (DECRYPTOR_ELEM *)(e->next);
|
|
1562 }
|
|
1563
|
|
1564 if(num[2] > 0){
|
|
1565 return 1;
|
|
1566 }
|
|
1567
|
|
1568 if(num[0] > 0){
|
|
1569 return 0;
|
|
1570 }
|
|
1571
|
|
1572 return 1;
|
|
1573 }
|
|
1574
|
|
1575 static int find_ecm(ARIB_STD_B25_PRIVATE_DATA *prv)
|
|
1576 {
|
|
1577 int r;
|
|
1578 int n,size;
|
|
1579
|
|
1580 int32_t unit;
|
|
1581
|
|
1582 uint8_t *p;
|
|
1583 uint8_t *curr;
|
|
1584 uint8_t *tail;
|
|
1585
|
|
1586 TS_HEADER hdr;
|
|
1587 DECRYPTOR_ELEM *dec;
|
|
1588
|
|
1589 r = 0;
|
|
1590 unit = prv->unit_size;
|
|
1591 curr = prv->sbuf.head + prv->sbuf_offset;
|
|
1592 tail = prv->sbuf.tail;
|
|
1593
|
|
1594 while( (curr+unit) < tail ){
|
|
1595 if( (curr[0] != 0x47) || (curr[unit] != 0x47) ){
|
|
1596 p = resync(curr, tail, unit);
|
|
1597 if(p == NULL){
|
|
1598 goto LAST;
|
|
1599 }
|
|
1600 curr = p;
|
|
1601 }
|
|
1602 extract_ts_header(&hdr, curr);
|
|
1603 if(prv->map[hdr.pid].type != PID_MAP_TYPE_ECM){
|
|
1604 goto NEXT;
|
|
1605 }
|
|
1606 dec = (DECRYPTOR_ELEM *)(prv->map[hdr.pid].target);
|
|
1607 if(dec == NULL){
|
|
1608 goto NEXT;
|
|
1609 }
|
|
1610
|
|
1611 if(dec->phase == 0){
|
|
1612
|
|
1613 p = curr + 4;
|
|
1614 if(hdr.adaptation_field_control & 0x02){
|
|
1615 p += (p[0]+1);
|
|
1616 }
|
|
1617 size = 188 - (p-curr);
|
|
1618 if(size < 1){
|
|
1619 goto NEXT;
|
|
1620 }
|
|
1621
|
|
1622 if(dec->ecm == NULL){
|
|
1623 /* this code will never execute */
|
|
1624 r = ARIB_STD_B25_ERROR_ECM_PARSE_FAILURE;
|
|
1625 curr += unit;
|
|
1626 goto LAST;
|
|
1627 }
|
|
1628
|
|
1629 n = dec->ecm->put(dec->ecm, &hdr, p, size);
|
|
1630 if(n < 0){
|
|
1631 r = ARIB_STD_B25_ERROR_ECM_PARSE_FAILURE;
|
|
1632 curr += unit;
|
|
1633 goto LAST;
|
|
1634 }
|
|
1635 n = dec->ecm->get_count(dec->ecm);
|
|
1636 if(n < 0){
|
|
1637 r = ARIB_STD_B25_ERROR_ECM_PARSE_FAILURE;
|
|
1638 curr += unit;
|
|
1639 goto LAST;
|
|
1640 }
|
|
1641 if(n == 0){
|
|
1642 goto NEXT;
|
|
1643 }
|
|
1644
|
|
1645 r = proc_ecm(dec, prv->bcas, prv->multi2_round);
|
|
1646 if(r < 0){
|
|
1647 curr += unit;
|
|
1648 goto LAST;
|
|
1649 }
|
|
1650 if( (r > 0) && (r != ARIB_STD_B25_WARN_UNPURCHASED_ECM) ){
|
|
1651 /* broken or unexpected section data */
|
|
1652 goto NEXT;
|
|
1653 }
|
|
1654
|
|
1655 dec->phase = 1;
|
|
1656 if(check_ecm_complete(prv)){
|
|
1657 curr += unit;
|
|
1658 goto LAST;
|
|
1659 }
|
|
1660
|
|
1661 }else{
|
|
1662 dec->phase = 2;
|
|
1663 curr += unit;
|
|
1664 goto LAST;
|
|
1665 }
|
|
1666
|
|
1667 NEXT:
|
|
1668 curr += unit;
|
|
1669 }
|
|
1670
|
|
1671 LAST:
|
|
1672 prv->sbuf_offset = curr - prv->sbuf.head;
|
|
1673
|
|
1674 return r;
|
|
1675 }
|
|
1676
|
|
1677 static int proc_ecm(DECRYPTOR_ELEM *dec, B_CAS_CARD *bcas, int32_t multi2_round)
|
|
1678 {
|
|
1679 int r,n;
|
|
1680 int length;
|
|
1681
|
|
1682 uint8_t *p;
|
|
1683
|
|
1684 B_CAS_INIT_STATUS is;
|
|
1685 B_CAS_ECM_RESULT res;
|
|
1686
|
|
1687 TS_SECTION sect;
|
|
1688
|
|
1689 r = 0;
|
|
1690 memset(§, 0, sizeof(sect));
|
|
1691
|
|
1692 if(bcas == NULL){
|
|
1693 r = ARIB_STD_B25_ERROR_EMPTY_B_CAS_CARD;
|
|
1694 goto LAST;
|
|
1695 }
|
|
1696
|
|
1697 n = dec->ecm->get(dec->ecm, §);
|
|
1698 if(n < 0){
|
|
1699 r = ARIB_STD_B25_ERROR_ECM_PARSE_FAILURE;
|
|
1700 goto LAST;
|
|
1701 }
|
|
1702 if(sect.hdr.table_id != TS_SECTION_ID_ECM_S){
|
|
1703 r = ARIB_STD_B25_WARN_TS_SECTION_ID_MISSMATCH;
|
|
1704 goto LAST;
|
|
1705 }
|
|
1706
|
|
1707 if(dec->locked){
|
|
1708 /* previous ECM has returned unpurchased
|
|
1709 skip this pid for B-CAS card load reduction */
|
|
1710 dec->unpurchased += 1;
|
|
1711 r = ARIB_STD_B25_WARN_UNPURCHASED_ECM;
|
|
1712 goto LAST;
|
|
1713 }
|
|
1714
|
|
1715 length = (sect.tail - sect.data) - 4;
|
|
1716 p = sect.data;
|
|
1717
|
|
1718 r = bcas->proc_ecm(bcas, &res, p, length);
|
|
1719 if(r < 0){
|
|
1720 if(dec->m2 != NULL){
|
|
1721 dec->m2->clear_scramble_key(dec->m2);
|
|
1722 }
|
|
1723 r = ARIB_STD_B25_ERROR_ECM_PROC_FAILURE;
|
|
1724 goto LAST;
|
|
1725 }
|
|
1726
|
|
1727 if( (res.return_code != 0x0800) &&
|
|
1728 (res.return_code != 0x0400) &&
|
|
1729 (res.return_code != 0x0200) ){
|
|
1730 /* return_code is not equal "purchased" */
|
|
1731 if(dec->m2 != NULL){
|
|
1732 dec->m2->release(dec->m2);
|
|
1733 dec->m2 = NULL;
|
|
1734 }
|
|
1735 dec->unpurchased += 1;
|
|
1736 dec->last_error = res.return_code;
|
|
1737 dec->locked += 1;
|
|
1738 r = ARIB_STD_B25_WARN_UNPURCHASED_ECM;
|
|
1739 goto LAST;
|
|
1740 }
|
|
1741
|
|
1742 if(dec->m2 == NULL){
|
|
1743 dec->m2 = create_multi2();
|
|
1744 if(dec->m2 == NULL){
|
|
1745 return ARIB_STD_B25_ERROR_NO_ENOUGH_MEMORY;
|
|
1746 }
|
|
1747 r = bcas->get_init_status(bcas, &is);
|
|
1748 if(r < 0){
|
|
1749 return ARIB_STD_B25_ERROR_INVALID_B_CAS_STATUS;
|
|
1750 }
|
|
1751 dec->m2->set_system_key(dec->m2, is.system_key);
|
|
1752 dec->m2->set_init_cbc(dec->m2, is.init_cbc);
|
|
1753 dec->m2->set_round(dec->m2, multi2_round);
|
|
1754 }
|
|
1755
|
|
1756 dec->m2->set_scramble_key(dec->m2, res.scramble_key);
|
|
1757
|
|
1758 LAST:
|
|
1759 if(sect.raw != NULL){
|
|
1760 n = dec->ecm->ret(dec->ecm, §);
|
|
1761 if( (n < 0) && (r == 0) ){
|
|
1762 r = ARIB_STD_B25_ERROR_ECM_PARSE_FAILURE;
|
|
1763 }
|
|
1764 }
|
|
1765
|
|
1766 return r;
|
|
1767 }
|
|
1768
|
|
1769 static int proc_arib_std_b25(ARIB_STD_B25_PRIVATE_DATA *prv)
|
|
1770 {
|
|
1771 int r;
|
|
1772 int m,n;
|
|
1773
|
|
1774 int32_t crypt;
|
|
1775 int32_t unit;
|
|
1776 int32_t pid;
|
|
1777
|
|
1778 uint8_t *p;
|
|
1779 uint8_t *curr;
|
|
1780 uint8_t *tail;
|
|
1781
|
|
1782 TS_HEADER hdr;
|
|
1783 DECRYPTOR_ELEM *dec;
|
|
1784 TS_PROGRAM *pgrm;
|
|
1785
|
|
1786 unit = prv->unit_size;
|
|
1787 curr = prv->sbuf.head;
|
|
1788 tail = prv->sbuf.tail;
|
|
1789
|
|
1790 m = prv->dbuf.tail - prv->dbuf.head;
|
|
1791 n = tail - curr;
|
|
1792 if(!reserve_work_buffer(&(prv->dbuf), m+n)){
|
|
1793 return ARIB_STD_B25_ERROR_NO_ENOUGH_MEMORY;
|
|
1794 }
|
|
1795
|
|
1796 r = 0;
|
|
1797
|
|
1798 while( (curr+unit) < tail ){
|
|
1799
|
|
1800 if( (curr[0] != 0x47) || (curr[unit] != 0x47) ){
|
|
1801 p = resync(curr, tail, unit);
|
|
1802 if(p == NULL){
|
|
1803 goto LAST;
|
|
1804 }
|
|
1805 curr = p;
|
|
1806 }
|
|
1807
|
|
1808 extract_ts_header(&hdr, curr);
|
|
1809 crypt = hdr.transport_scrambling_control;
|
|
1810 pid = hdr.pid;
|
|
1811
|
54
|
1812 if(hdr.transport_error_indicator != 0){
|
|
1813 /* bit error - append output buffer without parsing */
|
|
1814 if(!append_work_buffer(&(prv->dbuf), curr, 188)){
|
|
1815 r = ARIB_STD_B25_ERROR_NO_ENOUGH_MEMORY;
|
|
1816 goto LAST;
|
|
1817 }
|
|
1818 goto NEXT;
|
|
1819 }
|
|
1820
|
0
|
1821 if( (pid == 0x1fff) && (prv->strip) ){
|
|
1822 /* strip null(padding) stream */
|
|
1823 goto NEXT;
|
|
1824 }
|
|
1825
|
|
1826 p = curr+4;
|
|
1827 if(hdr.adaptation_field_control & 0x02){
|
|
1828 p += (p[0]+1);
|
|
1829 }
|
|
1830 n = 188 - (p-curr);
|
|
1831 if( (n < 1) && ((n < 0) || (hdr.adaptation_field_control & 0x01)) ){
|
|
1832 /* broken packet */
|
|
1833 curr += 1;
|
|
1834 continue;
|
|
1835 }
|
|
1836
|
|
1837 if( (crypt != 0) &&
|
|
1838 (hdr.adaptation_field_control & 0x01) ){
|
|
1839
|
|
1840 if(prv->map[pid].type == PID_MAP_TYPE_OTHER){
|
|
1841 dec = (DECRYPTOR_ELEM *)(prv->map[pid].target);
|
|
1842 }else if( (prv->map[pid].type == 0) &&
|
|
1843 (prv->decrypt.count == 1) ){
|
|
1844 dec = prv->decrypt.head;
|
|
1845 }else{
|
|
1846 dec = NULL;
|
|
1847 }
|
|
1848
|
|
1849 if( (dec != NULL) && (dec->m2 != NULL) ){
|
|
1850 m = dec->m2->decrypt(dec->m2, crypt, p, n);
|
|
1851 if(m < 0){
|
|
1852 r = ARIB_STD_B25_ERROR_DECRYPT_FAILURE;
|
|
1853 goto LAST;
|
|
1854 }
|
|
1855 curr[3] &= 0x3f;
|
|
1856 prv->map[pid].normal_packet += 1;
|
|
1857 }else{
|
|
1858 prv->map[pid].undecrypted += 1;
|
|
1859 }
|
|
1860 }else{
|
|
1861 prv->map[pid].normal_packet += 1;
|
|
1862 }
|
|
1863
|
|
1864 if(!append_work_buffer(&(prv->dbuf), curr, 188)){
|
|
1865 r = ARIB_STD_B25_ERROR_NO_ENOUGH_MEMORY;
|
|
1866 goto LAST;
|
|
1867 }
|
|
1868
|
|
1869 if(prv->map[pid].type == PID_MAP_TYPE_ECM){
|
|
1870 dec = (DECRYPTOR_ELEM *)(prv->map[pid].target);
|
|
1871 if( (dec == NULL) || (dec->ecm == NULL) ){
|
|
1872 /* this code will never execute */
|
|
1873 r = ARIB_STD_B25_ERROR_ECM_PARSE_FAILURE;
|
|
1874 goto LAST;
|
|
1875 }
|
|
1876 m = dec->ecm->put(dec->ecm, &hdr, p, n);
|
|
1877 if(m < 0){
|
|
1878 r = ARIB_STD_B25_ERROR_ECM_PARSE_FAILURE;
|
|
1879 goto LAST;
|
|
1880 }
|
|
1881 m = dec->ecm->get_count(dec->ecm);
|
|
1882 if(m < 0){
|
|
1883 r = ARIB_STD_B25_ERROR_ECM_PARSE_FAILURE;
|
|
1884 goto LAST;
|
|
1885 }
|
|
1886 if(m == 0){
|
|
1887 goto NEXT;
|
|
1888 }
|
|
1889 r = proc_ecm(dec, prv->bcas, prv->multi2_round);
|
|
1890 if(r < 0){
|
|
1891 goto LAST;
|
|
1892 }
|
|
1893 }else if(prv->map[pid].type == PID_MAP_TYPE_PMT){
|
|
1894 pgrm = (TS_PROGRAM *)(prv->map[pid].target);
|
|
1895 if( (pgrm == NULL) || (pgrm->pmt == NULL) ){
|
|
1896 /* this code will never execute */
|
|
1897 r = ARIB_STD_B25_ERROR_PMT_PARSE_FAILURE;
|
|
1898 goto LAST;
|
|
1899 }
|
|
1900 m = pgrm->pmt->put(pgrm->pmt, &hdr, p, n);
|
|
1901 if(m < 0){
|
|
1902 r = ARIB_STD_B25_ERROR_PMT_PARSE_FAILURE;
|
|
1903 goto LAST;
|
|
1904 }
|
|
1905 m = pgrm->pmt->get_count(pgrm->pmt);
|
|
1906 if(m < 0){
|
|
1907 r = ARIB_STD_B25_ERROR_PMT_PARSE_FAILURE;
|
|
1908 goto LAST;
|
|
1909 }
|
|
1910 if(m == 0){
|
|
1911 goto NEXT;
|
|
1912 }
|
|
1913 r = proc_pmt(prv, pgrm);
|
|
1914 if(r < 0){
|
|
1915 goto LAST;
|
|
1916 }
|
|
1917 }else if(prv->map[pid].type == PID_MAP_TYPE_EMM){
|
|
1918 if( prv->emm_proc_on == 0){
|
|
1919 goto NEXT;
|
|
1920 }
|
|
1921 if( prv->emm == NULL ){
|
|
1922 prv->emm = create_ts_section_parser();
|
|
1923 if(prv->emm == NULL){
|
|
1924 r = ARIB_STD_B25_ERROR_EMM_PARSE_FAILURE;
|
|
1925 goto LAST;
|
|
1926 }
|
|
1927 }
|
|
1928 m = prv->emm->put(prv->emm, &hdr, p, n);
|
|
1929 if(m < 0){
|
|
1930 r = ARIB_STD_B25_ERROR_EMM_PARSE_FAILURE;
|
|
1931 goto LAST;
|
|
1932 }
|
|
1933 m = prv->emm->get_count(prv->emm);
|
|
1934 if(m < 0){
|
|
1935 r = ARIB_STD_B25_ERROR_EMM_PARSE_FAILURE;
|
|
1936 goto LAST;
|
|
1937 }
|
|
1938 if(m == 0){
|
|
1939 goto NEXT;
|
|
1940 }
|
|
1941 r = proc_emm(prv);
|
|
1942 if(r < 0){
|
|
1943 goto LAST;
|
|
1944 }
|
|
1945 }else if(pid == 0x0001){
|
|
1946 if( prv->cat == NULL ){
|
|
1947 prv->cat = create_ts_section_parser();
|
|
1948 if(prv->cat == NULL){
|
|
1949 r = ARIB_STD_B25_ERROR_NO_ENOUGH_MEMORY;
|
|
1950 goto LAST;
|
|
1951 }
|
|
1952 }
|
|
1953 m = prv->cat->put(prv->cat, &hdr, p, n);
|
|
1954 if(m < 0){
|
|
1955 r = ARIB_STD_B25_ERROR_CAT_PARSE_FAILURE;
|
|
1956 goto LAST;
|
|
1957 }
|
|
1958 m = prv->cat->get_count(prv->cat);
|
|
1959 if(m < 0){
|
|
1960 r = ARIB_STD_B25_ERROR_CAT_PARSE_FAILURE;
|
|
1961 goto LAST;
|
|
1962 }
|
|
1963 if(m == 0){
|
|
1964 goto NEXT;
|
|
1965 }
|
|
1966 r = proc_cat(prv);
|
|
1967 if(r < 0){
|
|
1968 goto LAST;
|
|
1969 }
|
|
1970 }else if(pid == 0x0000){
|
|
1971 if( prv->pat == NULL ){
|
|
1972 prv->pat = create_ts_section_parser();
|
|
1973 if(prv->pat == NULL){
|
|
1974 r = ARIB_STD_B25_ERROR_NO_ENOUGH_MEMORY;
|
|
1975 goto LAST;
|
|
1976 }
|
|
1977 }
|
|
1978 m = prv->pat->put(prv->pat, &hdr, p, n);
|
|
1979 if(m < 0){
|
|
1980 r = ARIB_STD_B25_ERROR_PAT_PARSE_FAILURE;
|
|
1981 goto LAST;
|
|
1982 }
|
|
1983 m = prv->pat->get_count(prv->pat);
|
|
1984 if(m < 0){
|
|
1985 r = ARIB_STD_B25_ERROR_PAT_PARSE_FAILURE;
|
|
1986 goto LAST;
|
|
1987 }
|
|
1988 if(m == 0){
|
|
1989 goto NEXT;
|
|
1990 }
|
|
1991 r = proc_pat(prv);
|
|
1992 goto LAST;
|
|
1993 }
|
|
1994
|
|
1995 NEXT:
|
|
1996 curr += unit;
|
|
1997 }
|
|
1998
|
|
1999 LAST:
|
|
2000 m = curr - prv->sbuf.head;
|
|
2001 n = tail - curr;
|
|
2002 if( (n < 1024) || (m > (prv->sbuf.max/2) ) ){
|
|
2003 p = prv->sbuf.pool;
|
|
2004 memcpy(p, curr, n);
|
|
2005 prv->sbuf.head = p;
|
|
2006 prv->sbuf.tail = p+n;
|
|
2007 }else{
|
|
2008 prv->sbuf.head = curr;
|
|
2009 }
|
|
2010
|
|
2011 return r;
|
|
2012 }
|
|
2013
|
|
2014 static int proc_cat(ARIB_STD_B25_PRIVATE_DATA *prv)
|
|
2015 {
|
|
2016 int r;
|
|
2017 int n;
|
|
2018 int emm_pid;
|
|
2019
|
|
2020 TS_SECTION sect;
|
|
2021
|
|
2022 r = 0;
|
|
2023 memset(§, 0, sizeof(sect));
|
|
2024
|
|
2025 n = prv->cat->get(prv->cat, §);
|
|
2026 if(n < 0){
|
|
2027 r = ARIB_STD_B25_ERROR_CAT_PARSE_FAILURE;
|
|
2028 goto LAST;
|
|
2029 }
|
|
2030
|
|
2031 if(sect.hdr.table_id != TS_SECTION_ID_CONDITIONAL_ACCESS){
|
|
2032 r = ARIB_STD_B25_WARN_TS_SECTION_ID_MISSMATCH;
|
|
2033 goto LAST;
|
|
2034 }
|
|
2035
|
3
|
2036 emm_pid = find_ca_descriptor_pid(sect.data, sect.tail-4, prv->ca_system_id);
|
0
|
2037 if( (emm_pid != 0x0000) && (emm_pid != 0x1fff) ){
|
|
2038 if( (prv->map[emm_pid].target != NULL) &&
|
|
2039 (prv->map[emm_pid].type == PID_MAP_TYPE_OTHER) ){
|
|
2040 DECRYPTOR_ELEM *dec;
|
|
2041 dec = (DECRYPTOR_ELEM *)(prv->map[emm_pid].target);
|
|
2042 dec->ref -= 1;
|
|
2043 if(dec->ref < 1){
|
|
2044 remove_decryptor(prv, dec);
|
|
2045 }
|
|
2046 }
|
|
2047 prv->emm_pid = emm_pid;
|
|
2048 prv->map[emm_pid].ref = 1;
|
|
2049 prv->map[emm_pid].type = PID_MAP_TYPE_EMM;
|
|
2050 prv->map[emm_pid].target = NULL;
|
|
2051 }
|
|
2052
|
|
2053 prv->map[0x0001].ref = 1;
|
|
2054 prv->map[0x0001].type = PID_MAP_TYPE_CAT;
|
|
2055 prv->map[0x0001].target = NULL;
|
|
2056
|
|
2057 LAST:
|
|
2058
|
|
2059 if(sect.raw != NULL){
|
|
2060 n = prv->cat->ret(prv->cat, §);
|
|
2061 if( (n < 0) && (r == 0) ){
|
|
2062 r = ARIB_STD_B25_ERROR_CAT_PARSE_FAILURE;
|
|
2063 }
|
|
2064 }
|
|
2065
|
|
2066 return r;
|
|
2067 }
|
|
2068
|
|
2069 static int proc_emm(ARIB_STD_B25_PRIVATE_DATA *prv)
|
|
2070 {
|
|
2071 int r;
|
|
2072 int i,j,n;
|
|
2073
|
|
2074 int len;
|
|
2075
|
|
2076 uint8_t *head;
|
|
2077 uint8_t *tail;
|
|
2078
|
|
2079 TS_SECTION sect;
|
|
2080 EMM_FIXED_PART emm_hdr;
|
|
2081
|
|
2082 r = 0;
|
|
2083 memset(§, 0, sizeof(sect));
|
|
2084
|
|
2085 if(prv->bcas == NULL){
|
|
2086 r = ARIB_STD_B25_ERROR_EMPTY_B_CAS_CARD;
|
|
2087 goto LAST;
|
|
2088 }
|
|
2089
|
|
2090 while( (n = prv->emm->get_count(prv->emm)) > 0 ){
|
|
2091
|
|
2092 n = prv->emm->get(prv->emm, §);
|
|
2093 if(n < 0){
|
|
2094 r = ARIB_STD_B25_ERROR_CAT_PARSE_FAILURE;
|
|
2095 goto LAST;
|
|
2096 }
|
|
2097
|
|
2098 if(sect.hdr.table_id == TS_SECTION_ID_EMM_MESSAGE){
|
|
2099 /* EMM_MESSAGE is not supported */
|
|
2100 goto NEXT;
|
|
2101 }else if(sect.hdr.table_id != TS_SECTION_ID_EMM_S){
|
|
2102 r = ARIB_STD_B25_WARN_TS_SECTION_ID_MISSMATCH;
|
|
2103 goto LAST;
|
|
2104 }
|
|
2105
|
|
2106 head = sect.data;
|
|
2107 tail = sect.tail - 4;
|
|
2108
|
|
2109 i = 0;
|
|
2110 while( (head+13) <= tail ){
|
|
2111
|
|
2112 extract_emm_fixed_part(&emm_hdr, head);
|
|
2113 len = emm_hdr.associated_information_length+7;
|
|
2114 if( (head+len) > tail ){
|
|
2115 /* broken EMM element */
|
|
2116 goto NEXT;
|
|
2117 }
|
|
2118
|
|
2119 for(j=0;j<prv->casid.count;j++){
|
|
2120 if(prv->casid.data[j] == emm_hdr.card_id){
|
|
2121 n = prv->bcas->proc_emm(prv->bcas, head, len);
|
|
2122 if(n < 0){
|
|
2123 r = ARIB_STD_B25_ERROR_EMM_PROC_FAILURE;
|
|
2124 goto LAST;
|
|
2125 }
|
|
2126 unlock_all_decryptor(prv);
|
|
2127 }
|
|
2128 }
|
|
2129
|
|
2130 head += len;
|
|
2131 }
|
|
2132
|
|
2133 NEXT:
|
|
2134 if(sect.raw != NULL){
|
|
2135 n = prv->emm->ret(prv->emm, §);
|
|
2136 if( (n < 0) && (r == 0) ){
|
|
2137 r = ARIB_STD_B25_ERROR_EMM_PARSE_FAILURE;
|
|
2138 goto LAST;
|
|
2139 }
|
|
2140 memset(§, 0, sizeof(sect));
|
|
2141 }
|
|
2142 }
|
|
2143
|
|
2144 LAST:
|
|
2145 if(sect.raw != NULL){
|
|
2146 n = prv->emm->ret(prv->emm, §);
|
|
2147 if( (n < 0) && (r == 0) ){
|
|
2148 r = ARIB_STD_B25_ERROR_EMM_PARSE_FAILURE;
|
|
2149 }
|
|
2150 }
|
|
2151
|
|
2152 return r;
|
|
2153 }
|
|
2154
|
|
2155 static void release_program(ARIB_STD_B25_PRIVATE_DATA *prv, TS_PROGRAM *pgrm)
|
|
2156 {
|
|
2157 int32_t pid;
|
|
2158 TS_STREAM_ELEM *strm;
|
|
2159
|
|
2160 pid = pgrm->pmt_pid;
|
|
2161
|
|
2162 if(pgrm->pmt != NULL){
|
|
2163 pgrm->pmt->release(pgrm->pmt);
|
|
2164 pgrm->pmt = NULL;
|
|
2165 }
|
|
2166
|
|
2167 while( (strm = get_stream_list_head(&(pgrm->old_strm))) != NULL ){
|
|
2168 unref_stream(prv, strm->pid);
|
|
2169 memset(strm, 0, sizeof(TS_STREAM_ELEM));
|
|
2170 put_stream_list_tail(&(prv->strm_pool), strm);
|
|
2171 }
|
|
2172
|
|
2173 while( (strm = get_stream_list_head(&(pgrm->streams))) != NULL ){
|
|
2174 unref_stream(prv, strm->pid);
|
|
2175 memset(strm, 0, sizeof(TS_STREAM_ELEM));
|
|
2176 put_stream_list_tail(&(prv->strm_pool), strm);
|
|
2177 }
|
|
2178
|
|
2179 prv->map[pid].type = PID_MAP_TYPE_UNKNOWN;
|
|
2180 prv->map[pid].ref = 0;
|
|
2181 prv->map[pid].target = NULL;
|
|
2182 }
|
|
2183
|
|
2184 static void unref_stream(ARIB_STD_B25_PRIVATE_DATA *prv, int32_t pid)
|
|
2185 {
|
|
2186 DECRYPTOR_ELEM *dec;
|
|
2187
|
|
2188 prv->map[pid].ref -= 1;
|
|
2189 if( prv->map[pid].ref < 1 ){
|
|
2190 if( (prv->map[pid].target != NULL) &&
|
|
2191 (prv->map[pid].type == PID_MAP_TYPE_OTHER) ){
|
|
2192 dec = (DECRYPTOR_ELEM *)(prv->map[pid].target);
|
|
2193 dec->ref -= 1;
|
|
2194 if(dec->ref < 1){
|
|
2195 remove_decryptor(prv, dec);
|
|
2196 }
|
|
2197 }
|
|
2198 prv->map[pid].type = PID_MAP_TYPE_UNKNOWN;
|
|
2199 prv->map[pid].ref = 0;
|
|
2200 prv->map[pid].target = NULL;
|
|
2201 }
|
|
2202 }
|
|
2203
|
|
2204 static DECRYPTOR_ELEM *set_decryptor(ARIB_STD_B25_PRIVATE_DATA *prv, int32_t pid)
|
|
2205 {
|
|
2206 DECRYPTOR_ELEM *r;
|
|
2207
|
|
2208 r = NULL;
|
|
2209 if(prv->map[pid].type == PID_MAP_TYPE_ECM){
|
|
2210 r = (DECRYPTOR_ELEM *)(prv->map[pid].target);
|
|
2211 if(r != NULL){
|
|
2212 return r;
|
|
2213 }
|
|
2214 }
|
|
2215 r = (DECRYPTOR_ELEM *)calloc(1, sizeof(DECRYPTOR_ELEM));
|
|
2216 if(r == NULL){
|
|
2217 return NULL;
|
|
2218 }
|
|
2219 r->ecm_pid = pid;
|
|
2220 r->ecm = create_ts_section_parser();
|
|
2221 if(r->ecm == NULL){
|
|
2222 free(r);
|
|
2223 return NULL;
|
|
2224 }
|
|
2225
|
|
2226 if(prv->decrypt.tail != NULL){
|
|
2227 r->prev = prv->decrypt.tail;
|
|
2228 r->next = NULL;
|
|
2229 prv->decrypt.tail->next = r;
|
|
2230 prv->decrypt.tail = r;
|
|
2231 prv->decrypt.count += 1;
|
|
2232 }else{
|
|
2233 r->prev = NULL;
|
|
2234 r->next = NULL;
|
|
2235 prv->decrypt.head = r;
|
|
2236 prv->decrypt.tail = r;
|
|
2237 prv->decrypt.count = 1;
|
|
2238 }
|
|
2239
|
|
2240 if( (prv->map[pid].type == PID_MAP_TYPE_OTHER) &&
|
|
2241 (prv->map[pid].target != NULL) ){
|
|
2242 DECRYPTOR_ELEM *dec;
|
|
2243 dec = (DECRYPTOR_ELEM *)(prv->map[pid].target);
|
|
2244 dec->ref -= 1;
|
|
2245 if(dec->ref < 1){
|
|
2246 remove_decryptor(prv, dec);
|
|
2247 }
|
|
2248 }
|
|
2249
|
|
2250 prv->map[pid].type = PID_MAP_TYPE_ECM;
|
|
2251 prv->map[pid].target = r;
|
|
2252
|
|
2253 return r;
|
|
2254 }
|
|
2255
|
|
2256 static void remove_decryptor(ARIB_STD_B25_PRIVATE_DATA *prv, DECRYPTOR_ELEM *dec)
|
|
2257 {
|
|
2258 int32_t pid;
|
|
2259
|
|
2260 DECRYPTOR_ELEM *prev;
|
|
2261 DECRYPTOR_ELEM *next;
|
|
2262
|
|
2263 pid = dec->ecm_pid;
|
|
2264 if( (prv->map[pid].type == PID_MAP_TYPE_ECM) &&
|
|
2265 (prv->map[pid].target == ((void *)dec)) ){
|
|
2266 prv->map[pid].type = PID_MAP_TYPE_UNKNOWN;
|
|
2267 prv->map[pid].target = NULL;
|
|
2268 }
|
|
2269
|
|
2270 prev = (DECRYPTOR_ELEM *)(dec->prev);
|
|
2271 next = (DECRYPTOR_ELEM *)(dec->next);
|
|
2272 if(prev != NULL){
|
|
2273 prev->next = next;
|
|
2274 }else{
|
|
2275 prv->decrypt.head = next;
|
|
2276 }
|
|
2277 if(next != NULL){
|
|
2278 next->prev = prev;
|
|
2279 }else{
|
|
2280 prv->decrypt.tail = prev;
|
|
2281 }
|
|
2282 prv->decrypt.count -= 1;
|
|
2283
|
|
2284 if(dec->ecm != NULL){
|
|
2285 dec->ecm->release(dec->ecm);
|
|
2286 dec->ecm = NULL;
|
|
2287 }
|
|
2288
|
|
2289 if(dec->m2 != NULL){
|
|
2290 dec->m2->release(dec->m2);
|
|
2291 dec->m2 = NULL;
|
|
2292 }
|
|
2293
|
|
2294 free(dec);
|
|
2295 }
|
|
2296
|
|
2297 static DECRYPTOR_ELEM *select_active_decryptor(DECRYPTOR_ELEM *a, DECRYPTOR_ELEM *b, int32_t pid)
|
|
2298 {
|
|
2299 if( b != NULL ){
|
|
2300 return b;
|
|
2301 }
|
|
2302 if( pid == 0x1fff ){
|
|
2303 return NULL;
|
|
2304 }
|
|
2305 return a;
|
|
2306 }
|
|
2307
|
|
2308 static void bind_stream_decryptor(ARIB_STD_B25_PRIVATE_DATA *prv, int32_t pid, DECRYPTOR_ELEM *dec)
|
|
2309 {
|
|
2310 DECRYPTOR_ELEM *old;
|
|
2311
|
|
2312 old = (DECRYPTOR_ELEM *)(prv->map[pid].target);
|
|
2313 if(old == dec){
|
|
2314 /* already binded - do nothing */
|
|
2315 return;
|
|
2316 }
|
|
2317
|
|
2318 if(old != NULL){
|
|
2319 old->ref -= 1;
|
|
2320 if(old->ref == 0){
|
|
2321 remove_decryptor(prv, old);
|
|
2322 }
|
|
2323 prv->map[pid].target = NULL;
|
|
2324 }
|
|
2325
|
|
2326 if(dec != NULL){
|
|
2327 prv->map[pid].target = dec;
|
|
2328 dec->ref += 1;
|
|
2329 }
|
|
2330 }
|
|
2331
|
|
2332 static void unlock_all_decryptor(ARIB_STD_B25_PRIVATE_DATA *prv)
|
|
2333 {
|
|
2334 DECRYPTOR_ELEM *e;
|
|
2335
|
|
2336 e = prv->decrypt.head;
|
|
2337 while(e != NULL){
|
|
2338 e->locked = 0;
|
|
2339 e = (DECRYPTOR_ELEM *)(e->next);
|
|
2340 }
|
|
2341 }
|
|
2342
|
|
2343 static TS_STREAM_ELEM *get_stream_list_head(TS_STREAM_LIST *list)
|
|
2344 {
|
|
2345 TS_STREAM_ELEM *r;
|
|
2346
|
|
2347 r = list->head;
|
|
2348 if(r == NULL){
|
|
2349 return NULL;
|
|
2350 }
|
|
2351
|
|
2352 list->head = (TS_STREAM_ELEM *)(r->next);
|
|
2353 if(list->head == NULL){
|
|
2354 list->tail = NULL;
|
|
2355 list->count = 0;
|
|
2356 }else{
|
|
2357 list->head->prev = NULL;
|
|
2358 list->count -= 1;
|
|
2359 }
|
|
2360
|
|
2361 r->prev = NULL;
|
|
2362 r->next = NULL;
|
|
2363
|
|
2364 return r;
|
|
2365 }
|
|
2366
|
|
2367 static TS_STREAM_ELEM *find_stream_list_elem(TS_STREAM_LIST *list, int32_t pid)
|
|
2368 {
|
|
2369 TS_STREAM_ELEM *r;
|
|
2370
|
|
2371 r = list->head;
|
|
2372 while(r != NULL){
|
|
2373 if(r->pid == pid){
|
|
2374 break;
|
|
2375 }
|
|
2376 r = (TS_STREAM_ELEM *)(r->next);
|
|
2377 }
|
|
2378
|
|
2379 return r;
|
|
2380 }
|
|
2381
|
|
2382 static TS_STREAM_ELEM *create_stream_elem(int32_t pid, int32_t type)
|
|
2383 {
|
|
2384 TS_STREAM_ELEM *r;
|
|
2385
|
|
2386 r = (TS_STREAM_ELEM *)calloc(1, sizeof(TS_STREAM_ELEM));
|
|
2387 if(r == NULL){
|
|
2388 return NULL;
|
|
2389 }
|
|
2390
|
|
2391 r->pid = pid;
|
|
2392 r->type = type;
|
|
2393
|
|
2394 return r;
|
|
2395 }
|
|
2396
|
|
2397 static void put_stream_list_tail(TS_STREAM_LIST *list, TS_STREAM_ELEM *elem)
|
|
2398 {
|
|
2399 if(list->tail != NULL){
|
|
2400 elem->prev = list->tail;
|
|
2401 elem->next = NULL;
|
|
2402 list->tail->next = elem;
|
|
2403 list->tail = elem;
|
|
2404 list->count += 1;
|
|
2405 }else{
|
|
2406 elem->prev = NULL;
|
|
2407 elem->next = NULL;
|
|
2408 list->head = elem;
|
|
2409 list->tail = elem;
|
|
2410 list->count = 1;
|
|
2411 }
|
|
2412 }
|
|
2413
|
|
2414 static void clear_stream_list(TS_STREAM_LIST *list)
|
|
2415 {
|
|
2416 TS_STREAM_ELEM *p,*n;
|
|
2417
|
|
2418 p = list->head;
|
|
2419 while(p != NULL){
|
|
2420 n = (TS_STREAM_ELEM *)(p->next);
|
|
2421 free(p);
|
|
2422 p = n;
|
|
2423 }
|
|
2424
|
|
2425 list->head = NULL;
|
|
2426 list->tail = NULL;
|
|
2427 list->count = 0;
|
|
2428 }
|
|
2429
|
|
2430 static int reserve_work_buffer(TS_WORK_BUFFER *buf, int32_t size)
|
|
2431 {
|
|
2432 int m,n;
|
|
2433 uint8_t *p;
|
|
2434
|
|
2435 if(buf->max >= size){
|
|
2436 return 1;
|
|
2437 }
|
|
2438
|
|
2439 if(buf->max < 512){
|
|
2440 n = 512;
|
|
2441 }else{
|
|
2442 n = buf->max * 2;
|
|
2443 }
|
|
2444
|
|
2445 while(n < size){
|
|
2446 n += n;
|
|
2447 }
|
|
2448
|
|
2449 p = (uint8_t *)malloc(n);
|
|
2450 if(p == NULL){
|
|
2451 return 0;
|
|
2452 }
|
|
2453
|
|
2454 m = 0;
|
|
2455 if(buf->pool != NULL){
|
|
2456 m = buf->tail - buf->head;
|
|
2457 if(m > 0){
|
|
2458 memcpy(p, buf->head, m);
|
|
2459 }
|
|
2460 free(buf->pool);
|
|
2461 buf->pool = NULL;
|
|
2462 }
|
|
2463
|
|
2464 buf->pool = p;
|
|
2465 buf->head = p;
|
|
2466 buf->tail = p+m;
|
|
2467 buf->max = n;
|
|
2468
|
|
2469 return 1;
|
|
2470 }
|
|
2471
|
|
2472 static int append_work_buffer(TS_WORK_BUFFER *buf, uint8_t *data, int32_t size)
|
|
2473 {
|
|
2474 int m;
|
|
2475
|
|
2476 if(size < 1){
|
|
2477 /* ignore - do nothing */
|
|
2478 return 1;
|
|
2479 }
|
|
2480
|
|
2481 m = buf->tail - buf->pool;
|
|
2482
|
|
2483 if( (m+size) > buf->max ){
|
|
2484 if(!reserve_work_buffer(buf, m+size)){
|
|
2485 return 0;
|
|
2486 }
|
|
2487 }
|
|
2488
|
|
2489 memcpy(buf->tail, data, size);
|
|
2490 buf->tail += size;
|
|
2491
|
|
2492 return 1;
|
|
2493 }
|
|
2494
|
|
2495 static void reset_work_buffer(TS_WORK_BUFFER *buf)
|
|
2496 {
|
|
2497 buf->head = buf->pool;
|
|
2498 buf->tail = buf->pool;
|
|
2499 }
|
|
2500
|
|
2501 static void release_work_buffer(TS_WORK_BUFFER *buf)
|
|
2502 {
|
|
2503 if(buf->pool != NULL){
|
|
2504 free(buf->pool);
|
|
2505 }
|
|
2506 buf->pool = NULL;
|
|
2507 buf->head = NULL;
|
|
2508 buf->tail = NULL;
|
|
2509 buf->max = 0;
|
|
2510 }
|
|
2511
|
|
2512 static void extract_ts_header(TS_HEADER *dst, uint8_t *src)
|
|
2513 {
|
|
2514 dst->sync = src[0];
|
|
2515 dst->transport_error_indicator = (src[1] >> 7) & 0x01;
|
|
2516 dst->payload_unit_start_indicator = (src[1] >> 6) & 0x01;
|
|
2517 dst->transport_priority = (src[1] >> 5) & 0x01;
|
|
2518 dst->pid = ((src[1] & 0x1f) << 8) | src[2];
|
|
2519 dst->transport_scrambling_control = (src[3] >> 6) & 0x03;
|
|
2520 dst->adaptation_field_control = (src[3] >> 4) & 0x03;
|
|
2521 dst->continuity_counter = src[3] & 0x0f;
|
|
2522 }
|
|
2523
|
|
2524 static void extract_emm_fixed_part(EMM_FIXED_PART *dst, uint8_t *src)
|
|
2525 {
|
|
2526 int i;
|
|
2527
|
|
2528 dst->card_id = 0;
|
|
2529 for(i=0;i<6;i++){
|
|
2530 dst->card_id = (dst->card_id << 8) | src[i];
|
|
2531 }
|
|
2532
|
|
2533 dst->associated_information_length = src[ 6];
|
|
2534 dst->protocol_number = src[ 7];
|
|
2535 dst->broadcaster_group_id = src[ 8];
|
|
2536 dst->update_number = (src[ 9]<<8)|src[10];
|
|
2537 dst->expiration_date = (src[11]<<8)|src[12];
|
|
2538 }
|
|
2539
|
|
2540 static uint8_t *resync(uint8_t *head, uint8_t *tail, int32_t unit_size)
|
|
2541 {
|
|
2542 int i;
|
|
2543 unsigned char *buf;
|
|
2544
|
|
2545 buf = head;
|
|
2546 tail -= unit_size * 8;
|
|
2547 while( buf <= tail ){
|
|
2548 if(buf[0] == 0x47){
|
|
2549 for(i=1;i<8;i++){
|
|
2550 if(buf[unit_size*i] != 0x47){
|
|
2551 break;
|
|
2552 }
|
|
2553 }
|
|
2554 if(i == 8){
|
|
2555 return buf;
|
|
2556 }
|
|
2557 }
|
|
2558 buf += 1;
|
|
2559 }
|
|
2560
|
|
2561 return NULL;
|
|
2562 }
|
|
2563
|
|
2564 static uint8_t *resync_force(uint8_t *head, uint8_t *tail, int32_t unit_size)
|
|
2565 {
|
|
2566 int i,n;
|
|
2567 unsigned char *buf;
|
|
2568
|
|
2569 buf = head;
|
|
2570 while( buf <= (tail-188) ){
|
|
2571 if(buf[0] == 0x47){
|
|
2572 n = (tail - buf) / unit_size;
|
|
2573 if(n == 0){
|
|
2574 return buf;
|
|
2575 }
|
|
2576 for(i=1;i<n;i++){
|
|
2577 if(buf[unit_size*i] != 0x47){
|
|
2578 break;
|
|
2579 }
|
|
2580 }
|
|
2581 if(i == n){
|
|
2582 return buf;
|
|
2583 }
|
|
2584 }
|
|
2585 buf += 1;
|
|
2586 }
|
|
2587
|
|
2588 return NULL;
|
|
2589 }
|