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 
15 #define TS_SYNC_BYTE 0x47
16 #define TS_PACKET_LEN 188
17 #define TS_PAYLOAD_LEN 184
18 #define TS_NULL_PACKET_PID 0x1fff
19 #define TS_CC_MAX_VAL 0x0f /* 4 bits */
20 #define TS_LAST_VALID_PID 8191
21 #define TS_FILL_BYTE 0xff /* the byte used in packet stuffing */
22 
23 struct vidtv_mpeg_ts_adaption {
24 	u8 length;
25 	struct {
26 		u8 extension:1;
27 		u8 private_data:1;
28 		u8 splicing_point:1;
29 		u8 OPCR:1;
30 		u8 PCR:1;
31 		u8 priority:1;
32 		u8 random_access:1;
33 		u8 discontinued:1;
34 	} __packed;
35 	u8 data[];
36 } __packed;
37 
38 struct vidtv_mpeg_ts {
39 	u8 sync_byte;
40 	__be16 bitfield; /* tei: 1, payload_start:1 priority: 1, pid:13 */
41 	struct {
42 		u8 continuity_counter:4;
43 		u8 payload:1;
44 		u8 adaptation_field:1;
45 		u8 scrambling:2;
46 	} __packed;
47 	struct vidtv_mpeg_ts_adaption *adaption;
48 } __packed;
49 
50 /**
51  * struct pcr_write_args - Arguments for the pcr_write_into function.
52  * @dest_buf: The buffer to write into.
53  * @dest_offset: The byte offset into the buffer.
54  * @pid: The TS PID for the PCR packets.
55  * @buf_sz: The size of the buffer in bytes.
56  * @continuity_counter: The TS continuity_counter.
57  * @pcr: A sample from the system clock.
58  */
59 struct pcr_write_args {
60 	void *dest_buf;
61 	u32 dest_offset;
62 	u16 pid;
63 	u32 buf_sz;
64 	u8 *continuity_counter;
65 	u64 pcr;
66 };
67 
68 /**
69  * struct null_packet_write_args - Arguments for the null_write_into function
70  * @dest_buf: The buffer to write into.
71  * @dest_offset: The byte offset into the buffer.
72  * @buf_sz: The size of the buffer in bytes.
73  * @continuity_counter: The TS continuity_counter.
74  */
75 struct null_packet_write_args {
76 	void *dest_buf;
77 	u32 dest_offset;
78 	u32 buf_sz;
79 	u8 *continuity_counter;
80 };
81 
82 /* Increment the continuity counter */
83 void vidtv_ts_inc_cc(u8 *continuity_counter);
84 
85 /**
86  * vidtv_ts_null_write_into - Write a TS null packet into a buffer.
87  * @args: the arguments to use when writing.
88  *
89  * This function will write a null packet into a buffer. This is usually used to
90  * pad TS streams.
91  *
92  * Return: The number of bytes written into the buffer.
93  */
94 u32 vidtv_ts_null_write_into(struct null_packet_write_args args);
95 
96 /**
97  * vidtv_ts_pcr_write_into - Write a PCR  packet into a buffer.
98  * @args: the arguments to use when writing.
99  *
100  * This function will write a PCR packet into a buffer. This is used to
101  * synchronize the clocks between encoders and decoders.
102  *
103  * Return: The number of bytes written into the buffer.
104  */
105 u32 vidtv_ts_pcr_write_into(struct pcr_write_args args);
106 
107 #endif //VIDTV_TS_H
108