1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * This file contains the logic to work with MPEG Program-Specific Information.
4  * These are defined both in ISO/IEC 13818-1 (systems) and ETSI EN 300 468.
5  * PSI is carried in the form of table structures, and although each table might
6  * technically be broken into one or more sections, we do not do this here,
7  * hence 'table' and 'section' are interchangeable for vidtv.
8  *
9  * This code currently supports three tables: PAT, PMT and SDT. These are the
10  * bare minimum to get userspace to recognize our MPEG transport stream. It can
11  * be extended to support more PSI tables in the future.
12  *
13  * Copyright (C) 2020 Daniel W. S. Almeida
14  */
15 
16 #ifndef VIDTV_PSI_H
17 #define VIDTV_PSI_H
18 
19 #include <linux/types.h>
20 #include <asm/byteorder.h>
21 
22 /*
23  * all section lengths start immediately after the 'section_length' field
24  * see ISO/IEC 13818-1 : 2000 and ETSI EN 300 468 V 1.10.1 for
25  * reference
26  */
27 #define PAT_LEN_UNTIL_LAST_SECTION_NUMBER 5
28 #define PMT_LEN_UNTIL_PROGRAM_INFO_LENGTH 9
29 #define SDT_LEN_UNTIL_RESERVED_FOR_FUTURE_USE 8
30 #define MAX_SECTION_LEN 1021
31 #define VIDTV_PAT_PID 0 /* mandated by the specs */
32 #define VIDTV_SDT_PID 0x0011 /* mandated by the specs */
33 
34 enum vidtv_psi_descriptors {
35 	REGISTRATION_DESCRIPTOR	= 0x05, /* See ISO/IEC 13818-1 section 2.6.8 */
36 	SERVICE_DESCRIPTOR = 0x48, /* See ETSI EN 300 468 section 6.2.33 */
37 };
38 
39 enum vidtv_psi_stream_types {
40 	STREAM_PRIVATE_DATA = 0x06, /* see ISO/IEC 13818-1 2000 p. 48 */
41 };
42 
43 /**
44  * struct vidtv_psi_desc - A generic PSI descriptor type.
45  * The descriptor length is an 8-bit field specifying the total number of bytes of the data portion
46  * of the descriptor following the byte defining the value of this field.
47  */
48 struct vidtv_psi_desc {
49 	struct vidtv_psi_desc *next;
50 	u8 type;
51 	u8 length;
52 	u8 data[];
53 } __packed;
54 
55 /**
56  * struct vidtv_psi_desc_service - Service descriptor.
57  * See ETSI EN 300 468 section 6.2.33.
58  */
59 struct vidtv_psi_desc_service {
60 	struct vidtv_psi_desc *next;
61 	u8 type;
62 	u8 length;
63 
64 	u8 service_type;
65 	u8 provider_name_len;
66 	char *provider_name;
67 	u8 service_name_len;
68 	char *service_name;
69 } __packed;
70 
71 /**
72  * struct vidtv_psi_desc_registration - A registration descriptor.
73  * See ISO/IEC 13818-1 section 2.6.8
74  */
75 struct vidtv_psi_desc_registration {
76 	struct vidtv_psi_desc *next;
77 	u8 type;
78 	u8 length;
79 
80 	/*
81 	 * The format_identifier is a 32-bit value obtained from a Registration
82 	 * Authority as designated by ISO/IEC JTC 1/SC 29.
83 	 */
84 	__be32 format_id;
85 	/*
86 	 * The meaning of additional_identification_info bytes, if any, are
87 	 * defined by the assignee of that format_identifier, and once defined
88 	 * they shall not change.
89 	 */
90 	u8 additional_identification_info[];
91 } __packed;
92 
93 /**
94  * struct vidtv_psi_table_header - A header that is present for all PSI tables.
95  */
96 struct vidtv_psi_table_header {
97 	u8  table_id;
98 
99 	__be16 bitfield; /* syntax: 1, zero: 1, one: 2, section_length: 13 */
100 
101 	__be16 id; /* TS ID */
102 	u8  current_next:1;
103 	u8  version:5;
104 	u8  one2:2;
105 	u8  section_id;	/* section_number */
106 	u8  last_section; /* last_section_number */
107 } __packed;
108 
109 /**
110  * struct vidtv_psi_table_pat_program - A single program in the PAT
111  * See ISO/IEC 13818-1 : 2000 p.43
112  */
113 struct vidtv_psi_table_pat_program {
114 	__be16 service_id;
115 	__be16 bitfield; /* reserved: 3, program_map_pid/network_pid: 13 */
116 	struct vidtv_psi_table_pat_program *next;
117 } __packed;
118 
119 /**
120  * struct vidtv_psi_table_pat - The Program Allocation Table (PAT)
121  * See ISO/IEC 13818-1 : 2000 p.43
122  */
123 struct vidtv_psi_table_pat {
124 	struct vidtv_psi_table_header header;
125 	u16 programs; /* Included by libdvbv5, not part of the table and not actually serialized */
126 	struct vidtv_psi_table_pat_program *program;
127 } __packed;
128 
129 /**
130  * struct vidtv_psi_table_sdt_service - Represents a service in the SDT.
131  * see ETSI EN 300 468 v1.15.1 section 5.2.3.
132  */
133 struct vidtv_psi_table_sdt_service {
134 	__be16 service_id;
135 	u8 EIT_present_following:1;
136 	u8 EIT_schedule:1;
137 	u8 reserved:6;
138 	__be16 bitfield; /* running_status: 3, free_ca:1, desc_loop_len:12 */
139 	struct vidtv_psi_desc *descriptor;
140 	struct vidtv_psi_table_sdt_service *next;
141 } __packed;
142 
143 /**
144  * struct vidtv_psi_table_sdt - Represents the Service Description Table
145  * see ETSI EN 300 468 v1.15.1 section 5.2.3.
146  */
147 
148 struct vidtv_psi_table_sdt {
149 	struct vidtv_psi_table_header header;
150 	__be16 network_id; /* original_network_id */
151 	u8  reserved;
152 	struct vidtv_psi_table_sdt_service *service;
153 } __packed;
154 
155 /**
156  * enum service_running_status - Status of a SDT service.
157  * see ETSI EN 300 468 v1.15.1 section 5.2.3 table 6.
158  */
159 enum service_running_status {
160 	RUNNING = 0x4,
161 };
162 
163 /**
164  * enum service_type - The type of a SDT service.
165  * see ETSI EN 300 468 v1.15.1 section 6.2.33, table 81.
166  */
167 enum service_type {
168 	/* see ETSI EN 300 468 v1.15.1 p. 77 */
169 	DIGITAL_TELEVISION_SERVICE = 0x1,
170 };
171 
172 /**
173  * struct vidtv_psi_table_pmt_stream - A single stream in the PMT.
174  * See ISO/IEC 13818-1 : 2000 p.46.
175  */
176 struct vidtv_psi_table_pmt_stream {
177 	u8 type;
178 	__be16 bitfield; /* reserved: 3, elementary_pid: 13 */
179 	__be16 bitfield2; /*reserved: 4, zero: 2, desc_length: 10 */
180 	struct vidtv_psi_desc *descriptor;
181 	struct vidtv_psi_table_pmt_stream *next;
182 } __packed;
183 
184 /**
185  * struct vidtv_psi_table_pmt - The Program Map Table (PMT).
186  * See ISO/IEC 13818-1 : 2000 p.46.
187  */
188 struct vidtv_psi_table_pmt {
189 	struct vidtv_psi_table_header header;
190 	__be16 bitfield; /* reserved:3, pcr_pid: 13 */
191 	__be16 bitfield2; /* reserved: 4, zero: 2, desc_len: 10 */
192 	struct vidtv_psi_desc *descriptor;
193 	struct vidtv_psi_table_pmt_stream *stream;
194 } __packed;
195 
196 /**
197  * struct psi_write_args - Arguments for the PSI packetizer.
198  * @dest_buf: The buffer to write into.
199  * @from: PSI data to be copied.
200  * @len: How much to write.
201  * @dest_offset: where to start writing in the dest_buffer.
202  * @pid: TS packet ID.
203  * @new_psi_section: Set when starting a table section.
204  * @continuity_counter: Incremented on every new packet.
205  * @is_crc: Set when writing the CRC at the end.
206  * @dest_buf_sz: The size of the dest_buffer
207  * @crc: a pointer to store the crc for this chunk
208  */
209 struct psi_write_args {
210 	void *dest_buf;
211 	void *from;
212 	size_t len;
213 	u32 dest_offset;
214 	u16 pid;
215 	bool new_psi_section;
216 	u8 *continuity_counter;
217 	bool is_crc;
218 	u32 dest_buf_sz;
219 	u32 *crc;
220 };
221 
222 /**
223  * struct desc_write_args - Arguments in order to write a descriptor.
224  * @dest_buf: The buffer to write into.
225  * @dest_offset: where to start writing in the dest_buffer.
226  * @desc: A pointer to the descriptor
227  * @pid: TS packet ID.
228  * @continuity_counter: Incremented on every new packet.
229  * @dest_buf_sz: The size of the dest_buffer
230  * @crc: a pointer to store the crc for this chunk
231  */
232 struct desc_write_args {
233 	void *dest_buf;
234 	u32 dest_offset;
235 	struct vidtv_psi_desc *desc;
236 	u16 pid;
237 	u8 *continuity_counter;
238 	u32 dest_buf_sz;
239 	u32 *crc;
240 };
241 
242 /**
243  * struct crc32_write_args - Arguments in order to write the CRC at the end of
244  * the PSI tables.
245  * @dest_buf: The buffer to write into.
246  * @dest_offset: where to start writing in the dest_buffer.
247  * @crc: the CRC value to write
248  * @pid: TS packet ID.
249  * @continuity_counter: Incremented on every new packet.
250  * @dest_buf_sz: The size of the dest_buffer
251  */
252 struct crc32_write_args {
253 	void *dest_buf;
254 	u32 dest_offset;
255 	__be32 crc;
256 	u16 pid;
257 	u8 *continuity_counter;
258 	u32 dest_buf_sz;
259 };
260 
261 /**
262  * struct header_write_args - Arguments in order to write the common table
263  * header
264  * @dest_buf: The buffer to write into.
265  * @dest_offset: where to start writing in the dest_buffer.
266  * @h: a pointer to the header.
267  * @pid: TS packet ID.
268  * @continuity_counter: Incremented on every new packet.
269  * @dest_buf_sz: The size of the dest_buffer
270  * @crc: a pointer to store the crc for this chunk
271  */
272 struct header_write_args {
273 	void *dest_buf;
274 	u32 dest_offset;
275 	struct vidtv_psi_table_header *h;
276 	u16 pid;
277 	u8 *continuity_counter;
278 	u32 dest_buf_sz;
279 	u32 *crc;
280 };
281 
282 struct vidtv_psi_desc_service *vidtv_psi_service_desc_init(struct vidtv_psi_desc *head,
283 							   enum service_type service_type,
284 							   char *service_name,
285 							   char *provider_name);
286 
287 struct vidtv_psi_desc_registration
288 *vidtv_psi_registration_desc_init(struct vidtv_psi_desc *head,
289 				  __be32 format_id,
290 				  u8 *additional_ident_info,
291 				  u32 additional_info_len);
292 
293 struct vidtv_psi_table_pat_program
294 *vidtv_psi_pat_program_init(struct vidtv_psi_table_pat_program *head,
295 			    u16 service_id,
296 			    u16 program_map_pid);
297 
298 struct vidtv_psi_table_pmt_stream*
299 vidtv_psi_pmt_stream_init(struct vidtv_psi_table_pmt_stream *head,
300 			  enum vidtv_psi_stream_types stream_type,
301 			  u16 es_pid);
302 
303 struct vidtv_psi_table_pat *vidtv_psi_pat_table_init(u16 transport_stream_id);
304 
305 struct vidtv_psi_table_pmt *vidtv_psi_pmt_table_init(u16 program_number,
306 						     u16 pcr_pid);
307 
308 struct vidtv_psi_table_sdt *vidtv_psi_sdt_table_init(u16 transport_stream_id);
309 
310 struct vidtv_psi_table_sdt_service*
311 vidtv_psi_sdt_service_init(struct vidtv_psi_table_sdt_service *head,
312 			   u16 service_id);
313 
314 void
315 vidtv_psi_desc_destroy(struct vidtv_psi_desc *desc);
316 
317 void
318 vidtv_psi_pat_program_destroy(struct vidtv_psi_table_pat_program *p);
319 
320 void
321 vidtv_psi_pat_table_destroy(struct vidtv_psi_table_pat *p);
322 
323 void
324 vidtv_psi_pmt_stream_destroy(struct vidtv_psi_table_pmt_stream *s);
325 
326 void
327 vidtv_psi_pmt_table_destroy(struct vidtv_psi_table_pmt *pmt);
328 
329 void
330 vidtv_psi_sdt_table_destroy(struct vidtv_psi_table_sdt *sdt);
331 
332 void
333 vidtv_psi_sdt_service_destroy(struct vidtv_psi_table_sdt_service *service);
334 
335 /**
336  * vidtv_psi_sdt_service_assign - Assigns the service loop to the SDT.
337  * @sdt: The SDT to assign to.
338  * @service: The service loop (one or more services)
339  *
340  * This will free the previous service loop in the table.
341  * This will assign ownership of the service loop to the table, i.e. the table
342  * will free this service loop when a call to its destroy function is made.
343  */
344 void
345 vidtv_psi_sdt_service_assign(struct vidtv_psi_table_sdt *sdt,
346 			     struct vidtv_psi_table_sdt_service *service);
347 
348 /**
349  * vidtv_psi_desc_assign - Assigns a descriptor loop at some point
350  * @to: Where to assign this descriptor loop to
351  * @desc: The descriptor loop that will be assigned.
352  *
353  * This will free the loop in 'to', if any.
354  */
355 void vidtv_psi_desc_assign(struct vidtv_psi_desc **to,
356 			   struct vidtv_psi_desc *desc);
357 
358 /**
359  * vidtv_psi_pmt_desc_assign - Assigns a descriptor loop at some point in a PMT section.
360  * @pmt: The PMT section that will contain the descriptor loop
361  * @to: Where in the PMT to assign this descriptor loop to
362  * @desc: The descriptor loop that will be assigned.
363  *
364  * This will free the loop in 'to', if any.
365  * This will assign ownership of the loop to the table, i.e. the table
366  * will free this loop when a call to its destroy function is made.
367  */
368 void vidtv_pmt_desc_assign(struct vidtv_psi_table_pmt *pmt,
369 			   struct vidtv_psi_desc **to,
370 			   struct vidtv_psi_desc *desc);
371 
372 /**
373  * vidtv_psi_sdt_desc_assign - Assigns a descriptor loop at some point in a SDT.
374  * @sdt: The SDT that will contain the descriptor loop
375  * @to: Where in the PMT to assign this descriptor loop to
376  * @desc: The descriptor loop that will be assigned.
377  *
378  * This will free the loop in 'to', if any.
379  * This will assign ownership of the loop to the table, i.e. the table
380  * will free this loop when a call to its destroy function is made.
381  */
382 void vidtv_sdt_desc_assign(struct vidtv_psi_table_sdt *sdt,
383 			   struct vidtv_psi_desc **to,
384 			   struct vidtv_psi_desc *desc);
385 
386 /**
387  * vidtv_psi_pat_program_assign - Assigns the program loop to the PAT.
388  * @pat: The PAT to assign to.
389  * @p: The program loop (one or more programs)
390  *
391  * This will free the previous program loop in the table.
392  * This will assign ownership of the program loop to the table, i.e. the table
393  * will free this program loop when a call to its destroy function is made.
394  */
395 void vidtv_psi_pat_program_assign(struct vidtv_psi_table_pat *pat,
396 				  struct vidtv_psi_table_pat_program *p);
397 
398 /**
399  * vidtv_psi_pmt_stream_assign - Assigns the stream loop to the PAT.
400  * @pmt: The PMT to assign to.
401  * @s: The stream loop (one or more streams)
402  *
403  * This will free the previous stream loop in the table.
404  * This will assign ownership of the stream loop to the table, i.e. the table
405  * will free this stream loop when a call to its destroy function is made.
406  */
407 void vidtv_psi_pmt_stream_assign(struct vidtv_psi_table_pmt *pmt,
408 				 struct vidtv_psi_table_pmt_stream *s);
409 
410 struct vidtv_psi_desc *vidtv_psi_desc_clone(struct vidtv_psi_desc *desc);
411 
412 /**
413  * vidtv_psi_create_sec_for_each_pat_entry - Create a PMT section for each
414  * program found in the PAT
415  * @pat: The PAT to look for programs.
416  * @s: The stream loop (one or more streams)
417  * @pcr_pid: packet ID for the PCR to be used for the program described in this
418  * PMT section
419  */
420 struct vidtv_psi_table_pmt**
421 vidtv_psi_pmt_create_sec_for_each_pat_entry(struct vidtv_psi_table_pat *pat, u16 pcr_pid);
422 
423 /**
424  * vidtv_psi_pmt_get_pid - Get the TS PID for a PMT section.
425  * @section: The PMT section whose PID we want to retrieve.
426  * @pat: The PAT table to look into.
427  *
428  * Returns: the TS PID for 'section'
429  */
430 u16 vidtv_psi_pmt_get_pid(struct vidtv_psi_table_pmt *section,
431 			  struct vidtv_psi_table_pat *pat);
432 
433 /**
434  * vidtv_psi_pat_table_update_sec_len - Recompute and update the PAT section length.
435  * @pat: The PAT whose length is to be updated.
436  *
437  * This will traverse the table and accumulate the length of its components,
438  * which is then used to replace the 'section_length' field.
439  *
440  * If section_length > MAX_SECTION_LEN, the operation fails.
441  */
442 void vidtv_psi_pat_table_update_sec_len(struct vidtv_psi_table_pat *pat);
443 
444 /**
445  * vidtv_psi_pmt_table_update_sec_len - Recompute and update the PMT section length.
446  * @pmt: The PMT whose length is to be updated.
447  *
448  * This will traverse the table and accumulate the length of its components,
449  * which is then used to replace the 'section_length' field.
450  *
451  * If section_length > MAX_SECTION_LEN, the operation fails.
452  */
453 void vidtv_psi_pmt_table_update_sec_len(struct vidtv_psi_table_pmt *pmt);
454 
455 /**
456  * vidtv_psi_sdt_table_update_sec_len - Recompute and update the SDT section length.
457  * @sdt: The SDT whose length is to be updated.
458  *
459  * This will traverse the table and accumulate the length of its components,
460  * which is then used to replace the 'section_length' field.
461  *
462  * If section_length > MAX_SECTION_LEN, the operation fails.
463  */
464 void vidtv_psi_sdt_table_update_sec_len(struct vidtv_psi_table_sdt *sdt);
465 
466 /**
467  * struct vidtv_psi_pat_write_args - Arguments for writing a PAT table
468  * @buf: The destination buffer.
469  * @offset: The offset into the destination buffer.
470  * @pat: A pointer to the PAT.
471  * @buf_sz: The size of the destination buffer.
472  * @continuity_counter: A pointer to the CC. Incremented on every new packet.
473  *
474  */
475 struct vidtv_psi_pat_write_args {
476 	char *buf;
477 	u32 offset;
478 	struct vidtv_psi_table_pat *pat;
479 	u32 buf_sz;
480 	u8 *continuity_counter;
481 };
482 
483 /**
484  * vidtv_psi_pat_write_into - Write PAT as MPEG-TS packets into a buffer.
485  * @args: An instance of struct vidtv_psi_pat_write_args
486  *
487  * This function writes the MPEG TS packets for a PAT table into a buffer.
488  * Calling code will usually generate the PAT via a call to its init function
489  * and thus is responsible for freeing it.
490  *
491  * Return: The number of bytes written into the buffer. This is NOT
492  * equal to the size of the PAT, since more space is needed for TS headers during TS
493  * encapsulation.
494  */
495 u32 vidtv_psi_pat_write_into(struct vidtv_psi_pat_write_args args);
496 
497 /**
498  * struct vidtv_psi_sdt_write_args - Arguments for writing a SDT table
499  * @buf: The destination buffer.
500  * @offset: The offset into the destination buffer.
501  * @sdt: A pointer to the SDT.
502  * @buf_sz: The size of the destination buffer.
503  * @continuity_counter: A pointer to the CC. Incremented on every new packet.
504  *
505  */
506 
507 struct vidtv_psi_sdt_write_args {
508 	char *buf;
509 	u32 offset;
510 	struct vidtv_psi_table_sdt *sdt;
511 	u32 buf_sz;
512 	u8 *continuity_counter;
513 };
514 
515 /**
516  * vidtv_psi_sdt_write_into - Write SDT as MPEG-TS packets into a buffer.
517  * @args: an instance of struct vidtv_psi_sdt_write_args
518  *
519  * This function writes the MPEG TS packets for a SDT table into a buffer.
520  * Calling code will usually generate the SDT via a call to its init function
521  * and thus is responsible for freeing it.
522  *
523  * Return: The number of bytes written into the buffer. This is NOT
524  * equal to the size of the SDT, since more space is needed for TS headers during TS
525  * encapsulation.
526  */
527 u32 vidtv_psi_sdt_write_into(struct vidtv_psi_sdt_write_args args);
528 
529 /**
530  * struct vidtv_psi_pmt_write_args - Arguments for writing a PMT section
531  * @buf: The destination buffer.
532  * @offset: The offset into the destination buffer.
533  * @pmt: A pointer to the PMT.
534  * @buf_sz: The size of the destination buffer.
535  * @continuity_counter: A pointer to the CC. Incremented on every new packet.
536  *
537  */
538 struct vidtv_psi_pmt_write_args {
539 	char *buf;
540 	u32 offset;
541 	struct vidtv_psi_table_pmt *pmt;
542 	u16 pid;
543 	u32 buf_sz;
544 	u8 *continuity_counter;
545 	u16 pcr_pid;
546 };
547 
548 /**
549  * vidtv_psi_pmt_write_into - Write PMT as MPEG-TS packets into a buffer.
550  * @args: an instance of struct vidtv_psi_pmt_write_args
551  *
552  * This function writes the MPEG TS packets for a PMT section into a buffer.
553  * Calling code will usually generate the PMT section via a call to its init function
554  * and thus is responsible for freeing it.
555  *
556  * Return: The number of bytes written into the buffer. This is NOT
557  * equal to the size of the PMT section, since more space is needed for TS headers
558  * during TS encapsulation.
559  */
560 u32 vidtv_psi_pmt_write_into(struct vidtv_psi_pmt_write_args args);
561 
562 /**
563  * vidtv_psi_find_pmt_sec - Finds the PMT section for 'program_num'
564  * @pmt_sections: The sections to look into.
565  * @nsections: The number of sections.
566  * @program_num: The 'program_num' from PAT pointing to a PMT section.
567  *
568  * Return: A pointer to the PMT, if found, or NULL.
569  */
570 struct vidtv_psi_table_pmt *vidtv_psi_find_pmt_sec(struct vidtv_psi_table_pmt **pmt_sections,
571 						   u16 nsections,
572 						   u16 program_num);
573 
574 u16 vidtv_psi_get_pat_program_pid(struct vidtv_psi_table_pat_program *p);
575 u16 vidtv_psi_pmt_stream_get_elem_pid(struct vidtv_psi_table_pmt_stream *s);
576 
577 #endif // VIDTV_PSI_H
578