xref: /linux/drivers/media/test-drivers/vidtv/vidtv_ts.c (revision c489573b5b6ce6442ad4658d9d5ec77839b91622)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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.
6  *
7  * Copyright (C) 2020 Daniel W. S. Almeida
8  */
9 
10 #define pr_fmt(fmt) KBUILD_MODNAME ":%s, %d: " fmt, __func__, __LINE__
11 
12 #include <linux/printk.h>
13 #include <linux/ratelimit.h>
14 #include <linux/types.h>
15 #include <linux/math64.h>
16 #include <asm/byteorder.h>
17 
18 #include "vidtv_ts.h"
19 #include "vidtv_common.h"
20 
21 static u32 vidtv_ts_write_pcr_bits(u8 *to, u32 to_offset, u64 pcr)
22 {
23 	/* Exact same from ffmpeg. PCR is a counter driven by a 27Mhz clock */
24 	u64 div;
25 	u64 rem;
26 	u8 *buf = to + to_offset;
27 	u64 pcr_low;
28 	u64 pcr_high;
29 
30 	div = div64_u64_rem(pcr, 300, &rem);
31 
32 	pcr_low = rem; /* pcr_low = pcr % 300 */
33 	pcr_high = div; /* pcr_high = pcr / 300 */
34 
35 	*buf++ = pcr_high >> 25;
36 	*buf++ = pcr_high >> 17;
37 	*buf++ = pcr_high >>  9;
38 	*buf++ = pcr_high >>  1;
39 	*buf++ = pcr_high <<  7 | pcr_low >> 8 | 0x7e;
40 	*buf++ = pcr_low;
41 
42 	return 6;
43 }
44 
45 void vidtv_ts_inc_cc(u8 *continuity_counter)
46 {
47 	++*continuity_counter;
48 	if (*continuity_counter > TS_CC_MAX_VAL)
49 		*continuity_counter = 0;
50 }
51 
52 u32 vidtv_ts_null_write_into(struct null_packet_write_args args)
53 {
54 	u32 nbytes = 0;
55 	struct vidtv_mpeg_ts ts_header = {};
56 
57 	ts_header.sync_byte          = TS_SYNC_BYTE;
58 	ts_header.bitfield           = cpu_to_be16(TS_NULL_PACKET_PID);
59 	ts_header.payload            = 1;
60 	ts_header.continuity_counter = *args.continuity_counter;
61 
62 	/* copy TS header */
63 	nbytes += vidtv_memcpy(args.dest_buf,
64 			       args.dest_offset + nbytes,
65 			       args.buf_sz,
66 			       &ts_header,
67 			       sizeof(ts_header));
68 
69 	vidtv_ts_inc_cc(args.continuity_counter);
70 
71 	/* fill the rest with empty data */
72 	nbytes += vidtv_memset(args.dest_buf,
73 			       args.dest_offset + nbytes,
74 			       args.buf_sz,
75 			       TS_FILL_BYTE,
76 			       TS_PACKET_LEN - nbytes);
77 
78 	/* we should have written exactly _one_ 188byte packet */
79 	if (nbytes != TS_PACKET_LEN)
80 		pr_warn_ratelimited("Expected exactly %d bytes, got %d\n",
81 				    TS_PACKET_LEN,
82 				    nbytes);
83 
84 	return nbytes;
85 }
86 
87 u32 vidtv_ts_pcr_write_into(struct pcr_write_args args)
88 {
89 	u32 nbytes = 0;
90 	struct vidtv_mpeg_ts ts_header = {};
91 	struct vidtv_mpeg_ts_adaption ts_adap = {};
92 
93 	ts_header.sync_byte     = TS_SYNC_BYTE;
94 	ts_header.bitfield      = cpu_to_be16(args.pid);
95 	ts_header.scrambling    = 0;
96 	/* cc is not incremented, but it is needed. see 13818-1 clause 2.4.3.3 */
97 	ts_header.continuity_counter = *args.continuity_counter;
98 	ts_header.payload            = 0;
99 	ts_header.adaptation_field   = 1;
100 
101 	/* 13818-1 clause 2.4.3.5 */
102 	ts_adap.length = 183;
103 	ts_adap.PCR    = 1;
104 
105 	/* copy TS header */
106 	nbytes += vidtv_memcpy(args.dest_buf,
107 			       args.dest_offset + nbytes,
108 			       args.buf_sz,
109 			       &ts_header,
110 			       sizeof(ts_header));
111 
112 	/* write the adap after the TS header */
113 	nbytes += vidtv_memcpy(args.dest_buf,
114 			       args.dest_offset + nbytes,
115 			       args.buf_sz,
116 			       &ts_adap,
117 			       sizeof(ts_adap));
118 
119 	/* write the PCR optional */
120 	nbytes += vidtv_ts_write_pcr_bits(args.dest_buf,
121 					  args.dest_offset + nbytes,
122 					  args.pcr);
123 
124 	nbytes += vidtv_memset(args.dest_buf,
125 			       args.dest_offset + nbytes,
126 			       args.buf_sz,
127 			       TS_FILL_BYTE,
128 			       TS_PACKET_LEN - nbytes);
129 
130 	/* we should have written exactly _one_ 188byte packet */
131 	if (nbytes != TS_PACKET_LEN)
132 		pr_warn_ratelimited("Expected exactly %d bytes, got %d\n",
133 				    TS_PACKET_LEN,
134 				    nbytes);
135 
136 	return nbytes;
137 }
138