/*
 *               Copyright (C) 2006, BroadOn Communications Corp.
 *
 *  These coded instructions, statements, and computer programs contain
 *  unpublished  proprietary information of BroadOn Communications Corp.,
 *  and  are protected by Federal copyright law. They may not be disclosed
 *  to  third  parties or copied or duplicated in any form, in whole or in
 *  part, without the prior written consent of BroadOn Communications Corp.
 *
 */

#ifndef __NC_MP3UTIL_H_
#define __NC_MP3UTIL_H_

#include <sc/ios.h>

#define GET_FIELD(x,NAME)  (((x)&(NAME##_MASK))>>(NAME##_SHIFT))
#define PUT_FIELD(x,NAME)  (((x)<<(NAME##_SHIFT))&(NAME##_MASK))

/* MP3 header info */

#define MP3_HEADER_SYNC_SHIFT     20
#define MP3_HEADER_SYNC_MASK      0xfff00000
#define MP3_HEADER_ID_SHIFT       19
#define MP3_HEADER_ID_MASK        0x00080000
#define MP3_HEADER_LAYER_SHIFT    17     
#define MP3_HEADER_LAYER_MASK     0x00060000    
#define MP3_HEADER_PROT_SHIFT     16    
#define MP3_HEADER_PROT_MASK      0x00010000    
#define MP3_HEADER_BITRATE_SHIFT  12      
#define MP3_HEADER_BITRATE_MASK   0x0000f000      
#define MP3_HEADER_FREQ_SHIFT     10   
#define MP3_HEADER_FREQ_MASK      0x00000c00   
#define MP3_HEADER_PAD_SHIFT      9  
#define MP3_HEADER_PAD_MASK       0x00000200  
#define MP3_HEADER_PRIVACY_SHIFT  8   
#define MP3_HEADER_PRIVACY_MASK   0x00000100   
#define MP3_HEADER_MODE_SHIFT     6   
#define MP3_HEADER_MODE_MASK      0x000000c0   
#define MP3_HEADER_MODEEXT_SHIFT  4      
#define MP3_HEADER_MODEEXT_MASK   0x00000030      
#define MP3_HEADER_COPY_SHIFT     3   
#define MP3_HEADER_COPY_MASK      0x00000008   
#define MP3_HEADER_HOME_SHIFT     2   
#define MP3_HEADER_HOME_MASK      0x00000004   
#define MP3_HEADER_EMPHASIS_SHIFT 2      
#define MP3_HEADER_EMPHASIS_MASK  0x00000003      

#define MPEG_VERSION_1    0
#define MPEG_VERSION_2    1
#define MPEG_BITRATE_FREE 0
#define MPEG_LAYER_1      0
#define MPEG_LAYER_2      1
#define MPEG_LAYER_3      2
#define MPEG_MODE_STEREO  0
#define MPEG_MODE_JOINT   1
#define MPEG_MODE_DUAL    2
#define MPEG_MODE_MONO    3

typedef struct
{
    u32 head;
    u32 sync : 12;
    u32 id;
    u32 layer;
    u32 prot: 1;
    u32 bitrate;
    u32 freq;
    u32 pad: 1;
    u32 privacy: 1;
    u32 mode: 2;
    u32 modeExt: 2;
    u32 copy: 1;
    u32 home: 1;
    u32 emphasis: 2;
    u32 valid;
    u32 len;
} Mp3Info;

/* Xing header info */

#define XING_FRAMES_FLAG  0x1
#define XING_BYTES_FLAG   0x2
#define XING_TOC_FLAG     0x4
#define XING_QUALITY_FLAG 0x8

typedef struct 
{
    u32 valid;
    u32 id;
    u32 flags;
    u32 nFrames;
    u32 nBytes;
    u8 toc[100];
    u32 quality;
} XingInfo;

/* VBRI header info */

typedef struct 
{
    u32 valid;
    u32 id;
    u16 version;
    u16 delay;
    u16 quality;
    u32 nBytes;
    u32 nFrames;
    u16 tocEntries;
    u16 tocScale;
    u16 tocEntrySize;
    u16 tocEntryFrames;
    u8 toc[100];
} VbriInfo;

/* APE tag info */

#define APE_FLAGS_HEADER_PRESENT_SHIFT  31
#define APE_FLAGS_HEADER_PRESENT_MASK   0x80000000
#define APE_FLAGS_FOOTER_ABSENT_SHIFT   30
#define APE_FLAGS_FOOTER_ABSENT_MASK    0x40000000
#define APE_FLAGS_IS_HEADER_SHIFT       29
#define APE_FLAGS_IS_HEADER_MASK        0x20000000

/* function declarations */

int mp3FrameTime(Mp3Info *mp3i);

int mp3FrameSize(Mp3Info *mp3i);
int mp3IsValid(Mp3Info *mp3i);
int mp3IsSupported(Mp3Info *mp3i);
int mp3ParseHeader(u32 header, Mp3Info *mp3i);
void mp3PrintHeader(Mp3Info *mp3i);
int mp3ReadFrame(IOSFd fd, Mp3Info *mp3i, u32 *encBuf);
IOSError mp3FindFrame(IOSFd fd, s32 start, s32 end, u32 template, u32 mask, u32 *header);
IOSError mp3GetFreeBitrate(IOSFd fd, Mp3Info *mp3i, s32 start, s32 end);
IOSError mp3Resync(IOSFd fd, Mp3Info *mp3i, s32 start, s32 end, u32 mask);
IOSError mp3FindEnd(IOSFd fd, Mp3Info *mp3i);
IOSError mp3FindStart(IOSFd fd, Mp3Info *mp3i);
IOSError mp3FindXing(IOSFd fd, s32 start, s32 end, Mp3Info *mp3i, XingInfo *xi);
IOSError mp3FindVbri(IOSFd fd, s32 start, s32 end, Mp3Info *mp3i, VbriInfo *xi);
s32 mp3XingTimeToOffset(u32 time, u32 duration, s32 start, s32 end, XingInfo *xi);
s32 mp3VbriTimeToOffset(u32 time, u32 duration, s32 start, s32 end, VbriInfo *vi);
IOSError mp3FindStandaloneFrame(IOSFd fd, Mp3Info *mp3i, s32 start, s32 end, u32 mask, u32 *skipped);
IOSError mp3ResyncMainData(IOSFd fd, Mp3Info *mp3i, s32 start, s32 end, u32 mask);
IOSError mp3LibRmLoop(void *arg);
IOSError mp3LibStopRm(IOSMessageQueueId mq);

/* debugging macros */
/* #define MP3_DEBUG */

#ifdef MP3_DEBUG
 #define MP3_LOG(...) printf("MP3: " __VA_ARGS__)
 #define MP3_LOG_CONT(...) printf(__VA_ARGS__)
 #define MP3_ERROR(msg) printf("MP3: Error: %s failed\n", msg)
#else
 #define MP3_LOG(...)
 #define MP3_LOG_CONT(...)
 #ifdef MP3_DEBUG_ERROR
  #define MP3_ERROR(msg) printf("MP3: Error: %s failed\n", msg)
 #else
  #define MP3_ERROR(...)
 #endif
#endif

#endif
