1 // SPDX-License-Identifier: GPL-2.0
3 * The Virtual DVB test driver serves as a reference DVB driver and helps
4 * validate the existing APIs in the media subsystem. It can also aid
5 * developers working on userspace applications.
7 * Copyright (C) 2020 Daniel W. S. Almeida
10 #define pr_fmt(fmt) KBUILD_MODNAME ":%s, %d: " fmt, __func__, __LINE__
12 #include <linux/math64.h>
13 #include <linux/printk.h>
14 #include <linux/ratelimit.h>
15 #include <linux/types.h>
17 #include "vidtv_common.h"
20 static u32
vidtv_ts_write_pcr_bits(u8
*to
, u32 to_offset
, u64 pcr
)
22 /* Exact same from ffmpeg. PCR is a counter driven by a 27Mhz clock */
25 u8
*buf
= to
+ to_offset
;
29 div
= div64_u64_rem(pcr
, 300, &rem
);
31 pcr_low
= rem
; /* pcr_low = pcr % 300 */
32 pcr_high
= div
; /* pcr_high = pcr / 300 */
34 *buf
++ = pcr_high
>> 25;
35 *buf
++ = pcr_high
>> 17;
36 *buf
++ = pcr_high
>> 9;
37 *buf
++ = pcr_high
>> 1;
38 *buf
++ = pcr_high
<< 7 | pcr_low
>> 8 | 0x7e;
44 void vidtv_ts_inc_cc(u8
*continuity_counter
)
46 ++*continuity_counter
;
47 if (*continuity_counter
> TS_CC_MAX_VAL
)
48 *continuity_counter
= 0;
51 u32
vidtv_ts_null_write_into(struct null_packet_write_args args
)
54 struct vidtv_mpeg_ts ts_header
= {};
56 ts_header
.sync_byte
= TS_SYNC_BYTE
;
57 ts_header
.bitfield
= cpu_to_be16(TS_NULL_PACKET_PID
);
58 ts_header
.payload
= 1;
59 ts_header
.continuity_counter
= *args
.continuity_counter
;
62 nbytes
+= vidtv_memcpy(args
.dest_buf
,
63 args
.dest_offset
+ nbytes
,
68 vidtv_ts_inc_cc(args
.continuity_counter
);
70 /* fill the rest with empty data */
71 nbytes
+= vidtv_memset(args
.dest_buf
,
72 args
.dest_offset
+ nbytes
,
75 TS_PACKET_LEN
- nbytes
);
77 /* we should have written exactly _one_ 188byte packet */
78 if (nbytes
!= TS_PACKET_LEN
)
79 pr_warn_ratelimited("Expected exactly %d bytes, got %d\n",
86 u32
vidtv_ts_pcr_write_into(struct pcr_write_args args
)
89 struct vidtv_mpeg_ts ts_header
= {};
90 struct vidtv_mpeg_ts_adaption ts_adap
= {};
92 ts_header
.sync_byte
= TS_SYNC_BYTE
;
93 ts_header
.bitfield
= cpu_to_be16(args
.pid
);
94 ts_header
.scrambling
= 0;
95 /* cc is not incremented, but it is needed. see 13818-1 clause 2.4.3.3 */
96 ts_header
.continuity_counter
= *args
.continuity_counter
;
97 ts_header
.payload
= 0;
98 ts_header
.adaptation_field
= 1;
100 /* 13818-1 clause 2.4.3.5 */
101 ts_adap
.length
= 183;
105 nbytes
+= vidtv_memcpy(args
.dest_buf
,
106 args
.dest_offset
+ nbytes
,
111 /* write the adap after the TS header */
112 nbytes
+= vidtv_memcpy(args
.dest_buf
,
113 args
.dest_offset
+ nbytes
,
118 /* write the PCR optional */
119 nbytes
+= vidtv_ts_write_pcr_bits(args
.dest_buf
,
120 args
.dest_offset
+ nbytes
,
123 nbytes
+= vidtv_memset(args
.dest_buf
,
124 args
.dest_offset
+ nbytes
,
127 TS_PACKET_LEN
- nbytes
);
129 /* we should have written exactly _one_ 188byte packet */
130 if (nbytes
!= TS_PACKET_LEN
)
131 pr_warn_ratelimited("Expected exactly %d bytes, got %d\n",