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