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
23struct 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
38struct 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} __packed;
48
49/**
50 * struct pcr_write_args - Arguments for the pcr_write_into function.
51 * @dest_buf: The buffer to write into.
52 * @dest_offset: The byte offset into the buffer.
53 * @pid: The TS PID for the PCR packets.
54 * @buf_sz: The size of the buffer in bytes.
55 * @continuity_counter: The TS continuity_counter.
56 * @pcr: A sample from the system clock.
57 */
58struct pcr_write_args {
59	void *dest_buf;
60	u32 dest_offset;
61	u16 pid;
62	u32 buf_sz;
63	u8 *continuity_counter;
64	u64 pcr;
65};
66
67/**
68 * struct null_packet_write_args - Arguments for the null_write_into function
69 * @dest_buf: The buffer to write into.
70 * @dest_offset: The byte offset into the buffer.
71 * @buf_sz: The size of the buffer in bytes.
72 * @continuity_counter: The TS continuity_counter.
73 */
74struct null_packet_write_args {
75	void *dest_buf;
76	u32 dest_offset;
77	u32 buf_sz;
78	u8 *continuity_counter;
79};
80
81/* Increment the continuity counter */
82void vidtv_ts_inc_cc(u8 *continuity_counter);
83
84/**
85 * vidtv_ts_null_write_into - Write a TS null packet into a buffer.
86 * @args: the arguments to use when writing.
87 *
88 * This function will write a null packet into a buffer. This is usually used to
89 * pad TS streams.
90 *
91 * Return: The number of bytes written into the buffer.
92 */
93u32 vidtv_ts_null_write_into(struct null_packet_write_args args);
94
95/**
96 * vidtv_ts_pcr_write_into - Write a PCR  packet into a buffer.
97 * @args: the arguments to use when writing.
98 *
99 * This function will write a PCR packet into a buffer. This is used to
100 * synchronize the clocks between encoders and decoders.
101 *
102 * Return: The number of bytes written into the buffer.
103 */
104u32 vidtv_ts_pcr_write_into(struct pcr_write_args args);
105
106#endif //VIDTV_TS_H
107