| 1 | /* |
| 2 | * Copyright 2006 BBC and Fluendo S.A. |
| 3 | * |
| 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. |
| 7 | * |
| 8 | * MPL: |
| 9 | * |
| 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/. |
| 14 | * |
| 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 |
| 18 | * under the License. |
| 19 | * |
| 20 | * LGPL: |
| 21 | * |
| 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. |
| 26 | * |
| 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. |
| 31 | * |
| 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. |
| 36 | * |
| 37 | * MIT: |
| 38 | * |
| 39 | * Unless otherwise indicated, Source Code is licensed under MIT license. |
| 40 | * See further explanation attached in License Statement (distributed in the file |
| 41 | * LICENSE). |
| 42 | * |
| 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: |
| 49 | * |
| 50 | * The above copyright notice and this permission notice shall be included in all |
| 51 | * copies or substantial portions of the Software. |
| 52 | * |
| 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 |
| 59 | * SOFTWARE. |
| 60 | * |
| 61 | * SPDX-License-Identifier: MPL-1.1 OR MIT OR LGPL-2.0-or-later |
| 62 | */ |
| 63 | |
| 64 | #ifndef __TSMUXSTREAM_H__ |
| 65 | #define __TSMUXSTREAM_H__ |
| 66 | |
| 67 | #include <glib.h> |
| 68 | #include <gst/mpegts/mpegts.h> |
| 69 | |
| 70 | #include "tsmuxcommon.h" |
| 71 | |
| 72 | G_BEGIN_DECLS |
| 73 | |
| 74 | typedef enum TsMuxStreamType TsMuxStreamType; |
| 75 | typedef enum TsMuxStreamState TsMuxStreamState; |
| 76 | typedef struct TsMuxStreamBuffer TsMuxStreamBuffer; |
| 77 | |
| 78 | typedef void (*TsMuxStreamBufferReleaseFunc) (guint8 *data, void *user_data); |
| 79 | typedef void (*TsMuxStreamGetESDescriptorsFunc) (TsMuxStream *stream, GstMpegtsPMTStream *pmt_stream, gpointer user_data); |
| 80 | |
| 81 | /* Stream type assignments |
| 82 | * |
| 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 |
| 87 | * stream |
| 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 |
| 91 | * private_sections |
| 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 |
| 96 | * DSM CC |
| 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 |
| 105 | */ |
| 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, |
| 117 | |
| 118 | /* later extensions */ |
| 119 | TSMUX_ST_AUDIO_AAC = 0x0f, |
| 120 | TSMUX_ST_VIDEO_MPEG4 = 0x10, |
| 121 | TSMUX_ST_PES_METADATA = 0x15, |
| 122 | TSMUX_ST_VIDEO_H264 = 0x1b, |
| 123 | TSMUX_ST_VIDEO_HEVC = 0x24, |
| 124 | TSMUX_ST_VIDEO_JP2K = 0x21, |
| 125 | TSMUX_ST_VIDEO_JPEG_XS = 0x32, |
| 126 | TSMUX_ST_VIDEO_VVC = 0x33, |
| 127 | |
| 128 | /* private stream types */ |
| 129 | TSMUX_ST_PS_AUDIO_AC3 = 0x81, |
| 130 | TSMUX_ST_PS_AUDIO_DTS = 0x8a, |
| 131 | TSMUX_ST_PS_AUDIO_LPCM = 0x8b, |
| 132 | TSMUX_ST_PS_DVB_SUBPICTURE = 0x8c, |
| 133 | TSMUX_ST_PS_TELETEXT = 0x8d, |
| 134 | TSMUX_ST_PS_KLV = 0x8e, /* only used internally */ |
| 135 | TSMUX_ST_PS_OPUS = 0x8f, /* only used internally */ |
| 136 | TSMUX_ST_PS_ID3 = 0x90, /* only used internally */ |
| 137 | TSMUX_ST_PS_ST_2038 = 0x91, /* only used internally */ |
| 138 | TSMUX_ST_PS_S302M = 0x92, /* only used internally */ |
| 139 | TSMUX_ST_PS_VP9 = 0x93, /* only used internally */ |
| 140 | TSMUX_ST_PS_VIDEO_AV1 = 0x94, /* only used internally */ |
| 141 | TSMUX_ST_PS_DVD_SUBPICTURE = 0xff, |
| 142 | |
| 143 | /* Non-standard definitions */ |
| 144 | TSMUX_ST_VIDEO_DIRAC = 0xD1 |
| 145 | }; |
| 146 | |
| 147 | enum TsMuxStreamState { |
| 148 | TSMUX_STREAM_STATE_HEADER, |
| 149 | TSMUX_STREAM_STATE_PACKET |
| 150 | }; |
| 151 | |
| 152 | /* TsMuxStream receives elementary streams for parsing */ |
| 153 | struct TsMuxStream { |
| 154 | TsMuxStreamState state; |
| 155 | TsMuxPacketInfo pi; |
| 156 | /* Stream type to be used in PES/PSI */ |
| 157 | TsMuxStreamType stream_type; |
| 158 | /* internal stream identifier (used for private section/pes) */ |
| 159 | TsMuxStreamType internal_stream_type; |
| 160 | /* Nature of the stream (audio, video, ..) */ |
| 161 | GstStreamType gst_stream_type; |
| 162 | |
| 163 | /* stream_id (13818-1) */ |
| 164 | guint8 id; |
| 165 | /* extended stream id (13818-1 Amdt 2) */ |
| 166 | guint8 id_extended; |
| 167 | |
| 168 | struct TsMuxProgram *program; |
| 169 | |
| 170 | /* requested index in the PMT */ |
| 171 | gint pmt_index; |
| 172 | |
| 173 | /* data available for writing out |
| 174 | * and total sum of sizes */ |
| 175 | GList *buffers; |
| 176 | guint32 bytes_avail; |
| 177 | |
| 178 | /* current data buffer being consumed |
| 179 | * and amount already consumed */ |
| 180 | TsMuxStreamBuffer *cur_buffer; |
| 181 | guint32 cur_buffer_consumed; |
| 182 | |
| 183 | /* helper to release collected buffers */ |
| 184 | TsMuxStreamBufferReleaseFunc buffer_release; |
| 185 | |
| 186 | /* Override or extend the default Elementary Stream descriptors */ |
| 187 | TsMuxStreamGetESDescriptorsFunc get_es_descrs; |
| 188 | void *get_es_descrs_data; |
| 189 | |
| 190 | /* optional fixed PES size for stream type */ |
| 191 | guint16 pes_payload_size; |
| 192 | /* current PES payload size being written */ |
| 193 | guint32 cur_pes_payload_size; |
| 194 | /* ... of which already this much written */ |
| 195 | guint32 pes_bytes_written; |
| 196 | |
| 197 | /* PTS/DTS to write if the flags in the packet info are set */ |
| 198 | /* in MPEG PTS clock time */ |
| 199 | gint64 pts; |
| 200 | gint64 dts; |
| 201 | |
| 202 | /* last ts written, or maybe next one ... ?! */ |
| 203 | gint64 last_dts; |
| 204 | gint64 last_pts; |
| 205 | |
| 206 | gint64 first_ts; |
| 207 | |
| 208 | /* count of programs using this as PCR */ |
| 209 | gint pcr_ref; |
| 210 | /* Next time PCR should be written */ |
| 211 | gint64 next_pcr; |
| 212 | |
| 213 | /* audio parameters for stream |
| 214 | * (used in stream descriptor) */ |
| 215 | gint audio_sampling; |
| 216 | gint audio_channels; |
| 217 | gint audio_bitrate; |
| 218 | |
| 219 | gboolean is_dvb_sub; |
| 220 | gchar language[4]; |
| 221 | |
| 222 | /* Opus */ |
| 223 | guint8 opus_channel_config[1 + 2 + 1 + 1 + 255]; |
| 224 | gsize opus_channel_config_len; |
| 225 | |
| 226 | /* Jpeg2000 */ |
| 227 | gint32 horizontal_size; |
| 228 | gint32 vertical_size; |
| 229 | gint32 den; |
| 230 | gint32 num; |
| 231 | /* Maximum bitrate box */ |
| 232 | guint32 max_bitrate; |
| 233 | guint16 profile_and_level; |
| 234 | gboolean interlace_mode; |
| 235 | guint8 color_spec; |
| 236 | |
| 237 | /* PMT descriptor for the stream */ |
| 238 | GstMpegtsDescriptor *pmt_descriptor; |
| 239 | }; |
| 240 | |
| 241 | /* stream management */ |
| 242 | TsMuxStream * tsmux_stream_new (guint16 pid, guint stream_type, guint stream_number); |
| 243 | void tsmux_stream_free (TsMuxStream *stream); |
| 244 | |
| 245 | guint16 tsmux_stream_get_pid (TsMuxStream *stream); |
| 246 | |
| 247 | void tsmux_stream_set_buffer_release_func (TsMuxStream *stream, |
| 248 | TsMuxStreamBufferReleaseFunc func); |
| 249 | |
| 250 | void tsmux_stream_set_get_es_descriptors_func (TsMuxStream *stream, |
| 251 | TsMuxStreamGetESDescriptorsFunc func, |
| 252 | void *user_data); |
| 253 | |
| 254 | /* Add a new buffer to the pool of available bytes. If pts or dts are not -1, they |
| 255 | * indicate the PTS or DTS of the first access unit within this packet */ |
| 256 | void tsmux_stream_add_data (TsMuxStream *stream, guint8 *data, guint len, |
| 257 | void *user_data, gint64 pts, gint64 dts, |
| 258 | gboolean random_access); |
| 259 | |
| 260 | void tsmux_stream_pcr_ref (TsMuxStream *stream); |
| 261 | void tsmux_stream_pcr_unref (TsMuxStream *stream); |
| 262 | gboolean tsmux_stream_is_pcr (TsMuxStream *stream); |
| 263 | |
| 264 | gboolean tsmux_stream_at_pes_start (TsMuxStream *stream); |
| 265 | void tsmux_stream_get_es_descrs (TsMuxStream *stream, GstMpegtsPMTStream *pmt_stream); |
| 266 | void tsmux_stream_default_get_es_descrs (TsMuxStream * stream, GstMpegtsPMTStream * pmt_stream); |
| 267 | |
| 268 | gint tsmux_stream_bytes_in_buffer (TsMuxStream *stream); |
| 269 | gint tsmux_stream_bytes_avail (TsMuxStream *stream); |
| 270 | gboolean tsmux_stream_initialize_pes_packet (TsMuxStream *stream); |
| 271 | gboolean tsmux_stream_get_data (TsMuxStream *stream, guint8 *buf, guint len); |
| 272 | |
| 273 | gint64 tsmux_stream_get_pts (TsMuxStream *stream); |
| 274 | gint64 tsmux_stream_get_dts (TsMuxStream *stream); |
| 275 | |
| 276 | G_END_DECLS |
| 277 | |
| 278 | #endif |