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 <linux/types.h>
19 #include "vidtv_common.h"
21 #define PES_MAX_LEN 65536 /* Set 'length' to 0 if greater. Only possible for video. */
22 #define PES_START_CODE_PREFIX 0x001 /* 00 00 01 */
24 /* Used when sending PTS, but not DTS */
25 struct vidtv_pes_optional_pts
{
31 /* Used when sending both PTS and DTS */
32 struct vidtv_pes_optional_pts_dts
{
42 /* PES optional flags */
43 struct vidtv_pes_optional
{
45 * These flags show which components are actually
46 * present in the "optional fields" in the optional PES
47 * header and which are not
50 * u16 PES_scrambling_control:2;
52 * u16 data_alignment_indicator:1; // unused
54 * u16 original_or_copy:1;
58 * u16 DSM_trick_mode:1;
59 * u16 additional_copy_info:1;
61 * u16 PES_extension:1;
68 struct vidtv_mpeg_pes
{
69 __be32 bitfield
; /* packet_start_code_prefix:24, stream_id: 8 */
70 /* after this field until the end of the PES data payload */
72 struct vidtv_pes_optional optional
[];
76 * struct pes_header_write_args - Arguments to write a PES header.
77 * @dest_buf: The buffer to write into.
78 * @dest_offset: where to start writing in the dest_buffer.
79 * @dest_buf_sz: The size of the dest_buffer
80 * @encoder_id: Encoder id (see vidtv_encoder.h)
81 * @send_pts: Should we send PTS?
82 * @pts: PTS value to send.
83 * @send_dts: Should we send DTS?
84 * @dts: DTS value to send.
85 * @stream_id: The stream id to use. Ex: Audio streams (0xc0-0xdf), Video
86 * streams (0xe0-0xef).
87 * @n_pes_h_s_bytes: Padding bytes. Might be used by an encoder if needed, gets
88 * discarded by the decoder.
89 * @access_unit_len: The size of _one_ access unit (with any headers it might need)
91 struct pes_header_write_args
{
104 /* might be used by an encoder if needed, gets discarded by decoder */
110 * struct pes_ts_header_write_args - Arguments to write a TS header.
111 * @dest_buf: The buffer to write into.
112 * @dest_offset: where to start writing in the dest_buffer.
113 * @dest_buf_sz: The size of the dest_buffer
114 * @pid: The PID to use for the TS packets.
115 * @continuity_counter: Incremented on every new TS packet.
116 * @wrote_pes_header: Flag to indicate that the PES header was written
117 * @n_stuffing_bytes: Padding bytes. Might be used by an encoder if needed, gets
118 * discarded by the decoder.
119 * @pcr: counter driven by a 27Mhz clock.
121 struct pes_ts_header_write_args
{
126 u8
*continuity_counter
;
127 bool wrote_pes_header
;
128 u32 n_stuffing_bytes
;
133 * struct pes_write_args - Arguments for the packetizer.
134 * @dest_buf: The buffer to write into.
135 * @from: A pointer to the encoder buffer containing one access unit.
136 * @access_unit_len: The size of _one_ access unit (with any headers it might need)
137 * @dest_offset: where to start writing in the dest_buffer.
138 * @dest_buf_sz: The size of the dest_buffer
139 * @pid: The PID to use for the TS packets.
140 * @encoder_id: Encoder id (see vidtv_encoder.h)
141 * @continuity_counter: Incremented on every new TS packet.
142 * @stream_id: The stream id to use. Ex: Audio streams (0xc0-0xdf), Video
143 * streams (0xe0-0xef).
144 * @send_pts: Should we send PTS?
145 * @pts: PTS value to send.
146 * @send_dts: Should we send DTS?
147 * @dts: DTS value to send.
148 * @n_pes_h_s_bytes: Padding bytes. Might be used by an encoder if needed, gets
149 * discarded by the decoder.
150 * @pcr: counter driven by a 27Mhz clock.
152 struct pes_write_args
{
163 u8
*continuity_counter
;
178 * vidtv_pes_write_into - Write a PES packet as MPEG-TS packets into a buffer.
179 * @args: The args to use when writing
181 * This function translate the ES data for one access unit
182 * from an encoder into MPEG TS packets. It does so by first encapsulating it
183 * with a PES header and then splitting it into TS packets.
185 * The data is then written into the buffer pointed to by 'args.buf'
187 * Return: The number of bytes written into the buffer. This is usually NOT
188 * equal to the size of the access unit, since we need space for PES headers, TS headers
189 * and padding bytes, if any.
191 u32
vidtv_pes_write_into(struct pes_write_args
*args
);
193 #endif // VIDTV_PES_H