Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * Vidtv serves as a reference DVB driver and helps validate the existing APIs
0004  * in the media subsystem. It can also aid developers working on userspace
0005  * applications.
0006  *
0007  * This file contains the logic to translate the ES data for one access unit
0008  * from an encoder into MPEG TS packets. It does so by first encapsulating it
0009  * with a PES header and then splitting it into TS packets.
0010  *
0011  * Copyright (C) 2020 Daniel W. S. Almeida
0012  */
0013 
0014 #ifndef VIDTV_PES_H
0015 #define VIDTV_PES_H
0016 
0017 #include <linux/types.h>
0018 
0019 #include "vidtv_common.h"
0020 
0021 #define PES_MAX_LEN 65536 /* Set 'length' to 0 if greater. Only possible for video. */
0022 #define PES_START_CODE_PREFIX 0x001 /* 00 00 01 */
0023 
0024 /* Used when sending PTS, but not DTS */
0025 struct vidtv_pes_optional_pts {
0026     u8 pts1;
0027     __be16 pts2;
0028     __be16 pts3;
0029 } __packed;
0030 
0031 /* Used when sending both PTS and DTS */
0032 struct vidtv_pes_optional_pts_dts {
0033     u8 pts1;
0034     __be16 pts2;
0035     __be16 pts3;
0036 
0037     u8 dts1;
0038     __be16 dts2;
0039     __be16 dts3;
0040 } __packed;
0041 
0042 /* PES optional flags */
0043 struct vidtv_pes_optional {
0044     /*
0045      * These flags show which components are actually
0046      * present in the "optional fields" in the optional PES
0047      * header and which are not
0048      *
0049      * u16 two:2;  //0x2
0050      * u16 PES_scrambling_control:2;
0051      * u16 PES_priority:1;
0052      * u16 data_alignment_indicator:1; // unused
0053      * u16 copyright:1;
0054      * u16 original_or_copy:1;
0055      * u16 PTS_DTS:2;
0056      * u16 ESCR:1;
0057      * u16 ES_rate:1;
0058      * u16 DSM_trick_mode:1;
0059      * u16 additional_copy_info:1;
0060      * u16 PES_CRC:1;
0061      * u16 PES_extension:1;
0062      */
0063     __be16 bitfield;
0064     u8 length;
0065 } __packed;
0066 
0067 /* The PES header */
0068 struct vidtv_mpeg_pes {
0069     __be32 bitfield; /* packet_start_code_prefix:24, stream_id: 8 */
0070     /* after this field until the end of the PES data payload */
0071     __be16 length;
0072     struct vidtv_pes_optional optional[];
0073 } __packed;
0074 
0075 /**
0076  * struct pes_header_write_args - Arguments to write a PES header.
0077  * @dest_buf: The buffer to write into.
0078  * @dest_offset: where to start writing in the dest_buffer.
0079  * @dest_buf_sz: The size of the dest_buffer
0080  * @encoder_id: Encoder id (see vidtv_encoder.h)
0081  * @send_pts: Should we send PTS?
0082  * @pts: PTS value to send.
0083  * @send_dts: Should we send DTS?
0084  * @dts: DTS value to send.
0085  * @stream_id: The stream id to use. Ex: Audio streams (0xc0-0xdf), Video
0086  * streams (0xe0-0xef).
0087  * @n_pes_h_s_bytes: Padding bytes. Might be used by an encoder if needed, gets
0088  * discarded by the decoder.
0089  * @access_unit_len: The size of _one_ access unit (with any headers it might need)
0090  */
0091 struct pes_header_write_args {
0092     void *dest_buf;
0093     u32 dest_offset;
0094     u32 dest_buf_sz;
0095     u32 encoder_id;
0096 
0097     bool send_pts;
0098     u64 pts;
0099 
0100     bool send_dts;
0101     u64 dts;
0102 
0103     u16 stream_id;
0104     /* might be used by an encoder if needed, gets discarded by decoder */
0105     u32 n_pes_h_s_bytes;
0106     u32 access_unit_len;
0107 };
0108 
0109 /**
0110  * struct pes_ts_header_write_args - Arguments to write a TS header.
0111  * @dest_buf: The buffer to write into.
0112  * @dest_offset: where to start writing in the dest_buffer.
0113  * @dest_buf_sz: The size of the dest_buffer
0114  * @pid: The PID to use for the TS packets.
0115  * @continuity_counter: Incremented on every new TS packet.
0116  * @wrote_pes_header: Flag to indicate that the PES header was written
0117  * @n_stuffing_bytes: Padding bytes. Might be used by an encoder if needed, gets
0118  * discarded by the decoder.
0119  * @pcr: counter driven by a 27Mhz clock.
0120  */
0121 struct pes_ts_header_write_args {
0122     void *dest_buf;
0123     u32 dest_offset;
0124     u32 dest_buf_sz;
0125     u16 pid;
0126     u8 *continuity_counter;
0127     bool wrote_pes_header;
0128     u32 n_stuffing_bytes;
0129     u64 pcr;
0130 };
0131 
0132 /**
0133  * struct pes_write_args - Arguments for the packetizer.
0134  * @dest_buf: The buffer to write into.
0135  * @from: A pointer to the encoder buffer containing one access unit.
0136  * @access_unit_len: The size of _one_ access unit (with any headers it might need)
0137  * @dest_offset: where to start writing in the dest_buffer.
0138  * @dest_buf_sz: The size of the dest_buffer
0139  * @pid: The PID to use for the TS packets.
0140  * @encoder_id: Encoder id (see vidtv_encoder.h)
0141  * @continuity_counter: Incremented on every new TS packet.
0142  * @stream_id: The stream id to use. Ex: Audio streams (0xc0-0xdf), Video
0143  * streams (0xe0-0xef).
0144  * @send_pts: Should we send PTS?
0145  * @pts: PTS value to send.
0146  * @send_dts: Should we send DTS?
0147  * @dts: DTS value to send.
0148  * @n_pes_h_s_bytes: Padding bytes. Might be used by an encoder if needed, gets
0149  * discarded by the decoder.
0150  * @pcr: counter driven by a 27Mhz clock.
0151  */
0152 struct pes_write_args {
0153     void *dest_buf;
0154     void *from;
0155     u32 access_unit_len;
0156 
0157     u32 dest_offset;
0158     u32 dest_buf_sz;
0159     u16 pid;
0160 
0161     u32 encoder_id;
0162 
0163     u8 *continuity_counter;
0164 
0165     u16 stream_id;
0166 
0167     bool send_pts;
0168     u64 pts;
0169 
0170     bool send_dts;
0171     u64 dts;
0172 
0173     u32 n_pes_h_s_bytes;
0174     u64 pcr;
0175 };
0176 
0177 /**
0178  * vidtv_pes_write_into - Write a PES packet as MPEG-TS packets into a buffer.
0179  * @args: The args to use when writing
0180  *
0181  * This function translate the ES data for one access unit
0182  * from an encoder into MPEG TS packets. It does so by first encapsulating it
0183  * with a PES header and then splitting it into TS packets.
0184  *
0185  * The data is then written into the buffer pointed to by 'args.buf'
0186  *
0187  * Return: The number of bytes written into the buffer. This is usually NOT
0188  * equal to the size of the access unit, since we need space for PES headers, TS headers
0189  * and padding bytes, if any.
0190  */
0191 u32 vidtv_pes_write_into(struct pes_write_args *args);
0192 
0193 #endif // VIDTV_PES_H