First public contribution.
1 // Copyright (c) 2008-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.
20 #include <f32file64.h>
21 #include <caf/content.h>
23 #include <3gplibrary/mp4lib.h>
26 Error code for C3GPParse API
28 3GP Parser requires more data before the requested information can be retrieved
30 const TInt KErr3gpLibMoreDataRequired = (-12300);
32 class M3GPParseCallback;
35 File format to use when composing a file.
39 enum T3GPFileFormatType
41 /** 3GP file format. */
43 /** MPEG-4 file format. */
45 /** 3GPP2 file format. */
50 Optional flags to be used when composing a file.
56 /** Default value. No special setup. */
58 /** Metadata is placed at end of file. */
59 E3GPMetaDataLast = 0x0001,
60 /** Collect metadata in files instead of storing it in memory. */
61 E3GPLongClip = 0x0002,
65 Type of stream contained in the file.
71 /** Frame contains audio data. */
73 /** Frame contains video data. */
78 Type of video stream contained in the file.
86 /** MPEG-4 video (MPEG-4 Part 2) - Simple and Advance Simple Profiles */
92 /** AVC (also known as H.264) Baseline Profile */
93 E3GPAvcProfileBaseline,
94 /** AVC (also known as H.264) Main Profile */
96 /** AVC (also known as H.264) Extended Profile */
97 E3GPAvcProfileExtended,
98 /** AVC (also known as H.264) High Profile */
104 Type of audio stream contained in the file.
112 /** MPEG-4 audio (MPEG-4 Part 3) */
124 Location of the user data in the file.
128 enum T3GPUdtaLocation
130 /** User data resides within the Movie Box. */
132 /** User data resides within the Track Box for video. */
134 /** User data resides within the Track Box for audio. */
139 Storage method of QCELP audio stream.
143 enum T3GPQcelpStorageMode
145 /** QCELP Sample Entry ('sqcp') box is used for the storage of QCELP media data. */
146 E3GPQcelpSampleEntryBox,
147 /** MP4 Audio Sample Entry ('esds') box is used for the storage of QCELP media data. */
148 E3GPMP4AudioDescriptionBox
152 Define the dependency information between video frames.
156 enum T3GPVideoFrameDependency
158 /** The dependency of this sample is unknown or the dependency of other
159 samples on this sample is unknown. */
160 E3GPDependencyUnknown = 0,
161 /** This sample does depend on others or other samples depend on this sample. */
162 E3GPDependencyExists,
163 /** This sample does not depend on others or no other sample depends on this sample. */
168 Define the existence of redundant coding in a frame.
172 enum T3GPVideoFrameRedundancy
174 /** It is unknown whether there is redundant coding in this sample. */
175 E3GPRedundancyUnknown = 0,
176 /** There is redundant coding in this sample. */
177 E3GPRedundancyExists,
178 /** There is no redundant coding in this sample. */
183 Structure containing a frame's dependency information.
187 NONSHARABLE_STRUCT(T3GPFrameDependencies)
189 /** Defines the current frame’s dependency. It can be defined as an I-frame
190 (E3GPDependencyNone), not an I-frame (E3GPDependencyExists), or dependency
191 unknown (E3GPDependencyUnknown). See T3GPVideoFrameDependency.
193 T3GPVideoFrameDependency iDependsOn;
194 /** Defines the dependency of other frames on this frame. It can be defined as a disposable
195 frame (E3GPDependencyNone), non-disposable (E3GPDependencyExists), or dependency unknown
196 (E3GPDependencyUnknown). See T3GPVideoFrameDependency.
198 T3GPVideoFrameDependency iIsDependedOn;
199 /** Defines the existence of redundant coding in this frame. See T3GPVideoFrameRedundancy. */
200 T3GPVideoFrameRedundancy iHasRedundancy;
204 Structure containing video frame properties.
208 NONSHARABLE_STRUCT(T3GPFrameInfoParameters)
212 /** Frame start time. */
214 /** ETrue if frame is an intra or random access point. */
215 TBool iIsRandomAccessPoint;
219 This class should not be used directly. It stores common properties amongst all supported
222 Note: iTimescale should be chosen so that the duration of each sample is an integer. It should also
223 be chosen so that the duration of the presentation fits into 32 bits.
228 NONSHARABLE_CLASS(T3GPVideoPropertiesBase)
234 T3GPVideoPropertiesBase(T3GPVideoType aType, TUint aTimescale, const TSize& aSize);
236 /** Type of video stream contained in the file. */
238 /** Timescale of the video data. This is the number of time units that pass in one second. */
240 /** Video width and height in pixels */
245 This structure stores the common and MPEG-4 video specific properties of video data
249 NONSHARABLE_CLASS(T3GPVideoPropertiesMpeg4Video) : public T3GPVideoPropertiesBase
252 IMPORT_C T3GPVideoPropertiesMpeg4Video(TUint aTimescale, const TSize& aSize, TUint aMaxBitRate, TUint aAvgBitRate, const TDesC8& aDecoderSpecificInfo);
254 /** Maximum video bit rate */
256 /** Average video bit rate */
258 /** MPEG-4 video decoder specific info data stored in an ESDS atom */
259 const TDesC8& iDecoderSpecificInfo;
263 This structure stores the common and H.263 specific properties of video data
267 NONSHARABLE_CLASS(T3GPVideoPropertiesH263) : public T3GPVideoPropertiesBase
270 enum TProfile { EProfile0, EProfile3 };
273 IMPORT_C T3GPVideoPropertiesH263(TUint aTimescale, const TSize& aSize, TInt aVideoLevel, TProfile aProfile);
275 /** Indicates the H263 video level. */
280 This structure stores the common and AVC-specific properties of video data.
284 NONSHARABLE_CLASS(T3GPVideoPropertiesAvc) : public T3GPVideoPropertiesBase
287 IMPORT_C T3GPVideoPropertiesAvc(TUint aTimescale, const TSize& aSize, const TDesC8& aDecoderSpecificInfo);
289 /** AVCDecoderConfigurationRecord data that will be stored in the avcC atom. */
290 const TDesC8& iDecoderSpecificInfo;
294 This class should not be used directly. This structure stores common properties amongst all supported types of audio data
298 NONSHARABLE_CLASS(T3GPAudioPropertiesBase)
304 T3GPAudioPropertiesBase(T3GPAudioType aType, TUint aTimescale, TInt aFramesPerSample);
306 /** Type of audio stream contained in the file */
308 /** Timescale of the audio data. This is the number of time units that pass in one second. It
309 must be smaller than 65536. */
311 /** Frames per sample. It must be smaller than 512. MPEG-4 audio has a fixed value of 1. */
312 TInt iFramesPerSample;
316 This structure stores the common and MPEG-4 audio specific properties of audio data
320 NONSHARABLE_CLASS(T3GPAudioPropertiesMpeg4Audio) : public T3GPAudioPropertiesBase
323 IMPORT_C T3GPAudioPropertiesMpeg4Audio(TUint aTimescale, const TDesC8& aDecoderSpecificInfo);
325 /** MPEG-4 audio decoder specific info data stored in an ESDS atom */
326 const TDesC8& iDecoderSpecificInfo;
330 This structure stores the common and AMR specific properties of audio data
334 NONSHARABLE_CLASS(T3GPAudioPropertiesAmr) : public T3GPAudioPropertiesBase
337 enum TSpeechCodec{ EAmrWB, EAmrNB };
340 IMPORT_C T3GPAudioPropertiesAmr(TUint aTimescale, TInt aFramesPerSample, TInt aModeSet, TSpeechCodec aCodec);
347 This structure stores the common and Qcelp specific properties of audio data
351 NONSHARABLE_CLASS(T3GPAudioPropertiesQcelp) : public T3GPAudioPropertiesBase
354 IMPORT_C T3GPAudioPropertiesQcelp(TUint aTimescale, TInt aFramesPerSample, const TDesC8& aDecoderSpecificInfo);
355 IMPORT_C T3GPAudioPropertiesQcelp(TUint aTimescale, TInt aFramesPerSample);
357 /** Specifies the mode to store QCELP audio data.
358 In 3G2 files, QCELP can be registered to be stored in two ways:
359 - Use QCELP Sample Entry (‘sqcp’) Box. This is the default mode.
360 - Use MPEG4 Audio Sample Description (‘esds’) Box. If this mode is selected, decoder specific info should be supplied.
361 See T3GPQcelpStorageMode. */
362 T3GPQcelpStorageMode iMode;
363 /** MPEG-4 audio decoder specific info data stored in an ESDS atom.
364 If this is provided as an input parameter, the storage mode is automatically set to MPEG4 Audio
365 Sample Description Box mode */
366 const TDesC8& iDecoderSpecificInfo;
373 The 3GP composer accepts H.263 /H.264 / MPEG-4 / AVC video, and AMR / MPEG-4 AAC / QCELP
374 audio as input to create a 3GP / 3G2 / MP4 file.
376 NONSHARABLE_CLASS(C3GPCompose) : public CBase
379 IMPORT_C static C3GPCompose* NewL();
380 IMPORT_C static C3GPCompose* NewL(TInt aMediaWriteBufferSize, TInt aWriteBufferMaxCount);
381 IMPORT_C ~C3GPCompose();
383 IMPORT_C TInt Open(T3GPFileFormatType aFileFormat, const T3GPVideoPropertiesBase* aVideo, const T3GPAudioPropertiesBase* aAudio, const TDesC& aFilename, TUint aFlags = E3GPNoFlag);
384 IMPORT_C TInt Open(T3GPFileFormatType aFileFormat, const T3GPVideoPropertiesBase* aVideo, const T3GPAudioPropertiesBase* aAudio, RFile& aFile, TUint aFlags = E3GPNoFlag);
385 IMPORT_C TInt Open(T3GPFileFormatType aFileFormat, const T3GPVideoPropertiesBase* aVideo, const T3GPAudioPropertiesBase* aAudio, RFile64& aFile, TUint aFlags = E3GPNoFlag);
386 IMPORT_C TInt WriteVideoFrame(const TDesC8& aBuffer, TUint aDuration, TBool aKeyFrame);
387 IMPORT_C TInt WriteVideoFrame(const TDesC8& aBuffer, TUint aDuration, TBool aKeyFrame, const T3GPFrameDependencies& aDependencies);
388 IMPORT_C TInt WriteAudioFrames(const TDesC8& aBuffer, TUint aDuration);
389 IMPORT_C TInt SetUserData(T3GPUdtaLocation aLocation, const TDesC8& aBuffer);
390 IMPORT_C TInt Complete();
392 C3GPCompose(TInt aMediaWriteBufferSize = 0, TInt aWriteBufferMaxCount = 0);
394 TInt SymbianOSError(MP4Err aError);
395 mp4_u32 Mp4Type(const T3GPVideoPropertiesBase* aVideo, const T3GPAudioPropertiesBase* aAudio);
396 TInt SetComposeProperties(const T3GPVideoPropertiesBase* aVideo, const T3GPAudioPropertiesBase* aAudio, T3GPFileFormatType aFileFormat, TUint aFlag);
397 TInt SetMPeg4VideoProperties(const T3GPVideoPropertiesBase* aVideo);
398 TInt SetAvcVideoProperties(const T3GPVideoPropertiesBase* aVideo);
399 TInt SetH263VideoProperties(const T3GPVideoPropertiesBase* aVideo);
400 TInt SetMpeg4AudioProperties(const T3GPAudioPropertiesBase* aAudio);
401 TInt SetAmrAudioProperties(const T3GPAudioPropertiesBase* aAudio);
402 TInt SetQcelpAudioProperties(const T3GPAudioPropertiesBase* aAudio);
403 TInt SetComposeFlag(T3GPFileFormatType aFileFormat, TUint aFlag);
405 static void Panic(TInt aPanic);
408 TInt iMediaWriteBufferSize;
409 TInt iWriteBufferMaxCount;
412 TBool iDuplicateFileHandleCreated;
418 The Parser provides APIs to parse 3GP, 3G2 and MP4 contents (from a file, a CAF object, or given
419 through memory buffer), containing H.263/MPEG-4/H.264 video and AMR/MPEG-4/QCELP audio.
423 NONSHARABLE_CLASS(C3GPParse) : public CBase, public M3GPMP4LibAsyncObserver
426 IMPORT_C static C3GPParse* NewL();
427 IMPORT_C static C3GPParse* NewL(TInt aReadBufferSize);
428 IMPORT_C ~C3GPParse();
430 IMPORT_C TInt Open();
431 IMPORT_C TInt Open(const TDesC& aFilename);
432 IMPORT_C TInt Open(const RFile& aFile);
433 IMPORT_C TInt Open(const RFile64& aFile);
434 IMPORT_C TInt Open(const ContentAccess::CData& aData);
435 IMPORT_C TInt InsertData(const TDesC8& aBuffer);
436 IMPORT_C TInt GetVideoProperties(T3GPVideoType& aType, TUint& aLength, TReal& aFrameRate, TUint& aAvgBitRate, TSize& aSize, TUint& aTimeScale) const;
437 IMPORT_C TInt GetAudioProperties(T3GPAudioType& aType, TUint& aLength, TInt& aFramesPerSample, TUint& aAvgBitRate, TUint& aTimeScale) const;
438 IMPORT_C TInt GetContainerProperties(TUint& aSize, TUint& aAvgBitRate) const;
439 IMPORT_C TInt GetNumBufferedBytes(TInt& aNum) const;
440 IMPORT_C TInt GetFrameType(T3GPFrameType& aType) const;
441 IMPORT_C TInt GetVideoFrameSize(TUint& aSize) const;
442 IMPORT_C TInt GetAudioFramesSize(TUint& aSize) const;
443 IMPORT_C TInt GetVideoDecoderSpecificInfoSize(TInt& aSize) const;
444 IMPORT_C TInt GetVideoDecoderSpecificInfo(TDes8& aInfo) const;
445 IMPORT_C TInt GetAudioDecoderSpecificInfoSize(TInt& aSize) const;
446 IMPORT_C TInt GetAudioDecoderSpecificInfo(TDes8& aBuffer) const;
447 IMPORT_C TInt GetVideoTimestamp(TUint& aTimeStampInMs, TUint& aTimeStampInTimescale) const;
448 IMPORT_C TInt GetStreamable(TBool& aStreamable) const;
449 IMPORT_C TInt Seek(TUint aPosition, TBool aKeyFrame, TUint& aAudioPosition, TUint& aVideoPosition) const;
450 IMPORT_C TInt GetFrameAvailability(T3GPFrameType aType, TBool& aAvailable) const;
451 IMPORT_C TInt GetNumberOfVideoFrames(TUint& aNum) const;
452 IMPORT_C TInt GetVideoSampleEntryIndex(TUint& aIndex) const;
453 IMPORT_C TInt GetVideoFrameSize(TUint aIndex, TUint& aSize) const;
454 IMPORT_C TInt GetVideoFrameStartTime(TUint aIndex, TUint& aTimeStampInMs, TUint& aTimeStampInTimescale) const;
455 IMPORT_C TInt GetVideoFrameKeyType(TUint aIndex, TBool& aKeyFrame) const;
456 IMPORT_C TInt GetAudioSampleEntryIndex(TUint& aIndex) const;
457 IMPORT_C TInt GetQcelpStorageMode(T3GPQcelpStorageMode& aMode) const;
458 IMPORT_C TInt GetH263VideoLevel(TInt& aLevel) const;
459 IMPORT_C TInt GetUserDataAtomSize(TUint32 aType, T3GPUdtaLocation aLocation, TUint& aAtomIndex, TInt& aSize) const;
460 IMPORT_C TInt GetUserDataAtom(TUint32 aType, T3GPUdtaLocation aLocation, TDes8& aBuffer, TUint& aAtomIndex) const;
461 IMPORT_C TInt GetVideoFrameDependencies(T3GPFrameDependencies& aDependencies) const;
462 IMPORT_C TInt GetVideoFrameProperties(TUint aStartIndex, TUint aNumberOfFrames, RArray<T3GPFrameInfoParameters>& aArray) const;
463 IMPORT_C TInt ReadVideoFrame(TDes8& aBuffer, TBool& aKeyFrame, TUint& aTimeStampInMs, TUint& aTimeStampInTimescale) const;
464 IMPORT_C void ReadVideoFrame(M3GPParseCallback& aCallback, TDes8& aBuffer);
465 IMPORT_C TInt ReadAudioFrames(TDes8& aBuffer, TInt& aReturnedFrames, TUint& aTimeStampInMs, TUint& aTimeStampInTimescale) const;
466 IMPORT_C void ReadAudioFrames(M3GPParseCallback& aCallback, TDes8& aBuffer);
467 IMPORT_C void CancelReadFrame();
468 IMPORT_C TInt Complete();
470 C3GPParse(TInt aReadBufferSize = 0);
472 TInt SymbianOSError(MP4Err aError) const;
473 TInt DoGetVideoProperties() const;
474 TInt DoGetAudioProperties() const;
475 TInt DoGetStreamProperties() const;
476 T3GPVideoType WrapperVideoType(TUint aType) const;
477 T3GPAudioType WrapperAudioType(TUint aType) const;
478 mp4_u8 UdtaLocation(T3GPUdtaLocation aLocation) const;
480 static void Panic(TInt aPanic);
482 // From M3GPMP4LibAsyncObserver
483 void M3GPMP4LibVideoFrameAvailable(MP4Err aError, mp4_u32 aFrameSize, mp4_u32 aTimeStamp, mp4_bool aKeyFrame, mp4_u32 aTimestamp2);
484 void M3GPMP4LibAudioFramesAvailable(MP4Err aError, mp4_u32 aAudioSize, mp4_u32 aTimeStamp, mp4_u32 aReturnedFrames, mp4_u32 aTimestamp2);
488 TInt iReadBufferSize;
491 M3GPParseCallback* iCallback;
492 TDes8* iAsyncReadBuffer;
495 TBool iDuplicateFileHandleCreated;
498 mutable T3GPVideoType iVideoType;
499 mutable TUint iVideoLength;
500 mutable TUint iVideoMp4Type;
501 mutable TReal iVideoFrameRate;
502 mutable TSize iVideoSize;
503 mutable TUint iVideoTimeScale;
504 mutable TBool iVideoPropertiesSaved;
505 mutable TInt iVideoError;
508 mutable T3GPAudioType iAudioType;
509 mutable TUint iAudioLength;
510 mutable TUint iAudioMp4Type;
511 mutable TReal iAudioFramesPerSample;
512 mutable TSize iAudioSize;
513 mutable TUint iAudioTimeScale;
514 mutable TUint iAudioAvgBitRate;
515 mutable TBool iAudioPropertiesSaved;
516 mutable TInt iAudioError;
519 mutable TUint iStreamAvgBitRate;
520 mutable TUint iStreamSize;
521 mutable TBool iStreamPropertiesSaved;
522 mutable TInt iStreamError;
526 A mixin class to be implemented by the client in order to use the asynchronous frame retrieval
527 APIs provided by the C3GPParse class. The derived class methods are called by C3GPParse when
528 the audio/video frames retrieval completes, successful or not.
532 class M3GPParseCallback
536 This is called upon the completion of an asynchronous request to retrieve audio
537 frames by the API C3GPParse::ReadAudioFramesAsync.
539 @see C3GPParse::ReadAudioFramesAsync
541 @param aError Returns KErrNone on success. Returns KErrAccessDenied if the 3GP Parser is in buffer mode.
542 Otherwise, returns one of the system wide error codes. See synchronous version of
543 C3GPParse::ReadAudioFrames.
544 @param aReturnedFrames Number of the returned frames or 0 if not known.
545 @param aTimeStampInMs Audio frame presentation time in milliseconds from the beginning of the audio sequence.
546 @param aTimeStampInTimescale Audio frame presentation time in timescale from the beginning of the audio sequence.
548 virtual void AudioFramesAvailable(TInt aError, TUint aReturnedFrames, TUint aTimeStampInMs, TUint aTimeStampInTimescale) = 0;
551 VideoFrameAvailable is called upon completion of an asynchronous request to retrieve
552 video frames by the API C3GPParse::ReadVideoFrameAsync.
554 @see C3GPParse::ReadVideoFrameAsync
556 @param aError Returns KErrNone on success. Returns KErrAccessDenied if the 3GP Parser is in buffer mode.
557 Otherwise, returns one of the system wide error codes. See synchronous version of
558 C3GPParse::ReadVideoFrame.
559 @param aKeyFrame Returns ETrue if the current frame is a key frame (intra), otherwise the value is EFalse.
560 @param aTimeStampInMs Video frame presentation time in milliseconds from the beginning of the video sequence.
561 @param aTimeStampInTimescale Video frame presentation time in timescale from the beginning of the video sequence.
563 virtual void VideoFrameAvailable(TInt aError, TBool aKeyFrame, TUint aTimeStampInMs, TUint aTimeStampInTimescale) = 0;
566 #endif // C3GPLIBRARY_H