/*
 *  MEPacket.h
 *  MediaEncoding
 *
 *  Created by Henry Mason on Thu Mar 18 2004.
 *  Copyright (c) 2004 Henry Mason. All rights reserved.
 *
 */

#include  <sys/cdefs.h>

__BEGIN_DECLS

#include <sys/types.h>

enum MEFormatType {
    FORMAT_TYPE_AUDIO,
    FORMAT_TYPE_VIDEO,
};

enum MEPixelAspectRatio {
    PIXEL_ASPECT_RATIO_SQUARE = 0,
    PIXEL_ASPECT_RATIO_CUSTOM = 1,
    PIXEL_ASPECT_RATIO_NTSC_4_3 = 2,
    PIXEL_ASPECT_RATIO_PAL_4_3 = 3,
    PIXEL_ASPECT_RATIO_NTSC_16_9 = 4,
    PIXEL_ASPECT_RATIO_PAL_16_9 = 5,
    PIXEL_ASPECT_RATIO_SVCD_NTSC_4_3 = 6,
    PIXEL_ASPECT_RATIO_SVCD_PAL_4_3 = 7,
    PIXEL_ASPECT_RATIO_SVCD_NTSC_16_9 = 8,
    PIXEL_ASPECT_RATIO_SVCD_PAL_16_9  = 9  
};

#define FRAME_FLAG_IS_INTERLACED        1
#define FRAME_FLAG_TOP_FIELD_FIRST      2
#define FRAME_FLAG_SKIP_THIS_DURATION   4
#define FRAME_FLAG_SOURCE_POS_IS_PTS    8

// This is the largest an audio packet is allowed to be
#define MEMaxAudioPacketSize            393216

typedef struct MEPacketHeader {
    unsigned int    length;
    unsigned int    offset;
	
    int             trackIndex;
    
    unsigned int    timeDuration;
    unsigned int    timeScale; 
	
    int                 formatCode;
    enum MEFormatType   formatType;
    
    // Video Only
    unsigned int    width;
    unsigned int    height;
    unsigned int    rowStride;
    
    enum MEPixelAspectRatio pixelAspectRatioCode;
    int                     pixelAspectRatioWidth;
    int                     pixelAspectRatioHeight;
    
    int             frameFlags;
    
    // Audio Only    
    /*
     Channel layout information:
     1 = C
     2 = L R
     3 = L R C
     4 = L R Ls Rs
     5 = L R C Ls Rs
     6 = L R C LFE Ls Rs
     
     Anything more than that...wait for a newer release
     */
    unsigned int    channels;
    unsigned int    sampleRate;
    
    short           isNotSync;
    
    // Other stuff
    
    void            *extraCodecParams;
    unsigned int    extraCodecParamsLength;
    
    int             endOfStream;
    
    unsigned int    extraFrameInfoOffset;
    unsigned int    extraFrameInfoLength;
    
    long long       frameSourcePosition;
    
    // These two were added to support QT7-style muxing
    
    unsigned int    userFlags;
    long long       timeDuration64;
    long long       timeDurationOffset; // realistically, this could be 32-bit, but for completeness...
    
    // For alignment purposes
    unsigned char   unused[26];
    
} MEPacketHeader;

size_t MEPacketHeaderSize();
size_t MEPacketTotalLengthToCopy(MEPacketHeader *header);
MEPacketHeader *MEAllocPacket(size_t size);
void MEFreePacket(MEPacketHeader *header);
void *MEBytesFromPacket(MEPacketHeader *header);
void  MEAdvanceBytesInPacket(MEPacketHeader *header, unsigned int bytesToAdvance);
void *MEExtraFrameInfoFromPacket(MEPacketHeader *header);

int MEPacketVideoIsPlanar(MEPacketHeader *header);
void *MEPacketVideoYPlaneBytes(MEPacketHeader *header, int *width, int *height, int *rowStride);
void *MEPacketVideoCbPlaneBytes(MEPacketHeader *header, int *width, int *height, int *rowStride);
void *MEPacketVideoCrPlaneBytes(MEPacketHeader *header, int *width, int *height, int *rowStride);

__END_DECLS
