2 * Copyright 2006 BBC and Fluendo S.A.
4 * This library is licensed under 3 different licenses and you
5 * can choose to use it under the terms of any one of them. The
6 * three licenses are the MPL 1.1, the LGPL, and the MIT license.
10 * The contents of this file are subject to the Mozilla Public License
11 * Version 1.1 (the "License"); you may not use this file except in
12 * compliance with the License. You may obtain a copy of the License at
13 * http://www.mozilla.org/MPL/.
15 * Software distributed under the License is distributed on an "AS IS"
16 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
17 * License for the specific language governing rights and limitations
22 * This library is free software; you can redistribute it and/or
23 * modify it under the terms of the GNU Library General Public
24 * License as published by the Free Software Foundation; either
25 * version 2 of the License, or (at your option) any later version.
27 * This library is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30 * Library General Public License for more details.
32 * You should have received a copy of the GNU Library General Public
33 * License along with this library; if not, write to the
34 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
35 * Boston, MA 02110-1301, USA.
39 * Unless otherwise indicated, Source Code is licensed under MIT license.
40 * See further explanation attached in License Statement (distributed in the file
43 * Permission is hereby granted, free of charge, to any person obtaining a copy of
44 * this software and associated documentation files (the "Software"), to deal in
45 * the Software without restriction, including without limitation the rights to
46 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
47 * of the Software, and to permit persons to whom the Software is furnished to do
48 * so, subject to the following conditions:
50 * The above copyright notice and this permission notice shall be included in all
51 * copies or substantial portions of the Software.
53 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
54 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
55 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
56 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
57 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
58 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
61 * SPDX-License-Identifier: MPL-1.1 OR MIT OR LGPL-2.0-or-later
64 #ifndef __TSMUXSTREAM_H__
65 #define __TSMUXSTREAM_H__
68 #include <gst/mpegts/mpegts.h>
70 #include "tsmuxcommon.h"
74 typedef enum TsMuxStreamType TsMuxStreamType;
75 typedef enum TsMuxStreamState TsMuxStreamState;
76 typedef struct TsMuxStreamBuffer TsMuxStreamBuffer;
78 typedef void (*TsMuxStreamBufferReleaseFunc) (guint8 *data, void *user_data);
79 typedef void (*TsMuxStreamGetESDescriptorsFunc) (TsMuxStream *stream, GstMpegtsPMTStream *pmt_stream, gpointer user_data);
81 /* Stream type assignments
83 * 0x00 ITU-T | ISO/IEC Reserved
84 * 0x01 ISO/IEC 11172 Video
85 * 0x02 ITU-T Rec. H.262 | ISO/IEC 13818-2 Video or
86 * ISO/IEC 11172-2 constrained parameter video
88 * 0x03 ISO/IEC 11172 Audio
89 * 0x04 ISO/IEC 13818-3 Audio
90 * 0x05 ITU-T Rec. H.222.0 | ISO/IEC 13818-1
92 * 0x06 ITU-T Rec. H.222.0 | ISO/IEC 13818-1 PES
93 * packets containing private data
94 * 0x07 ISO/IEC 13522 MHEG
95 * 0x08 ITU-T Rec. H.222.0 | ISO/IEC 13818-1 Annex A
97 * 0x09 ITU-T Rec. H.222.1
98 * 0x0A ISO/IEC 13818-6 type A
99 * 0x0B ISO/IEC 13818-6 type B
100 * 0x0C ISO/IEC 13818-6 type C
101 * 0x0D ISO/IEC 13818-6 type D
102 * 0x0E ISO/IEC 13818-1 auxiliary
103 * 0x0F-0x7F ITU-T Rec. H.222.0 | ISO/IEC 13818-1 Reserved
104 * 0x80-0xFF User Private
106 enum TsMuxStreamType {
107 TSMUX_ST_RESERVED = 0x00,
108 TSMUX_ST_VIDEO_MPEG1 = 0x01,
109 TSMUX_ST_VIDEO_MPEG2 = 0x02,
110 TSMUX_ST_AUDIO_MPEG1 = 0x03,
111 TSMUX_ST_AUDIO_MPEG2 = 0x04,
112 TSMUX_ST_PRIVATE_SECTIONS = 0x05,
113 TSMUX_ST_PRIVATE_DATA = 0x06,
114 TSMUX_ST_MHEG = 0x07,
115 TSMUX_ST_DSMCC = 0x08,
116 TSMUX_ST_H222_1 = 0x09,
118 /* later extensions */
119 TSMUX_ST_AUDIO_AAC = 0x0f,
120 TSMUX_ST_VIDEO_MPEG4 = 0x10,
121 TSMUX_ST_VIDEO_H264 = 0x1b,
122 TSMUX_ST_VIDEO_HEVC = 0x24,
123 TSMUX_ST_VIDEO_JP2K = 0x21,
125 /* private stream types */
126 TSMUX_ST_PS_AUDIO_AC3 = 0x81,
127 TSMUX_ST_PS_AUDIO_DTS = 0x8a,
128 TSMUX_ST_PS_AUDIO_LPCM = 0x8b,
129 TSMUX_ST_PS_DVB_SUBPICTURE = 0x8c,
130 TSMUX_ST_PS_TELETEXT = 0x8d,
131 TSMUX_ST_PS_KLV = 0x8e, /* only used internally */
132 TSMUX_ST_PS_OPUS = 0x8f, /* only used internally */
133 TSMUX_ST_PS_DVD_SUBPICTURE = 0xff,
135 /* Non-standard definitions */
136 TSMUX_ST_VIDEO_DIRAC = 0xD1
139 enum TsMuxStreamState {
140 TSMUX_STREAM_STATE_HEADER,
141 TSMUX_STREAM_STATE_PACKET
144 /* TsMuxStream receives elementary streams for parsing */
146 TsMuxStreamState state;
150 /* stream_id (13818-1) */
152 /* extended stream id (13818-1 Amdt 2) */
154 /* requested index in the PMT */
157 gboolean is_video_stream;
159 /* data available for writing out
160 * and total sum of sizes */
164 /* current data buffer being consumed
165 * and amount already consumed */
166 TsMuxStreamBuffer *cur_buffer;
167 guint32 cur_buffer_consumed;
169 /* helper to release collected buffers */
170 TsMuxStreamBufferReleaseFunc buffer_release;
172 /* Override or extend the default Elementary Stream descriptors */
173 TsMuxStreamGetESDescriptorsFunc get_es_descrs;
174 void *get_es_descrs_data;
176 /* optional fixed PES size for stream type */
177 guint16 pes_payload_size;
178 /* current PES payload size being written */
179 guint32 cur_pes_payload_size;
180 /* ... of which already this much written */
181 guint32 pes_bytes_written;
183 /* PTS/DTS to write if the flags in the packet info are set */
184 /* in MPEG PTS clock time */
188 /* last ts written, or maybe next one ... ?! */
194 /* count of programs using this as PCR */
196 /* Next time PCR should be written */
199 /* audio parameters for stream
200 * (used in stream descriptor) */
213 guint8 opus_channel_config_code;
215 gint32 horizontal_size;
216 gint32 vertical_size;
219 /* Maximum bitrate box */
221 guint16 profile_and_level;
222 gboolean interlace_mode;
226 /* stream management */
227 TsMuxStream * tsmux_stream_new (guint16 pid, guint stream_type);
228 void tsmux_stream_free (TsMuxStream *stream);
230 guint16 tsmux_stream_get_pid (TsMuxStream *stream);
232 void tsmux_stream_set_buffer_release_func (TsMuxStream *stream,
233 TsMuxStreamBufferReleaseFunc func);
235 void tsmux_stream_set_get_es_descriptors_func (TsMuxStream *stream,
236 TsMuxStreamGetESDescriptorsFunc func,
239 /* Add a new buffer to the pool of available bytes. If pts or dts are not -1, they
240 * indicate the PTS or DTS of the first access unit within this packet */
241 void tsmux_stream_add_data (TsMuxStream *stream, guint8 *data, guint len,
242 void *user_data, gint64 pts, gint64 dts,
243 gboolean random_access);
245 void tsmux_stream_pcr_ref (TsMuxStream *stream);
246 void tsmux_stream_pcr_unref (TsMuxStream *stream);
247 gboolean tsmux_stream_is_pcr (TsMuxStream *stream);
249 gboolean tsmux_stream_at_pes_start (TsMuxStream *stream);
250 void tsmux_stream_get_es_descrs (TsMuxStream *stream, GstMpegtsPMTStream *pmt_stream);
251 void tsmux_stream_default_get_es_descrs (TsMuxStream * stream, GstMpegtsPMTStream * pmt_stream);
253 gint tsmux_stream_bytes_in_buffer (TsMuxStream *stream);
254 gint tsmux_stream_bytes_avail (TsMuxStream *stream);
255 gboolean tsmux_stream_initialize_pes_packet (TsMuxStream *stream);
256 gboolean tsmux_stream_get_data (TsMuxStream *stream, guint8 *buf, guint len);
258 gint64 tsmux_stream_get_pts (TsMuxStream *stream);
259 gint64 tsmux_stream_get_dts (TsMuxStream *stream);