xref: /utopia/UTPA2-700.0.x/modules/mfe/api/mfe/MuxCommon.c (revision 53ee8cc121a030b8d368113ac3e966b4705770ef)
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 
79 
80 #include <string.h>
81 
82 #include "MFE_chip.h"
83 #include "mfe_common.h"
84 #include "mfe_type.h"
85 #include "madp_ms_dprintf.h"
86 
87 #include "MuxTypes.h"
88 #include "MuxCommon.h"
89 #include <pthread.h>
90 //#ifdef FILE_TEST
91 #include <stdio.h>
92 //#endif
93 
94 //////////////////////////////////////////////////////////////////////////
95 // Outstream related functions
96 //////////////////////////////////////////////////////////////////////////
97 pthread_mutex_t TS_mutex;// = PTHREAD_MUTEX_INITIALIZER;
98 
outbuf_init(Outstream * s)99 void outbuf_init(Outstream *s)
100 {
101     MFE_U32 i;
102 
103     assert(s->bbuffer);
104     assert((s->bbufend_ptr-s->bbuffer)>0);
105 
106     s->total_size = 0;
107     s->read_ptr = s->write_ptr = s->bbuffer;
108 
109     s->locked = 0;
110     s->bookmark = s->write_ptr;
111 
112     for (i=0; i<s->ring_size; i++) {
113         s->index_ring[i].index_data.indexType = INDEX_TYPE_NONE;
114         s->index_ring[i].index_data.byteOffset = 0;
115         s->index_ring[i].used = 0;
116         s->index_ring[i].next = i+1;
117     }
118     s->index_ring[s->ring_size-1].next = 0;
119     s->index_read = s->index_write = 0;
120 
121 #ifdef FILE_TEST
122     s->handle = fopen("d:\\test.ts", "wb");
123     assert(s->handle);
124     s->total_size = 0;
125 #endif
126 }
127 
128 #ifdef CHECK_READ_WRITE_PTR
129 // NOTE 1: Hazard does not necessary equal to actual overrun.
130 // NOTE 2: Because of the lock, this checking is intrusive.
131 // NOTE 3: Index buffer is not checked here!
check_conservative(Outstream * s,int write_size)132 void check_conservative(Outstream *s, int write_size)
133 {
134 	MFE_U8* tmp_ptr;
135     pthread_mutex_lock(&TS_mutex);
136 	if (s->write_ptr >= s->read_ptr) {
137 		if (s->write_ptr+write_size > s->bbufend_ptr) {
138 			tmp_ptr = s->bbuffer + ((s->write_ptr+write_size) - s->bbufend_ptr);
139 			if (tmp_ptr>=s->read_ptr)
140 				printf("[TSMUX] Buffer overrun hazard condition 1\n");
141 		}
142 	}
143 	else {
144 		if (s->write_ptr+write_size>=s->read_ptr)
145 			printf("[TSMUX] Buffer overrun hazard condition 2\n");
146 	}
147     pthread_mutex_unlock(&TS_mutex);
148 }
149 #endif
150 
outbuf_put(Outstream * s,OutDataInfo * pinfo)151 void outbuf_put(Outstream *s, OutDataInfo* pinfo)
152 {
153     int len, size, i, allsize;
154     MFE_U8 *buf, *write_ptr;
155     MFE_U8 tc_buf[4];
156 
157 //    printf("[TSMUX] outbuf_put\n");
158 
159     write_ptr = s->write_ptr;    // No hazard because only this function will update write_ptr
160     pthread_mutex_lock(&TS_mutex);
161     //write_ptr = s->write_ptr;
162     if (pinfo->lock_write && !s->locked) {
163         s->locked = 1;
164         s->bookmark = write_ptr;
165     }
166     pthread_mutex_unlock(&TS_mutex);
167 
168 	allsize = size = pinfo->size1+pinfo->size2;
169     if (pinfo->write_timecode) {
170         allsize += 4;	// plus 4-byte timecode
171         buf = tc_buf;
172         put32_msbf(&buf, pinfo->timecode);
173     }
174     s->total_size += allsize;
175 //    printf("[TSMUX] Before check_conservative\n");
176 #ifdef CHECK_READ_WRITE_PTR
177     check_conservative(s, allsize);
178 #endif
179 //    printf("%d %d ", pinfo->write_type, pinfo->type);
180     if (pinfo->write_type && pinfo->type!=INDEX_TYPE_NONE) {
181 #ifdef CHECK_READ_WRITE_PTR
182  		if (s->index_ring[s->index_write].used)
183  			printf("[TSMUX] Index buffer overrun\n");
184         //assert(s->index_ring[s->index_write].used==0);
185 #endif
186         s->index_ring[s->index_write].index_data.indexType = pinfo->type;
187         s->index_ring[s->index_write].index_data.byteOffset = s->bookmark - s->bbuffer;
188         s->index_ring[s->index_write].used = 1;
189         s->index_write = s->index_ring[s->index_write].next;
190     }
191 
192     // Write timecode first
193     if (pinfo->write_timecode) {
194         buf = tc_buf;
195         len = (s->bbufend_ptr - write_ptr);
196         if (len>4) {
197             memcpy(write_ptr, buf, 4);
198             write_ptr += 4;
199         }
200         else {
201             memcpy(write_ptr, buf, len);
202             buf += len;
203             len = 4 - len;
204 #ifdef FILE_TEST
205             fwrite(s->bbuffer, 1, s->bbufend_ptr-s->bbuffer, (FILE*)s->handle);
206 #endif
207             write_ptr = s->bbuffer;
208             if (len>0) {
209                 memcpy(write_ptr, buf, len);
210                 write_ptr += len;
211             }
212         }
213     }
214 
215     // Write header
216     if (pinfo->size1>0) {
217         buf = pinfo->header;
218         size = pinfo->size1;
219         len = (s->bbufend_ptr - write_ptr);
220         if (len>size) {
221             memcpy(write_ptr, buf, size);
222             write_ptr += size;
223         }
224         else {
225             memcpy(write_ptr, buf, len);
226             buf += len;
227             len = size - len;
228 #ifdef FILE_TEST
229             fwrite(s->bbuffer, 1, s->bbufend_ptr-s->bbuffer, (FILE*)s->handle);
230 #endif
231             write_ptr = s->bbuffer;
232             if (len>0) {
233                 memcpy(write_ptr, buf, len);
234                 write_ptr += len;
235             }
236         }
237     }
238 
239     // Write data
240     if (pinfo->size2>0) {
241         buf = pinfo->data;
242         size = pinfo->size2;
243         len = (s->bbufend_ptr - write_ptr);
244         if (len>size) {
245             memcpy(write_ptr, buf, size);
246             write_ptr += size;
247         }
248         else {
249             memcpy(write_ptr, buf, len);
250             buf += len;
251             len = size - len;
252 #ifdef FILE_TEST
253             fwrite(s->bbuffer, 1, s->bbufend_ptr-s->bbuffer, (FILE*)s->handle);
254 #endif
255             write_ptr = s->bbuffer;
256             if (len>0) {
257                 memcpy(write_ptr, buf, len);
258                 write_ptr += len;
259             }
260         }
261     }
262 
263     // Stuffing byte is necessary for some container, say AVI.
264     if (pinfo->stuffing>0) {
265         size = pinfo->stuffing;
266         len = (s->bbufend_ptr - write_ptr);
267         if (len > size) {
268             memset(write_ptr, 0, size);
269             write_ptr += size;
270         }
271         else {
272             memset(write_ptr, 0, len);
273             len = size - len;
274             assert(write_ptr==s->bbufend_ptr);
275 #ifdef FILE_TEST
276             fwrite(s->bbuffer, 1, s->bbufend_ptr-s->bbuffer, (FILE*)s->handle);
277 #endif
278             write_ptr = s->bbuffer;
279             if (len>0) {
280                 memset(write_ptr, 0, len);
281                 write_ptr += len;
282             }
283         }
284     }
285 
286     for (i=0; i<pinfo->update_count; i++) {
287         len = pinfo->update_addr[i];
288         size = pinfo->update_size[i];
289         buf = s->bookmark+len;
290         if (buf>=s->bbufend_ptr)
291             buf -= (s->bbufend_ptr-s->bbuffer);
292         len = s->bbufend_ptr - buf;
293         if (len>size) {
294             memcpy(buf, pinfo->update_data[i], size);
295         }
296         else {
297             memcpy(buf, pinfo->update_data[i], len);
298             memcpy(s->bbuffer, pinfo->update_data[i]+len, size-len);
299         }
300     }
301 
302     pthread_mutex_lock(&TS_mutex);
303     s->write_ptr = write_ptr;
304     if (!pinfo->lock_write) {
305         s->locked = 0;
306         s->bookmark = write_ptr;
307     }
308     pthread_mutex_unlock(&TS_mutex);
309 }
310 
311 // return 0 if success (buffer not empty)
outbuf_get(Outstream * s,MFE_U8 ** ppbuf,MFE_U8 ** ppread,MFE_U8 ** ppwrite,IndexRingEntry ** ppindex)312 int outbuf_get(Outstream *s, MFE_U8 **ppbuf, MFE_U8 **ppread, MFE_U8 **ppwrite, IndexRingEntry **ppindex)
313 {
314     int rtn;
315 //    int i;
316     pthread_mutex_lock(&TS_mutex);
317 
318     *ppbuf = s->bbuffer;
319     *ppread = s->read_ptr;
320     *ppwrite = s->bookmark;//s->write_ptr;
321     *ppindex = &s->index_ring[s->index_read];
322     rtn = (s->read_ptr==s->write_ptr) ? 1 : 0;
323     /*
324     for (i=0; i<4; i++)
325         printf("outbuf_get: %d %d %d %d\n", s->index_ring[i].used, (*ppindex)[i].used, s->index_read
326                 , s->index_write);
327                 */
328     pthread_mutex_unlock(&TS_mutex);
329     return rtn;
330 }
331 
outbuf_getdone(Outstream * s,MFE_U8 * pread,MFE_U32 index_read)332 void outbuf_getdone(Outstream *s, MFE_U8 *pread, MFE_U32 index_read)
333 {
334     pthread_mutex_lock(&TS_mutex);
335 //    printf("in outbuf_getdone %d\n", index_read);
336     s->read_ptr = pread;
337     while (index_read>0) {
338         s->index_ring[s->index_read].used = 0;
339         s->index_read = s->index_ring[s->index_read].next;
340         index_read--;
341     }
342 
343     pthread_mutex_unlock(&TS_mutex);
344 }
345 
outbuf_flush(Outstream * s)346 void outbuf_flush(Outstream *s)
347 {
348 #ifdef FILE_TEST
349     if (s->handle) {
350         fwrite(s->bbuffer, 1, s->write_ptr-s->bbuffer, (FILE*)s->handle);
351     }
352 #endif
353 }
354 
outbuf_close(Outstream * s)355 void outbuf_close(Outstream *s)
356 {
357 #ifdef FILE_TEST
358     if (s->handle) {
359         fclose((FILE*)s->handle);
360         s->handle=0;
361     }
362 #endif
363 }
364 
365 //////////////////////////////////////////////////////////////////////////
366 // MUX interface common routines
367 
368 /* return 0 if success, 1 if fail (no data) */
mux_read(struct avmux_s * inst,MFE_U8 ** ppStreamBuffer,MFE_U8 ** ppBufReadPtr,MFE_U8 ** ppBufWritePtr,IndexRingEntry ** ppIndexPtr)369 int mux_read(struct avmux_s *inst, MFE_U8 **ppStreamBuffer, MFE_U8 **ppBufReadPtr, MFE_U8 **ppBufWritePtr, IndexRingEntry **ppIndexPtr)
370 {
371     MediaContext *s = inst->mctx;
372     int rtn;
373     rtn = outbuf_get(s->outstream, ppStreamBuffer, ppBufReadPtr, ppBufWritePtr, ppIndexPtr);
374     return rtn;
375 }
376 
mux_readdone(struct avmux_s * inst,MFE_U8 * pBufReadPtr,MFE_U32 IndexRead)377 void mux_readdone(struct avmux_s *inst, MFE_U8 *pBufReadPtr, MFE_U32 IndexRead)
378 {
379     MediaContext *s = inst->mctx;
380     outbuf_getdone(s->outstream, pBufReadPtr, IndexRead);
381 }
382