1 /*
2  * Copyright 2006 BBC and Fluendo S.A.
3  *
4  * This library is licensed under 4 different licenses and you
5  * can choose to use it under the terms of any one of them. The
6  * four licenses are the MPL 1.1, the LGPL, the GPL and the MIT
7  * license.
8  *
9  * MPL:
10  *
11  * The contents of this file are subject to the Mozilla Public License
12  * Version 1.1 (the "License"); you may not use this file except in
13  * compliance with the License. You may obtain a copy of the License at
14  * http://www.mozilla.org/MPL/.
15  *
16  * Software distributed under the License is distributed on an "AS IS"
17  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
18  * License for the specific language governing rights and limitations
19  * under the License.
20  *
21  * LGPL:
22  *
23  * This library is free software; you can redistribute it and/or
24  * modify it under the terms of the GNU Library General Public
25  * License as published by the Free Software Foundation; either
26  * version 2 of the License, or (at your option) any later version.
27  *
28  * This library is distributed in the hope that it will be useful,
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
31  * Library General Public License for more details.
32  *
33  * You should have received a copy of the GNU Library General Public
34  * License along with this library; if not, write to the
35  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
36  * Boston, MA 02110-1301, USA.
37  *
38  * GPL:
39  *
40  * This program is free software; you can redistribute it and/or modify
41  * it under the terms of the GNU General Public License as published by
42  * the Free Software Foundation; either version 2 of the License, or
43  * (at your option) any later version.
44  *
45  * This program is distributed in the hope that it will be useful,
46  * but WITHOUT ANY WARRANTY; without even the implied warranty of
47  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
48  * GNU General Public License for more details.
49  *
50  * You should have received a copy of the GNU General Public License
51  * along with this program; if not, write to the Free Software
52  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
53  *
54  * MIT:
55  *
56  * Unless otherwise indicated, Source Code is licensed under MIT license.
57  * See further explanation attached in License Statement (distributed in the file
58  * LICENSE).
59  *
60  * Permission is hereby granted, free of charge, to any person obtaining a copy of
61  * this software and associated documentation files (the "Software"), to deal in
62  * the Software without restriction, including without limitation the rights to
63  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
64  * of the Software, and to permit persons to whom the Software is furnished to do
65  * so, subject to the following conditions:
66  *
67  * The above copyright notice and this permission notice shall be included in all
68  * copies or substantial portions of the Software.
69  *
70  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
71  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
72  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
73  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
74  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
75  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
76  * SOFTWARE.
77  *
78  */
79 
80 #ifndef __TSMUXSTREAM_H__
81 #define __TSMUXSTREAM_H__
82 
83 #include <glib.h>
84 
85 #include "tsmuxcommon.h"
86 
87 G_BEGIN_DECLS
88 
89 typedef enum TsMuxStreamType TsMuxStreamType;
90 typedef enum TsMuxStreamState TsMuxStreamState;
91 typedef struct TsMuxStreamBuffer TsMuxStreamBuffer;
92 
93 typedef void (*TsMuxStreamBufferReleaseFunc) (guint8 *data, void *user_data);
94 
95 /* Stream type assignments
96  *
97  *   0x00    ITU-T | ISO/IEC Reserved
98  *   0x01    ISO/IEC 11172 Video
99  *   0x02    ITU-T Rec. H.262 | ISO/IEC 13818-2 Video or
100  *           ISO/IEC 11172-2 constrained parameter video
101  *           stream
102  *   0x03    ISO/IEC 11172 Audio
103  *   0x04    ISO/IEC 13818-3 Audio
104  *   0x05    ITU-T Rec. H.222.0 | ISO/IEC 13818-1
105  *           private_sections
106  *   0x06    ITU-T Rec. H.222.0 | ISO/IEC 13818-1 PES
107  *           packets containing private data
108  *   0x07    ISO/IEC 13522 MHEG
109  *   0x08    ITU-T Rec. H.222.0 | ISO/IEC 13818-1 Annex A
110  *           DSM CC
111  *   0x09    ITU-T Rec. H.222.1
112  *   0x0A    ISO/IEC 13818-6 type A
113  *   0x0B    ISO/IEC 13818-6 type B
114  *   0x0C    ISO/IEC 13818-6 type C
115  *   0x0D    ISO/IEC 13818-6 type D
116  *   0x0E    ISO/IEC 13818-1 auxiliary
117  * 0x0F-0x7F ITU-T Rec. H.222.0 | ISO/IEC 13818-1 Reserved
118  * 0x80-0xFF User Private
119  */
120 enum TsMuxStreamType {
121   TSMUX_ST_RESERVED                   = 0x00,
122   TSMUX_ST_VIDEO_MPEG1                = 0x01,
123   TSMUX_ST_VIDEO_MPEG2                = 0x02,
124   TSMUX_ST_AUDIO_MPEG1                = 0x03,
125   TSMUX_ST_AUDIO_MPEG2                = 0x04,
126   TSMUX_ST_PRIVATE_SECTIONS           = 0x05,
127   TSMUX_ST_PRIVATE_DATA               = 0x06,
128   TSMUX_ST_MHEG                       = 0x07,
129   TSMUX_ST_DSMCC                      = 0x08,
130   TSMUX_ST_H222_1                     = 0x09,
131 
132   /* later extensions */
133   TSMUX_ST_AUDIO_AAC                  = 0x0f,
134   TSMUX_ST_VIDEO_MPEG4                = 0x10,
135   TSMUX_ST_VIDEO_H264                 = 0x1b,
136   TSMUX_ST_VIDEO_HEVC                 = 0x24,
137   TSMUX_ST_VIDEO_JP2K = 0x21,
138 
139   /* private stream types */
140   TSMUX_ST_PS_AUDIO_AC3               = 0x81,
141   TSMUX_ST_PS_AUDIO_DTS               = 0x8a,
142   TSMUX_ST_PS_AUDIO_LPCM              = 0x8b,
143   TSMUX_ST_PS_DVB_SUBPICTURE          = 0x8c,
144   TSMUX_ST_PS_TELETEXT                = 0x8d,
145   TSMUX_ST_PS_KLV                     = 0x8e,    /* only used internally */
146   TSMUX_ST_PS_OPUS                    = 0x8f,    /* only used internally */
147   TSMUX_ST_PS_DVD_SUBPICTURE          = 0xff,
148 
149   /* Non-standard definitions */
150   TSMUX_ST_VIDEO_DIRAC                = 0xD1
151 };
152 
153 enum TsMuxStreamState {
154     TSMUX_STREAM_STATE_HEADER,
155     TSMUX_STREAM_STATE_PACKET
156 };
157 
158 /* TsMuxStream receives elementary streams for parsing */
159 struct TsMuxStream {
160   TsMuxStreamState state;
161   TsMuxPacketInfo pi;
162   TsMuxStreamType stream_type;
163 
164   /* stream_id (13818-1) */
165   guint8 id;
166   /* extended stream id (13818-1 Amdt 2) */
167   guint8 id_extended;
168 
169   gboolean is_video_stream;
170 
171   /* data available for writing out
172    * and total sum of sizes */
173   GList *buffers;
174   guint32 bytes_avail;
175 
176   /* current data buffer being consumed
177    * and amount already consumed */
178   TsMuxStreamBuffer *cur_buffer;
179   guint32 cur_buffer_consumed;
180 
181   /* helper to release collected buffers */
182   TsMuxStreamBufferReleaseFunc buffer_release;
183 
184   /* optional fixed PES size for stream type */
185   guint16 pes_payload_size;
186   /* current PES payload size being written */
187   guint32 cur_pes_payload_size;
188   /* ... of which already this much written */
189   guint32 pes_bytes_written;
190 
191   /* PTS/DTS to write if the flags in the packet info are set */
192   /* in MPEG PTS clock time */
193   gint64 pts;
194   gint64 dts;
195 
196   /* last ts written, or maybe next one ... ?! */
197   gint64 last_dts;
198   gint64 last_pts;
199 
200   /* count of programs using this as PCR */
201   gint   pcr_ref;
202   /* last time PCR written */
203   gint64 last_pcr;
204 
205   /* audio parameters for stream
206    * (used in stream descriptor) */
207   gint audio_sampling;
208   gint audio_channels;
209   gint audio_bitrate;
210 
211   gboolean is_dvb_sub;
212   gchar language[4];
213 
214   gboolean is_meta;
215   gboolean is_audio;
216 
217   /* Opus */
218   gboolean is_opus;
219   guint8 opus_channel_config_code;
220   /* Jpeg2000 */
221   gint32 horizontal_size;
222   gint32 vertical_size;
223   gint32 den;
224   gint32 num;
225   /* Maximum bitrate box */
226   guint32 max_bitrate;
227   guint16 profile_and_level;
228   gboolean interlace_mode;
229   guint8 color_spec;
230 };
231 
232 /* stream management */
233 TsMuxStream *	tsmux_stream_new 		(guint16 pid, TsMuxStreamType stream_type);
234 void 		tsmux_stream_free 		(TsMuxStream *stream);
235 
236 guint16         tsmux_stream_get_pid            (TsMuxStream *stream);
237 
238 void 		tsmux_stream_set_buffer_release_func 	(TsMuxStream *stream,
239        							 TsMuxStreamBufferReleaseFunc func);
240 
241 /* Add a new buffer to the pool of available bytes. If pts or dts are not -1, they
242  * indicate the PTS or DTS of the first access unit within this packet */
243 void 		tsmux_stream_add_data 		(TsMuxStream *stream, guint8 *data, guint len,
244        						 void *user_data, gint64 pts, gint64 dts,
245                                                  gboolean random_access);
246 
247 void 		tsmux_stream_pcr_ref 		(TsMuxStream *stream);
248 void 		tsmux_stream_pcr_unref  	(TsMuxStream *stream);
249 gboolean	tsmux_stream_is_pcr 		(TsMuxStream *stream);
250 
251 gboolean 	tsmux_stream_at_pes_start 	(TsMuxStream *stream);
252 void 		tsmux_stream_get_es_descrs 	(TsMuxStream *stream, GstMpegtsPMTStream *pmt_stream);
253 
254 gint 		tsmux_stream_bytes_in_buffer 	(TsMuxStream *stream);
255 gint 		tsmux_stream_bytes_avail 	(TsMuxStream *stream);
256 gboolean 	tsmux_stream_initialize_pes_packet (TsMuxStream *stream);
257 gboolean 	tsmux_stream_get_data 		(TsMuxStream *stream, guint8 *buf, guint len);
258 
259 guint64 	tsmux_stream_get_pts 		(TsMuxStream *stream);
260 
261 G_END_DECLS
262 
263 #endif
264