1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
|
// SPDX-License-Identifier: GPL-2.0
/*
* Vidtv serves as a reference DVB driver and helps validate the existing APIs
* in the media subsystem. It can also aid developers working on userspace
* applications.
*
* This file contains the logic to translate the ES data for one access unit
* from an encoder into MPEG TS packets. It does so by first encapsulating it
* with a PES header and then splitting it into TS packets.
*
* Copyright (C) 2020 Daniel W. S. Almeida
*/
#define pr_fmt(fmt) KBUILD_MODNAME ":%s, %d: " fmt, __func__, __LINE__
#include <linux/types.h>
#include <linux/printk.h>
#include <linux/ratelimit.h>
#include <asm/byteorder.h>
#include "vidtv_pes.h"
#include "vidtv_common.h"
#include "vidtv_encoder.h"
#include "vidtv_ts.h"
#define PRIVATE_STREAM_1_ID 0xbd /* private_stream_1. See SMPTE 302M-2007 p.6 */
#define PES_HEADER_MAX_STUFFING_BYTES 32
#define PES_TS_HEADER_MAX_STUFFING_BYTES 182
static u32 vidtv_pes_op_get_len(bool send_pts, bool send_dts)
{
u32 len = 0;
/* the flags must always be sent */
len += sizeof(struct vidtv_pes_optional);
/* From all optionals, we might send these for now */
if (send_pts && send_dts)
len += sizeof(struct vidtv_pes_optional_pts_dts);
else if (send_pts)
len += sizeof(struct vidtv_pes_optional_pts);
return len;
}
static u32 vidtv_pes_h_get_len(bool send_pts, bool send_dts)
{
/* PES header length notwithstanding stuffing bytes */
u32 len = 0;
len += sizeof(struct vidtv_mpeg_pes);
len += vidtv_pes_op_get_len(send_pts, send_dts);
return len;
}
static u32 vidtv_pes_write_header_stuffing(struct pes_header_write_args args)
{
/*
* This is a fixed 8-bit value equal to '0xFF' that can be inserted
* by the encoder, for example to meet the requirements of the channel.
* It is discarded by the decoder. No more than 32 stuffing bytes shall
* be present in one PES packet header.
*/
if (args.n_pes_h_s_bytes > PES_HEADER_MAX_STUFFING_BYTES) {
pr_warn_ratelimited("More than %d stuffing bytes in PES packet header\n",
PES_HEADER_MAX_STUFFING_BYTES);
args.n_pes_h_s_bytes = PES_HEADER_MAX_STUFFING_BYTES;
}
return vidtv_memset(args.dest_buf,
args.dest_offset,
args.dest_buf_sz,
TS_FILL_BYTE,
args.n_pes_h_s_bytes);
}
static u32 vidtv_pes_write_pts_dts(struct pes_header_write_args args)
{
u32 nbytes = 0; /* the number of bytes written by this function */
struct vidtv_pes_optional_pts pts = {};
struct vidtv_pes_optional_pts_dts pts_dts = {};
void *op = NULL;
size_t op_sz = 0;
u64 mask1;
u64 mask2;
u64 mask3;
if (!args.send_pts && args.send_dts)
return 0;
#ifdef __BIG_ENDIAN
mask1 = GENMASK(30, 32);
mask2 = GENMASK(15, 29);
mask3 = GENMASK(0, 14);
#else
mask1 = GENMASK(32, 30);
mask2 = GENMASK(29, 15);
mask3 = GENMASK(14, 0);
#endif
/* see ISO/IEC 13818-1 : 2000 p. 32 */
if (args.send_pts && args.send_dts) {
pts_dts.pts1 = (0x3 << 4) | ((args.pts & mask1) >> 29) | 0x1;
pts_dts.pts2 = cpu_to_be16(((args.pts & mask2) >> 14) | 0x1);
pts_dts.pts3 = cpu_to_be16(((args.pts & mask3) << 1) | 0x1);
pts_dts.dts1 = (0x1 << 4) | ((args.dts & mask1) >> 29) | 0x1;
pts_dts.dts2 = cpu_to_be16(((args.dts & mask2) >> 14) | 0x1);
pts_dts.dts3 = cpu_to_be16(((args.dts & mask3) << 1) | 0x1);
op = &pts_dts;
op_sz = sizeof(pts_dts);
} else if (args.send_pts) {
pts.pts1 = (0x1 << 5) | ((args.pts & mask1) >> 29) | 0x1;
pts.pts2 = cpu_to_be16(((args.pts & mask2) >> 14) | 0x1);
pts.pts3 = cpu_to_be16(((args.pts & mask3) << 1) | 0x1);
op = &pts;
op_sz = sizeof(pts);
}
/* copy PTS/DTS optional */
nbytes += vidtv_memcpy(args.dest_buf,
args.dest_offset + nbytes,
args.dest_buf_sz,
op,
op_sz);
return nbytes;
}
static u32 vidtv_pes_write_h(struct pes_header_write_args args)
{
u32 nbytes = 0; /* the number of bytes written by this function */
struct vidtv_mpeg_pes pes_header = {};
struct vidtv_pes_optional pes_optional = {};
struct pes_header_write_args pts_dts_args = args;
u32 stream_id = (args.encoder_id == S302M) ? PRIVATE_STREAM_1_ID : args.stream_id;
u16 pes_opt_bitfield = 0x2 << 13;
pes_header.bitfield = cpu_to_be32((PES_START_CODE_PREFIX << 8) | stream_id);
pes_header.length = cpu_to_be16(vidtv_pes_op_get_len(args.send_pts,
args.send_dts) +
args.access_unit_len);
if (args.send_pts && args.send_dts)
pes_opt_bitfield |= (0x3 << 6);
else if (args.send_pts)
pes_opt_bitfield |= (0x1 << 7);
pes_optional.bitfield = cpu_to_be16(pes_opt_bitfield);
pes_optional.length = vidtv_pes_op_get_len(args.send_pts, args.send_dts) +
args.n_pes_h_s_bytes -
sizeof(struct vidtv_pes_optional);
/* copy header */
nbytes += vidtv_memcpy(args.dest_buf,
args.dest_offset + nbytes,
args.dest_buf_sz,
&pes_header,
sizeof(pes_header));
/* copy optional header bits */
nbytes += vidtv_memcpy(args.dest_buf,
args.dest_offset + nbytes,
args.dest_buf_sz,
&pes_optional,
sizeof(pes_optional));
/* copy the timing information */
pts_dts_args.dest_offset = args.dest_offset + nbytes;
nbytes += vidtv_pes_write_pts_dts(pts_dts_args);
/* write any PES header stuffing */
nbytes += vidtv_pes_write_header_stuffing(args);
return nbytes;
}
static u32 vidtv_pes_write_stuffing(void *dest_buf,
u32 dest_offset,
u32 n_stuffing_bytes,
u32 buf_sz)
{
u32 nbytes = 0;
struct vidtv_mpeg_ts_adaption ts_adap = {};
u32 stuff_nbytes = 0;
if (!n_stuffing_bytes)
goto out;
if (n_stuffing_bytes > PES_TS_HEADER_MAX_STUFFING_BYTES) {
pr_warn_ratelimited("More than %d stuffing bytes for a PES packet!\n",
PES_TS_HEADER_MAX_STUFFING_BYTES);
n_stuffing_bytes = PES_TS_HEADER_MAX_STUFFING_BYTES;
}
/* the AF will only be its 'length' field with a value of zero */
if (n_stuffing_bytes == 1) {
nbytes += vidtv_memset(dest_buf,
dest_offset + nbytes,
buf_sz,
0,
n_stuffing_bytes);
goto out;
}
stuff_nbytes = n_stuffing_bytes - sizeof(ts_adap);
/* length _immediately_ following 'adaptation_field_length' */
ts_adap.length = sizeof(ts_adap) -
sizeof(ts_adap.length) +
stuff_nbytes;
/* write the adap after the TS header */
nbytes += vidtv_memcpy(dest_buf,
dest_offset + nbytes,
buf_sz,
&ts_adap,
sizeof(ts_adap));
/* write the stuffing bytes */
nbytes += vidtv_memset(dest_buf,
dest_offset + nbytes,
buf_sz,
TS_FILL_BYTE,
stuff_nbytes);
out:
if (nbytes != n_stuffing_bytes)
pr_warn_ratelimited("write size was %d, expected %d\n",
nbytes,
n_stuffing_bytes);
return nbytes;
}
static u32 vidtv_pes_write_ts_h(struct pes_ts_header_write_args args)
{
/* number of bytes written by this function */
u32 nbytes = 0;
struct vidtv_mpeg_ts ts_header = {};
u16 payload_start = !args.wrote_pes_header;
ts_header.sync_byte = TS_SYNC_BYTE;
ts_header.bitfield = cpu_to_be16((payload_start << 14) | args.pid);
ts_header.scrambling = 0;
ts_header.adaptation_field = (args.n_stuffing_bytes) > 0;
ts_header.payload = (args.n_stuffing_bytes) < PES_TS_HEADER_MAX_STUFFING_BYTES;
ts_header.continuity_counter = *args.continuity_counter;
vidtv_ts_inc_cc(args.continuity_counter);
/* write the TS header */
nbytes += vidtv_memcpy(args.dest_buf,
args.dest_offset + nbytes,
args.dest_buf_sz,
&ts_header,
sizeof(ts_header));
/* write stuffing, if any */
nbytes += vidtv_pes_write_stuffing(args.dest_buf,
args.dest_offset + nbytes,
args.n_stuffing_bytes,
args.dest_buf_sz);
return nbytes;
}
u32 vidtv_pes_write_into(struct pes_write_args args)
{
u32 nbytes_past_boundary = (args.dest_offset % TS_PACKET_LEN);
bool aligned = (nbytes_past_boundary == 0);
struct pes_ts_header_write_args ts_header_args = {};
struct pes_header_write_args pes_header_args = {};
/* number of bytes written by this function */
u32 nbytes = 0;
u32 remaining_len = args.access_unit_len;
bool wrote_pes_header = false;
/* whether we need to stuff the TS packet to align the buffer */
bool should_insert_stuffing_bytes = false;
u32 available_space = 0;
u32 payload_write_len = 0;
u32 num_stuffing_bytes = 0;
if (!aligned) {
pr_warn_ratelimited("Cannot start a PES packet in a misaligned buffer\n");
/* forcibly align and hope for the best */
nbytes += vidtv_memset(args.dest_buf,
args.dest_offset + nbytes,
args.dest_buf_sz,
TS_FILL_BYTE,
TS_PACKET_LEN - nbytes_past_boundary);
aligned = true;
}
if (args.send_dts && !args.send_pts) {
pr_warn_ratelimited("forbidden value '01' for PTS_DTS flags\n");
args.send_pts = true;
args.pts = args.dts;
}
/* see SMPTE 302M clause 6.4 */
if (args.encoder_id == S302M) {
args.send_dts = false;
args.send_pts = true;
}
while (remaining_len) {
/*
* The amount of space initially available in the TS packet.
* if this is the beginning of the PES packet, take into account
* the space needed for the TS header _and_ for the PES header
*/
available_space = (!wrote_pes_header) ?
TS_PAYLOAD_LEN -
vidtv_pes_h_get_len(args.send_pts, args.send_dts) :
TS_PAYLOAD_LEN;
/* if the encoder has inserted stuffing bytes in the PES
* header, account for them.
*/
available_space -= args.n_pes_h_s_bytes;
/* whether stuffing bytes are needed to align the buffer */
should_insert_stuffing_bytes = remaining_len < available_space;
/*
* how much of the _actual_ payload should be written in this
* packet.
*/
payload_write_len = (should_insert_stuffing_bytes) ?
remaining_len :
available_space;
num_stuffing_bytes = available_space - payload_write_len;
/* write ts header */
ts_header_args.dest_buf = args.dest_buf;
ts_header_args.dest_offset = args.dest_offset + nbytes;
ts_header_args.dest_buf_sz = args.dest_buf_sz;
ts_header_args.pid = args.pid;
ts_header_args.continuity_counter = args.continuity_counter;
ts_header_args.wrote_pes_header = wrote_pes_header;
ts_header_args.n_stuffing_bytes = num_stuffing_bytes;
nbytes += vidtv_pes_write_ts_h(ts_header_args);
if (!wrote_pes_header) {
/* write the PES header only once */
pes_header_args.dest_buf = args.dest_buf;
pes_header_args.dest_offset = args.dest_offset +
nbytes;
pes_header_args.dest_buf_sz = args.dest_buf_sz;
pes_header_args.encoder_id = args.encoder_id;
pes_header_args.send_pts = args.send_pts;
pes_header_args.pts = args.pts;
pes_header_args.send_dts = args.send_dts;
pes_header_args.dts = args.dts;
pes_header_args.stream_id = args.stream_id;
pes_header_args.n_pes_h_s_bytes = args.n_pes_h_s_bytes;
pes_header_args.access_unit_len = args.access_unit_len;
nbytes += vidtv_pes_write_h(pes_header_args);
wrote_pes_header = true;
}
/* write as much of the payload as we possibly can */
nbytes += vidtv_memcpy(args.dest_buf,
args.dest_offset + nbytes,
args.dest_buf_sz,
args.from,
payload_write_len);
args.from += payload_write_len;
remaining_len -= payload_write_len;
}
return nbytes;
}
|