/*
 *			GPAC - MPEG-4 Systems C Development Kit
 *
 *			Copyright (c) Cyril Concolato 2004
 *					All rights reserved
 *
 *  This file is part of GPAC / SVG Loader plugin
 *
 *  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_esm_dev.h>
#include "./svg_parser/svg_parser_dev.h"

#ifndef M4_DISABLE_SVG

/*************************************************************************
 *																	 	 *
 * Functions implementing the Basic Decoder and Scene Decoder interfaces *
 *																		 *
 *************************************************************************/
static M4Err SVG_ProcessData(SceneDecoder *plug, unsigned char *inBuffer, u32 inBufferLength, 
								u16 ES_ID, u32 stream_time, u32 mmlevel)
{
	M4Err e = M4OK;
	SVGParser *parser = plug->privateStack;

	if (parser->status == 0) {
		parser->status = 1;
		e = SVGParser_Parse(parser);
		if (!e) {
			SG_SetSizeInfo(parser->graph, parser->svg_w, parser->svg_h, 1);
			/*attach graph to renderer*/
			IS_AttachGraphToRender(parser->inline_scene);
			SVGParser_Terminate(parser);
			plug->privateStack = NULL;
		} else {
			parser->status = 0;
			return e;
		}
	}
	return M4EOF;
}

static M4Err SVG_AttachScene(SceneDecoder *plug, InlineScene *scene, Bool is_scene_decoder)
{
	SVGParser *parser = plug->privateStack;
	parser->inline_scene = scene;
	parser->graph = scene->graph;
	return M4OK;
}

static M4Err SVG_ReleaseScene(SceneDecoder *plug)
{
	return M4OK;
}


static M4Err SVG_AttachStream(BaseDecoder *plug, 
									 u16 ES_ID, 
									 unsigned char *decSpecInfo, 
									 u32 decSpecInfoSize, 
									 u16 DependsOnES_ID,
									 u32 objectTypeIndication, 
									 Bool Upstream)
{
	SVGParser *parser = plug->privateStack;
	if (Upstream) return M4NotSupported;

	/*main dummy stream we need a dsi*/
	if (!decSpecInfo) return M4NonCompliantBitStream;

	parser->fileName = strdup(decSpecInfo);
	return M4OK;
}

static M4Err SVG_DetachStream(BaseDecoder *plug, u16 ES_ID)
{
	return M4OK;
}

const char *SVG_GetName(struct _basedecoder *plug)
{
	return "GPAC SVG Parser";
}

Bool SVG_CanHandleStream(BaseDecoder *ifce, u32 StreamType, u32 ObjectType, unsigned char *decSpecInfo, u32 decSpecInfoSize, u32 PL)
{
	if ((StreamType==M4ST_PRIVATE_SCENE) && (ObjectType==2)) return 1;
	return 0;
}

static M4Err SVG_GetCapabilities(BaseDecoder *plug, CapObject *cap)
{
	cap->cap.valueInt = 0;
	return M4NotSupported;
}

static M4Err SVG_SetCapabilities(BaseDecoder *plug, const CapObject capability)
{
	return M4OK;
}

/*interface create*/
void *LoadInterface(u32 InterfaceType)
{
	SVGParser *parser;
	SceneDecoder *sdec;
	if (InterfaceType != M4SCENEDECODERINTERFACE) return NULL;
	
	SAFEALLOC(sdec, sizeof(SceneDecoder))
	M4_REG_PLUG(sdec, M4SCENEDECODERINTERFACE, "GPAC SVG Parser", "gpac distribution", 0);

	parser = NewSVGParser();

	sdec->privateStack = parser;
#ifdef USE_GPAC_CACHE_MECHANISM
	parser->sdec = sdec;
#endif
	sdec->AttachStream = SVG_AttachStream;
	sdec->CanHandleStream = SVG_CanHandleStream;
	sdec->DetachStream = SVG_DetachStream;
	sdec->AttachScene = SVG_AttachScene;
	sdec->ReleaseScene = SVG_ReleaseScene;
	sdec->ProcessData = SVG_ProcessData;
	sdec->GetName = SVG_GetName;
	sdec->SetCapabilities = SVG_SetCapabilities;
	sdec->GetCapabilities = SVG_GetCapabilities;
	return sdec;
}


/*interface destroy*/
void ShutdownInterface(void *ifce)
{
	SceneDecoder *sdec = (SceneDecoder *)ifce;
	if (sdec->InterfaceType != M4SCENEDECODERINTERFACE) return;
	if (sdec->privateStack) SVGParser_Terminate((SVGParser *)sdec->privateStack);
	sdec->privateStack = NULL;
	free(sdec);
}

/*interface query*/
Bool QueryInterface(u32 InterfaceType)
{
	if (InterfaceType == M4SCENEDECODERINTERFACE) return 1;
	return 0;
}
#else


/*interface create*/
void *LoadInterface(u32 InterfaceType)
{
	return NULL;
}


/*interface destroy*/
void ShutdownInterface(void *ifce)
{
}

/*interface query*/
Bool QueryInterface(u32 InterfaceType)
{
	return 0;
}
#endif
