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