summaryrefslogtreecommitdiff
path: root/gst/mpegpsmux/psmuxstream.h
blob: ff4c2892bf33ea7e7304a6f9e8511af1fbb2bf02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* MPEG-PS muxer plugin for GStreamer
 * Copyright 2008 Lin YANG <oxcsnicho@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
/*
 * Unless otherwise indicated, Source Code is licensed under MIT license.
 * See further explanation attached in License Statement (distributed in the file
 * LICENSE).
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */



#ifndef __PSMUXSTREAM_H__
#define __PSMUXSTREAM_H__

#include <glib.h>

#include "psmuxcommon.h"

G_BEGIN_DECLS


typedef void (*PsMuxStreamBufferReleaseFunc) (guint8 *data, void *user_data);

enum PsMuxStreamType { /* Table 2-29 in spec */
  PSMUX_ST_RESERVED                   = 0x00,
  PSMUX_ST_VIDEO_MPEG1                = 0x01,
  PSMUX_ST_VIDEO_MPEG2                = 0x02,
  PSMUX_ST_AUDIO_MPEG1                = 0x03,
  PSMUX_ST_AUDIO_MPEG2                = 0x04,
  PSMUX_ST_PRIVATE_SECTIONS           = 0x05,
  PSMUX_ST_PRIVATE_DATA               = 0x06,
  PSMUX_ST_MHEG                       = 0x07,
  PSMUX_ST_DSMCC                      = 0x08,
  PSMUX_ST_H222_1                     = 0x09,

  /* later extensions */
  PSMUX_ST_AUDIO_AAC                  = 0x0f,
  PSMUX_ST_VIDEO_MPEG4                = 0x10,
  PSMUX_ST_VIDEO_H264                 = 0x1b,

  /* private stream types */
  PSMUX_ST_PS_AUDIO_AC3               = 0x81,
  PSMUX_ST_PS_AUDIO_DTS               = 0x8a,
  PSMUX_ST_PS_AUDIO_LPCM              = 0x8b,
  PSMUX_ST_PS_DVD_SUBPICTURE          = 0xff,

  /* Non-standard definitions */
  PSMUX_ST_VIDEO_DIRAC                = 0xD1
};

struct PsMuxStreamBuffer
{
  guint8 *data;
  guint32 size;

  gboolean keyunit;

  /* PTS & DTS associated with the contents of this buffer */
  GstClockTime pts;
  GstClockTime dts;

  void *user_data;
};

/* PsMuxStream receives elementary streams for parsing.
 * Via the write_bytes() method, it can output a PES stream piecemeal */
struct PsMuxStream{
  PsMuxPacketInfo pi;

  PsMuxStreamType stream_type;
  guint8 stream_id;
  guint8 stream_id_ext; /* extended stream id (13818-1 Amdt 2) */

  /* List of data buffers available for writing out */
  GList *buffers;
  guint32 bytes_avail;

  /* Current data buffer being consumed */
  PsMuxStreamBuffer *cur_buffer;
  guint32 cur_buffer_consumed;

  /* PES payload */
  guint16 cur_pes_payload_size;
  guint16 pes_bytes_written; /* delete*/

  /* Release function */
  PsMuxStreamBufferReleaseFunc buffer_release;

  /* PTS/DTS to write if the flags in the packet info are set */
  gint64 pts; /* TODO: cur_buffer->pts?*/
  gint64 dts; /* TODO: cur_buffer->dts?*/
  gint64 last_pts;

  /* stream type */
  gboolean is_video_stream;
  gboolean is_audio_stream;

  /* for writing descriptors */
  gint audio_sampling;
  gint audio_channels;
  gint audio_bitrate;

  /* for writing buffer size in system header */
  guint max_buffer_size;
};

/* stream management */
PsMuxStream*    psmux_stream_new                (PsMux * mux, PsMuxStreamType stream_type);
void 		psmux_stream_free 		(PsMuxStream *stream);

/* The callback when a buffer is released. Used to unref the buffer in GStreamer */
void 		psmux_stream_set_buffer_release_func 	(PsMuxStream *stream,
       							 PsMuxStreamBufferReleaseFunc func);

/* Add a new buffer to the pool of available bytes. If pts or dts are not -1, they
 * indicate the PTS or DTS of the first access unit within this packet */
void 		psmux_stream_add_data 		(PsMuxStream *stream, guint8 *data, guint len,
						 void *user_data, gint64 pts, gint64 dts,
						 gboolean keyunit);

/* total bytes in buffer */
gint 		psmux_stream_bytes_in_buffer 	(PsMuxStream *stream);
/* number of bytes of raw data available for writing */
gint 		psmux_stream_bytes_avail 	(PsMuxStream *stream);

/* write PES data */
guint	 	psmux_stream_get_data 		(PsMuxStream *stream, guint8 *buf, guint len);

/* write corresponding descriptors of the stream */
void 		psmux_stream_get_es_descrs 	(PsMuxStream *stream, guint8 *buf, guint16 *len);

/* get the pts of stream */
guint64 	psmux_stream_get_pts 		(PsMuxStream *stream);

/* stream_id assignemnt */
#define PSMUX_STREAM_ID_MPGA_INIT       0xc0
#define PSMUX_STREAM_ID_MPGA_MAX        0xcf

#define PSMUX_STREAM_ID_MPGV_INIT       0xe0
#define PSMUX_STREAM_ID_MPGV_MAX        0xef

#define PSMUX_STREAM_ID_AC3_INIT        0x80
#define PSMUX_STREAM_ID_AC3_MAX         0x87

#define PSMUX_STREAM_ID_SPU_INIT        0x20
#define PSMUX_STREAM_ID_SPU_MAX        	0x3f

#define PSMUX_STREAM_ID_DTS_INIT        0x88
#define PSMUX_STREAM_ID_DTS_MAX         0x8f

#define PSMUX_STREAM_ID_LPCM_INIT       0xa0
#define PSMUX_STREAM_ID_LPCM_MAX        0xaf

#define PSMUX_STREAM_ID_DIRAC_INIT      0x60
#define PSMUX_STREAM_ID_DIRAC_MAX       0x6f

struct PsMuxStreamIdInfo {
    guint8 id_mpga;
    guint8 id_mpgv;
    guint8 id_ac3;
    guint8 id_spu;
    guint8 id_dts;
    guint8 id_lpcm;
    guint8 id_dirac;
};

static inline void
psmux_stream_id_info_init (PsMuxStreamIdInfo * info)
{
    g_return_if_fail (info != NULL);
    info->id_mpga = PSMUX_STREAM_ID_MPGA_INIT;
    info->id_mpgv = PSMUX_STREAM_ID_MPGV_INIT;
    info->id_ac3  = PSMUX_STREAM_ID_AC3_INIT;
    info->id_spu  = PSMUX_STREAM_ID_SPU_INIT;
    info->id_dts  = PSMUX_STREAM_ID_DTS_INIT;
    info->id_lpcm = PSMUX_STREAM_ID_LPCM_INIT;
    info->id_dirac= PSMUX_STREAM_ID_DIRAC_INIT;
}

G_END_DECLS

#endif