Update contrib.
1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
16 #include <3gplibrary/mp4config.h>
17 #include <3gplibrary/mp4lib.h>
19 #include "mp4memwrap.h"
21 #include "mp4compose.h"
33 #define STTSMAXENTRYCOUNT 100
34 #define STSZMAXSAMPLECOUNT 100
35 #define STSCMAXENTRYCOUNT 2
36 #define STCOMAXENTRYCOUNT 100
37 #define STSSMAXENTRYCOUNT 100
38 #define SDTPMAXENTRYCOUNT 100
41 extern EXPORT_C MP4Err MP4ComposeOpen(MP4Handle *apihandle,
45 MP4Err error = MP4_OK;
46 MP4HandleImp* handle = (MP4HandleStruct **)apihandle;
48 if ( filename != NULL )
50 *handle = (MP4HandleImp)mp4malloc(sizeof(MP4HandleStruct));
53 return MP4_OUT_OF_MEMORY;
55 (*handle)->bufferWrite=MP4FALSE;
59 if ( *handle == NULL )
65 (*handle)->file32Duplicate = NULL;
66 (*handle)->FileHandleFromOutside = EFalse;
68 if(!(*handle)->bufferWrite)
72 error = MP4_FILE_ERROR;
75 if ( error == MP4_OK )
77 if (saveFileName(filename, *handle) < 0)
78 error = MP4_OUT_OF_MEMORY;
83 if (createTmpFileName(filename, &((*handle)->tmpFileName)) == -1)
84 error = MP4_OUT_OF_MEMORY;
89 if (initFileWrite(filename, *handle) == -1)
90 error = MP4_FILE_ERROR;
95 if (initTmpFileWrite((*handle)->tmpFileName, *handle) == -1)
96 error = MP4_FILE_ERROR;
102 (*handle)->diskWriteBuf = (mp4_u8 *)mp4malloc(WRITEBUFSIZE);
103 if ((*handle)->diskWriteBuf == NULL)
104 error = MP4_OUT_OF_MEMORY;
109 (*handle)->audioSampleTable = (sampleTable *)mp4malloc(sizeof(sampleTable));
110 if ((*handle)->audioSampleTable == NULL)
111 error = MP4_OUT_OF_MEMORY;
116 (*handle)->audioSampleTable->sttsSampleCount = (mp4_u32 *)mp4malloc(sizeof(mp4_u32) * STTSMAXENTRYCOUNT);
117 if ((*handle)->audioSampleTable->sttsSampleCount == NULL)
118 error = MP4_OUT_OF_MEMORY;
123 (*handle)->audioSampleTable->sttsSampleDelta = (mp4_u32 *)mp4malloc(sizeof(mp4_u32) * STTSMAXENTRYCOUNT);
124 if ((*handle)->audioSampleTable->sttsSampleDelta == NULL)
125 error = MP4_OUT_OF_MEMORY;
130 (*handle)->audioSampleTable->stszEntrySize = (mp4_u32 *)mp4malloc(sizeof(mp4_u32) * STSZMAXSAMPLECOUNT);
131 if ((*handle)->audioSampleTable->stszEntrySize == NULL)
132 error = MP4_OUT_OF_MEMORY;
137 (*handle)->audioSampleTable->stscFirstChunk = (mp4_u32 *)mp4malloc(sizeof(mp4_u32) * STSCMAXENTRYCOUNT);
138 if ((*handle)->audioSampleTable->stscFirstChunk == NULL)
139 error = MP4_OUT_OF_MEMORY;
144 (*handle)->audioSampleTable->stscSamplesPerChunk = (mp4_u32 *)mp4malloc(sizeof(mp4_u32) * STSCMAXENTRYCOUNT);
145 if ((*handle)->audioSampleTable->stscSamplesPerChunk == NULL)
146 error = MP4_OUT_OF_MEMORY;
151 (*handle)->audioSampleTable->stscSampleDescriptionIndex = (mp4_u32 *)mp4malloc(sizeof(mp4_u32) * STSCMAXENTRYCOUNT);
152 if ((*handle)->audioSampleTable->stscSampleDescriptionIndex == NULL)
153 error = MP4_OUT_OF_MEMORY;
158 (*handle)->audioSampleTable->stcoChunkOffset = (mp4_u64 *)mp4malloc(sizeof(mp4_u64) * STCOMAXENTRYCOUNT);
159 if ((*handle)->audioSampleTable->stcoChunkOffset == NULL)
160 error = MP4_OUT_OF_MEMORY;
165 (*handle)->videoSampleTable = (sampleTable *)mp4malloc(sizeof(sampleTable));
166 if ((*handle)->videoSampleTable == NULL)
167 error = MP4_OUT_OF_MEMORY;
172 (*handle)->videoSampleTable->sttsSampleCount = (mp4_u32 *)mp4malloc(sizeof(mp4_u32) * STTSMAXENTRYCOUNT);
173 if ((*handle)->videoSampleTable->sttsSampleCount == NULL)
174 error = MP4_OUT_OF_MEMORY;
179 (*handle)->videoSampleTable->sttsSampleDelta = (mp4_u32 *)mp4malloc(sizeof(mp4_u32) * STTSMAXENTRYCOUNT);
180 if ((*handle)->videoSampleTable->sttsSampleDelta == NULL)
181 error = MP4_OUT_OF_MEMORY;
186 (*handle)->videoSampleTable->stszEntrySize = (mp4_u32 *)mp4malloc(sizeof(mp4_u32) * STSZMAXSAMPLECOUNT);
187 if ((*handle)->videoSampleTable->stszEntrySize == NULL)
188 error = MP4_OUT_OF_MEMORY;
193 (*handle)->videoSampleTable->stscFirstChunk = (mp4_u32 *)mp4malloc(sizeof(mp4_u32) * STSCMAXENTRYCOUNT);
194 if ((*handle)->videoSampleTable->stscFirstChunk == NULL)
195 error = MP4_OUT_OF_MEMORY;
200 (*handle)->videoSampleTable->stscSamplesPerChunk = (mp4_u32 *)mp4malloc(sizeof(mp4_u32) * STSCMAXENTRYCOUNT);
201 if ((*handle)->videoSampleTable->stscSamplesPerChunk == NULL)
202 error = MP4_OUT_OF_MEMORY;
207 (*handle)->videoSampleTable->stscSampleDescriptionIndex = (mp4_u32 *)mp4malloc(sizeof(mp4_u32) * STSCMAXENTRYCOUNT);
208 if ((*handle)->videoSampleTable->stscSampleDescriptionIndex == NULL)
209 error = MP4_OUT_OF_MEMORY;
214 (*handle)->videoSampleTable->stcoChunkOffset = (mp4_u64 *)mp4malloc(sizeof(mp4_u64) * STCOMAXENTRYCOUNT);
215 if ((*handle)->videoSampleTable->stcoChunkOffset == NULL)
216 error = MP4_OUT_OF_MEMORY;
221 (*handle)->videoSampleTable->stssSampleNumber = (mp4_u32 *)mp4malloc(sizeof(mp4_u32) * STSSMAXENTRYCOUNT);
222 if ((*handle)->videoSampleTable->stssSampleNumber == NULL)
223 error = MP4_OUT_OF_MEMORY;
228 (*handle)->videoSampleTable->sdtpSampleDependency = (mp4_u8 *)mp4malloc(sizeof(mp4_u8) * SDTPMAXENTRYCOUNT);
229 if ((*handle)->videoSampleTable->sdtpSampleDependency == NULL)
230 error = MP4_OUT_OF_MEMORY;
233 // register for stblib use
236 if (openStdlib() != MP4_OK)
244 if(!(*handle)->bufferWrite)
248 closeTmpFile(*handle);
250 freeTmpFileName((*handle)->tmpFileName);
253 if ((*handle)->diskWriteBuf)
254 mp4free((*handle)->diskWriteBuf);
256 if ((*handle)->videoSampleTable)
258 if ((*handle)->videoSampleTable->stssSampleNumber)
259 mp4free((*handle)->videoSampleTable->stssSampleNumber);
261 if ((*handle)->videoSampleTable->stcoChunkOffset)
262 mp4free((*handle)->videoSampleTable->stcoChunkOffset);
264 if ((*handle)->videoSampleTable->stscSampleDescriptionIndex)
265 mp4free((*handle)->videoSampleTable->stscSampleDescriptionIndex);
267 if ((*handle)->videoSampleTable->stscSamplesPerChunk)
268 mp4free((*handle)->videoSampleTable->stscSamplesPerChunk);
270 if ((*handle)->videoSampleTable->stscFirstChunk)
271 mp4free((*handle)->videoSampleTable->stscFirstChunk);
273 if ((*handle)->videoSampleTable->stszEntrySize)
274 mp4free((*handle)->videoSampleTable->stszEntrySize);
276 if ((*handle)->videoSampleTable->sttsSampleCount)
277 mp4free((*handle)->videoSampleTable->sttsSampleCount);
279 if ((*handle)->videoSampleTable->sttsSampleDelta)
280 mp4free((*handle)->videoSampleTable->sttsSampleDelta);
282 if ((*handle)->videoSampleTable->sdtpSampleDependency)
283 mp4free((*handle)->videoSampleTable->sdtpSampleDependency);
285 mp4free((*handle)->videoSampleTable);
288 if ((*handle)->audioSampleTable)
290 if ((*handle)->audioSampleTable->stcoChunkOffset)
291 mp4free((*handle)->audioSampleTable->stcoChunkOffset);
293 if ((*handle)->audioSampleTable->stscSampleDescriptionIndex)
294 mp4free((*handle)->audioSampleTable->stscSampleDescriptionIndex);
296 if ((*handle)->audioSampleTable->stscSamplesPerChunk)
297 mp4free((*handle)->audioSampleTable->stscSamplesPerChunk);
299 if ((*handle)->audioSampleTable->stscFirstChunk)
300 mp4free((*handle)->audioSampleTable->stscFirstChunk);
302 if ((*handle)->audioSampleTable->stszEntrySize)
303 mp4free((*handle)->audioSampleTable->stszEntrySize);
305 if ((*handle)->audioSampleTable->sttsSampleDelta)
306 mp4free((*handle)->audioSampleTable->sttsSampleDelta);
308 if ((*handle)->audioSampleTable->sttsSampleCount)
309 mp4free((*handle)->audioSampleTable->sttsSampleCount);
311 mp4free((*handle)->audioSampleTable);
314 if ((*handle)->fileName)
315 mp4free((*handle)->fileName);
324 (*handle)->audioSampleTable->sttsMaxEntryCount = STTSMAXENTRYCOUNT;
325 (*handle)->audioSampleTable->stszMaxSampleCount = STSZMAXSAMPLECOUNT;
326 (*handle)->audioSampleTable->stscMaxEntryCount = STSCMAXENTRYCOUNT;
327 (*handle)->audioSampleTable->stcoMaxEntryCount = STCOMAXENTRYCOUNT;
328 (*handle)->audioSampleTable->stcoNeed64Bits = EFalse;
330 (*handle)->videoSampleTable->sttsMaxEntryCount = STTSMAXENTRYCOUNT;
331 (*handle)->videoSampleTable->stszMaxSampleCount = STSZMAXSAMPLECOUNT;
332 (*handle)->videoSampleTable->stscMaxEntryCount = STSCMAXENTRYCOUNT;
333 (*handle)->videoSampleTable->stcoMaxEntryCount = STCOMAXENTRYCOUNT;
334 (*handle)->videoSampleTable->stssMaxEntryCount = STSSMAXENTRYCOUNT;
335 (*handle)->videoSampleTable->sdtpMaxEntryCount = SDTPMAXENTRYCOUNT;
336 (*handle)->videoSampleTable->stcoNeed64Bits = EFalse;
338 (*handle)->type = type;
340 /* Check if a 3GPP2 codec is being used */
341 if ((*handle)->type & MP4_TYPE_QCELP_13K)
343 (*handle)->generate3G2 = MP4TRUE;
349 extern EXPORT_C MP4Err MP4ComposeOpenToBuffer(MP4Handle *apihandle,
351 mp4_u8* composeBuffer,
352 mp4_u32 *composedSize)
354 MP4HandleImp* handle = (MP4HandleStruct **)apihandle;
356 *handle = (MP4HandleImp)mp4malloc(sizeof(MP4HandleStruct));
358 return MP4_OUT_OF_MEMORY;
360 (*handle)->file32Duplicate = NULL;
361 (*handle)->bufferWrite=MP4TRUE;
362 (*handle)->FileHandleFromOutside = EFalse;
364 if(composeBuffer == NULL)
366 return MP4_NO_OUTPUT_BUFFER; //This is to indicate that compose buffer has not been initialized
369 (*handle)->composeBuffer=composeBuffer;
370 (*handle)->composedSize=composedSize;
372 return MP4ComposeOpen( apihandle, NULL, type );
375 extern EXPORT_C MP4Err MP4ComposeOpenFileHandle(MP4Handle *apihandle,
377 TDriveNumber metadataTempDrive,
381 RFile64 *f64 = new RFile64;
384 return MP4_OUT_OF_MEMORY;
386 if (f64->Duplicate(*composedfile) != KErrNone)
391 err = MP4ComposeOpenFileHandle64(apihandle, f64, metadataTempDrive, type);
394 MP4HandleImp* handle = (MP4HandleStruct **)apihandle;
395 (*handle)->file32Duplicate = (void*)f64;
402 extern EXPORT_C MP4Err MP4ComposeOpenFileHandle64(MP4Handle *apihandle,
403 RFile64 *composedfile,
404 TDriveNumber metadataTempDrive,
407 MP4Err error = MP4_OK;
408 MP4HandleImp* handle = (MP4HandleStruct **)apihandle;
410 *handle = (MP4HandleImp)mp4malloc(sizeof(MP4HandleStruct));
413 return MP4_OUT_OF_MEMORY;
415 (*handle)->bufferWrite=MP4FALSE;
416 (*handle)->file32Duplicate = NULL;
417 // since file handle we can´t use temporary file for mediadata safely.
418 (*handle)->flags |= MP4_FLAG_METADATALAST;
419 (*handle)->FileHandleFromOutside = ETrue;
420 (*handle)->fileHandleDrive = metadataTempDrive;
424 (*handle)->fs = (void *)fs;
426 error = MP4_FILE_ERROR;
430 if (fs->Connect() != KErrNone)
431 error = MP4_FILE_ERROR;
436 (*handle)->rfile = (void *)composedfile;
437 if (composedfile == NULL)
438 error = MP4_FILE_ERROR;
440 (*handle)->file = (*handle)->rfile;
442 TRAPD(traperror, (*handle)->filewriter = CFileWriter::NewL( *composedfile ));
443 if ( traperror != KErrNone )
445 error = MP4_FILE_ERROR;
451 (*handle)->diskWriteBuf = (mp4_u8 *)mp4malloc(WRITEBUFSIZE);
452 if ((*handle)->diskWriteBuf == NULL)
453 error = MP4_OUT_OF_MEMORY;
458 (*handle)->audioSampleTable = (sampleTable *)mp4malloc(sizeof(sampleTable));
459 if ((*handle)->audioSampleTable == NULL)
460 error = MP4_OUT_OF_MEMORY;
465 (*handle)->audioSampleTable->sttsSampleCount = (mp4_u32 *)mp4malloc(sizeof(mp4_u32) * STTSMAXENTRYCOUNT);
466 if ((*handle)->audioSampleTable->sttsSampleCount == NULL)
467 error = MP4_OUT_OF_MEMORY;
472 (*handle)->audioSampleTable->sttsSampleDelta = (mp4_u32 *)mp4malloc(sizeof(mp4_u32) * STTSMAXENTRYCOUNT);
473 if ((*handle)->audioSampleTable->sttsSampleDelta == NULL)
474 error = MP4_OUT_OF_MEMORY;
479 (*handle)->audioSampleTable->stszEntrySize = (mp4_u32 *)mp4malloc(sizeof(mp4_u32) * STSZMAXSAMPLECOUNT);
480 if ((*handle)->audioSampleTable->stszEntrySize == NULL)
481 error = MP4_OUT_OF_MEMORY;
486 (*handle)->audioSampleTable->stscFirstChunk = (mp4_u32 *)mp4malloc(sizeof(mp4_u32) * STSCMAXENTRYCOUNT);
487 if ((*handle)->audioSampleTable->stscFirstChunk == NULL)
488 error = MP4_OUT_OF_MEMORY;
493 (*handle)->audioSampleTable->stscSamplesPerChunk = (mp4_u32 *)mp4malloc(sizeof(mp4_u32) * STSCMAXENTRYCOUNT);
494 if ((*handle)->audioSampleTable->stscSamplesPerChunk == NULL)
495 error = MP4_OUT_OF_MEMORY;
500 (*handle)->audioSampleTable->stscSampleDescriptionIndex = (mp4_u32 *)mp4malloc(sizeof(mp4_u32) * STSCMAXENTRYCOUNT);
501 if ((*handle)->audioSampleTable->stscSampleDescriptionIndex == NULL)
502 error = MP4_OUT_OF_MEMORY;
507 (*handle)->audioSampleTable->stcoChunkOffset = (mp4_u64 *)mp4malloc(sizeof(mp4_u64) * STCOMAXENTRYCOUNT);
508 if ((*handle)->audioSampleTable->stcoChunkOffset == NULL)
509 error = MP4_OUT_OF_MEMORY;
514 (*handle)->videoSampleTable = (sampleTable *)mp4malloc(sizeof(sampleTable));
515 if ((*handle)->videoSampleTable == NULL)
516 error = MP4_OUT_OF_MEMORY;
521 (*handle)->videoSampleTable->sttsSampleCount = (mp4_u32 *)mp4malloc(sizeof(mp4_u32) * STTSMAXENTRYCOUNT);
522 if ((*handle)->videoSampleTable->sttsSampleCount == NULL)
523 error = MP4_OUT_OF_MEMORY;
528 (*handle)->videoSampleTable->sttsSampleDelta = (mp4_u32 *)mp4malloc(sizeof(mp4_u32) * STTSMAXENTRYCOUNT);
529 if ((*handle)->videoSampleTable->sttsSampleDelta == NULL)
530 error = MP4_OUT_OF_MEMORY;
535 (*handle)->videoSampleTable->stszEntrySize = (mp4_u32 *)mp4malloc(sizeof(mp4_u32) * STSZMAXSAMPLECOUNT);
536 if ((*handle)->videoSampleTable->stszEntrySize == NULL)
537 error = MP4_OUT_OF_MEMORY;
542 (*handle)->videoSampleTable->stscFirstChunk = (mp4_u32 *)mp4malloc(sizeof(mp4_u32) * STSCMAXENTRYCOUNT);
543 if ((*handle)->videoSampleTable->stscFirstChunk == NULL)
544 error = MP4_OUT_OF_MEMORY;
549 (*handle)->videoSampleTable->stscSamplesPerChunk = (mp4_u32 *)mp4malloc(sizeof(mp4_u32) * STSCMAXENTRYCOUNT);
550 if ((*handle)->videoSampleTable->stscSamplesPerChunk == NULL)
551 error = MP4_OUT_OF_MEMORY;
556 (*handle)->videoSampleTable->stscSampleDescriptionIndex = (mp4_u32 *)mp4malloc(sizeof(mp4_u32) * STSCMAXENTRYCOUNT);
557 if ((*handle)->videoSampleTable->stscSampleDescriptionIndex == NULL)
558 error = MP4_OUT_OF_MEMORY;
563 (*handle)->videoSampleTable->stcoChunkOffset = (mp4_u64 *)mp4malloc(sizeof(mp4_u64) * STCOMAXENTRYCOUNT);
564 if ((*handle)->videoSampleTable->stcoChunkOffset == NULL)
565 error = MP4_OUT_OF_MEMORY;
570 (*handle)->videoSampleTable->stssSampleNumber = (mp4_u32 *)mp4malloc(sizeof(mp4_u32) * STSSMAXENTRYCOUNT);
571 if ((*handle)->videoSampleTable->stssSampleNumber == NULL)
572 error = MP4_OUT_OF_MEMORY;
577 (*handle)->videoSampleTable->sdtpSampleDependency = (mp4_u8 *)mp4malloc(sizeof(mp4_u8) * SDTPMAXENTRYCOUNT);
578 if ((*handle)->videoSampleTable->sdtpSampleDependency == NULL)
579 error = MP4_OUT_OF_MEMORY;
582 // register for stblib use
585 if (openStdlib() != MP4_OK)
593 if(!(*handle)->bufferWrite)
598 if ((*handle)->diskWriteBuf)
599 mp4free((*handle)->diskWriteBuf);
601 if ((*handle)->videoSampleTable)
603 if ((*handle)->videoSampleTable->stssSampleNumber)
604 mp4free((*handle)->videoSampleTable->stssSampleNumber);
606 if ((*handle)->videoSampleTable->stcoChunkOffset)
607 mp4free((*handle)->videoSampleTable->stcoChunkOffset);
609 if ((*handle)->videoSampleTable->stscSampleDescriptionIndex)
610 mp4free((*handle)->videoSampleTable->stscSampleDescriptionIndex);
612 if ((*handle)->videoSampleTable->stscSamplesPerChunk)
613 mp4free((*handle)->videoSampleTable->stscSamplesPerChunk);
615 if ((*handle)->videoSampleTable->stscFirstChunk)
616 mp4free((*handle)->videoSampleTable->stscFirstChunk);
618 if ((*handle)->videoSampleTable->stszEntrySize)
619 mp4free((*handle)->videoSampleTable->stszEntrySize);
621 if ((*handle)->videoSampleTable->sttsSampleCount)
622 mp4free((*handle)->videoSampleTable->sttsSampleCount);
624 if ((*handle)->videoSampleTable->sttsSampleDelta)
625 mp4free((*handle)->videoSampleTable->sttsSampleDelta);
627 if ((*handle)->videoSampleTable->sdtpSampleDependency)
628 mp4free((*handle)->videoSampleTable->sdtpSampleDependency);
630 mp4free((*handle)->videoSampleTable);
633 if ((*handle)->audioSampleTable)
635 if ((*handle)->audioSampleTable->stcoChunkOffset)
636 mp4free((*handle)->audioSampleTable->stcoChunkOffset);
638 if ((*handle)->audioSampleTable->stscSampleDescriptionIndex)
639 mp4free((*handle)->audioSampleTable->stscSampleDescriptionIndex);
641 if ((*handle)->audioSampleTable->stscSamplesPerChunk)
642 mp4free((*handle)->audioSampleTable->stscSamplesPerChunk);
644 if ((*handle)->audioSampleTable->stscFirstChunk)
645 mp4free((*handle)->audioSampleTable->stscFirstChunk);
647 if ((*handle)->audioSampleTable->stszEntrySize)
648 mp4free((*handle)->audioSampleTable->stszEntrySize);
650 if ((*handle)->audioSampleTable->sttsSampleDelta)
651 mp4free((*handle)->audioSampleTable->sttsSampleDelta);
653 if ((*handle)->audioSampleTable->sttsSampleCount)
654 mp4free((*handle)->audioSampleTable->sttsSampleCount);
656 mp4free((*handle)->audioSampleTable);
659 if ((*handle)->fileName)
660 mp4free((*handle)->fileName);
668 (*handle)->audioSampleTable->sttsMaxEntryCount = STTSMAXENTRYCOUNT;
669 (*handle)->audioSampleTable->stszMaxSampleCount = STSZMAXSAMPLECOUNT;
670 (*handle)->audioSampleTable->stscMaxEntryCount = STSCMAXENTRYCOUNT;
671 (*handle)->audioSampleTable->stcoMaxEntryCount = STCOMAXENTRYCOUNT;
672 (*handle)->audioSampleTable->stcoNeed64Bits = EFalse;
674 (*handle)->videoSampleTable->sttsMaxEntryCount = STTSMAXENTRYCOUNT;
675 (*handle)->videoSampleTable->stszMaxSampleCount = STSZMAXSAMPLECOUNT;
676 (*handle)->videoSampleTable->stscMaxEntryCount = STSCMAXENTRYCOUNT;
677 (*handle)->videoSampleTable->stcoMaxEntryCount = STCOMAXENTRYCOUNT;
678 (*handle)->videoSampleTable->stssMaxEntryCount = STSSMAXENTRYCOUNT;
679 (*handle)->videoSampleTable->sdtpMaxEntryCount = SDTPMAXENTRYCOUNT;
680 (*handle)->videoSampleTable->stcoNeed64Bits = EFalse;
682 (*handle)->type = type;
684 /* Check if a 3GPP2 codec is being used */
685 if ((*handle)->type & MP4_TYPE_QCELP_13K)
687 (*handle)->generate3G2 = MP4TRUE;
693 extern EXPORT_C MP4Err MP4ComposeClose(MP4Handle apihandle)
695 PRINT((_L("e_composeclose 1")));
698 MP4HandleImp handle = (MP4HandleImp)apihandle;
703 PRINT((_L("e_composeclose_writedatatofile 1")));
705 error = writeDataToFile(handle);
707 PRINT((_L("e_composeclose_writedatatofile 0")));
709 if(handle->bufferWrite && handle->composedSize)
711 *(handle->composedSize)=handle->bytesProgressed;
714 PRINT((_L("e_composeclose_free_decspecinfos 1")));
715 if (handle->videoDecSpecificInfo)
716 mp4free(handle->videoDecSpecificInfo);
718 if (handle->audioDecSpecificInfo)
719 mp4free(handle->audioDecSpecificInfo);
721 PRINT((_L("e_composeclose_free_decspecinfos 0")));
722 PRINT((_L("e_composeclose_free_audiosampletables 1")));
724 if (handle->audioSampleTable && handle->audioSampleTable->sttsSampleCount)
725 mp4free(handle->audioSampleTable->sttsSampleCount);
727 if (handle->audioSampleTable && handle->audioSampleTable->sttsSampleDelta)
728 mp4free(handle->audioSampleTable->sttsSampleDelta);
730 if (handle->audioSampleTable && handle->audioSampleTable->stszEntrySize)
731 mp4free(handle->audioSampleTable->stszEntrySize);
733 if (handle->audioSampleTable && handle->audioSampleTable->stscFirstChunk)
734 mp4free(handle->audioSampleTable->stscFirstChunk);
736 if (handle->audioSampleTable && handle->audioSampleTable->stscSamplesPerChunk)
737 mp4free(handle->audioSampleTable->stscSamplesPerChunk);
739 if (handle->audioSampleTable && handle->audioSampleTable->stscSampleDescriptionIndex)
740 mp4free(handle->audioSampleTable->stscSampleDescriptionIndex);
742 if (handle->audioSampleTable && handle->audioSampleTable->stcoChunkOffset)
743 mp4free(handle->audioSampleTable->stcoChunkOffset);
745 if (handle->audioSampleTable)
746 mp4free(handle->audioSampleTable);
747 PRINT((_L("e_composeclose_free_audiosampletables 0")));
748 PRINT((_L("e_composeclose_free_videosampletables 1")));
750 if (handle->videoSampleTable && handle->videoSampleTable->sttsSampleCount)
751 mp4free(handle->videoSampleTable->sttsSampleCount);
753 if (handle->videoSampleTable && handle->videoSampleTable->sttsSampleDelta)
754 mp4free(handle->videoSampleTable->sttsSampleDelta);
756 if (handle->videoSampleTable && handle->videoSampleTable->stszEntrySize)
757 mp4free(handle->videoSampleTable->stszEntrySize);
759 if (handle->videoSampleTable && handle->videoSampleTable->stscFirstChunk)
760 mp4free(handle->videoSampleTable->stscFirstChunk);
762 if (handle->videoSampleTable && handle->videoSampleTable->stscSamplesPerChunk)
763 mp4free(handle->videoSampleTable->stscSamplesPerChunk);
765 if (handle->videoSampleTable && handle->videoSampleTable->stscSampleDescriptionIndex)
766 mp4free(handle->videoSampleTable->stscSampleDescriptionIndex);
768 if (handle->videoSampleTable && handle->videoSampleTable->stcoChunkOffset)
769 mp4free(handle->videoSampleTable->stcoChunkOffset);
771 if (handle->videoSampleTable && handle->videoSampleTable->stssSampleNumber)
772 mp4free(handle->videoSampleTable->stssSampleNumber);
774 if (handle->videoSampleTable && handle->videoSampleTable->sdtpSampleDependency)
775 mp4free(handle->videoSampleTable->sdtpSampleDependency);
777 if (handle->videoSampleTable)
778 mp4free(handle->videoSampleTable);
779 PRINT((_L("e_composeclose_free_videosampletables 0")));
780 PRINT((_L("e_composeclose_free_dskbuf_and_udta 1")));
781 if (handle->diskWriteBuf)
782 mp4free(handle->diskWriteBuf);
784 if (handle->moovUDTA)
786 if ( handle->moovUDTA->contentdata )
788 mp4free(handle->moovUDTA->contentdata);
790 mp4free(handle->moovUDTA);
792 if (handle->audioUDTA)
794 if ( handle->audioUDTA->contentdata )
796 mp4free(handle->audioUDTA->contentdata);
798 mp4free(handle->audioUDTA);
800 if (handle->videoUDTA)
802 if ( handle->videoUDTA->contentdata )
804 mp4free(handle->videoUDTA->contentdata);
806 mp4free(handle->videoUDTA);
808 PRINT((_L("e_composeclose_free_dskbuf_and_udta 0")));
810 if (!(handle->flags & MP4_FLAG_METADATALAST))
814 PRINT((_L("e_composeclose_close_temp_files 1")));
815 closeTmpFile(handle);
816 PRINT((_L("e_composeclose_close_temp_files 0")));
817 PRINT((_L("e_composeclose_del_temp_files 1")));
818 deleteTmpFile(handle);
819 PRINT((_L("e_composeclose_del_temp_files 0")));
823 PRINT((_L("e_composeclose_free_temp_filename 1")));
824 freeTmpFileName(handle->tmpFileName);
825 PRINT((_L("e_composeclose_free_temp_filename 0")));
827 if (handle->flags & MP4_FLAG_LONGCLIP)
829 PRINT((_L("e_composeclose_close_metadata_files 1")));
830 closeMetaDataFiles(handle);
831 PRINT((_L("e_composeclose_close_metadata_files 0")));
832 PRINT((_L("e_composeclose_del_metadata_files 1")));
833 if (deleteMetaDataFiles(handle) < 0)
835 PRINT((_L("e_composeclose_del_metadata_files 0")));
838 PRINT((_L("e_composeclose_free_filename 1")));
839 if (handle->fileName)
840 mp4free(handle->fileName);
841 PRINT((_L("e_composeclose_free_filename 0")));
843 PRINT((_L("e_composeclose_close_file 1")));
846 PRINT((_L("e_composeclose_close_file 0")));
848 PRINT((_L("e_composeclose_close_std_lib 1")));
850 PRINT((_L("e_composeclose_close_std_lib 0")));
852 if (handle->file32Duplicate)
853 ((RFile64*)handle->file32Duplicate)->Close();
855 PRINT((_L("e_composeclose_free_handle 1")));
861 PRINT((_L("e_composeclose_free_handle 0")));
864 return MP4_METADATA_ERROR;
866 PRINT((_L("e_composeclose 0")));
870 extern EXPORT_C MP4Err MP4ComposeAddVideoDescription(MP4Handle apihandle,
877 MP4HandleImp handle = (MP4HandleImp)apihandle;
880 handle->videoTimeScale = timescale;
884 handle->videoWidth = width;
885 handle->videoHeight = height;
886 handle->videoMaxBitrate = maxbitrate;
887 handle->videoAvgBitrate = avgbitrate;
889 if ( handle->type & (MP4_TYPE_H263_PROFILE_0 | MP4_TYPE_H263_PROFILE_3) )
891 // default H.263 level is 10; it may be overwritten by MP4ComposeWriteVideoDecoderSpecificInfo
892 handle->videoLevel = 10;
895 /* Write FTYP and media data size & type if meta data flag is set */
896 if (handle->flags & MP4_FLAG_METADATALAST)
898 if (handle->ftypWritten != MP4TRUE)
900 if (writeFTYPAndMDATToFile(handle) < 0)
908 extern EXPORT_C MP4Err MP4ComposeAddAudioDescription(MP4Handle apihandle,
910 mp4_u8 audioFramesPerSample,
913 MP4HandleImp handle = (MP4HandleImp)apihandle;
915 PRINT((_L("3GPMP4Lib::MP4ComposeAddAudioDescription in. TimeScale=%d, AudioFrames=%d, ModeSet=%d "), timescale, audioFramesPerSample, modeSet));
918 if (timescale > (mp4_u32)0xffff)
922 handle->audioTimeScale = timescale;
923 PRINT((_L("3GPMP4Lib::MP4ComposeAddAudioDescription in. TimeScale set to = %d"), handle->audioTimeScale ));
929 if ((handle->type & MP4_TYPE_AMR_NB) ||
930 (handle->type & MP4_TYPE_AMR_WB)) /* Audio is AMR */
932 if (audioFramesPerSample)
933 handle->audioFramesPerSample = audioFramesPerSample;
938 handle->audioModeSet = modeSet;
942 else if (handle->type & MP4_TYPE_QCELP_13K) /* Audio is QCELP 13K */
944 if (audioFramesPerSample)
945 handle->audioFramesPerSample = audioFramesPerSample;
949 else /* MPEG AAC audio */
951 handle->audioFramesPerSample = 1;
954 /* Write FTYP and media data size & type if meta data flag is set */
955 if (handle->flags & MP4_FLAG_METADATALAST)
957 if (handle->ftypWritten != MP4TRUE)
959 if (writeFTYPAndMDATToFile(handle) < 0)
964 PRINT((_L("3GPMP4Lib::MP4ComposeAddAudioDescription out")));
968 extern EXPORT_C MP4Err MP4ComposeWriteVideoFrame(MP4Handle apihandle,
974 MP4HandleImp handle = (MP4HandleImp)apihandle;
975 PRINT((_L("3GPMP4Lib::MP4ComposeWriteVideoFrame in")));
976 if (handle->videoTimeScale == 0)
977 return MP4_TIMESCALE_NOT_SET;
982 handle->videoDuration += duration;
983 handle->videoSampleNum++;
985 if (updateVideoMetaData(handle, framesize, duration, keyframe) < 0)
988 if (handle->flags & MP4_FLAG_METADATALAST)
990 if (writeFile(handle, buffer, framesize) < 0)
993 handle->mediaDataBytes += framesize;
997 if (writeTmpFile(handle, buffer, framesize) < 0)
1001 PRINT((_L("3GPMP4Lib::MP4ComposeWriteVideoFrame out")));
1005 extern EXPORT_C MP4Err MP4ComposeWriteAudioFrames(MP4Handle apihandle,
1007 mp4_u32 bytestowrite,
1008 mp4_u32 numberofframes,
1011 MP4HandleImp handle = (MP4HandleImp)apihandle;
1012 PRINT((_L("3GPMP4Lib::MP4ComposeWriteAudioFrames in")));
1014 if ( handle->audioTimeScale == 0)
1016 PRINT((_L("3GPMP4Lib::MP4ComposeWriteAudioFrames Error 14 - AudioTimeScale is 0")));
1017 return MP4_TIMESCALE_NOT_SET;
1020 PRINT((_L("3GPMP4Lib::MP4ComposeWriteAudioFrames Audio Timescale ok")));
1022 if (bytestowrite == 0)
1025 handle->audioDuration += duration;
1026 handle->audioFrameCount += numberofframes;
1027 handle->audioMediaDataSize += bytestowrite;
1029 PRINT((_L("3GPMP4Lib::MP4ComposeWriteAudioFrames updating audio metadata")));
1031 if (updateAudioMetaData(handle, bytestowrite, duration) < 0)
1034 if (handle->flags & MP4_FLAG_METADATALAST)
1036 PRINT((_L("3GPMP4Lib::MP4ComposeWriteAudioFrames writing to file")));
1037 if (writeFile(handle, buffer, bytestowrite) < 0)
1040 handle->mediaDataBytes += bytestowrite;
1044 PRINT((_L("3GPMP4Lib::MP4ComposeWriteAudioFrames writing to temp file")));
1045 if (writeTmpFile(handle, buffer, bytestowrite) < 0)
1049 PRINT((_L("3GPMP4Lib::MP4ComposeWriteAudioFrames out")));
1053 extern EXPORT_C MP4Err MP4ComposeWriteVideoDecoderSpecificInfo(MP4Handle apihandle,
1057 MP4HandleImp handle = (MP4HandleImp)apihandle;
1059 if ( (handle->type & MP4_TYPE_MPEG4_VIDEO) || containsAvcVideo( handle->type ) )
1061 if ((handle->videoDecSpecificInfo = (mp4_u8 *)mp4malloc(infosize)) == NULL)
1064 mp4memcpy(handle->videoDecSpecificInfo, info, infosize);
1065 handle->videoDecSpecificInfoSize = infosize;
1069 // H.263, save level only
1070 if ( *info >= 10 && *info < 100 )
1072 handle->videoLevel = *info;
1078 extern EXPORT_C MP4Err MP4ComposeWriteAudioDecoderSpecificInfo(MP4Handle apihandle,
1082 MP4HandleImp handle = (MP4HandleImp)apihandle;
1084 if ((handle->audioDecSpecificInfo = (mp4_u8 *)mp4malloc(infosize)) == NULL)
1087 mp4memcpy(handle->audioDecSpecificInfo, info, infosize);
1088 handle->audioDecSpecificInfoSize = infosize;
1093 extern EXPORT_C MP4Err MP4ComposeSetFlags(MP4Handle apihandle,
1096 MP4HandleImp handle = (MP4HandleImp)apihandle;
1097 handle->flags |= flags;
1099 if (handle->flags & MP4_FLAG_METADATALAST)
1101 if (handle->tmpfile)
1103 closeTmpFile(handle);
1104 deleteTmpFile(handle);
1108 if (handle->flags & MP4_FLAG_LONGCLIP)
1110 // Open temporary files
1111 MP4Err error = MP4_OK;
1112 error = initMetaDataFiles(handle);
1113 if ( error == MP4_OUT_OF_MEMORY )
1115 return MP4_OUT_OF_MEMORY;
1117 else if ( error != MP4_OK )
1123 handle->generate3G2 = MP4FALSE;
1124 handle->generateMP4 = MP4FALSE;
1126 if (handle->flags & MP4_FLAG_GENERATE_3G2) // 3G2
1128 // Generate 3G2 file.
1129 handle->generate3G2 = MP4TRUE;
1130 handle->generateMP4 = MP4FALSE;
1132 else if ( handle->flags & MP4_FLAG_GENERATE_MP4 ) // MP4
1134 // if at least ONE audio/video codec is specified
1135 if (handle->type != MP4_TYPE_NONE)
1137 /* Check if a 3GPP2 codec is being used */
1138 if ( handle->type & MP4_TYPE_QCELP_13K )
1140 handle->generate3G2 = MP4TRUE;
1141 handle->generateMP4 = MP4FALSE;
1145 // types other than MPEG-4 AUDIO & VIDEO
1146 mp4_u32 type = handle->type >> 2;
1148 // Check if a MPEG4 codec is being used
1149 if (type == MP4_TYPE_NONE)
1151 // if ONLY MPEG4 Video and/or Audio codec is used, generate MP4 file.
1152 handle->generateMP4 = MP4TRUE;
1153 handle->generate3G2 = MP4FALSE;
1157 // ignoring both MPEG-4 audio and video, check again if only AVC codecs are
1160 if ( isAvcVideo(type) )
1162 // generate MP4 file
1163 handle->generateMP4 = MP4TRUE;
1164 handle->generate3G2 = MP4FALSE;
1172 /* Check if a 3GPP2 codec is being used */
1173 if ( handle->type & MP4_TYPE_QCELP_13K )
1175 handle->generate3G2 = MP4TRUE;
1176 handle->generateMP4 = MP4FALSE;
1178 // use defaults -> 3GP
1181 if ( (handle->flags & MP4_FLAG_LARGEFILEBUFFER) && !(handle->bufferWrite) )
1183 TInt bufferSizeError = KErrNone;
1184 bufferSizeError = handle->filewriter->SetOutputBufferSize( CFileWriter::EBufferSizeLarge, apihandle );
1185 if ( bufferSizeError == KErrNoMemory )
1187 return MP4_OUT_OF_MEMORY;
1189 else if ( bufferSizeError != KErrNone )
1197 extern EXPORT_C MP4Err MP4ComposeSetQCELPStorageMode(MP4Handle apihandle, mp4_u8 qcelpStorageMode)
1199 MP4HandleImp handle = (MP4HandleImp)apihandle;
1201 if (qcelpStorageMode)
1202 handle->qcelpStoredAsMPEGAudio = MP4TRUE;
1204 handle->qcelpStoredAsMPEGAudio = MP4FALSE;
1210 extern EXPORT_C MP4Err MP4ComposeSetVideoClipProperties(MP4Handle apihandle, const TVideoClipProperties& aVideoClipProperties)
1212 MP4HandleImp handle = (MP4HandleImp)apihandle;
1220 if ( aVideoClipProperties.iH263Level )
1222 handle->videoLevel = aVideoClipProperties.iH263Level;
1228 extern EXPORT_C MP4Err MP4ComposeSetUserDataAtom(MP4Handle apihandle,
1229 mp4_u8& udtaLocation,
1231 mp4_u32& bufferSize )
1233 userDataAtom* udta = NULL;
1234 MP4HandleImp handle = (MP4HandleImp)apihandle;
1239 if ( buffer == NULL )
1248 // Check which UDTA atom to use
1249 switch ( udtaLocation )
1253 if ( handle->moovUDTA == NULL )
1255 handle->moovUDTA = (userDataAtom *)mp4malloc(sizeof(userDataAtom));
1257 udta = handle->moovUDTA;
1260 case MP4_UDTA_VIDEOTRAK:
1262 if ( handle->videoUDTA == NULL )
1264 handle->videoUDTA = (userDataAtom *)mp4malloc(sizeof(userDataAtom));
1266 udta = handle->videoUDTA;
1269 case MP4_UDTA_AUDIOTRAK:
1271 if ( handle->audioUDTA == NULL )
1273 handle->audioUDTA = (userDataAtom *)mp4malloc(sizeof(userDataAtom));
1275 udta = handle->audioUDTA;
1280 return MP4_INVALID_TYPE;
1286 return MP4_OUT_OF_MEMORY;
1289 // CHECK if there is old data in UDTA
1290 if ( udta->contentdata && udta->atomcontentsize )
1293 if ((temp = (mp4_u8 *)mp4malloc(bufferSize + udta->atomcontentsize)) == NULL)
1295 return MP4_OUT_OF_MEMORY;
1297 mp4memcpy(temp, udta->contentdata, udta->atomcontentsize);
1298 mp4memcpy(temp+udta->atomcontentsize, buffer, bufferSize);
1299 mp4free(udta->contentdata);
1300 udta->contentdata = temp;
1301 udta->atomcontentsize += bufferSize;
1305 if ((udta->contentdata = (mp4_u8 *)mp4malloc(bufferSize)) == NULL)
1306 return MP4_OUT_OF_MEMORY;
1307 // Copy data from buffer to atom contentdata
1308 mp4memcpy(udta->contentdata, buffer, bufferSize);
1309 udta->atomcontentsize = bufferSize;
1316 extern EXPORT_C MP4Err MP4SetCustomFileBufferSizes( MP4Handle apihandle,
1317 mp4_u32 mediaWriteBufferSize,
1318 mp4_u32 writeBufferMaxCount,
1319 mp4_u32 readBufferSize )
1321 MP4HandleImp handle = (MP4HandleImp)apihandle;
1326 // If no specific file size is given we try to use an 'optimal' buffer size.
1327 if (readBufferSize == 0)
1329 readBufferSize = RecommendedBufferSize(handle);
1332 if (readBufferSize > handle->readBufferSize)
1334 handle->readBufferSize = readBufferSize;
1335 if (handle->diskReadBuf)
1337 mp4free(handle->diskReadBuf);
1338 handle->diskReadBuf = NULL;
1339 if ((handle->diskReadBuf = (mp4_u8 *)mp4malloc(handle->readBufferSize)) == NULL)
1341 return MP4_OUT_OF_MEMORY;
1346 // Media Write buffer size
1347 if ( (mediaWriteBufferSize) &&
1348 (mediaWriteBufferSize != handle->mediaWriteBufferSize) &&
1349 (!(handle->bufferWrite)) )
1351 handle->mediaWriteBufferSize = mediaWriteBufferSize;
1352 if ( (handle->filewriter) )
1354 if ( handle->filewriter->SetOutputBufferSize( CFileWriter::EBufferSizeCustom, apihandle ) < 0 )
1361 // Write Buffer Max Count change
1362 if ( (writeBufferMaxCount) &&
1363 (writeBufferMaxCount != handle->writeBufferMaxCount) &&
1364 !(handle->bufferWrite) )
1366 if ( writeBufferMaxCount >= 6 ) // min number of buffers is 4, +1 for soft limit, +1 for hardlimit = 6
1368 handle->writeBufferMaxCount = writeBufferMaxCount;
1369 if ( (handle->filewriter) )
1371 handle->filewriter->SetOutputBufferCount( apihandle );
1382 extern EXPORT_C MP4Err MP4ComposeWriteNextVideoFrameDependencies(MP4Handle apihandle, mp4_u8 aDependsOn, mp4_u8 aIsDependentOn, mp4_u8 aHasRedundancy)
1384 MP4HandleImp handle = (MP4HandleImp)apihandle;
1386 if( (aDependsOn > 2) || (aIsDependentOn > 2) || (aHasRedundancy > 2) )
1391 if (updateVideoDependencyMetaData(handle, aDependsOn, aIsDependentOn, aHasRedundancy) < 0)
1399 extern EXPORT_C MP4Err MP4ComposeSetTempFileRemoverObserver(
1400 MP4Handle *apihandle,
1401 M3GPMP4LibAsyncTempFileRemoverObserver *aObserver)
1403 PRINT((_L("3GPMP4Lib::MP4ComposeSetTempFileRemoverObserver in")));
1405 MP4Err error = MP4_OK;
1406 MP4HandleImp* handle = (MP4HandleStruct **)apihandle;
1408 if ( handle != NULL && *handle != NULL )
1410 (*handle)->tempFileRemoverObserver = aObserver;
1418 PRINT((_L("3GPMP4Lib::MP4ComposeSetTempFileRemoverObserver out error=%d"), error));