1 /* MPEG-PS muxer plugin for GStreamer
2  * Copyright 2008 Lin YANG <oxcsnicho@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 /*
20  * Unless otherwise indicated, Source Code is licensed under MIT license.
21  * See further explanation attached in License Statement (distributed in the file
22  * LICENSE).
23  *
24  * Permission is hereby granted, free of charge, to any person obtaining a copy of
25  * this software and associated documentation files (the "Software"), to deal in
26  * the Software without restriction, including without limitation the rights to
27  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
28  * of the Software, and to permit persons to whom the Software is furnished to do
29  * so, subject to the following conditions:
30  *
31  * The above copyright notice and this permission notice shall be included in all
32  * copies or substantial portions of the Software.
33  *
34  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
35  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
36  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
37  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
38  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
39  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
40  * SOFTWARE.
41  */
42 
43 
44 
45 #ifndef __PSMUXSTREAM_H__
46 #define __PSMUXSTREAM_H__
47 
48 #include <gst/gst.h>
49 
50 #include "psmuxcommon.h"
51 
52 G_BEGIN_DECLS
53 
54 
55 typedef void (*PsMuxStreamBufferReleaseFunc) (guint8 *data, void *user_data);
56 
57 enum PsMuxStreamType { /* Table 2-29 in spec */
58   PSMUX_ST_RESERVED                   = 0x00,
59   PSMUX_ST_VIDEO_MPEG1                = 0x01,
60   PSMUX_ST_VIDEO_MPEG2                = 0x02,
61   PSMUX_ST_AUDIO_MPEG1                = 0x03,
62   PSMUX_ST_AUDIO_MPEG2                = 0x04,
63   PSMUX_ST_PRIVATE_SECTIONS           = 0x05,
64   PSMUX_ST_PRIVATE_DATA               = 0x06,
65   PSMUX_ST_MHEG                       = 0x07,
66   PSMUX_ST_DSMCC                      = 0x08,
67   PSMUX_ST_H222_1                     = 0x09,
68 
69   /* later extensions */
70   PSMUX_ST_AUDIO_AAC                  = 0x0f,
71   PSMUX_ST_VIDEO_MPEG4                = 0x10,
72   PSMUX_ST_VIDEO_H264                 = 0x1b,
73 
74   /* private stream types */
75   PSMUX_ST_PS_AUDIO_AC3               = 0x81,
76   PSMUX_ST_PS_AUDIO_DTS               = 0x8a,
77   PSMUX_ST_PS_AUDIO_LPCM              = 0x8b,
78   PSMUX_ST_PS_DVD_SUBPICTURE          = 0xff,
79 
80   /* Non-standard definitions */
81   PSMUX_ST_VIDEO_DIRAC                = 0xD1
82 };
83 
84 struct PsMuxStreamBuffer
85 {
86   gboolean keyunit;
87 
88   /* PTS & DTS associated with the contents of this buffer */
89   GstClockTime pts;
90   GstClockTime dts;
91 
92   GstBuffer *buf;
93   GstMapInfo map;
94 };
95 
96 /* PsMuxStream receives elementary streams for parsing.
97  * Via the write_bytes() method, it can output a PES stream piecemeal */
98 struct PsMuxStream{
99   PsMuxPacketInfo pi;
100 
101   PsMuxStreamType stream_type;
102   guint8 stream_id;
103   guint8 stream_id_ext; /* extended stream id (13818-1 Amdt 2) */
104 
105   /* List of data buffers available for writing out */
106   GList *buffers;
107   guint32 bytes_avail;
108 
109   /* Current data buffer being consumed */
110   PsMuxStreamBuffer *cur_buffer;
111   guint32 cur_buffer_consumed;
112 
113   /* PES payload */
114   guint16 cur_pes_payload_size;
115   guint16 pes_bytes_written; /* delete*/
116 
117   /* PTS/DTS to write if the flags in the packet info are set */
118   gint64 pts; /* TODO: cur_buffer->pts?*/
119   gint64 dts; /* TODO: cur_buffer->dts?*/
120   gint64 last_pts;
121 
122   /* stream type */
123   gboolean is_video_stream;
124   gboolean is_audio_stream;
125 
126   /* for writing descriptors */
127   gint audio_sampling;
128   gint audio_channels;
129   gint audio_bitrate;
130 
131   /* for writing buffer size in system header */
132   guint max_buffer_size;
133 };
134 
135 /* stream management */
136 PsMuxStream*    psmux_stream_new                (PsMux * mux, PsMuxStreamType stream_type);
137 void 		psmux_stream_free 		(PsMuxStream *stream);
138 
139 /* Add a new buffer to the pool of available bytes. If pts or dts are not -1, they
140  * indicate the PTS or DTS of the first access unit within this packet */
141 void 		psmux_stream_add_data 		(PsMuxStream *stream,
142 						 GstBuffer * buffer,
143 						 gint64 pts, gint64 dts,
144 						 gboolean keyunit);
145 
146 /* total bytes in buffer */
147 gint 		psmux_stream_bytes_in_buffer 	(PsMuxStream *stream);
148 /* number of bytes of raw data available for writing */
149 gint 		psmux_stream_bytes_avail 	(PsMuxStream *stream);
150 
151 /* write PES data */
152 guint	 	psmux_stream_get_data 		(PsMuxStream *stream, guint8 *buf, guint len);
153 
154 /* write corresponding descriptors of the stream */
155 void 		psmux_stream_get_es_descrs 	(PsMuxStream *stream, guint8 *buf, guint16 *len);
156 
157 /* get the pts of stream */
158 guint64 	psmux_stream_get_pts 		(PsMuxStream *stream);
159 
160 /* stream_id assignemnt */
161 #define PSMUX_STREAM_ID_MPGA_INIT       0xc0
162 #define PSMUX_STREAM_ID_MPGA_MAX        0xcf
163 
164 #define PSMUX_STREAM_ID_MPGV_INIT       0xe0
165 #define PSMUX_STREAM_ID_MPGV_MAX        0xef
166 
167 #define PSMUX_STREAM_ID_AC3_INIT        0x80
168 #define PSMUX_STREAM_ID_AC3_MAX         0x87
169 
170 #define PSMUX_STREAM_ID_SPU_INIT        0x20
171 #define PSMUX_STREAM_ID_SPU_MAX        	0x3f
172 
173 #define PSMUX_STREAM_ID_DTS_INIT        0x88
174 #define PSMUX_STREAM_ID_DTS_MAX         0x8f
175 
176 #define PSMUX_STREAM_ID_LPCM_INIT       0xa0
177 #define PSMUX_STREAM_ID_LPCM_MAX        0xaf
178 
179 #define PSMUX_STREAM_ID_DIRAC_INIT      0x60
180 #define PSMUX_STREAM_ID_DIRAC_MAX       0x6f
181 
182 struct PsMuxStreamIdInfo {
183     guint8 id_mpga;
184     guint8 id_mpgv;
185     guint8 id_ac3;
186     guint8 id_spu;
187     guint8 id_dts;
188     guint8 id_lpcm;
189     guint8 id_dirac;
190 };
191 
192 static inline void
psmux_stream_id_info_init(PsMuxStreamIdInfo * info)193 psmux_stream_id_info_init (PsMuxStreamIdInfo * info)
194 {
195     g_return_if_fail (info != NULL);
196     info->id_mpga = PSMUX_STREAM_ID_MPGA_INIT;
197     info->id_mpgv = PSMUX_STREAM_ID_MPGV_INIT;
198     info->id_ac3  = PSMUX_STREAM_ID_AC3_INIT;
199     info->id_spu  = PSMUX_STREAM_ID_SPU_INIT;
200     info->id_dts  = PSMUX_STREAM_ID_DTS_INIT;
201     info->id_lpcm = PSMUX_STREAM_ID_LPCM_INIT;
202     info->id_dirac= PSMUX_STREAM_ID_DIRAC_INIT;
203 }
204 
205 G_END_DECLS
206 
207 #endif
208