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