WIP FPC-III support
[linux/fpc-iii.git] / drivers / media / test-drivers / vidtv / vidtv_ts.c
blobca4bb9c40b78ef12e6116965942cc36c3f536408
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.
7 * Copyright (C) 2020 Daniel W. S. Almeida
8 */
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"
18 #include "vidtv_ts.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 */
23 u64 div;
24 u64 rem;
25 u8 *buf = to + to_offset;
26 u64 pcr_low;
27 u64 pcr_high;
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;
39 *buf++ = pcr_low;
41 return 6;
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)
53 u32 nbytes = 0;
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;
61 /* copy TS header */
62 nbytes += vidtv_memcpy(args.dest_buf,
63 args.dest_offset + nbytes,
64 args.buf_sz,
65 &ts_header,
66 sizeof(ts_header));
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,
73 args.buf_sz,
74 TS_FILL_BYTE,
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",
80 TS_PACKET_LEN,
81 nbytes);
83 return nbytes;
86 u32 vidtv_ts_pcr_write_into(struct pcr_write_args args)
88 u32 nbytes = 0;
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;
102 ts_adap.PCR = 1;
104 /* copy TS header */
105 nbytes += vidtv_memcpy(args.dest_buf,
106 args.dest_offset + nbytes,
107 args.buf_sz,
108 &ts_header,
109 sizeof(ts_header));
111 /* write the adap after the TS header */
112 nbytes += vidtv_memcpy(args.dest_buf,
113 args.dest_offset + nbytes,
114 args.buf_sz,
115 &ts_adap,
116 sizeof(ts_adap));
118 /* write the PCR optional */
119 nbytes += vidtv_ts_write_pcr_bits(args.dest_buf,
120 args.dest_offset + nbytes,
121 args.pcr);
123 nbytes += vidtv_memset(args.dest_buf,
124 args.dest_offset + nbytes,
125 args.buf_sz,
126 TS_FILL_BYTE,
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",
132 TS_PACKET_LEN,
133 nbytes);
135 return nbytes;