/*
 *			GPAC - MPEG-4 Systems C Development Kit
 *
 *			Copyright (c) Jean Le Feuvre 2000-2005 
 *					All rights reserved
 *
 *  This file is part of GPAC / ISO Media File Format sub-project
 *
 *  GPAC is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *   
 *  GPAC is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *   
 *  You should have received a copy of the GNU General Public License
 *  along with GNU Make; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
 *
 */

#include <gpac/intern/m4_isomedia_dev.h>

void InitVideoSampleEntry(VisualSampleEntryAtom *ent)
{
	ent->horiz_res = ent->vert_res = 0x00480000;
	ent->frames_per_sample = 1;
	ent->bit_depth = 0x18;
	ent->color_table_index = -1;
}

void ReadVideoSampleEntry(VisualSampleEntryAtom *ptr, BitStream *bs, u64 *read)
{
	BS_ReadData(bs, (unsigned char*)ptr->reserved, 6);
	ptr->dataReferenceIndex = BS_ReadU16(bs);
	ptr->version = BS_ReadU16(bs);
	ptr->revision = BS_ReadU16(bs);
	ptr->vendor = BS_ReadU32(bs);
	ptr->temporal_quality  = BS_ReadU32(bs);
	ptr->spacial_quality = BS_ReadU32(bs);
	ptr->Width = BS_ReadU16(bs);
	ptr->Height = BS_ReadU16(bs);
	ptr->horiz_res = BS_ReadU32(bs);
	ptr->vert_res = BS_ReadU32(bs);
	ptr->entry_data_size = BS_ReadU32(bs);
	ptr->frames_per_sample = BS_ReadU16(bs);
	BS_ReadData(bs, ptr->compressor_name, 32);
	ptr->compressor_name[32] = 0;
	ptr->bit_depth = BS_ReadU16(bs);
	ptr->color_table_index = BS_ReadU16(bs);
	*read += 78;
}

#ifndef M4_READ_ONLY
void WriteVideoSampleEntry(VisualSampleEntryAtom *ptr, BitStream *bs)
{

	BS_WriteData(bs, (unsigned char*)ptr->reserved, 6);
	BS_WriteU16(bs, ptr->dataReferenceIndex);
	
	BS_WriteU16(bs, ptr->version);
	BS_WriteU16(bs, ptr->revision);
	BS_WriteU32(bs, ptr->vendor);
	BS_WriteU32(bs, ptr->temporal_quality);
	BS_WriteU32(bs, ptr->spacial_quality);
	BS_WriteU16(bs, ptr->Width);
	BS_WriteU16(bs, ptr->Height);
	BS_WriteU32(bs, ptr->horiz_res);
	BS_WriteU32(bs, ptr->vert_res);
	BS_WriteU32(bs, ptr->entry_data_size);
	BS_WriteU16(bs, ptr->frames_per_sample);
	BS_WriteData(bs, ptr->compressor_name, 32);
	BS_WriteU16(bs, ptr->bit_depth);
	BS_WriteU16(bs, ptr->color_table_index);
}

void SizeVideoSampleEntry(VisualSampleEntryAtom *ent)
{
	ent->size += 78;
}


#endif



void InitAudioSampleEntry(AudioSampleEntryAtom *ptr)
{
	ptr->channel_count = 2;
	ptr->bitspersample = 16;
}

void ReadAudioSampleEntry(AudioSampleEntryAtom *ptr, BitStream *bs, u64 *read)
{
	BS_ReadData(bs, (unsigned char*)ptr->reserved, 6);
	ptr->dataReferenceIndex = BS_ReadU16(bs);
	ptr->version = BS_ReadU16(bs);
	ptr->revision = BS_ReadU16(bs);
	ptr->vendor = BS_ReadU32(bs);
	ptr->channel_count = BS_ReadU16(bs);
	ptr->bitspersample = BS_ReadU16(bs);
	ptr->compression_id = BS_ReadU16(bs);
	ptr->packet_size = BS_ReadU16(bs);
	ptr->samplerate_hi = BS_ReadU16(bs);
	ptr->samplerate_lo = BS_ReadU16(bs);
	*read += 28;
}

#ifndef M4_READ_ONLY

void WriteAudioSampleEntry(AudioSampleEntryAtom *ptr, BitStream *bs)
{
	BS_WriteData(bs, (unsigned char*) ptr->reserved, 6);
	BS_WriteU16(bs, ptr->dataReferenceIndex);

	BS_WriteU16(bs, ptr->version);
	BS_WriteU16(bs, ptr->revision);
	BS_WriteU32(bs, ptr->vendor);
	BS_WriteU16(bs, ptr->channel_count);
	BS_WriteU16(bs, ptr->bitspersample);
	BS_WriteU16(bs, ptr->compression_id);
	BS_WriteU16(bs, ptr->packet_size);
	BS_WriteU16(bs, ptr->samplerate_hi);
	BS_WriteU16(bs, ptr->samplerate_lo);
}

void SizeAudioSampleEntry(AudioSampleEntryAtom *ptr)
{
	ptr->size += 28;
}


#endif	//M4_READ_ONLY
