18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Vidtv serves as a reference DVB driver and helps validate the existing APIs
48c2ecf20Sopenharmony_ci * in the media subsystem. It can also aid developers working on userspace
58c2ecf20Sopenharmony_ci * applications.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * This file contains the muxer logic for TS packets from different
88c2ecf20Sopenharmony_ci * elementary streams.
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * Loosely based on libavcodec/mpegtsenc.c
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci * Copyright (C) 2020 Daniel W. S. Almeida
138c2ecf20Sopenharmony_ci */
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#ifndef VIDTV_MUX_H
168c2ecf20Sopenharmony_ci#define VIDTV_MUX_H
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#include <linux/hashtable.h>
198c2ecf20Sopenharmony_ci#include <linux/types.h>
208c2ecf20Sopenharmony_ci#include <linux/workqueue.h>
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#include <media/dvb_frontend.h>
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#include "vidtv_psi.h"
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci/**
278c2ecf20Sopenharmony_ci * struct vidtv_mux_timing - Timing related information
288c2ecf20Sopenharmony_ci *
298c2ecf20Sopenharmony_ci * This is used to decide when PCR or PSI packets should be sent. This will also
308c2ecf20Sopenharmony_ci * provide storage for the clock, which is used to compute the value for the PCR.
318c2ecf20Sopenharmony_ci *
328c2ecf20Sopenharmony_ci * @start_jiffies: The value of 'jiffies' when we started the mux thread.
338c2ecf20Sopenharmony_ci * @current_jiffies: The value of 'jiffies' for the current iteration.
348c2ecf20Sopenharmony_ci * @past_jiffies: The value of 'jiffies' for the past iteration.
358c2ecf20Sopenharmony_ci * @clk: A 27Mhz clock from which we will drive the PCR. Updated proportionally
368c2ecf20Sopenharmony_ci * on every iteration.
378c2ecf20Sopenharmony_ci * @pcr_period_usecs: How often we should send PCR packets.
388c2ecf20Sopenharmony_ci * @si_period_usecs: How often we should send PSI packets.
398c2ecf20Sopenharmony_ci */
408c2ecf20Sopenharmony_cistruct vidtv_mux_timing {
418c2ecf20Sopenharmony_ci	u64 start_jiffies;
428c2ecf20Sopenharmony_ci	u64 current_jiffies;
438c2ecf20Sopenharmony_ci	u64 past_jiffies;
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	u64 clk;
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	u64 pcr_period_usecs;
488c2ecf20Sopenharmony_ci	u64 si_period_usecs;
498c2ecf20Sopenharmony_ci};
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci/**
528c2ecf20Sopenharmony_ci * struct vidtv_mux_si - Store the PSI context.
538c2ecf20Sopenharmony_ci *
548c2ecf20Sopenharmony_ci * This is used to store the PAT, PMT sections and SDT in use by the muxer.
558c2ecf20Sopenharmony_ci *
568c2ecf20Sopenharmony_ci * The muxer acquire these by looking into the hardcoded channels in
578c2ecf20Sopenharmony_ci * vidtv_channel and then periodically sends the TS packets for them>
588c2ecf20Sopenharmony_ci *
598c2ecf20Sopenharmony_ci * @pat: The PAT in use by the muxer.
608c2ecf20Sopenharmony_ci * @pmt_secs: The PMT sections in use by the muxer. One for each program in the PAT.
618c2ecf20Sopenharmony_ci * @sdt: The SDT in use by the muxer.
628c2ecf20Sopenharmony_ci * @nit: The NIT in use by the muxer.
638c2ecf20Sopenharmony_ci * @eit: the EIT in use by the muxer.
648c2ecf20Sopenharmony_ci */
658c2ecf20Sopenharmony_cistruct vidtv_mux_si {
668c2ecf20Sopenharmony_ci	/* the SI tables */
678c2ecf20Sopenharmony_ci	struct vidtv_psi_table_pat *pat;
688c2ecf20Sopenharmony_ci	struct vidtv_psi_table_pmt **pmt_secs; /* the PMT sections */
698c2ecf20Sopenharmony_ci	struct vidtv_psi_table_sdt *sdt;
708c2ecf20Sopenharmony_ci	struct vidtv_psi_table_nit *nit;
718c2ecf20Sopenharmony_ci	struct vidtv_psi_table_eit *eit;
728c2ecf20Sopenharmony_ci};
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci/**
758c2ecf20Sopenharmony_ci * struct vidtv_mux_pid_ctx - Store the context for a given TS PID.
768c2ecf20Sopenharmony_ci * @pid: The TS PID.
778c2ecf20Sopenharmony_ci * @cc: The continuity counter for this PID. It is incremented on every TS
788c2ecf20Sopenharmony_ci * pack and it will wrap around at 0xf0. If the decoder notices a sudden jump in
798c2ecf20Sopenharmony_ci * this counter this will trigger a discontinuity state.
808c2ecf20Sopenharmony_ci * @h: This is embedded in a hash table, mapping pid -> vidtv_mux_pid_ctx
818c2ecf20Sopenharmony_ci */
828c2ecf20Sopenharmony_cistruct vidtv_mux_pid_ctx {
838c2ecf20Sopenharmony_ci	u16 pid;
848c2ecf20Sopenharmony_ci	u8 cc; /* continuity counter */
858c2ecf20Sopenharmony_ci	struct hlist_node h;
868c2ecf20Sopenharmony_ci};
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci/**
898c2ecf20Sopenharmony_ci * struct vidtv_mux - A muxer abstraction loosely based in libavcodec/mpegtsenc.c
908c2ecf20Sopenharmony_ci * @fe: The frontend structure allocated by the muxer.
918c2ecf20Sopenharmony_ci * @dev: pointer to struct device.
928c2ecf20Sopenharmony_ci * @timing: Keeps track of timing related information.
938c2ecf20Sopenharmony_ci * @mux_rate_kbytes_sec: The bit rate for the TS, in kbytes.
948c2ecf20Sopenharmony_ci * @pid_ctx: A hash table to keep track of per-PID metadata.
958c2ecf20Sopenharmony_ci * @on_new_packets_available_cb: A callback to inform of new TS packets ready.
968c2ecf20Sopenharmony_ci * @mux_buf: A pointer to a buffer for this muxer. TS packets are stored there
978c2ecf20Sopenharmony_ci * and then passed on to the bridge driver.
988c2ecf20Sopenharmony_ci * @mux_buf_sz: The size for 'mux_buf'.
998c2ecf20Sopenharmony_ci * @mux_buf_offset: The current offset into 'mux_buf'.
1008c2ecf20Sopenharmony_ci * @channels: The channels associated with this muxer.
1018c2ecf20Sopenharmony_ci * @si: Keeps track of the PSI context.
1028c2ecf20Sopenharmony_ci * @num_streamed_pcr: Number of PCR packets streamed.
1038c2ecf20Sopenharmony_ci * @num_streamed_si: The number of PSI packets streamed.
1048c2ecf20Sopenharmony_ci * @mpeg_thread: Thread responsible for the muxer loop.
1058c2ecf20Sopenharmony_ci * @streaming: whether 'mpeg_thread' is running.
1068c2ecf20Sopenharmony_ci * @pcr_pid: The TS PID used for the PSI packets. All channels will share the
1078c2ecf20Sopenharmony_ci * same PCR.
1088c2ecf20Sopenharmony_ci * @transport_stream_id: The transport stream ID
1098c2ecf20Sopenharmony_ci * @network_id: The network ID
1108c2ecf20Sopenharmony_ci * @network_name: The network name
1118c2ecf20Sopenharmony_ci * @priv: Private data.
1128c2ecf20Sopenharmony_ci */
1138c2ecf20Sopenharmony_cistruct vidtv_mux {
1148c2ecf20Sopenharmony_ci	struct dvb_frontend *fe;
1158c2ecf20Sopenharmony_ci	struct device *dev;
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	struct vidtv_mux_timing timing;
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	u32 mux_rate_kbytes_sec;
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	DECLARE_HASHTABLE(pid_ctx, 3);
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	void (*on_new_packets_available_cb)(void *priv, u8 *buf, u32 npackets);
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	u8 *mux_buf;
1268c2ecf20Sopenharmony_ci	u32 mux_buf_sz;
1278c2ecf20Sopenharmony_ci	u32 mux_buf_offset;
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	struct vidtv_channel  *channels;
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	struct vidtv_mux_si si;
1328c2ecf20Sopenharmony_ci	u64 num_streamed_pcr;
1338c2ecf20Sopenharmony_ci	u64 num_streamed_si;
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	struct work_struct mpeg_thread;
1368c2ecf20Sopenharmony_ci	bool streaming;
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	u16 pcr_pid;
1398c2ecf20Sopenharmony_ci	u16 transport_stream_id;
1408c2ecf20Sopenharmony_ci	u16 network_id;
1418c2ecf20Sopenharmony_ci	char *network_name;
1428c2ecf20Sopenharmony_ci	void *priv;
1438c2ecf20Sopenharmony_ci};
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci/**
1468c2ecf20Sopenharmony_ci * struct vidtv_mux_init_args - Arguments used to inix the muxer.
1478c2ecf20Sopenharmony_ci * @mux_rate_kbytes_sec: The bit rate for the TS, in kbytes.
1488c2ecf20Sopenharmony_ci * @on_new_packets_available_cb: A callback to inform of new TS packets ready.
1498c2ecf20Sopenharmony_ci * @mux_buf_sz: The size for 'mux_buf'.
1508c2ecf20Sopenharmony_ci * @pcr_period_usecs: How often we should send PCR packets.
1518c2ecf20Sopenharmony_ci * @si_period_usecs: How often we should send PSI packets.
1528c2ecf20Sopenharmony_ci * @pcr_pid: The TS PID used for the PSI packets. All channels will share the
1538c2ecf20Sopenharmony_ci * same PCR.
1548c2ecf20Sopenharmony_ci * @transport_stream_id: The transport stream ID
1558c2ecf20Sopenharmony_ci * @channels: an optional list of channels to use
1568c2ecf20Sopenharmony_ci * @network_id: The network ID
1578c2ecf20Sopenharmony_ci * @network_name: The network name
1588c2ecf20Sopenharmony_ci * @priv: Private data.
1598c2ecf20Sopenharmony_ci */
1608c2ecf20Sopenharmony_cistruct vidtv_mux_init_args {
1618c2ecf20Sopenharmony_ci	u32 mux_rate_kbytes_sec;
1628c2ecf20Sopenharmony_ci	void (*on_new_packets_available_cb)(void *priv, u8 *buf, u32 npackets);
1638c2ecf20Sopenharmony_ci	u32 mux_buf_sz;
1648c2ecf20Sopenharmony_ci	u64 pcr_period_usecs;
1658c2ecf20Sopenharmony_ci	u64 si_period_usecs;
1668c2ecf20Sopenharmony_ci	u16 pcr_pid;
1678c2ecf20Sopenharmony_ci	u16 transport_stream_id;
1688c2ecf20Sopenharmony_ci	struct vidtv_channel *channels;
1698c2ecf20Sopenharmony_ci	u16 network_id;
1708c2ecf20Sopenharmony_ci	char *network_name;
1718c2ecf20Sopenharmony_ci	void *priv;
1728c2ecf20Sopenharmony_ci};
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_cistruct vidtv_mux *vidtv_mux_init(struct dvb_frontend *fe,
1758c2ecf20Sopenharmony_ci				 struct device *dev,
1768c2ecf20Sopenharmony_ci				 struct vidtv_mux_init_args *args);
1778c2ecf20Sopenharmony_civoid vidtv_mux_destroy(struct vidtv_mux *m);
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_civoid vidtv_mux_start_thread(struct vidtv_mux *m);
1808c2ecf20Sopenharmony_civoid vidtv_mux_stop_thread(struct vidtv_mux *m);
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci#endif //VIDTV_MUX_H
183