162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * The Virtual DVB test driver serves as a reference DVB driver and helps
462306a36Sopenharmony_ci * validate the existing APIs in the media subsystem. It can also aid
562306a36Sopenharmony_ci * developers working on userspace applications.
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * Copyright (C) 2020 Daniel W. S. Almeida
862306a36Sopenharmony_ci */
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#ifndef VIDTV_TS_H
1162306a36Sopenharmony_ci#define VIDTV_TS_H
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci#include <linux/types.h>
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci#define TS_SYNC_BYTE 0x47
1662306a36Sopenharmony_ci#define TS_PACKET_LEN 188
1762306a36Sopenharmony_ci#define TS_PAYLOAD_LEN 184
1862306a36Sopenharmony_ci#define TS_NULL_PACKET_PID 0x1fff
1962306a36Sopenharmony_ci#define TS_CC_MAX_VAL 0x0f /* 4 bits */
2062306a36Sopenharmony_ci#define TS_LAST_VALID_PID 8191
2162306a36Sopenharmony_ci#define TS_FILL_BYTE 0xff /* the byte used in packet stuffing */
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_cistruct vidtv_mpeg_ts_adaption {
2462306a36Sopenharmony_ci	u8 length;
2562306a36Sopenharmony_ci	struct {
2662306a36Sopenharmony_ci		u8 extension:1;
2762306a36Sopenharmony_ci		u8 private_data:1;
2862306a36Sopenharmony_ci		u8 splicing_point:1;
2962306a36Sopenharmony_ci		u8 OPCR:1;
3062306a36Sopenharmony_ci		u8 PCR:1;
3162306a36Sopenharmony_ci		u8 priority:1;
3262306a36Sopenharmony_ci		u8 random_access:1;
3362306a36Sopenharmony_ci		u8 discontinued:1;
3462306a36Sopenharmony_ci	} __packed;
3562306a36Sopenharmony_ci	u8 data[];
3662306a36Sopenharmony_ci} __packed;
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_cistruct vidtv_mpeg_ts {
3962306a36Sopenharmony_ci	u8 sync_byte;
4062306a36Sopenharmony_ci	__be16 bitfield; /* tei: 1, payload_start:1 priority: 1, pid:13 */
4162306a36Sopenharmony_ci	struct {
4262306a36Sopenharmony_ci		u8 continuity_counter:4;
4362306a36Sopenharmony_ci		u8 payload:1;
4462306a36Sopenharmony_ci		u8 adaptation_field:1;
4562306a36Sopenharmony_ci		u8 scrambling:2;
4662306a36Sopenharmony_ci	} __packed;
4762306a36Sopenharmony_ci} __packed;
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ci/**
5062306a36Sopenharmony_ci * struct pcr_write_args - Arguments for the pcr_write_into function.
5162306a36Sopenharmony_ci * @dest_buf: The buffer to write into.
5262306a36Sopenharmony_ci * @dest_offset: The byte offset into the buffer.
5362306a36Sopenharmony_ci * @pid: The TS PID for the PCR packets.
5462306a36Sopenharmony_ci * @buf_sz: The size of the buffer in bytes.
5562306a36Sopenharmony_ci * @continuity_counter: The TS continuity_counter.
5662306a36Sopenharmony_ci * @pcr: A sample from the system clock.
5762306a36Sopenharmony_ci */
5862306a36Sopenharmony_cistruct pcr_write_args {
5962306a36Sopenharmony_ci	void *dest_buf;
6062306a36Sopenharmony_ci	u32 dest_offset;
6162306a36Sopenharmony_ci	u16 pid;
6262306a36Sopenharmony_ci	u32 buf_sz;
6362306a36Sopenharmony_ci	u8 *continuity_counter;
6462306a36Sopenharmony_ci	u64 pcr;
6562306a36Sopenharmony_ci};
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci/**
6862306a36Sopenharmony_ci * struct null_packet_write_args - Arguments for the null_write_into function
6962306a36Sopenharmony_ci * @dest_buf: The buffer to write into.
7062306a36Sopenharmony_ci * @dest_offset: The byte offset into the buffer.
7162306a36Sopenharmony_ci * @buf_sz: The size of the buffer in bytes.
7262306a36Sopenharmony_ci * @continuity_counter: The TS continuity_counter.
7362306a36Sopenharmony_ci */
7462306a36Sopenharmony_cistruct null_packet_write_args {
7562306a36Sopenharmony_ci	void *dest_buf;
7662306a36Sopenharmony_ci	u32 dest_offset;
7762306a36Sopenharmony_ci	u32 buf_sz;
7862306a36Sopenharmony_ci	u8 *continuity_counter;
7962306a36Sopenharmony_ci};
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci/* Increment the continuity counter */
8262306a36Sopenharmony_civoid vidtv_ts_inc_cc(u8 *continuity_counter);
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci/**
8562306a36Sopenharmony_ci * vidtv_ts_null_write_into - Write a TS null packet into a buffer.
8662306a36Sopenharmony_ci * @args: the arguments to use when writing.
8762306a36Sopenharmony_ci *
8862306a36Sopenharmony_ci * This function will write a null packet into a buffer. This is usually used to
8962306a36Sopenharmony_ci * pad TS streams.
9062306a36Sopenharmony_ci *
9162306a36Sopenharmony_ci * Return: The number of bytes written into the buffer.
9262306a36Sopenharmony_ci */
9362306a36Sopenharmony_ciu32 vidtv_ts_null_write_into(struct null_packet_write_args args);
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci/**
9662306a36Sopenharmony_ci * vidtv_ts_pcr_write_into - Write a PCR  packet into a buffer.
9762306a36Sopenharmony_ci * @args: the arguments to use when writing.
9862306a36Sopenharmony_ci *
9962306a36Sopenharmony_ci * This function will write a PCR packet into a buffer. This is used to
10062306a36Sopenharmony_ci * synchronize the clocks between encoders and decoders.
10162306a36Sopenharmony_ci *
10262306a36Sopenharmony_ci * Return: The number of bytes written into the buffer.
10362306a36Sopenharmony_ci */
10462306a36Sopenharmony_ciu32 vidtv_ts_pcr_write_into(struct pcr_write_args args);
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ci#endif //VIDTV_TS_H
107