tsmux: Fix default get_es_descrs_func
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / gst / mpegtsmux / tsmux / tsmuxstream.h
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_VIDEO_H264                 = 0x1b,
122   TSMUX_ST_VIDEO_HEVC                 = 0x24,
123   TSMUX_ST_VIDEO_JP2K = 0x21,
124
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,
134
135   /* Non-standard definitions */
136   TSMUX_ST_VIDEO_DIRAC                = 0xD1
137 };
138
139 enum TsMuxStreamState {
140     TSMUX_STREAM_STATE_HEADER,
141     TSMUX_STREAM_STATE_PACKET
142 };
143
144 /* TsMuxStream receives elementary streams for parsing */
145 struct TsMuxStream {
146   TsMuxStreamState state;
147   TsMuxPacketInfo pi;
148   guint stream_type;
149
150   /* stream_id (13818-1) */
151   guint8 id;
152   /* extended stream id (13818-1 Amdt 2) */
153   guint8 id_extended;
154   /* requested index in the PMT */
155   gint pmt_index;
156
157   gboolean is_video_stream;
158
159   /* data available for writing out
160    * and total sum of sizes */
161   GList *buffers;
162   guint32 bytes_avail;
163
164   /* current data buffer being consumed
165    * and amount already consumed */
166   TsMuxStreamBuffer *cur_buffer;
167   guint32 cur_buffer_consumed;
168
169   /* helper to release collected buffers */
170   TsMuxStreamBufferReleaseFunc buffer_release;
171
172   /* Override or extend the default Elementary Stream descriptors */
173   TsMuxStreamGetESDescriptorsFunc get_es_descrs;
174   void *get_es_descrs_data;
175
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;
182
183   /* PTS/DTS to write if the flags in the packet info are set */
184   /* in MPEG PTS clock time */
185   gint64 pts;
186   gint64 dts;
187
188   /* last ts written, or maybe next one ... ?! */
189   gint64 last_dts;
190   gint64 last_pts;
191
192   gint64 first_ts;
193
194   /* count of programs using this as PCR */
195   gint   pcr_ref;
196   /* Next time PCR should be written */
197   gint64 next_pcr;
198
199   /* audio parameters for stream
200    * (used in stream descriptor) */
201   gint audio_sampling;
202   gint audio_channels;
203   gint audio_bitrate;
204
205   gboolean is_dvb_sub;
206   gchar language[4];
207
208   gboolean is_meta;
209   gboolean is_audio;
210
211   /* Opus */
212   gboolean is_opus;
213   guint8 opus_channel_config_code;
214   /* Jpeg2000 */
215   gint32 horizontal_size;
216   gint32 vertical_size;
217   gint32 den;
218   gint32 num;
219   /* Maximum bitrate box */
220   guint32 max_bitrate;
221   guint16 profile_and_level;
222   gboolean interlace_mode;
223   guint8 color_spec;
224 };
225
226 /* stream management */
227 TsMuxStream *   tsmux_stream_new                (guint16 pid, guint stream_type);
228 void            tsmux_stream_free               (TsMuxStream *stream);
229
230 guint16         tsmux_stream_get_pid            (TsMuxStream *stream);
231
232 void            tsmux_stream_set_buffer_release_func    (TsMuxStream *stream,
233                                                          TsMuxStreamBufferReleaseFunc func);
234
235 void            tsmux_stream_set_get_es_descriptors_func        (TsMuxStream *stream,
236                                                    TsMuxStreamGetESDescriptorsFunc func,
237                                                    void *user_data);
238
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);
244
245 void            tsmux_stream_pcr_ref            (TsMuxStream *stream);
246 void            tsmux_stream_pcr_unref          (TsMuxStream *stream);
247 gboolean        tsmux_stream_is_pcr             (TsMuxStream *stream);
248
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);
252
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);
257
258 gint64  tsmux_stream_get_pts            (TsMuxStream *stream);
259 gint64  tsmux_stream_get_dts            (TsMuxStream *stream);
260
261 G_END_DECLS
262
263 #endif