M4MP4W_Utils.c revision 694816d7291f17364502ac5d3319684a0b180860
1/*
2 * Copyright (C) 2004-2011 NXP Software
3 * Copyright (C) 2011 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18/**
19******************************************************************************
20 * @file    M4MP4W_Utils.c
21 * @brief   Utilities and private functions for the MP4 writer
22******************************************************************************
23*/
24
25#include "NXPSW_CompilerSwitches.h"
26
27#ifndef _M4MP4W_USE_CST_MEMORY_WRITER
28
29#include "M4MP4W_Utils.h"
30#include "M4OSA_Error.h"
31#include "M4MP4W_Types.h"
32
33#define ERR_CHECK(exp, err) if (!(exp)) { return err; }
34
35/*******************************************************************************/
36M4OSA_ERR M4MP4W_putByte(M4OSA_UChar c, M4OSA_FileWriterPointer* fileFunction,
37                         M4OSA_Context context)
38/*******************************************************************************/
39{
40    M4OSA_ERR err = fileFunction->writeData(context, (M4OSA_MemAddr8)&c, 1);
41    return err;
42}
43
44/*******************************************************************************/
45M4OSA_ERR M4MP4W_putBE16(M4OSA_UInt32 val, M4OSA_FileWriterPointer* fileFunction,
46                         M4OSA_Context context)
47/*******************************************************************************/
48{
49    M4OSA_ERR err;
50    err = M4MP4W_putByte((M4OSA_UChar)(val >> 8), fileFunction, context);
51    ERR_CHECK(err == M4NO_ERROR, err);
52    err = M4MP4W_putByte((M4OSA_UChar)val, fileFunction, context);
53    return err;
54}
55
56/*******************************************************************************/
57M4OSA_ERR M4MP4W_putBE24(M4OSA_UInt32 val, M4OSA_FileWriterPointer* fileFunction,
58                         M4OSA_Context context)
59/*******************************************************************************/
60{
61    M4OSA_ERR err;
62    err = M4MP4W_putByte((M4OSA_UChar)(val >> 16), fileFunction, context);
63    ERR_CHECK(err == M4NO_ERROR, err);
64    err = M4MP4W_putByte((M4OSA_UChar)(val >> 8), fileFunction, context);
65    ERR_CHECK(err == M4NO_ERROR, err);
66    err = M4MP4W_putByte((M4OSA_UChar)val, fileFunction, context);
67    return err;
68}
69
70/*******************************************************************************/
71M4OSA_ERR M4MP4W_putBE32(M4OSA_UInt32 val, M4OSA_FileWriterPointer* fileFunction,
72                         M4OSA_Context context)
73/*******************************************************************************/
74{
75    M4OSA_ERR err;
76    err = M4MP4W_putByte((M4OSA_UChar)(val >> 24), fileFunction, context);
77    ERR_CHECK(err == M4NO_ERROR, err);
78    err = M4MP4W_putByte((M4OSA_UChar)(val >> 16), fileFunction, context);
79    ERR_CHECK(err == M4NO_ERROR, err);
80    err = M4MP4W_putByte((M4OSA_UChar)(val >> 8), fileFunction, context);
81    ERR_CHECK(err == M4NO_ERROR, err);
82    err = M4MP4W_putByte((M4OSA_UChar)val, fileFunction, context);
83    return err;
84}
85
86/*******************************************************************************/
87M4OSA_ERR M4MP4W_putBlock(const M4OSA_UChar* Block, M4OSA_UInt32 size,
88                           M4OSA_FileWriterPointer* fileFunction, M4OSA_Context context)
89/*******************************************************************************/
90{
91    M4OSA_ERR err = fileFunction->writeData(context, (M4OSA_MemAddr8)Block, size);
92    return err;
93}
94
95/*******************************************************************************/
96void M4MP4W_convertInt32BE(M4OSA_UInt32* valPtr)
97/*******************************************************************************/
98{
99    M4OSA_UChar a, b;
100    M4OSA_UChar* c = (M4OSA_UChar*)valPtr;
101    a       = *(c);
102    b       = *(c+1);
103    *(c)   = *(c+3);
104    *(c+1) = *(c+2);
105    *(c+2) = b;
106    *(c+3) = a;
107}
108
109/*******************************************************************************/
110void M4MP4W_table32ToBE(M4OSA_UInt32* tab, M4OSA_UInt32 nb)
111/*******************************************************************************/
112{
113    M4OSA_UInt32 i;
114    for (i=0; i<nb; i++)
115        M4MP4W_convertInt32BE(&(tab)[i]);
116}
117
118/*******************************************************************************/
119void* M4MP4W_realloc(M4OSA_MemAddr32 ptr, M4OSA_UInt32 oldSize, M4OSA_UInt32 newSize)
120/*******************************************************************************/
121{
122    M4OSA_MemAddr32 ptr2 = (M4OSA_MemAddr32)M4OSA_32bitAlignedMalloc(newSize, M4MP4_WRITER,
123                                                          (M4OSA_Char *)"realloc");
124    if (M4OSA_NULL != ptr2)
125    {
126        memcpy((void *)ptr2, (void *)ptr, oldSize);
127    }
128    free(ptr);
129    return ptr2;
130}
131
132/*******************************************************************************/
133M4OSA_ERR M4MP4W_freeContext(M4OSA_Context context)
134/*******************************************************************************/
135{
136#ifdef _M4MP4W_MOOV_FIRST
137    M4OSA_UInt32 i;
138#endif /*_M4MP4W_MOOV_FIRST*/
139    M4MP4W_Mp4FileData* mMp4FileDataPtr = (M4MP4W_Mp4FileData*)context;
140    ERR_CHECK(context != M4OSA_NULL, M4ERR_PARAMETER);
141
142    /*freeContext is now called after closeWrite*/
143    ERR_CHECK( mMp4FileDataPtr->state == M4MP4W_closed, M4ERR_STATE);
144    mMp4FileDataPtr->state = M4MP4W_closed;
145
146    if (mMp4FileDataPtr->audioTrackPtr != M4OSA_NULL)
147    {
148        /*delete also other chunks if any*/
149        /*for (i=0; i<=mMp4FileDataPtr->audioTrackPtr->currentChunk; i++)*/
150
151#ifdef _M4MP4W_MOOV_FIRST
152        for (i=0; i<=mMp4FileDataPtr->audioTrackPtr->LastAllocatedChunk; i++)
153        {
154            free(mMp4FileDataPtr->audioTrackPtr->Chunk[i]);
155        }
156#else
157        if ((M4OSA_NULL != mMp4FileDataPtr->audioTrackPtr->Chunk) &&
158             (M4OSA_NULL != mMp4FileDataPtr->audioTrackPtr->Chunk[0]))
159        {
160            free(mMp4FileDataPtr->audioTrackPtr->Chunk[0]);
161        }
162        if (M4OSA_NULL != mMp4FileDataPtr->audioTrackPtr->chunkOffsetTable)
163        {
164            free(mMp4FileDataPtr->audioTrackPtr->chunkOffsetTable);
165        }
166#endif /*_M4MP4W_MOOV_FIRST*/
167
168        /*now dynamic*/
169        if (M4OSA_NULL != mMp4FileDataPtr->audioTrackPtr->Chunk)
170        {
171            free(mMp4FileDataPtr->audioTrackPtr->Chunk);
172        }
173        if (M4OSA_NULL != mMp4FileDataPtr->audioTrackPtr->chunkSizeTable)
174        {
175            free(mMp4FileDataPtr->audioTrackPtr->chunkSizeTable);
176        }
177        if (M4OSA_NULL != mMp4FileDataPtr->audioTrackPtr->chunkSampleNbTable)
178        {
179            free(mMp4FileDataPtr->audioTrackPtr->chunkSampleNbTable);
180        }
181        if (M4OSA_NULL != mMp4FileDataPtr->audioTrackPtr->chunkTimeMsTable)
182        {
183            free(mMp4FileDataPtr->audioTrackPtr->chunkTimeMsTable);
184        }
185
186        if (mMp4FileDataPtr->audioTrackPtr->TABLE_STTS != M4OSA_NULL)
187        {
188            free(mMp4FileDataPtr->audioTrackPtr->TABLE_STTS);
189        }
190
191        if (mMp4FileDataPtr->audioTrackPtr->TABLE_STSZ != M4OSA_NULL)
192        {
193            free(mMp4FileDataPtr->audioTrackPtr->TABLE_STSZ);
194        }
195
196        if (mMp4FileDataPtr->audioTrackPtr->DSI != M4OSA_NULL)
197        {
198            free(mMp4FileDataPtr->audioTrackPtr->DSI);
199            mMp4FileDataPtr->audioTrackPtr->DSI = M4OSA_NULL;
200        }
201
202        free(mMp4FileDataPtr->audioTrackPtr);
203        mMp4FileDataPtr->audioTrackPtr = M4OSA_NULL;
204    }
205    if (mMp4FileDataPtr->videoTrackPtr != M4OSA_NULL)
206    {
207        /*delete also other chunks if any*/
208        /*for (i=0; i<=mMp4FileDataPtr->videoTrackPtr->currentChunk; i++)*/
209
210#ifdef _M4MP4W_MOOV_FIRST
211        for (i=0; i<=mMp4FileDataPtr->videoTrackPtr->LastAllocatedChunk; i++)
212        {
213            free(mMp4FileDataPtr->videoTrackPtr->Chunk[i]);
214        }
215#else
216        if ((M4OSA_NULL != mMp4FileDataPtr->videoTrackPtr->Chunk) &&
217             (M4OSA_NULL != mMp4FileDataPtr->videoTrackPtr->Chunk[0]))
218        {
219            free(mMp4FileDataPtr->videoTrackPtr->Chunk[0]);
220        }
221        if (M4OSA_NULL != mMp4FileDataPtr->videoTrackPtr->chunkOffsetTable)
222        {
223            free(mMp4FileDataPtr->videoTrackPtr->chunkOffsetTable);
224        }
225#endif /*_M4MP4W_MOOV_FIRST*/
226
227        /*now dynamic*/
228        if (M4OSA_NULL != mMp4FileDataPtr->videoTrackPtr->Chunk)
229        {
230            free(mMp4FileDataPtr->videoTrackPtr->Chunk);
231        }
232        if (M4OSA_NULL != mMp4FileDataPtr->videoTrackPtr->chunkSizeTable)
233        {
234            free(mMp4FileDataPtr->videoTrackPtr->chunkSizeTable);
235        }
236        if (M4OSA_NULL != mMp4FileDataPtr->videoTrackPtr->chunkSampleNbTable)
237        {
238            free(mMp4FileDataPtr->videoTrackPtr->chunkSampleNbTable);
239        }
240        if (M4OSA_NULL != mMp4FileDataPtr->videoTrackPtr->chunkTimeMsTable)
241        {
242            free(mMp4FileDataPtr->videoTrackPtr->chunkTimeMsTable);
243        }
244
245        if (mMp4FileDataPtr->videoTrackPtr->DSI != M4OSA_NULL)
246        {
247            free(mMp4FileDataPtr->videoTrackPtr->DSI);
248            mMp4FileDataPtr->videoTrackPtr->DSI = M4OSA_NULL;
249        }
250
251        /*now dynamic*/
252        if (M4OSA_NULL != mMp4FileDataPtr->videoTrackPtr->TABLE_STTS)
253        {
254            free(mMp4FileDataPtr->videoTrackPtr->TABLE_STTS);
255        }
256        if (M4OSA_NULL != mMp4FileDataPtr->videoTrackPtr->TABLE_STSZ)
257        {
258            free(mMp4FileDataPtr->videoTrackPtr->TABLE_STSZ);
259        }
260        if (M4OSA_NULL != mMp4FileDataPtr->videoTrackPtr->TABLE_STSS)
261        {
262            free(mMp4FileDataPtr->videoTrackPtr->TABLE_STSS);
263        }
264
265        free(mMp4FileDataPtr->videoTrackPtr);
266        mMp4FileDataPtr->videoTrackPtr = M4OSA_NULL;
267    }
268
269    if (mMp4FileDataPtr->embeddedString != M4OSA_NULL)
270    {
271        free(mMp4FileDataPtr->embeddedString);
272        mMp4FileDataPtr->embeddedString = M4OSA_NULL;
273    }
274
275    free(mMp4FileDataPtr);
276
277    return M4NO_ERROR;
278}
279
280#ifdef _M4MP4W_OPTIMIZE_FOR_PHONE
281/*******************************************************************************/
282M4OSA_Void M4MP4W_put32_Hi(M4OSA_UInt32* tab, M4OSA_UInt16 Hi)
283/*******************************************************************************/
284{
285    *tab &= 0xFFFF;
286    *tab |= Hi<<16;
287}
288
289/*******************************************************************************/
290M4OSA_Void M4MP4W_put32_Lo(M4OSA_UInt32* tab, M4OSA_UInt16 Lo)
291/*******************************************************************************/
292{
293    *tab &= 0xFFFF0000;
294    *tab |= Lo;
295}
296
297/*******************************************************************************/
298M4OSA_UInt16 M4MP4W_get32_Hi(M4OSA_UInt32* tab)
299/*******************************************************************************/
300{
301    return (*tab >> 16) & 0xFFFF;
302}
303
304/*******************************************************************************/
305M4OSA_UInt16 M4MP4W_get32_Lo(M4OSA_UInt32* tab)
306/*******************************************************************************/
307{
308    return *tab & 0xFFFF;
309}
310#endif
311
312#endif /* _M4MP4W_USE_CST_MEMORY_WRITER */
313
314