1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */ 2*4882a593Smuzhiyun /* 3*4882a593Smuzhiyun * Vidtv serves as a reference DVB driver and helps validate the existing APIs 4*4882a593Smuzhiyun * in the media subsystem. It can also aid developers working on userspace 5*4882a593Smuzhiyun * applications. 6*4882a593Smuzhiyun * 7*4882a593Smuzhiyun * This file contains the muxer logic for TS packets from different 8*4882a593Smuzhiyun * elementary streams. 9*4882a593Smuzhiyun * 10*4882a593Smuzhiyun * Loosely based on libavcodec/mpegtsenc.c 11*4882a593Smuzhiyun * 12*4882a593Smuzhiyun * Copyright (C) 2020 Daniel W. S. Almeida 13*4882a593Smuzhiyun */ 14*4882a593Smuzhiyun 15*4882a593Smuzhiyun #ifndef VIDTV_MUX_H 16*4882a593Smuzhiyun #define VIDTV_MUX_H 17*4882a593Smuzhiyun 18*4882a593Smuzhiyun #include <linux/hashtable.h> 19*4882a593Smuzhiyun #include <linux/types.h> 20*4882a593Smuzhiyun #include <linux/workqueue.h> 21*4882a593Smuzhiyun 22*4882a593Smuzhiyun #include <media/dvb_frontend.h> 23*4882a593Smuzhiyun 24*4882a593Smuzhiyun #include "vidtv_psi.h" 25*4882a593Smuzhiyun 26*4882a593Smuzhiyun /** 27*4882a593Smuzhiyun * struct vidtv_mux_timing - Timing related information 28*4882a593Smuzhiyun * 29*4882a593Smuzhiyun * This is used to decide when PCR or PSI packets should be sent. This will also 30*4882a593Smuzhiyun * provide storage for the clock, which is used to compute the value for the PCR. 31*4882a593Smuzhiyun * 32*4882a593Smuzhiyun * @start_jiffies: The value of 'jiffies' when we started the mux thread. 33*4882a593Smuzhiyun * @current_jiffies: The value of 'jiffies' for the current iteration. 34*4882a593Smuzhiyun * @past_jiffies: The value of 'jiffies' for the past iteration. 35*4882a593Smuzhiyun * @clk: A 27Mhz clock from which we will drive the PCR. Updated proportionally 36*4882a593Smuzhiyun * on every iteration. 37*4882a593Smuzhiyun * @pcr_period_usecs: How often we should send PCR packets. 38*4882a593Smuzhiyun * @si_period_usecs: How often we should send PSI packets. 39*4882a593Smuzhiyun */ 40*4882a593Smuzhiyun struct vidtv_mux_timing { 41*4882a593Smuzhiyun u64 start_jiffies; 42*4882a593Smuzhiyun u64 current_jiffies; 43*4882a593Smuzhiyun u64 past_jiffies; 44*4882a593Smuzhiyun 45*4882a593Smuzhiyun u64 clk; 46*4882a593Smuzhiyun 47*4882a593Smuzhiyun u64 pcr_period_usecs; 48*4882a593Smuzhiyun u64 si_period_usecs; 49*4882a593Smuzhiyun }; 50*4882a593Smuzhiyun 51*4882a593Smuzhiyun /** 52*4882a593Smuzhiyun * struct vidtv_mux_si - Store the PSI context. 53*4882a593Smuzhiyun * 54*4882a593Smuzhiyun * This is used to store the PAT, PMT sections and SDT in use by the muxer. 55*4882a593Smuzhiyun * 56*4882a593Smuzhiyun * The muxer acquire these by looking into the hardcoded channels in 57*4882a593Smuzhiyun * vidtv_channel and then periodically sends the TS packets for them> 58*4882a593Smuzhiyun * 59*4882a593Smuzhiyun * @pat: The PAT in use by the muxer. 60*4882a593Smuzhiyun * @pmt_secs: The PMT sections in use by the muxer. One for each program in the PAT. 61*4882a593Smuzhiyun * @sdt: The SDT in use by the muxer. 62*4882a593Smuzhiyun * @nit: The NIT in use by the muxer. 63*4882a593Smuzhiyun * @eit: the EIT in use by the muxer. 64*4882a593Smuzhiyun */ 65*4882a593Smuzhiyun struct vidtv_mux_si { 66*4882a593Smuzhiyun /* the SI tables */ 67*4882a593Smuzhiyun struct vidtv_psi_table_pat *pat; 68*4882a593Smuzhiyun struct vidtv_psi_table_pmt **pmt_secs; /* the PMT sections */ 69*4882a593Smuzhiyun struct vidtv_psi_table_sdt *sdt; 70*4882a593Smuzhiyun struct vidtv_psi_table_nit *nit; 71*4882a593Smuzhiyun struct vidtv_psi_table_eit *eit; 72*4882a593Smuzhiyun }; 73*4882a593Smuzhiyun 74*4882a593Smuzhiyun /** 75*4882a593Smuzhiyun * struct vidtv_mux_pid_ctx - Store the context for a given TS PID. 76*4882a593Smuzhiyun * @pid: The TS PID. 77*4882a593Smuzhiyun * @cc: The continuity counter for this PID. It is incremented on every TS 78*4882a593Smuzhiyun * pack and it will wrap around at 0xf0. If the decoder notices a sudden jump in 79*4882a593Smuzhiyun * this counter this will trigger a discontinuity state. 80*4882a593Smuzhiyun * @h: This is embedded in a hash table, mapping pid -> vidtv_mux_pid_ctx 81*4882a593Smuzhiyun */ 82*4882a593Smuzhiyun struct vidtv_mux_pid_ctx { 83*4882a593Smuzhiyun u16 pid; 84*4882a593Smuzhiyun u8 cc; /* continuity counter */ 85*4882a593Smuzhiyun struct hlist_node h; 86*4882a593Smuzhiyun }; 87*4882a593Smuzhiyun 88*4882a593Smuzhiyun /** 89*4882a593Smuzhiyun * struct vidtv_mux - A muxer abstraction loosely based in libavcodec/mpegtsenc.c 90*4882a593Smuzhiyun * @fe: The frontend structure allocated by the muxer. 91*4882a593Smuzhiyun * @dev: pointer to struct device. 92*4882a593Smuzhiyun * @timing: Keeps track of timing related information. 93*4882a593Smuzhiyun * @mux_rate_kbytes_sec: The bit rate for the TS, in kbytes. 94*4882a593Smuzhiyun * @pid_ctx: A hash table to keep track of per-PID metadata. 95*4882a593Smuzhiyun * @on_new_packets_available_cb: A callback to inform of new TS packets ready. 96*4882a593Smuzhiyun * @mux_buf: A pointer to a buffer for this muxer. TS packets are stored there 97*4882a593Smuzhiyun * and then passed on to the bridge driver. 98*4882a593Smuzhiyun * @mux_buf_sz: The size for 'mux_buf'. 99*4882a593Smuzhiyun * @mux_buf_offset: The current offset into 'mux_buf'. 100*4882a593Smuzhiyun * @channels: The channels associated with this muxer. 101*4882a593Smuzhiyun * @si: Keeps track of the PSI context. 102*4882a593Smuzhiyun * @num_streamed_pcr: Number of PCR packets streamed. 103*4882a593Smuzhiyun * @num_streamed_si: The number of PSI packets streamed. 104*4882a593Smuzhiyun * @mpeg_thread: Thread responsible for the muxer loop. 105*4882a593Smuzhiyun * @streaming: whether 'mpeg_thread' is running. 106*4882a593Smuzhiyun * @pcr_pid: The TS PID used for the PSI packets. All channels will share the 107*4882a593Smuzhiyun * same PCR. 108*4882a593Smuzhiyun * @transport_stream_id: The transport stream ID 109*4882a593Smuzhiyun * @network_id: The network ID 110*4882a593Smuzhiyun * @network_name: The network name 111*4882a593Smuzhiyun * @priv: Private data. 112*4882a593Smuzhiyun */ 113*4882a593Smuzhiyun struct vidtv_mux { 114*4882a593Smuzhiyun struct dvb_frontend *fe; 115*4882a593Smuzhiyun struct device *dev; 116*4882a593Smuzhiyun 117*4882a593Smuzhiyun struct vidtv_mux_timing timing; 118*4882a593Smuzhiyun 119*4882a593Smuzhiyun u32 mux_rate_kbytes_sec; 120*4882a593Smuzhiyun 121*4882a593Smuzhiyun DECLARE_HASHTABLE(pid_ctx, 3); 122*4882a593Smuzhiyun 123*4882a593Smuzhiyun void (*on_new_packets_available_cb)(void *priv, u8 *buf, u32 npackets); 124*4882a593Smuzhiyun 125*4882a593Smuzhiyun u8 *mux_buf; 126*4882a593Smuzhiyun u32 mux_buf_sz; 127*4882a593Smuzhiyun u32 mux_buf_offset; 128*4882a593Smuzhiyun 129*4882a593Smuzhiyun struct vidtv_channel *channels; 130*4882a593Smuzhiyun 131*4882a593Smuzhiyun struct vidtv_mux_si si; 132*4882a593Smuzhiyun u64 num_streamed_pcr; 133*4882a593Smuzhiyun u64 num_streamed_si; 134*4882a593Smuzhiyun 135*4882a593Smuzhiyun struct work_struct mpeg_thread; 136*4882a593Smuzhiyun bool streaming; 137*4882a593Smuzhiyun 138*4882a593Smuzhiyun u16 pcr_pid; 139*4882a593Smuzhiyun u16 transport_stream_id; 140*4882a593Smuzhiyun u16 network_id; 141*4882a593Smuzhiyun char *network_name; 142*4882a593Smuzhiyun void *priv; 143*4882a593Smuzhiyun }; 144*4882a593Smuzhiyun 145*4882a593Smuzhiyun /** 146*4882a593Smuzhiyun * struct vidtv_mux_init_args - Arguments used to inix the muxer. 147*4882a593Smuzhiyun * @mux_rate_kbytes_sec: The bit rate for the TS, in kbytes. 148*4882a593Smuzhiyun * @on_new_packets_available_cb: A callback to inform of new TS packets ready. 149*4882a593Smuzhiyun * @mux_buf_sz: The size for 'mux_buf'. 150*4882a593Smuzhiyun * @pcr_period_usecs: How often we should send PCR packets. 151*4882a593Smuzhiyun * @si_period_usecs: How often we should send PSI packets. 152*4882a593Smuzhiyun * @pcr_pid: The TS PID used for the PSI packets. All channels will share the 153*4882a593Smuzhiyun * same PCR. 154*4882a593Smuzhiyun * @transport_stream_id: The transport stream ID 155*4882a593Smuzhiyun * @channels: an optional list of channels to use 156*4882a593Smuzhiyun * @network_id: The network ID 157*4882a593Smuzhiyun * @network_name: The network name 158*4882a593Smuzhiyun * @priv: Private data. 159*4882a593Smuzhiyun */ 160*4882a593Smuzhiyun struct vidtv_mux_init_args { 161*4882a593Smuzhiyun u32 mux_rate_kbytes_sec; 162*4882a593Smuzhiyun void (*on_new_packets_available_cb)(void *priv, u8 *buf, u32 npackets); 163*4882a593Smuzhiyun u32 mux_buf_sz; 164*4882a593Smuzhiyun u64 pcr_period_usecs; 165*4882a593Smuzhiyun u64 si_period_usecs; 166*4882a593Smuzhiyun u16 pcr_pid; 167*4882a593Smuzhiyun u16 transport_stream_id; 168*4882a593Smuzhiyun struct vidtv_channel *channels; 169*4882a593Smuzhiyun u16 network_id; 170*4882a593Smuzhiyun char *network_name; 171*4882a593Smuzhiyun void *priv; 172*4882a593Smuzhiyun }; 173*4882a593Smuzhiyun 174*4882a593Smuzhiyun struct vidtv_mux *vidtv_mux_init(struct dvb_frontend *fe, 175*4882a593Smuzhiyun struct device *dev, 176*4882a593Smuzhiyun struct vidtv_mux_init_args *args); 177*4882a593Smuzhiyun void vidtv_mux_destroy(struct vidtv_mux *m); 178*4882a593Smuzhiyun 179*4882a593Smuzhiyun void vidtv_mux_start_thread(struct vidtv_mux *m); 180*4882a593Smuzhiyun void vidtv_mux_stop_thread(struct vidtv_mux *m); 181*4882a593Smuzhiyun 182*4882a593Smuzhiyun #endif //VIDTV_MUX_H 183