#include "saf_demuxer.h"

static void writeToBitLog(SAFDemux *demux, u32 nbBits, u32 value, unsigned char *type, unsigned char *name)
{
//	if (demux->bitlog) fprintf(demux->bitlog, "   %5d  \t   %2d\t%10d\t     %s\t  %s\n", (u32)BS_GetPosition(demux->bs), nbBits, value, type, name);
}

/*
u8 SAF_Read_SimpleDecoderConfigDescriptor(SAFDemux *demux, saf_sl_packet_header header, simple_decoder_config_descriptor *sdcd)
{
	u64 pos;

	sdcd->objectTypeIndication = BS_ReadInt(demux->bs, 8); writeToBitLog(demux, 8, sdcd->objectTypeIndication, "u8", "objectTypeIndication");
	sdcd->streamType = BS_ReadInt(demux->bs, 8); writeToBitLog(demux, 8, sdcd->streamType, "u8", "streamType");
	sdcd->bufferSizeDB = BS_ReadInt(demux->bs, 16); writeToBitLog(demux, 16, sdcd->bufferSizeDB, "u16", "bufferSizeDB");

	pos = BS_GetPosition(demux->bs);
	if (header.start_offset + header.accessUnitLength > pos) {
		sdcd->decSpecificInfo_length = (u32)(header.start_offset + header.accessUnitLength - pos);
		sdcd->decSpecificInfo_data = malloc(sdcd->decSpecificInfo_length);
		memset(sdcd->decSpecificInfo_data, 0, sdcd->decSpecificInfo_length);
		BS_ReadData(demux->bs, sdcd->decSpecificInfo_data, sdcd->decSpecificInfo_length);
	} else {
		sdcd->decSpecificInfo_length = 0;
		sdcd->decSpecificInfo_data = NULL;
	}
	return 0;
}
*/
/*
u8 SAF_Read_SL_Packet(SAFDemux *demux)
{
	saf_sl_packet_header header;
	u8 reserved;

	header.accessUnitStartFlag = BS_ReadBit(demux->bs); writeToBitLog(demux, 1, header.accessUnitStartFlag, "bool", "accessUnitStartFlag");
	header.accessUnitEndFlag = BS_ReadBit(demux->bs); writeToBitLog(demux, 1, header.accessUnitEndFlag, "bool", "accessUnitEndFlag");
	header.packetSequenceNumber = BS_ReadInt(demux->bs, 14); writeToBitLog(demux, 14, header.packetSequenceNumber, "u14", "packetSequenceNumber");
	reserved = BS_ReadBit(demux->bs); writeToBitLog(demux, 1, reserved, "bool", "reserved");
	header.degradationPriority = BS_ReadInt(demux->bs, 4); writeToBitLog(demux, 4, header.degradationPriority, "u4", "degradationPriority");
	header.randomAccessPointFlag = BS_ReadBit(demux->bs); writeToBitLog(demux, 1, header.randomAccessPointFlag, "bool", "randomAccessPointFlag");
	reserved = BS_ReadBit(demux->bs); writeToBitLog(demux, 1, reserved, "bool", "reserved");
	reserved = BS_ReadBit(demux->bs); writeToBitLog(demux, 1, reserved, "bool", "reserved");
	header.compositionTimeStamp = BS_ReadInt(demux->bs, 32); writeToBitLog(demux, 32, header.compositionTimeStamp, "u32", "compositionTimeStamp");
	header.accessUnitLength = BS_ReadInt(demux->bs, 16); writeToBitLog(demux, 16, header.accessUnitLength, "u16", "accessUnitLength");

	header.start_offset = BS_GetPosition(demux->bs);

	header.accessUnitType = BS_ReadInt(demux->bs, 8); writeToBitLog(demux, 8, header.accessUnitType, "u8", "accessUnitType");
	header.streamID = BS_ReadInt(demux->bs, 8); writeToBitLog(demux, 8, header.streamID, "u8", "streamID");

	switch (header.accessUnitType) {
	case 0x00:
		// reserved AU type
		break;
		{
			simple_decoder_config_descriptor *sdcd = malloc(sizeof(simple_decoder_config_descriptor));
			SAF_Read_SimpleDecoderConfigDescriptor(demux, header, sdcd);
			demux->on_new_header(demux->parent, header.streamID, sdcd);
		}
		break;
	case 0x02:
		demux->on_end_of_scene(demux->parent);
		return 0;
		break;
	case 0x03:
		demux->on_end_of_stream(demux->parent, header.streamID);
		break;
	case 0x01:
	case 0x04:
	case 0x05:
	case 0x06:
		{
			simple_decoder_config_descriptor *sdcd = malloc(sizeof(simple_decoder_config_descriptor));
			SAF_Read_SimpleDecoderConfigDescriptor(demux, header, sdcd);
			demux->on_new_header(demux->parent, header.streamID, sdcd);
		}
		break;
	case 0x07:
		{
			unsigned char *au_data;
			u32 au_length;
			au_length = header.accessUnitLength - 2;
			au_data = malloc(au_length);
			memset(au_data, 0, au_length);
			BS_ReadData(demux->bs, au_data, au_length);
			demux->on_new_au(demux->parent, header.streamID, header.compositionTimeStamp, au_data, au_length);
		}
		break;
	case 0x08:
		break;
	case 0x09:
		{
			unsigned char *au_data;
			u32 au_length;
			au_length = header.accessUnitLength - 2;
			au_data = malloc(au_length);
			memset(au_data, 0, au_length);
			BS_ReadData(demux->bs, au_data, au_length);
			demux->on_new_au(demux->parent, header.streamID, header.compositionTimeStamp, au_data, au_length);
		}
		break;
	case 0x0A:
		{
			unsigned char *au_data;
			u32 au_length;
			au_length = header.accessUnitLength - 2;
			au_data = malloc(au_length);
			memset(au_data, 0, au_length);
			BS_ReadData(demux->bs, au_data, au_length);
			demux->on_new_au(demux->parent, header.streamID, header.compositionTimeStamp, au_data, au_length);
		}
		break;
	case 0x0B:
		break;
	case 0x0C:
		break;
	case 0x0D:
		break;
	case 0x0E:
		break;
	case 0x0F:
		break;
	}
	BS_Seek(demux->bs, header.start_offset + header.accessUnitLength);				
	if (demux->bitlog) fflush(demux->bitlog);

	return 1;
}
*/