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 #ifndef VIDTV_PES_H 15 #define VIDTV_PES_H 16 17 #include <asm/byteorder.h> 18 #include <linux/types.h> 19 20 #include "vidtv_common.h" 21 22 #define PES_MAX_LEN 65536 /* Set 'length' to 0 if greater. Only possible for video. */ 23 #define PES_START_CODE_PREFIX 0x001 /* 00 00 01 */ 24 25 /* Used when sending PTS, but not DTS */ 26 struct vidtv_pes_optional_pts { 27 u8 pts1; 28 __be16 pts2; 29 __be16 pts3; 30 } __packed; 31 32 /* Used when sending both PTS and DTS */ 33 struct vidtv_pes_optional_pts_dts { 34 u8 pts1; 35 __be16 pts2; 36 __be16 pts3; 37 38 u8 dts1; 39 __be16 dts2; 40 __be16 dts3; 41 } __packed; 42 43 /* PES optional flags */ 44 struct vidtv_pes_optional { 45 /* 46 * These flags show which components are actually 47 * present in the "optional fields" in the optional PES 48 * header and which are not 49 * 50 * u16 two:2; //0x2 51 * u16 PES_scrambling_control:2; 52 * u16 PES_priority:1; 53 * u16 data_alignment_indicator:1; // unused 54 * u16 copyright:1; 55 * u16 original_or_copy:1; 56 * u16 PTS_DTS:2; 57 * u16 ESCR:1; 58 * u16 ES_rate:1; 59 * u16 DSM_trick_mode:1; 60 * u16 additional_copy_info:1; 61 * u16 PES_CRC:1; 62 * u16 PES_extension:1; 63 */ 64 __be16 bitfield; 65 u8 length; 66 } __packed; 67 68 /* The PES header */ 69 struct vidtv_mpeg_pes { 70 __be32 bitfield; /* packet_start_code_prefix:24, stream_id: 8 */ 71 /* after this field until the end of the PES data payload */ 72 __be16 length; 73 struct vidtv_pes_optional optional[]; 74 } __packed; 75 76 /** 77 * struct pes_header_write_args - Arguments to write a PES header. 78 * @dest_buf: The buffer to write into. 79 * @dest_offset: where to start writing in the dest_buffer. 80 * @dest_buf_sz: The size of the dest_buffer 81 * @encoder_id: Encoder id (see vidtv_encoder.h) 82 * @send_pts: Should we send PTS? 83 * @pts: PTS value to send. 84 * @send_dts: Should we send DTS? 85 * @dts: DTS value to send. 86 * @stream_id: The stream id to use. Ex: Audio streams (0xc0-0xdf), Video 87 * streams (0xe0-0xef). 88 * @n_pes_h_s_bytes: Padding bytes. Might be used by an encoder if needed, gets 89 * discarded by the decoder. 90 * @access_unit_len: The size of _one_ access unit (with any headers it might need) 91 */ 92 struct pes_header_write_args { 93 void *dest_buf; 94 u32 dest_offset; 95 u32 dest_buf_sz; 96 u32 encoder_id; 97 98 bool send_pts; 99 u64 pts; 100 101 bool send_dts; 102 u64 dts; 103 104 u16 stream_id; 105 /* might be used by an encoder if needed, gets discarded by decoder */ 106 u32 n_pes_h_s_bytes; 107 u32 access_unit_len; 108 }; 109 110 /** 111 * struct pes_ts_header_write_args - Arguments to write a TS header. 112 * @dest_buf: The buffer to write into. 113 * @dest_offset: where to start writing in the dest_buffer. 114 * @dest_buf_sz: The size of the dest_buffer 115 * @pid: The PID to use for the TS packets. 116 * @continuity_counter: Incremented on every new TS packet. 117 * @n_pes_h_s_bytes: Padding bytes. Might be used by an encoder if needed, gets 118 * discarded by the decoder. 119 */ 120 struct pes_ts_header_write_args { 121 void *dest_buf; 122 u32 dest_offset; 123 u32 dest_buf_sz; 124 u16 pid; 125 u8 *continuity_counter; 126 bool wrote_pes_header; 127 u32 n_stuffing_bytes; 128 u64 pcr; 129 }; 130 131 /** 132 * struct pes_write_args - Arguments for the packetizer. 133 * @dest_buf: The buffer to write into. 134 * @from: A pointer to the encoder buffer containing one access unit. 135 * @access_unit_len: The size of _one_ access unit (with any headers it might need) 136 * @dest_offset: where to start writing in the dest_buffer. 137 * @dest_buf_sz: The size of the dest_buffer 138 * @pid: The PID to use for the TS packets. 139 * @encoder_id: Encoder id (see vidtv_encoder.h) 140 * @continuity_counter: Incremented on every new TS packet. 141 * @stream_id: The stream id to use. Ex: Audio streams (0xc0-0xdf), Video 142 * streams (0xe0-0xef). 143 * @send_pts: Should we send PTS? 144 * @pts: PTS value to send. 145 * @send_dts: Should we send DTS? 146 * @dts: DTS value to send. 147 * @n_pes_h_s_bytes: Padding bytes. Might be used by an encoder if needed, gets 148 * discarded by the decoder. 149 */ 150 struct pes_write_args { 151 void *dest_buf; 152 void *from; 153 u32 access_unit_len; 154 155 u32 dest_offset; 156 u32 dest_buf_sz; 157 u16 pid; 158 159 u32 encoder_id; 160 161 u8 *continuity_counter; 162 163 u16 stream_id; 164 165 bool send_pts; 166 u64 pts; 167 168 bool send_dts; 169 u64 dts; 170 171 u32 n_pes_h_s_bytes; 172 u64 pcr; 173 }; 174 175 /** 176 * vidtv_pes_write_into - Write a PES packet as MPEG-TS packets into a buffer. 177 * @args: The args to use when writing 178 * 179 * This function translate the ES data for one access unit 180 * from an encoder into MPEG TS packets. It does so by first encapsulating it 181 * with a PES header and then splitting it into TS packets. 182 * 183 * The data is then written into the buffer pointed to by 'args.buf' 184 * 185 * Return: The number of bytes written into the buffer. This is usually NOT 186 * equal to the size of the access unit, since we need space for PES headers, TS headers 187 * and padding bytes, if any. 188 */ 189 u32 vidtv_pes_write_into(struct pes_write_args args); 190 191 #endif // VIDTV_PES_H 192