1 // SPDX-License-Identifier: GPL-2.0
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
7 * This file contains the multiplexer logic for TS packets from different
10 * Loosely based on libavcodec/mpegtsenc.c
12 * Copyright (C) 2020 Daniel W. S. Almeida
15 #include <linux/delay.h>
16 #include <linux/dev_printk.h>
17 #include <linux/jiffies.h>
18 #include <linux/kernel.h>
19 #include <linux/math64.h>
20 #include <linux/ratelimit.h>
21 #include <linux/slab.h>
22 #include <linux/types.h>
23 #include <linux/vmalloc.h>
25 #include "vidtv_channel.h"
26 #include "vidtv_common.h"
27 #include "vidtv_encoder.h"
28 #include "vidtv_mux.h"
29 #include "vidtv_pes.h"
30 #include "vidtv_psi.h"
33 static struct vidtv_mux_pid_ctx
34 *vidtv_mux_get_pid_ctx(struct vidtv_mux
*m
, u16 pid
)
36 struct vidtv_mux_pid_ctx
*ctx
;
38 hash_for_each_possible(m
->pid_ctx
, ctx
, h
, pid
)
44 static struct vidtv_mux_pid_ctx
45 *vidtv_mux_create_pid_ctx_once(struct vidtv_mux
*m
, u16 pid
)
47 struct vidtv_mux_pid_ctx
*ctx
;
49 ctx
= vidtv_mux_get_pid_ctx(m
, pid
);
53 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
59 hash_add(m
->pid_ctx
, &ctx
->h
, pid
);
64 static void vidtv_mux_pid_ctx_destroy(struct vidtv_mux
*m
)
66 struct vidtv_mux_pid_ctx
*ctx
;
67 struct hlist_node
*tmp
;
70 hash_for_each_safe(m
->pid_ctx
, bkt
, tmp
, ctx
, h
) {
76 static int vidtv_mux_pid_ctx_init(struct vidtv_mux
*m
)
78 struct vidtv_psi_table_pat_program
*p
= m
->si
.pat
->program
;
81 hash_init(m
->pid_ctx
);
82 /* push the pcr pid ctx */
83 if (!vidtv_mux_create_pid_ctx_once(m
, m
->pcr_pid
))
85 /* push the NULL packet pid ctx */
86 if (!vidtv_mux_create_pid_ctx_once(m
, TS_NULL_PACKET_PID
))
88 /* push the PAT pid ctx */
89 if (!vidtv_mux_create_pid_ctx_once(m
, VIDTV_PAT_PID
))
91 /* push the SDT pid ctx */
92 if (!vidtv_mux_create_pid_ctx_once(m
, VIDTV_SDT_PID
))
94 /* push the NIT pid ctx */
95 if (!vidtv_mux_create_pid_ctx_once(m
, VIDTV_NIT_PID
))
97 /* push the EIT pid ctx */
98 if (!vidtv_mux_create_pid_ctx_once(m
, VIDTV_EIT_PID
))
101 /* add a ctx for all PMT sections */
103 pid
= vidtv_psi_get_pat_program_pid(p
);
104 vidtv_mux_create_pid_ctx_once(m
, pid
);
111 vidtv_mux_pid_ctx_destroy(m
);
115 static void vidtv_mux_update_clk(struct vidtv_mux
*m
)
117 /* call this at every thread iteration */
120 m
->timing
.past_jiffies
= m
->timing
.current_jiffies
;
121 m
->timing
.current_jiffies
= get_jiffies_64();
123 elapsed_time
= jiffies_to_usecs(m
->timing
.current_jiffies
-
124 m
->timing
.past_jiffies
);
126 /* update the 27Mhz clock proportionally to the elapsed time */
127 m
->timing
.clk
+= (CLOCK_UNIT_27MHZ
/ USEC_PER_SEC
) * elapsed_time
;
130 static u32
vidtv_mux_push_si(struct vidtv_mux
*m
)
132 struct vidtv_psi_pat_write_args pat_args
= {
134 .buf_sz
= m
->mux_buf_sz
,
137 struct vidtv_psi_pmt_write_args pmt_args
= {
139 .buf_sz
= m
->mux_buf_sz
,
140 .pcr_pid
= m
->pcr_pid
,
142 struct vidtv_psi_sdt_write_args sdt_args
= {
144 .buf_sz
= m
->mux_buf_sz
,
147 struct vidtv_psi_nit_write_args nit_args
= {
149 .buf_sz
= m
->mux_buf_sz
,
153 struct vidtv_psi_eit_write_args eit_args
= {
155 .buf_sz
= m
->mux_buf_sz
,
158 u32 initial_offset
= m
->mux_buf_offset
;
159 struct vidtv_mux_pid_ctx
*pat_ctx
;
160 struct vidtv_mux_pid_ctx
*pmt_ctx
;
161 struct vidtv_mux_pid_ctx
*sdt_ctx
;
162 struct vidtv_mux_pid_ctx
*nit_ctx
;
163 struct vidtv_mux_pid_ctx
*eit_ctx
;
168 pat_ctx
= vidtv_mux_get_pid_ctx(m
, VIDTV_PAT_PID
);
169 sdt_ctx
= vidtv_mux_get_pid_ctx(m
, VIDTV_SDT_PID
);
170 nit_ctx
= vidtv_mux_get_pid_ctx(m
, VIDTV_NIT_PID
);
171 eit_ctx
= vidtv_mux_get_pid_ctx(m
, VIDTV_EIT_PID
);
173 pat_args
.offset
= m
->mux_buf_offset
;
174 pat_args
.continuity_counter
= &pat_ctx
->cc
;
176 m
->mux_buf_offset
+= vidtv_psi_pat_write_into(&pat_args
);
178 for (i
= 0; i
< m
->si
.pat
->num_pmt
; ++i
) {
179 pmt_pid
= vidtv_psi_pmt_get_pid(m
->si
.pmt_secs
[i
],
182 if (pmt_pid
> TS_LAST_VALID_PID
) {
183 dev_warn_ratelimited(m
->dev
,
184 "PID: %d not found\n", pmt_pid
);
188 pmt_ctx
= vidtv_mux_get_pid_ctx(m
, pmt_pid
);
190 pmt_args
.offset
= m
->mux_buf_offset
;
191 pmt_args
.pmt
= m
->si
.pmt_secs
[i
];
192 pmt_args
.pid
= pmt_pid
;
193 pmt_args
.continuity_counter
= &pmt_ctx
->cc
;
195 /* write each section into buffer */
196 m
->mux_buf_offset
+= vidtv_psi_pmt_write_into(&pmt_args
);
199 sdt_args
.offset
= m
->mux_buf_offset
;
200 sdt_args
.continuity_counter
= &sdt_ctx
->cc
;
202 m
->mux_buf_offset
+= vidtv_psi_sdt_write_into(&sdt_args
);
204 nit_args
.offset
= m
->mux_buf_offset
;
205 nit_args
.continuity_counter
= &nit_ctx
->cc
;
207 m
->mux_buf_offset
+= vidtv_psi_nit_write_into(&nit_args
);
209 eit_args
.offset
= m
->mux_buf_offset
;
210 eit_args
.continuity_counter
= &eit_ctx
->cc
;
212 m
->mux_buf_offset
+= vidtv_psi_eit_write_into(&eit_args
);
214 nbytes
= m
->mux_buf_offset
- initial_offset
;
216 m
->num_streamed_si
++;
221 static u32
vidtv_mux_push_pcr(struct vidtv_mux
*m
)
223 struct pcr_write_args args
= {};
224 struct vidtv_mux_pid_ctx
*ctx
;
227 ctx
= vidtv_mux_get_pid_ctx(m
, m
->pcr_pid
);
228 args
.dest_buf
= m
->mux_buf
;
229 args
.pid
= m
->pcr_pid
;
230 args
.buf_sz
= m
->mux_buf_sz
;
231 args
.continuity_counter
= &ctx
->cc
;
233 /* the 27Mhz clock will feed both parts of the PCR bitfield */
234 args
.pcr
= m
->timing
.clk
;
236 nbytes
+= vidtv_ts_pcr_write_into(args
);
237 m
->mux_buf_offset
+= nbytes
;
239 m
->num_streamed_pcr
++;
244 static bool vidtv_mux_should_push_pcr(struct vidtv_mux
*m
)
248 if (m
->num_streamed_pcr
== 0)
251 next_pcr_at
= m
->timing
.start_jiffies
+
252 usecs_to_jiffies(m
->num_streamed_pcr
*
253 m
->timing
.pcr_period_usecs
);
255 return time_after64(m
->timing
.current_jiffies
, next_pcr_at
);
258 static bool vidtv_mux_should_push_si(struct vidtv_mux
*m
)
262 if (m
->num_streamed_si
== 0)
265 next_si_at
= m
->timing
.start_jiffies
+
266 usecs_to_jiffies(m
->num_streamed_si
*
267 m
->timing
.si_period_usecs
);
269 return time_after64(m
->timing
.current_jiffies
, next_si_at
);
272 static u32
vidtv_mux_packetize_access_units(struct vidtv_mux
*m
,
273 struct vidtv_encoder
*e
)
275 struct pes_write_args args
= {
276 .dest_buf
= m
->mux_buf
,
277 .dest_buf_sz
= m
->mux_buf_sz
,
278 .pid
= be16_to_cpu(e
->es_pid
),
280 .stream_id
= be16_to_cpu(e
->stream_id
),
281 .send_pts
= true, /* forbidden value '01'... */
282 .send_dts
= false, /* ...for PTS_DTS flags */
284 struct vidtv_access_unit
*au
= e
->access_units
;
285 u32 initial_offset
= m
->mux_buf_offset
;
286 struct vidtv_mux_pid_ctx
*pid_ctx
;
290 /* see SMPTE 302M clause 6.4 */
291 if (args
.encoder_id
== S302M
) {
292 args
.send_dts
= false;
293 args
.send_pts
= true;
296 pid_ctx
= vidtv_mux_create_pid_ctx_once(m
, be16_to_cpu(e
->es_pid
));
297 args
.continuity_counter
= &pid_ctx
->cc
;
300 buf
= e
->encoder_buf
+ au
->offset
;
302 args
.access_unit_len
= au
->nbytes
;
303 args
.dest_offset
= m
->mux_buf_offset
;
305 args
.pcr
= m
->timing
.clk
;
307 m
->mux_buf_offset
+= vidtv_pes_write_into(&args
);
313 * clear the encoder state once the ES data has been written to the mux
318 nbytes
= m
->mux_buf_offset
- initial_offset
;
322 static u32
vidtv_mux_poll_encoders(struct vidtv_mux
*m
)
324 struct vidtv_channel
*cur_chnl
= m
->channels
;
325 struct vidtv_encoder
*e
= NULL
;
330 e
= cur_chnl
->encoders
;
334 /* get the TS packets into the mux buffer */
335 au_nbytes
= vidtv_mux_packetize_access_units(m
, e
);
337 m
->mux_buf_offset
+= au_nbytes
;
338 /* grab next encoder */
342 /* grab the next channel */
343 cur_chnl
= cur_chnl
->next
;
349 static u32
vidtv_mux_pad_with_nulls(struct vidtv_mux
*m
, u32 npkts
)
351 struct null_packet_write_args args
= {
352 .dest_buf
= m
->mux_buf
,
353 .buf_sz
= m
->mux_buf_sz
,
354 .dest_offset
= m
->mux_buf_offset
,
356 u32 initial_offset
= m
->mux_buf_offset
;
357 struct vidtv_mux_pid_ctx
*ctx
;
361 ctx
= vidtv_mux_get_pid_ctx(m
, TS_NULL_PACKET_PID
);
363 args
.continuity_counter
= &ctx
->cc
;
365 for (i
= 0; i
< npkts
; ++i
) {
366 m
->mux_buf_offset
+= vidtv_ts_null_write_into(args
);
367 args
.dest_offset
= m
->mux_buf_offset
;
370 nbytes
= m
->mux_buf_offset
- initial_offset
;
373 if (nbytes
!= npkts
* TS_PACKET_LEN
)
374 dev_err_ratelimited(m
->dev
, "%d != %d\n",
375 nbytes
, npkts
* TS_PACKET_LEN
);
380 static void vidtv_mux_clear(struct vidtv_mux
*m
)
382 /* clear the packets currently in the mux */
383 memset(m
->mux_buf
, 0, m
->mux_buf_sz
* sizeof(*m
->mux_buf
));
384 /* point to the beginning of the buffer again */
385 m
->mux_buf_offset
= 0;
388 #define ERR_RATE 10000000
389 static void vidtv_mux_tick(struct work_struct
*work
)
391 struct vidtv_mux
*m
= container_of(work
,
394 struct dtv_frontend_properties
*c
= &m
->fe
->dtv_property_cache
;
399 while (m
->streaming
) {
402 vidtv_mux_update_clk(m
);
404 if (vidtv_mux_should_push_pcr(m
))
405 nbytes
+= vidtv_mux_push_pcr(m
);
407 if (vidtv_mux_should_push_si(m
))
408 nbytes
+= vidtv_mux_push_si(m
);
410 nbytes
+= vidtv_mux_poll_encoders(m
);
411 nbytes
+= vidtv_mux_pad_with_nulls(m
, 256);
413 npkts
= nbytes
/ TS_PACKET_LEN
;
415 /* if the buffer is not aligned there is a bug somewhere */
416 if (nbytes
% TS_PACKET_LEN
)
417 dev_err_ratelimited(m
->dev
, "Misaligned buffer\n");
419 if (m
->on_new_packets_available_cb
)
420 m
->on_new_packets_available_cb(m
->priv
,
427 * Update bytes and packet counts at DVBv5 stats
429 * For now, both pre and post bit counts are identical,
430 * but post BER count can be lower than pre BER, if the error
431 * correction logic discards packages.
433 c
->pre_bit_count
.stat
[0].uvalue
= nbytes
* 8;
434 c
->post_bit_count
.stat
[0].uvalue
= nbytes
* 8;
435 c
->block_count
.stat
[0].uvalue
+= npkts
;
438 * Even without any visible errors for the user, the pre-BER
439 * stats usually have an error range up to 1E-6. So,
440 * add some random error increment count to it.
442 * Please notice that this is a poor guy's implementation,
443 * as it will produce one corrected bit error every time
444 * ceil(total bytes / ERR_RATE) is incremented, without
445 * any sort of (pseudo-)randomness.
447 tot_bits
+= nbytes
* 8;
448 if (tot_bits
> ERR_RATE
) {
449 c
->pre_bit_error
.stat
[0].uvalue
++;
450 tot_bits
-= ERR_RATE
;
453 usleep_range(VIDTV_SLEEP_USECS
, VIDTV_MAX_SLEEP_USECS
);
457 void vidtv_mux_start_thread(struct vidtv_mux
*m
)
460 dev_warn_ratelimited(m
->dev
, "Already streaming. Skipping.\n");
465 m
->timing
.start_jiffies
= get_jiffies_64();
466 schedule_work(&m
->mpeg_thread
);
469 void vidtv_mux_stop_thread(struct vidtv_mux
*m
)
472 m
->streaming
= false; /* thread will quit */
473 cancel_work_sync(&m
->mpeg_thread
);
477 struct vidtv_mux
*vidtv_mux_init(struct dvb_frontend
*fe
,
479 struct vidtv_mux_init_args
*args
)
483 m
= kzalloc(sizeof(*m
), GFP_KERNEL
);
489 m
->timing
.pcr_period_usecs
= args
->pcr_period_usecs
;
490 m
->timing
.si_period_usecs
= args
->si_period_usecs
;
492 m
->mux_rate_kbytes_sec
= args
->mux_rate_kbytes_sec
;
494 m
->on_new_packets_available_cb
= args
->on_new_packets_available_cb
;
496 m
->mux_buf
= vzalloc(args
->mux_buf_sz
);
500 m
->mux_buf_sz
= args
->mux_buf_sz
;
502 m
->pcr_pid
= args
->pcr_pid
;
503 m
->transport_stream_id
= args
->transport_stream_id
;
504 m
->priv
= args
->priv
;
505 m
->network_id
= args
->network_id
;
506 m
->network_name
= kstrdup(args
->network_name
, GFP_KERNEL
);
507 m
->timing
.current_jiffies
= get_jiffies_64();
510 m
->channels
= args
->channels
;
512 if (vidtv_channels_init(m
) < 0)
515 /* will alloc data for pmt_sections after initializing pat */
516 if (vidtv_channel_si_init(m
) < 0)
519 INIT_WORK(&m
->mpeg_thread
, vidtv_mux_tick
);
521 if (vidtv_mux_pid_ctx_init(m
) < 0)
522 goto free_channel_si
;
527 vidtv_channel_si_destroy(m
);
529 vidtv_channels_destroy(m
);
537 void vidtv_mux_destroy(struct vidtv_mux
*m
)
539 vidtv_mux_stop_thread(m
);
540 vidtv_mux_pid_ctx_destroy(m
);
541 vidtv_channel_si_destroy(m
);
542 vidtv_channels_destroy(m
);
543 kfree(m
->network_name
);