• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
69 #include "tsmuxcommon.h"
70 
71 G_BEGIN_DECLS
72 
73 typedef enum TsMuxStreamType TsMuxStreamType;
74 typedef enum TsMuxStreamState TsMuxStreamState;
75 typedef struct TsMuxStreamBuffer TsMuxStreamBuffer;
76 
77 typedef void (*TsMuxStreamBufferReleaseFunc) (guint8 *data, void *user_data);
78 typedef void (*TsMuxStreamGetESDescriptorsFunc) (TsMuxStream *stream, GstMpegtsPMTStream *pmt_stream, void *user_data);
79 
80 /* Stream type assignments
81  *
82  *   0x00    ITU-T | ISO/IEC Reserved
83  *   0x01    ISO/IEC 11172 Video
84  *   0x02    ITU-T Rec. H.262 | ISO/IEC 13818-2 Video or
85  *           ISO/IEC 11172-2 constrained parameter video
86  *           stream
87  *   0x03    ISO/IEC 11172 Audio
88  *   0x04    ISO/IEC 13818-3 Audio
89  *   0x05    ITU-T Rec. H.222.0 | ISO/IEC 13818-1
90  *           private_sections
91  *   0x06    ITU-T Rec. H.222.0 | ISO/IEC 13818-1 PES
92  *           packets containing private data
93  *   0x07    ISO/IEC 13522 MHEG
94  *   0x08    ITU-T Rec. H.222.0 | ISO/IEC 13818-1 Annex A
95  *           DSM CC
96  *   0x09    ITU-T Rec. H.222.1
97  *   0x0A    ISO/IEC 13818-6 type A
98  *   0x0B    ISO/IEC 13818-6 type B
99  *   0x0C    ISO/IEC 13818-6 type C
100  *   0x0D    ISO/IEC 13818-6 type D
101  *   0x0E    ISO/IEC 13818-1 auxiliary
102  * 0x0F-0x7F ITU-T Rec. H.222.0 | ISO/IEC 13818-1 Reserved
103  * 0x80-0xFF User Private
104  */
105 enum TsMuxStreamType {
106   TSMUX_ST_RESERVED                   = 0x00,
107   TSMUX_ST_VIDEO_MPEG1                = 0x01,
108   TSMUX_ST_VIDEO_MPEG2                = 0x02,
109   TSMUX_ST_AUDIO_MPEG1                = 0x03,
110   TSMUX_ST_AUDIO_MPEG2                = 0x04,
111   TSMUX_ST_PRIVATE_SECTIONS           = 0x05,
112   TSMUX_ST_PRIVATE_DATA               = 0x06,
113   TSMUX_ST_MHEG                       = 0x07,
114   TSMUX_ST_DSMCC                      = 0x08,
115   TSMUX_ST_H222_1                     = 0x09,
116 
117   /* later extensions */
118   TSMUX_ST_AUDIO_AAC                  = 0x0f,
119   TSMUX_ST_VIDEO_MPEG4                = 0x10,
120   TSMUX_ST_VIDEO_H264                 = 0x1b,
121   TSMUX_ST_VIDEO_HEVC                 = 0x24,
122   TSMUX_ST_VIDEO_JP2K = 0x21,
123 
124   /* private stream types */
125   TSMUX_ST_PS_AUDIO_AC3               = 0x81,
126   TSMUX_ST_PS_AUDIO_DTS               = 0x8a,
127   TSMUX_ST_PS_AUDIO_LPCM              = 0x8b,
128   TSMUX_ST_PS_DVB_SUBPICTURE          = 0x8c,
129   TSMUX_ST_PS_TELETEXT                = 0x8d,
130   TSMUX_ST_PS_KLV                     = 0x8e,    /* only used internally */
131   TSMUX_ST_PS_OPUS                    = 0x8f,    /* only used internally */
132   TSMUX_ST_PS_DVD_SUBPICTURE          = 0xff,
133 
134   /* Non-standard definitions */
135   TSMUX_ST_VIDEO_DIRAC                = 0xD1
136 };
137 
138 enum TsMuxStreamState {
139     TSMUX_STREAM_STATE_HEADER,
140     TSMUX_STREAM_STATE_PACKET
141 };
142 
143 /* TsMuxStream receives elementary streams for parsing */
144 struct TsMuxStream {
145   TsMuxStreamState state;
146   TsMuxPacketInfo pi;
147   guint stream_type;
148 
149   /* stream_id (13818-1) */
150   guint8 id;
151   /* extended stream id (13818-1 Amdt 2) */
152   guint8 id_extended;
153   /* requested index in the PMT */
154   gint pmt_index;
155 
156   gboolean is_video_stream;
157 
158   /* data available for writing out
159    * and total sum of sizes */
160   GList *buffers;
161   guint32 bytes_avail;
162 
163   /* current data buffer being consumed
164    * and amount already consumed */
165   TsMuxStreamBuffer *cur_buffer;
166   guint32 cur_buffer_consumed;
167 
168   /* helper to release collected buffers */
169   TsMuxStreamBufferReleaseFunc buffer_release;
170 
171   /* Override or extend the default Elementary Stream descriptors */
172   TsMuxStreamGetESDescriptorsFunc get_es_descrs;
173   void *get_es_descrs_data;
174 
175   /* optional fixed PES size for stream type */
176   guint16 pes_payload_size;
177   /* current PES payload size being written */
178   guint32 cur_pes_payload_size;
179   /* ... of which already this much written */
180   guint32 pes_bytes_written;
181 
182   /* PTS/DTS to write if the flags in the packet info are set */
183   /* in MPEG PTS clock time */
184   gint64 pts;
185   gint64 dts;
186 
187   /* last ts written, or maybe next one ... ?! */
188   gint64 last_dts;
189   gint64 last_pts;
190 
191   gint64 first_ts;
192 
193   /* count of programs using this as PCR */
194   gint   pcr_ref;
195   /* Next time PCR should be written */
196   gint64 next_pcr;
197 
198   /* audio parameters for stream
199    * (used in stream descriptor) */
200   gint audio_sampling;
201   gint audio_channels;
202   gint audio_bitrate;
203 
204   gboolean is_dvb_sub;
205   gchar language[4];
206 
207   gboolean is_meta;
208   gboolean is_audio;
209 
210   /* Opus */
211   gboolean is_opus;
212   guint8 opus_channel_config_code;
213   /* Jpeg2000 */
214   gint32 horizontal_size;
215   gint32 vertical_size;
216   gint32 den;
217   gint32 num;
218   /* Maximum bitrate box */
219   guint32 max_bitrate;
220   guint16 profile_and_level;
221   gboolean interlace_mode;
222   guint8 color_spec;
223 };
224 
225 /* stream management */
226 TsMuxStream *	tsmux_stream_new 		(guint16 pid, guint stream_type);
227 void 		tsmux_stream_free 		(TsMuxStream *stream);
228 
229 guint16         tsmux_stream_get_pid            (TsMuxStream *stream);
230 
231 void 		tsmux_stream_set_buffer_release_func 	(TsMuxStream *stream,
232        							 TsMuxStreamBufferReleaseFunc func);
233 
234 void 		tsmux_stream_set_get_es_descriptors_func 	(TsMuxStream *stream,
235                                                    TsMuxStreamGetESDescriptorsFunc func,
236                                                    void *user_data);
237 
238 /* Add a new buffer to the pool of available bytes. If pts or dts are not -1, they
239  * indicate the PTS or DTS of the first access unit within this packet */
240 void 		tsmux_stream_add_data 		(TsMuxStream *stream, guint8 *data, guint len,
241        						 void *user_data, gint64 pts, gint64 dts,
242                                                  gboolean random_access);
243 
244 void 		tsmux_stream_pcr_ref 		(TsMuxStream *stream);
245 void 		tsmux_stream_pcr_unref  	(TsMuxStream *stream);
246 gboolean	tsmux_stream_is_pcr 		(TsMuxStream *stream);
247 
248 gboolean 	tsmux_stream_at_pes_start 	(TsMuxStream *stream);
249 void 		tsmux_stream_get_es_descrs 	(TsMuxStream *stream, GstMpegtsPMTStream *pmt_stream);
250 void    tsmux_stream_default_get_es_descrs (TsMuxStream * stream, GstMpegtsPMTStream * pmt_stream);
251 
252 gint 		tsmux_stream_bytes_in_buffer 	(TsMuxStream *stream);
253 gint 		tsmux_stream_bytes_avail 	(TsMuxStream *stream);
254 gboolean 	tsmux_stream_initialize_pes_packet (TsMuxStream *stream);
255 gboolean 	tsmux_stream_get_data 		(TsMuxStream *stream, guint8 *buf, guint len);
256 
257 gint64 	tsmux_stream_get_pts 		(TsMuxStream *stream);
258 gint64 	tsmux_stream_get_dts 		(TsMuxStream *stream);
259 
260 G_END_DECLS
261 
262 #endif
263