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 __PSMUX_COMMON_H__
46 #define __PSMUX_COMMON_H__
47 
48 #include <glib.h>
49 #include <gst/gst.h>
50 #include "bits.h" /* from VLC */
51 
52 G_BEGIN_DECLS
53 
54 #define PSMUX_PACK_HDR_FREQ	30
55 #define PSMUX_SYS_HDR_FREQ	300
56 #define PSMUX_PSM_FREQ		300
57 
58 #define PSMUX_PES_MAX_PAYLOAD 65500 /* from VLC */
59 #define PSMUX_PES_MAX_HDR_LEN 30
60 #define PSMUX_MAX_PACKET_LEN (PSMUX_PES_MAX_PAYLOAD + PSMUX_PES_MAX_HDR_LEN)
61 
62 #define CLOCKBASE 90000
63 #define PSMUX_PACK_HDR_INTERVAL		( 0.7 * CLOCKBASE) /* interval to update pack header. 0.7 sec */
64 #define PSMUX_BITRATE_CALC_INTERVAL	CLOCKBASE /* interval to update bitrate in pack header. 1 sec */
65 
66 #define PSMUX_PES_BITRATE_DEFAULT 1000 /* Default bit_rate to write in the first pack header */
67 
68 #define PSMUX_START_CODE_PREFIX         0x01
69 
70 /* stream_id */
71 #define PSMUX_PACK_HEADER               0xba
72 #define PSMUX_SYSTEM_HEADER             0xbb
73 #define PSMUX_PROGRAM_STREAM_MAP        0xbc
74 #define PSMUX_PRIVATE_STREAM_1          0xbd
75 #define PSMUX_PADDING_STREAM            0xbe
76 #define PSMUX_PRIVATE_STREAM_2          0xbf
77 #define PSMUX_ECM                       0xb0
78 #define PSMUX_EMM                       0xb1
79 #define PSMUX_PROGRAM_STREAM_DIRECTORY  0xff
80 #define PSMUX_DSMCC_STREAM              0xf2
81 #define PSMUX_ITU_T_H222_1_TYPE_E       0xf8
82 #define PSMUX_EXTENDED_STREAM           0xfd
83 #define PSMUX_PROGRAM_END               0xb9
84 
85 #define PSMUX_MIN_ES_DESC_LEN 8
86 
87 /* Frequency for PCR representation */
88 #define PSMUX_SYS_CLOCK_FREQ (27000000L)
89 /* Frequency for PTS values */
90 #define PSMUX_CLOCK_FREQ (PSMUX_SYS_CLOCK_FREQ / 300)
91 
92 /* TODO: flags? looks that we don't need these */
93 #define PSMUX_PACKET_FLAG_NONE            (0)
94 #define PSMUX_PACKET_FLAG_ADAPTATION      (1 << 0)
95 #define PSMUX_PACKET_FLAG_DISCONT         (1 << 1)
96 #define PSMUX_PACKET_FLAG_RANDOM_ACCESS   (1 << 2)
97 #define PSMUX_PACKET_FLAG_PRIORITY        (1 << 3)
98 #define PSMUX_PACKET_FLAG_WRITE_PCR       (1 << 4)
99 #define PSMUX_PACKET_FLAG_WRITE_OPCR      (1 << 5)
100 #define PSMUX_PACKET_FLAG_WRITE_SPLICE    (1 << 6)
101 #define PSMUX_PACKET_FLAG_WRITE_ADAPT_EXT (1 << 7)
102 
103 /* PES stream specific flags */
104 #define PSMUX_PACKET_FLAG_PES_FULL_HEADER   (1 << 8)
105 #define PSMUX_PACKET_FLAG_PES_WRITE_PTS     (1 << 9)
106 #define PSMUX_PACKET_FLAG_PES_WRITE_PTS_DTS (1 << 10)
107 #define PSMUX_PACKET_FLAG_PES_WRITE_ESCR    (1 << 11)
108 #define PSMUX_PACKET_FLAG_PES_EXT_STREAMID  (1 << 12)
109 #define PSMUX_PACKET_FLAG_PES_DATA_ALIGN    (1 << 13)
110 
111 typedef struct PsMuxPacketInfo PsMuxPacketInfo;
112 typedef struct PsMuxProgram PsMuxProgram;
113 typedef struct PsMuxStream PsMuxStream;
114 typedef struct PsMuxStreamIdInfo PsMuxStreamIdInfo;
115 typedef struct PsMux PsMux;
116 typedef enum PsMuxStreamType PsMuxStreamType;
117 typedef struct PsMuxStreamBuffer PsMuxStreamBuffer;
118 
119 /* clearup and see if we need this anymore */
120 struct PsMuxPacketInfo {
121   guint32 flags;
122 };
123 
124 /* bitstream writers */
125 static inline void
psmux_put16(guint8 ** pos,guint16 val)126 psmux_put16 (guint8 **pos, guint16 val)
127 {
128   *(*pos)++ = (val >> 8) & 0xff;
129   *(*pos)++ = val & 0xff;
130 }
131 
132 static inline void
psmux_put32(guint8 ** pos,guint32 val)133 psmux_put32 (guint8 **pos, guint32 val)
134 {
135   *(*pos)++ = (val >> 24) & 0xff;
136   *(*pos)++ = (val >> 16) & 0xff;
137   *(*pos)++ = (val >> 8) & 0xff;
138   *(*pos)++ = val & 0xff;
139 }
140 
141 static inline void
psmux_put_ts(guint8 ** pos,guint8 id,gint64 ts)142 psmux_put_ts (guint8 **pos, guint8 id, gint64 ts)
143 {
144   /* 1: 4 bit id value | TS [32..30] | marker_bit */
145   *(*pos)++ = ((id << 4) | ((ts >> 29) & 0x0E) | 0x01) & 0xff;
146   /* 2, 3: TS[29..15] | marker_bit */
147   psmux_put16 (pos, ((ts >> 14) & 0xfffe) | 0x01);
148   /* 4, 5: TS[14..0] | marker_bit */
149   psmux_put16 (pos, ((ts << 1) & 0xfffe) | 0x01);
150 }
151 
152 G_END_DECLS
153 
154 #endif
155