1 //<MStar Software>
2 //******************************************************************************
3 // MStar Software
4 // Copyright (c) 2010 - 2012 MStar Semiconductor, Inc. All rights reserved.
5 // All software, firmware and related documentation herein ("MStar Software") are
6 // intellectual property of MStar Semiconductor, Inc. ("MStar") and protected by
7 // law, including, but not limited to, copyright law and international treaties.
8 // Any use, modification, reproduction, retransmission, or republication of all
9 // or part of MStar Software is expressly prohibited, unless prior written
10 // permission has been granted by MStar.
11 //
12 // By accessing, browsing and/or using MStar Software, you acknowledge that you
13 // have read, understood, and agree, to be bound by below terms ("Terms") and to
14 // comply with all applicable laws and regulations:
15 //
16 // 1. MStar shall retain any and all right, ownership and interest to MStar
17 // Software and any modification/derivatives thereof.
18 // No right, ownership, or interest to MStar Software and any
19 // modification/derivatives thereof is transferred to you under Terms.
20 //
21 // 2. You understand that MStar Software might include, incorporate or be
22 // supplied together with third party`s software and the use of MStar
23 // Software may require additional licenses from third parties.
24 // Therefore, you hereby agree it is your sole responsibility to separately
25 // obtain any and all third party right and license necessary for your use of
26 // such third party`s software.
27 //
28 // 3. MStar Software and any modification/derivatives thereof shall be deemed as
29 // MStar`s confidential information and you agree to keep MStar`s
30 // confidential information in strictest confidence and not disclose to any
31 // third party.
32 //
33 // 4. MStar Software is provided on an "AS IS" basis without warranties of any
34 // kind. Any warranties are hereby expressly disclaimed by MStar, including
35 // without limitation, any warranties of merchantability, non-infringement of
36 // intellectual property rights, fitness for a particular purpose, error free
37 // and in conformity with any international standard. You agree to waive any
38 // claim against MStar for any loss, damage, cost or expense that you may
39 // incur related to your use of MStar Software.
40 // In no event shall MStar be liable for any direct, indirect, incidental or
41 // consequential damages, including without limitation, lost of profit or
42 // revenues, lost or damage of data, and unauthorized system use.
43 // You agree that this Section 4 shall still apply without being affected
44 // even if MStar Software has been modified by MStar in accordance with your
45 // request or instruction for your use, except otherwise agreed by both
46 // parties in writing.
47 //
48 // 5. If requested, MStar may from time to time provide technical supports or
49 // services in relation with MStar Software to you for your use of
50 // MStar Software in conjunction with your or your customer`s product
51 // ("Services").
52 // You understand and agree that, except otherwise agreed by both parties in
53 // writing, Services are provided on an "AS IS" basis and the warranty
54 // disclaimer set forth in Section 4 above shall apply.
55 //
56 // 6. Nothing contained herein shall be construed as by implication, estoppels
57 // or otherwise:
58 // (a) conferring any license or right to use MStar name, trademark, service
59 // mark, symbol or any other identification;
60 // (b) obligating MStar or any of its affiliates to furnish any person,
61 // including without limitation, you and your customers, any assistance
62 // of any kind whatsoever, or any information; or
63 // (c) conferring any license or right under any intellectual property right.
64 //
65 // 7. These terms shall be governed by and construed in accordance with the laws
66 // of Taiwan, R.O.C., excluding its conflict of law rules.
67 // Any and all dispute arising out hereof or related hereto shall be finally
68 // settled by arbitration referred to the Chinese Arbitration Association,
69 // Taipei in accordance with the ROC Arbitration Law and the Arbitration
70 // Rules of the Association by three (3) arbitrators appointed in accordance
71 // with the said Rules.
72 // The place of arbitration shall be in Taipei, Taiwan and the language shall
73 // be English.
74 // The arbitration award shall be final and binding to both parties.
75 //
76 //******************************************************************************
77 //<MStar Software>
78 #ifndef _MUX_COMMON_H_
79 #define _MUX_COMMON_H_
80
81
82 //#define FILE_TEST // If defined, will write out muxed file into D: drive.
83 // Allow reading all unread data
84 //#define CHECK_READ_WRITE_PTR
85
86 #define MAX_STREAM_COUNT 2
87 #define SAMPLES_IN_AUDIO_FRAME 1152
88
89 typedef enum{
90 MUX_TS = 0,
91 MUX_AVI,
92 MUX_MP4
93 } MUX_TYPE;
94 //////////////////////////////////////////////////////////////////////////
95 // OutStream
96
97 typedef enum {
98 INDEX_TYPE_SEQ = 0,
99 INDEX_TYPE_I_PIC,
100 INDEX_TYPE_SEQ_I_PIC,
101 INDEX_TYPE_P_PIC,
102 INDEX_TYPE_B_PIC,
103 INDEX_TYPE_NONE
104 } INDEX_TYPE_T;
105
106 typedef struct {
107 INDEX_TYPE_T indexType;
108 MFE_U32 byteOffset;
109 } IndexType;
110
111 typedef struct {
112 IndexType index_data;
113 MFE_U32 used;
114 MFE_U32 next;
115 } IndexRingEntry;
116
117 typedef struct {
118 #ifdef FILE_TEST
119 void* handle; // open file handle
120 #endif
121 MFE_U8 *bbuffer; // big buffer start
122 MFE_U8 *bbufend_ptr; // (bbufend_ptr - bbuffer) is the size of big buffer
123 MFE_U8 *write_ptr, *read_ptr;
124 MFE_U64 total_size; // latest ts stream length
125
126 // Index buffer
127 IndexRingEntry* index_ring;
128 MFE_U32 ring_size;
129 MFE_U32 index_read, index_write;
130
131 // Bookmark: for delayed read/write
132 MFE_U32 locked;
133 MFE_U8 *bookmark;
134 } Outstream;
135
136 #define MAX_UPDATE_COUNT 1
137 typedef struct OutDataInfo {
138 MFE_U8 *header;
139 int size1;
140 MFE_U8 *data;
141 int size2;
142 int write_type; // 0 or 1
143 INDEX_TYPE_T type;
144 int write_timecode; // 0 or 1
145 MFE_U32 timecode;
146 // AVI stuffing
147 MFE_U32 stuffing;
148 // Support multiple segments in one video/audio frame
149 MFE_U8 lock_write;
150 MFE_U8 update_count;
151 MFE_U32 update_addr[MAX_UPDATE_COUNT]; // Offset from the bookmark position.
152 MFE_U8* update_data[MAX_UPDATE_COUNT];
153 MFE_U32 update_size[MAX_UPDATE_COUNT];
154 } OutDataInfo;
155
156 void outbuf_init(Outstream *s);
157 void outbuf_put(Outstream *s, OutDataInfo* pinfo);
158 void outbuf_flush(Outstream *s);
159 void outbuf_close(Outstream *s);
160 int outbuf_get(Outstream *s, MFE_U8 **ppbuf, MFE_U8 **ppread, MFE_U8 **ppwrite, IndexRingEntry **ppindex);
161 void outbuf_getdone(Outstream *s, MFE_U8 *pread, MFE_U32 index_read);
162
put16_msbf(MFE_U8 ** ptr,int val)163 static __inline int put16_msbf(MFE_U8 **ptr, int val)
164 {
165 MFE_U8 *q;
166 q = *ptr;
167 *q++ = val>>8;
168 *q++ = val&0xFF;
169 *ptr = q;
170 return 2;
171 }
172
put16_lsbf(MFE_U8 ** ptr,int val)173 static __inline int put16_lsbf(MFE_U8 **ptr, int val)
174 {
175 MFE_U8 *q;
176 q = *ptr;
177 *q++ = val&0xFF;
178 *q++ = val>>8;
179 *ptr = q;
180 return 2;
181 }
182
put32_msbf(MFE_U8 ** ptr,int val)183 static __inline int put32_msbf(MFE_U8 **ptr, int val)
184 {
185 MFE_U8 *q;
186 q = *ptr;
187 *q++ = val>>24;
188 *q++ = (val>>16)&0xFF;
189 *q++ = (val>>8)&0xFF;
190 *q++ = val&0xFF;
191 *ptr = q;
192 return 4;
193 }
194
put32_lsbf(MFE_U8 ** ptr,int val)195 static __inline int put32_lsbf(MFE_U8 **ptr, int val)
196 {
197 MFE_U8 *q;
198 q = *ptr;
199 *q++ = val&0xFF;
200 *q++ = (val>>8)&0xFF;
201 *q++ = (val>>16)&0xFF;
202 *q++ = val>>24;
203 *ptr = q;
204 return 4;
205 }
206
207 //////////////////////////////////////////////////////////////////////////
208 // Media and Access Unit
209 #define MAX_VALUE (1<<30)
210 #ifdef _WIN32
211 #if (_MSC_VER < 1400) //vc 6 or older version
212 #define INVALID_PTS (0x8000000000000000##i64)
213 #define MAXVALUE_PTS (0x7FFFFFFFFFFFFFFF##i64)
214 #else
215 #define INVALID_PTS ((MFE_S64)(0x8000000000000000))
216 #define MAXVALUE_PTS ((MFE_S64)(0x7FFFFFFFFFFFFFFF))
217 #endif
218 //#define INVALID_PTS ((MFE_S64)(0x8000000000000000))
219 //#define MAXVALUE_PTS ((MFE_S64)(0x7FFFFFFFFFFFFFFF))
220 #else
221
222 #define INVALID_PTS (0x8000000000000000##ULL)
223 #define MAXVALUE_PTS ((0x7FFFFFFFFFFFFFFF##ULL)
224
225 //#define INVALID_PTS ((MFE_S64)(0x8000000000000000LL))
226 //#define MAXVALUE_PTS ((MFE_S64)(0x7FFFFFFFFFFFFFFFLL))
227 #endif
228 typedef enum _CodecType {
229 CODEC_TYPE_VIDEO,
230 CODEC_TYPE_AUDIO,
231 } CodecType;
232
233 typedef enum _CodecID {
234 CODEC_ID_NONE = 999,
235 // Video
236 CODEC_ID_VIDEO_MPEG2 = 10,
237 CODEC_ID_VIDEO_MPEG4 = 0,
238 CODEC_ID_VIDEO_H264 = 2,
239 // Audio
240 CODEC_ID_AUDIO_MP2 = 101,
241 CODEC_ID_AUDIO_MP3 = 102,
242 } CodecID;
243
244 typedef struct StreamContext {
245 CodecType codec_type;
246 CodecID codec_id;
247 int bit_rate; // Average bitrate
248 MFE_S64 payload_pts;
249 MFE_S64 payload_dts;
250 MFE_U8 *payload;
251
252 /* MPEG TS related */
253
254 /*MpegTSProgram*/void *program;
255 int pid; // stream pid
256 int cc; // continuity_counter
257
258 /* AVI related */
259 // Internal usage
260 MFE_U32 frame_count;
261 // Input: Video
262 MFE_U32 width;
263 MFE_U32 height;
264 float framerate;
265 // Input: Audio
266 MFE_U32 samplerate;
267 } StreamContext;
268
269 typedef struct MediaContext { // Media Context
270 void* muxinfo; // Hold mux-format specific internal data
271 Outstream* outstream; // Output context
272 MFE_U32 stream_count;
273 StreamContext* streams[MAX_STREAM_COUNT];
274 MFE_S64 timestamp;
275 } MediaContext;
276
277 typedef struct AUContext { // Access Unit Context
278 MFE_U8 *access_unit;
279 MFE_U32 unit_size;
280 MFE_S64 pts;
281 MFE_S64 dts;
282 int stream_index; // index into MediaContext.streams
283 INDEX_TYPE_T index_type;
284 // (For multiple segments for one video/audio frame)
285 MFE_U8 first_segment;
286 MFE_U8 last_segment;
287 } AUContext;
288
289 //////////////////////////////////////////////////////////////////////////
290 // muxer structures
291
292 typedef struct avmux_s
293 {
294 MFE_U32 state;
295
296 // internal function
297 int (*write_open)(struct avmux_s *inst);
298 int (*write_pause)(struct avmux_s *inst);
299 int (*write_packet)(struct avmux_s *inst, AUContext *packet);
300 int (*write_resume)(struct avmux_s *inst);
301 int (*write_close)(struct avmux_s *inst);
302 // read functions
303 int (*read)(struct avmux_s *inst, MFE_U8 **ppStreamBuffer, MFE_U8 **ppBufReadPtr, MFE_U8 **ppBufWritePtr, IndexRingEntry **ppIndexPtr);
304 void (*read_done)(struct avmux_s *inst, MFE_U8 *pBufReadPtr, MFE_U32 IndexRead);
305
306 MediaContext* mctx;
307 void* endinfo;
308 }avmux_st, *pavmux_st;
309
310 // Return 0 if success (buffer not empty).
311 int mux_read(struct avmux_s *inst, MFE_U8 **ppStreamBuffer, MFE_U8 **ppBufReadPtr, MFE_U8 **ppBufWritePtr, IndexRingEntry **ppIndexPtr);
312 // pBufReadPtr: Updated read pointer of bitstream buffer
313 // IndexRead: Number of index read.
314 void mux_readdone(struct avmux_s *inst, MFE_U8 *pBufReadPtr, MFE_U32 IndexRead);
315
316 #endif // _MUX_COMMON_H_
317