1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * The Virtual DVB test driver serves as a reference DVB driver and helps
4  * validate the existing APIs in the media subsystem. It can also aid
5  * developers working on userspace applications.
6  *
7  * Copyright (C) 2020 Daniel W. S. Almeida
8  */
9 
10 #ifndef VIDTV_TS_H
11 #define VIDTV_TS_H
12 
13 #include <linux/types.h>
14 #include <asm/byteorder.h>
15 
16 #define TS_SYNC_BYTE 0x47
17 #define TS_PACKET_LEN 188
18 #define TS_PAYLOAD_LEN 184
19 #define TS_NULL_PACKET_PID 0x1fff
20 #define TS_CC_MAX_VAL 0x0f /* 4 bits */
21 #define TS_LAST_VALID_PID 8191
22 #define TS_FILL_BYTE 0xff /* the byte used in packet stuffing */
23 
24 struct vidtv_mpeg_ts_adaption {
25 	u8 length;
26 	struct {
27 #if defined(__LITTLE_ENDIAN_BITFIELD)
28 		u8 extension:1;
29 		u8 private_data:1;
30 		u8 splicing_point:1;
31 		u8 OPCR:1;
32 		u8 PCR:1;
33 		u8 priority:1;
34 		u8 random_access:1;
35 		u8 discontinued:1;
36 #elif defined(__BIG_ENDIAN_BITFIELD)
37 		u8 discontinued:1;
38 		u8 random_access:1;
39 		u8 priority:1;
40 		u8 PCR:1;
41 		u8 OPCR:1;
42 		u8 splicing_point:1;
43 		u8 private_data:1;
44 		u8 extension:1;
45 #else
46 #error  "Unknown bitfield ordering"
47 #endif
48 	} __packed;
49 	u8 data[];
50 } __packed;
51 
52 struct vidtv_mpeg_ts {
53 	u8 sync_byte;
54 	__be16 bitfield; /* tei: 1, payload_start:1 priority: 1, pid:13 */
55 	struct {
56 		u8 continuity_counter:4;
57 		u8 payload:1;
58 		u8 adaptation_field:1;
59 		u8 scrambling:2;
60 	} __packed;
61 	struct vidtv_mpeg_ts_adaption adaption[];
62 } __packed;
63 
64 /**
65  * struct pcr_write_args - Arguments for the pcr_write_into function.
66  * @dest_buf: The buffer to write into.
67  * @dest_offset: The byte offset into the buffer.
68  * @pid: The TS PID for the PCR packets.
69  * @buf_sz: The size of the buffer in bytes.
70  * @countinuity_counter: The TS continuity_counter.
71  * @pcr: A sample from the system clock.
72  */
73 struct pcr_write_args {
74 	void *dest_buf;
75 	u32 dest_offset;
76 	u16 pid;
77 	u32 buf_sz;
78 	u8 *continuity_counter;
79 	u64 pcr;
80 };
81 
82 /**
83  * struct null_packet_write_args - Arguments for the null_write_into function
84  * @dest_buf: The buffer to write into.
85  * @dest_offset: The byte offset into the buffer.
86  * @buf_sz: The size of the buffer in bytes.
87  * @countinuity_counter: The TS continuity_counter.
88  */
89 struct null_packet_write_args {
90 	void *dest_buf;
91 	u32 dest_offset;
92 	u32 buf_sz;
93 	u8 *continuity_counter;
94 };
95 
96 /* Increment the continuity counter */
97 void vidtv_ts_inc_cc(u8 *continuity_counter);
98 
99 /**
100  * vidtv_ts_null_write_into - Write a TS null packet into a buffer.
101  * @args: the arguments to use when writing.
102  *
103  * This function will write a null packet into a buffer. This is usually used to
104  * pad TS streams.
105  *
106  * Return: The number of bytes written into the buffer.
107  */
108 u32 vidtv_ts_null_write_into(struct null_packet_write_args args);
109 
110 /**
111  * vidtv_ts_pcr_write_into - Write a PCR  packet into a buffer.
112  * @args: the arguments to use when writing.
113  *
114  * This function will write a PCR packet into a buffer. This is used to
115  * synchronize the clocks between encoders and decoders.
116  *
117  * Return: The number of bytes written into the buffer.
118  */
119 u32 vidtv_ts_pcr_write_into(struct pcr_write_args args);
120 
121 #endif //VIDTV_TS_H
122