162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Vidtv serves as a reference DVB driver and helps validate the existing APIs
462306a36Sopenharmony_ci * in the media subsystem. It can also aid developers working on userspace
562306a36Sopenharmony_ci * applications.
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * This file contains the muxer logic for TS packets from different
862306a36Sopenharmony_ci * elementary streams.
962306a36Sopenharmony_ci *
1062306a36Sopenharmony_ci * Loosely based on libavcodec/mpegtsenc.c
1162306a36Sopenharmony_ci *
1262306a36Sopenharmony_ci * Copyright (C) 2020 Daniel W. S. Almeida
1362306a36Sopenharmony_ci */
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci#ifndef VIDTV_MUX_H
1662306a36Sopenharmony_ci#define VIDTV_MUX_H
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ci#include <linux/hashtable.h>
1962306a36Sopenharmony_ci#include <linux/types.h>
2062306a36Sopenharmony_ci#include <linux/workqueue.h>
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci#include <media/dvb_frontend.h>
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci#include "vidtv_psi.h"
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci/**
2762306a36Sopenharmony_ci * struct vidtv_mux_timing - Timing related information
2862306a36Sopenharmony_ci *
2962306a36Sopenharmony_ci * This is used to decide when PCR or PSI packets should be sent. This will also
3062306a36Sopenharmony_ci * provide storage for the clock, which is used to compute the value for the PCR.
3162306a36Sopenharmony_ci *
3262306a36Sopenharmony_ci * @start_jiffies: The value of 'jiffies' when we started the mux thread.
3362306a36Sopenharmony_ci * @current_jiffies: The value of 'jiffies' for the current iteration.
3462306a36Sopenharmony_ci * @past_jiffies: The value of 'jiffies' for the past iteration.
3562306a36Sopenharmony_ci * @clk: A 27Mhz clock from which we will drive the PCR. Updated proportionally
3662306a36Sopenharmony_ci * on every iteration.
3762306a36Sopenharmony_ci * @pcr_period_usecs: How often we should send PCR packets.
3862306a36Sopenharmony_ci * @si_period_usecs: How often we should send PSI packets.
3962306a36Sopenharmony_ci */
4062306a36Sopenharmony_cistruct vidtv_mux_timing {
4162306a36Sopenharmony_ci	u64 start_jiffies;
4262306a36Sopenharmony_ci	u64 current_jiffies;
4362306a36Sopenharmony_ci	u64 past_jiffies;
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci	u64 clk;
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci	u64 pcr_period_usecs;
4862306a36Sopenharmony_ci	u64 si_period_usecs;
4962306a36Sopenharmony_ci};
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci/**
5262306a36Sopenharmony_ci * struct vidtv_mux_si - Store the PSI context.
5362306a36Sopenharmony_ci *
5462306a36Sopenharmony_ci * This is used to store the PAT, PMT sections and SDT in use by the muxer.
5562306a36Sopenharmony_ci *
5662306a36Sopenharmony_ci * The muxer acquire these by looking into the hardcoded channels in
5762306a36Sopenharmony_ci * vidtv_channel and then periodically sends the TS packets for them>
5862306a36Sopenharmony_ci *
5962306a36Sopenharmony_ci * @pat: The PAT in use by the muxer.
6062306a36Sopenharmony_ci * @pmt_secs: The PMT sections in use by the muxer. One for each program in the PAT.
6162306a36Sopenharmony_ci * @sdt: The SDT in use by the muxer.
6262306a36Sopenharmony_ci * @nit: The NIT in use by the muxer.
6362306a36Sopenharmony_ci * @eit: the EIT in use by the muxer.
6462306a36Sopenharmony_ci */
6562306a36Sopenharmony_cistruct vidtv_mux_si {
6662306a36Sopenharmony_ci	/* the SI tables */
6762306a36Sopenharmony_ci	struct vidtv_psi_table_pat *pat;
6862306a36Sopenharmony_ci	struct vidtv_psi_table_pmt **pmt_secs; /* the PMT sections */
6962306a36Sopenharmony_ci	struct vidtv_psi_table_sdt *sdt;
7062306a36Sopenharmony_ci	struct vidtv_psi_table_nit *nit;
7162306a36Sopenharmony_ci	struct vidtv_psi_table_eit *eit;
7262306a36Sopenharmony_ci};
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci/**
7562306a36Sopenharmony_ci * struct vidtv_mux_pid_ctx - Store the context for a given TS PID.
7662306a36Sopenharmony_ci * @pid: The TS PID.
7762306a36Sopenharmony_ci * @cc: The continuity counter for this PID. It is incremented on every TS
7862306a36Sopenharmony_ci * pack and it will wrap around at 0xf0. If the decoder notices a sudden jump in
7962306a36Sopenharmony_ci * this counter this will trigger a discontinuity state.
8062306a36Sopenharmony_ci * @h: This is embedded in a hash table, mapping pid -> vidtv_mux_pid_ctx
8162306a36Sopenharmony_ci */
8262306a36Sopenharmony_cistruct vidtv_mux_pid_ctx {
8362306a36Sopenharmony_ci	u16 pid;
8462306a36Sopenharmony_ci	u8 cc; /* continuity counter */
8562306a36Sopenharmony_ci	struct hlist_node h;
8662306a36Sopenharmony_ci};
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci/**
8962306a36Sopenharmony_ci * struct vidtv_mux - A muxer abstraction loosely based in libavcodec/mpegtsenc.c
9062306a36Sopenharmony_ci * @fe: The frontend structure allocated by the muxer.
9162306a36Sopenharmony_ci * @dev: pointer to struct device.
9262306a36Sopenharmony_ci * @timing: Keeps track of timing related information.
9362306a36Sopenharmony_ci * @mux_rate_kbytes_sec: The bit rate for the TS, in kbytes.
9462306a36Sopenharmony_ci * @pid_ctx: A hash table to keep track of per-PID metadata.
9562306a36Sopenharmony_ci * @on_new_packets_available_cb: A callback to inform of new TS packets ready.
9662306a36Sopenharmony_ci * @mux_buf: A pointer to a buffer for this muxer. TS packets are stored there
9762306a36Sopenharmony_ci * and then passed on to the bridge driver.
9862306a36Sopenharmony_ci * @mux_buf_sz: The size for 'mux_buf'.
9962306a36Sopenharmony_ci * @mux_buf_offset: The current offset into 'mux_buf'.
10062306a36Sopenharmony_ci * @channels: The channels associated with this muxer.
10162306a36Sopenharmony_ci * @si: Keeps track of the PSI context.
10262306a36Sopenharmony_ci * @num_streamed_pcr: Number of PCR packets streamed.
10362306a36Sopenharmony_ci * @num_streamed_si: The number of PSI packets streamed.
10462306a36Sopenharmony_ci * @mpeg_thread: Thread responsible for the muxer loop.
10562306a36Sopenharmony_ci * @streaming: whether 'mpeg_thread' is running.
10662306a36Sopenharmony_ci * @pcr_pid: The TS PID used for the PSI packets. All channels will share the
10762306a36Sopenharmony_ci * same PCR.
10862306a36Sopenharmony_ci * @transport_stream_id: The transport stream ID
10962306a36Sopenharmony_ci * @network_id: The network ID
11062306a36Sopenharmony_ci * @network_name: The network name
11162306a36Sopenharmony_ci * @priv: Private data.
11262306a36Sopenharmony_ci */
11362306a36Sopenharmony_cistruct vidtv_mux {
11462306a36Sopenharmony_ci	struct dvb_frontend *fe;
11562306a36Sopenharmony_ci	struct device *dev;
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci	struct vidtv_mux_timing timing;
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci	u32 mux_rate_kbytes_sec;
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci	DECLARE_HASHTABLE(pid_ctx, 3);
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ci	void (*on_new_packets_available_cb)(void *priv, u8 *buf, u32 npackets);
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci	u8 *mux_buf;
12662306a36Sopenharmony_ci	u32 mux_buf_sz;
12762306a36Sopenharmony_ci	u32 mux_buf_offset;
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ci	struct vidtv_channel  *channels;
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_ci	struct vidtv_mux_si si;
13262306a36Sopenharmony_ci	u64 num_streamed_pcr;
13362306a36Sopenharmony_ci	u64 num_streamed_si;
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ci	struct work_struct mpeg_thread;
13662306a36Sopenharmony_ci	bool streaming;
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_ci	u16 pcr_pid;
13962306a36Sopenharmony_ci	u16 transport_stream_id;
14062306a36Sopenharmony_ci	u16 network_id;
14162306a36Sopenharmony_ci	char *network_name;
14262306a36Sopenharmony_ci	void *priv;
14362306a36Sopenharmony_ci};
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_ci/**
14662306a36Sopenharmony_ci * struct vidtv_mux_init_args - Arguments used to inix the muxer.
14762306a36Sopenharmony_ci * @mux_rate_kbytes_sec: The bit rate for the TS, in kbytes.
14862306a36Sopenharmony_ci * @on_new_packets_available_cb: A callback to inform of new TS packets ready.
14962306a36Sopenharmony_ci * @mux_buf_sz: The size for 'mux_buf'.
15062306a36Sopenharmony_ci * @pcr_period_usecs: How often we should send PCR packets.
15162306a36Sopenharmony_ci * @si_period_usecs: How often we should send PSI packets.
15262306a36Sopenharmony_ci * @pcr_pid: The TS PID used for the PSI packets. All channels will share the
15362306a36Sopenharmony_ci * same PCR.
15462306a36Sopenharmony_ci * @transport_stream_id: The transport stream ID
15562306a36Sopenharmony_ci * @channels: an optional list of channels to use
15662306a36Sopenharmony_ci * @network_id: The network ID
15762306a36Sopenharmony_ci * @network_name: The network name
15862306a36Sopenharmony_ci * @priv: Private data.
15962306a36Sopenharmony_ci */
16062306a36Sopenharmony_cistruct vidtv_mux_init_args {
16162306a36Sopenharmony_ci	u32 mux_rate_kbytes_sec;
16262306a36Sopenharmony_ci	void (*on_new_packets_available_cb)(void *priv, u8 *buf, u32 npackets);
16362306a36Sopenharmony_ci	u32 mux_buf_sz;
16462306a36Sopenharmony_ci	u64 pcr_period_usecs;
16562306a36Sopenharmony_ci	u64 si_period_usecs;
16662306a36Sopenharmony_ci	u16 pcr_pid;
16762306a36Sopenharmony_ci	u16 transport_stream_id;
16862306a36Sopenharmony_ci	struct vidtv_channel *channels;
16962306a36Sopenharmony_ci	u16 network_id;
17062306a36Sopenharmony_ci	char *network_name;
17162306a36Sopenharmony_ci	void *priv;
17262306a36Sopenharmony_ci};
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_cistruct vidtv_mux *vidtv_mux_init(struct dvb_frontend *fe,
17562306a36Sopenharmony_ci				 struct device *dev,
17662306a36Sopenharmony_ci				 struct vidtv_mux_init_args *args);
17762306a36Sopenharmony_civoid vidtv_mux_destroy(struct vidtv_mux *m);
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_civoid vidtv_mux_start_thread(struct vidtv_mux *m);
18062306a36Sopenharmony_civoid vidtv_mux_stop_thread(struct vidtv_mux *m);
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ci#endif //VIDTV_MUX_H
183