1 /* SPDX-License-Identifier: GPL-2.0 */
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
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.
11 * Copyright (C) 2020 Daniel W. S. Almeida
17 #include <asm/byteorder.h>
18 #include <linux/types.h>
20 #include "vidtv_common.h"
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 */
25 /* Used when sending PTS, but not DTS */
26 struct vidtv_pes_optional_pts {
32 /* Used when sending both PTS and DTS */
33 struct vidtv_pes_optional_pts_dts {
43 /* PES optional flags */
44 struct vidtv_pes_optional {
46 * These flags show which components are actually
47 * present in the "optional fields" in the optional PES
48 * header and which are not
51 * u16 PES_scrambling_control:2;
53 * u16 data_alignment_indicator:1; // unused
55 * u16 original_or_copy:1;
59 * u16 DSM_trick_mode:1;
60 * u16 additional_copy_info:1;
62 * u16 PES_extension:1;
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 */
73 struct vidtv_pes_optional optional[];
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)
92 struct pes_header_write_args {
105 /* might be used by an encoder if needed, gets discarded by decoder */
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.
120 struct pes_ts_header_write_args {
125 u8 *continuity_counter;
126 bool wrote_pes_header;
127 u32 n_stuffing_bytes;
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.
150 struct pes_write_args {
161 u8 *continuity_counter;
176 * vidtv_pes_write_into - Write a PES packet as MPEG-TS packets into a buffer.
177 * @args: The args to use when writing
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.
183 * The data is then written into the buffer pointed to by 'args.buf'
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.
189 u32 vidtv_pes_write_into(struct pes_write_args args);
191 #endif // VIDTV_PES_H