/*********************************************************************
*	COggVideoPlayer
*	HEADER FILE
*	Autor:	Michal Jirouš
*	Datum: 16.7.2008
*	Soubor: oggvideoplayer.h
*	Popis: Prehravac ogg video souboru
**********************************************************************/

#ifndef __OGG_VIDEO_PLAYER_H__
#define __OGG_VIDEO_PLAYER_H__

#include "theora/theora.h"
#include <SDL/SDL.h>

class COggVideoPlayer
{
	unsigned int m_uiVideoWidth, m_uiVideoHeight;
	unsigned char *m_pVideoData;
	
	SDL_Rect m_Rect;
	SDL_Surface		*m_pMainSurface;
	SDL_Overlay     *m_pYUV_overlay;

	FILE *infile;

	ogg_sync_state    m_OggSyncState;
	ogg_page          m_OggPage;

	ogg_stream_state  m_OggStreamState;
	theora_info       m_TheoraInfo;
	theora_comment    m_TheoraComment;
	theora_state      m_TheoraState;

	ogg_packet m_OggPacket;

	int              theora_p;
	int              stateflag;

	/* single frame video buffering */
	int          videobuf_ready;
	ogg_int64_t  videobuf_granulepos;
	double       videobuf_time;


	int queue_page( ogg_page *page );
	int buffer_data( FILE *in, ogg_sync_state *m_OggSyncState );

	/* dump the theora comment header */
	int dump_comments( theora_comment *m_TheoraComment );

	void video_write(void);
public:
	
	COggVideoPlayer();
	void setMainSurface( SDL_Surface *surface )
	{ 
		m_pMainSurface = surface;
		SDL_UpdateRect( m_pMainSurface, 0, 0, m_pMainSurface->w, m_pMainSurface->h );
	}
	bool loadFile( const char * filename );
	bool update();		//nacte dalsi frame ze souboru
	unsigned int getWidth()	{ return m_uiVideoWidth;	}
	unsigned int getHeight()	{ return m_uiVideoHeight;	}
	float getFps()	{ return ( (float)m_TheoraInfo.fps_numerator / m_TheoraInfo.fps_denominator); }
	void reset();
	void draw();
	~COggVideoPlayer()	{ m_pMainSurface = NULL; reset(); }
};


#endif
