1 /*
2 -----------------------------------------------------------------------------
3 This source file is part of OGRE
4 (Object-oriented Graphics Rendering Engine)
5 For the latest info, see http://www.ogre3d.org/
6 
7 Copyright (c) 2000-2013 Torus Knot Software Ltd
8 
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15 
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 THE SOFTWARE.
26 -----------------------------------------------------------------------------
27 */
28 #ifndef _Codec_H__
29 #define _Codec_H__
30 
31 #include "OgrePrerequisites.h"
32 #include "OgreSharedPtr.h"
33 #include "OgreDataStream.h"
34 #include "OgreIteratorWrappers.h"
35 #include "OgreStringVector.h"
36 #include "OgreException.h"
37 #include "OgreHeaderPrefix.h"
38 
39 namespace Ogre {
40 	/** \addtogroup Core
41 	*  @{
42 	*/
43 	/** \addtogroup General
44 	*  @{
45 	*/
46 
47     /** Abstract class that defines a 'codec'.
48         @remarks
49             A codec class works like a two-way filter for data - data entered on
50             one end (the decode end) gets processed and transformed into easily
51             usable data while data passed the other way around codes it back.
52         @par
53             The codec concept is a pretty generic one - you can easily understand
54             how it can be used for images, sounds, archives, even compressed data.
55     */
56 	class _OgreExport Codec : public CodecAlloc
57     {
58     protected:
59         typedef map< String, Codec* >::type CodecList;
60         /** A map that contains all the registered codecs.
61         */
62         static CodecList msMapCodecs;
63 
64     public:
65         class _OgrePrivate CodecData : public CodecAlloc
66         {
67         public:
~CodecData()68             virtual ~CodecData() {}
69 
70             /** Returns the type of the data.
71             */
dataType()72             virtual String dataType() const { return "CodecData"; }
73         };
74         typedef SharedPtr<CodecData> CodecDataPtr;
75 
76         typedef ConstMapIterator<CodecList> CodecIterator;
77 
78     public:
79     	virtual ~Codec();
80 
81         /** Registers a new codec in the database.
82         */
registerCodec(Codec * pCodec)83         static void registerCodec( Codec *pCodec )
84         {
85 			CodecList::iterator i = msMapCodecs.find(pCodec->getType());
86 			if (i != msMapCodecs.end())
87 				OGRE_EXCEPT(Exception::ERR_DUPLICATE_ITEM,
88 					pCodec->getType() + " already has a registered codec. ", __FUNCTION__);
89 
90             msMapCodecs[pCodec->getType()] = pCodec;
91         }
92 
93 		/** Return whether a codec is registered already.
94 		*/
isCodecRegistered(const String & codecType)95 		static bool isCodecRegistered( const String& codecType )
96 		{
97 			return msMapCodecs.find(codecType) != msMapCodecs.end();
98 		}
99 
100 		/** Unregisters a codec from the database.
101         */
unregisterCodec(Codec * pCodec)102         static void unregisterCodec( Codec *pCodec )
103         {
104             msMapCodecs.erase(pCodec->getType());
105         }
106 
107         /** Gets the iterator for the registered codecs. */
getCodecIterator(void)108         static CodecIterator getCodecIterator(void)
109         {
110             return CodecIterator(msMapCodecs.begin(), msMapCodecs.end());
111         }
112 
113         /** Gets the file extension list for the registered codecs. */
114         static StringVector getExtensions(void);
115 
116         /** Gets the codec registered for the passed in file extension. */
117         static Codec* getCodec(const String& extension);
118 
119 		/** Gets the codec that can handle the given 'magic' identifier.
120 		@param magicNumberPtr Pointer to a stream of bytes which should identify the file.
121 			Note that this may be more than needed - each codec may be looking for
122 			a different size magic number.
123 		@param maxbytes The number of bytes passed
124 		*/
125 		static Codec* getCodec(char *magicNumberPtr, size_t maxbytes);
126 
127         /** Codes the data in the input stream and saves the result in the output
128             stream.
129         */
130         virtual DataStreamPtr encode(MemoryDataStreamPtr& input, CodecDataPtr& pData) const = 0;
131         /** Codes the data in the input chunk and saves the result in the output
132             filename provided. Provided for efficiency since coding to memory is
133             progressive therefore memory required is unknown leading to reallocations.
134         @param input The input data
135         @param outFileName The filename to write to
136         @param pData Extra information to be passed to the codec (codec type specific)
137         */
138         virtual void encodeToFile(MemoryDataStreamPtr& input, const String& outFileName, CodecDataPtr& pData) const = 0;
139 
140         /// Result of a decoding; both a decoded data stream and CodecData metadata
141         typedef std::pair<MemoryDataStreamPtr, CodecDataPtr> DecodeResult;
142         /** Codes the data from the input chunk into the output chunk.
143             @param input Stream containing the encoded data
144         */
145         virtual DecodeResult decode(DataStreamPtr& input) const = 0;
146 
147         /** Returns the type of the codec as a String
148         */
149         virtual String getType() const = 0;
150 
151         /** Returns the type of the data that supported by this codec as a String
152         */
153         virtual String getDataType() const = 0;
154 
155 		/** Returns whether a magic number header matches this codec.
156 		@param magicNumberPtr Pointer to a stream of bytes which should identify the file.
157 			Note that this may be more than needed - each codec may be looking for
158 			a different size magic number.
159 		@param maxbytes The number of bytes passed
160 		*/
magicNumberMatch(const char * magicNumberPtr,size_t maxbytes)161 		virtual bool magicNumberMatch(const char *magicNumberPtr, size_t maxbytes) const
162 		{ return !magicNumberToFileExt(magicNumberPtr, maxbytes).empty(); }
163 		/** Maps a magic number header to a file extension, if this codec recognises it.
164 		@param magicNumberPtr Pointer to a stream of bytes which should identify the file.
165 			Note that this may be more than needed - each codec may be looking for
166 			a different size magic number.
167 		@param maxbytes The number of bytes passed
168 		@return A blank string if the magic number was unknown, or a file extension.
169 		*/
170 		virtual String magicNumberToFileExt(const char *magicNumberPtr, size_t maxbytes) const = 0;
171     };
172 	/** @} */
173 	/** @} */
174 
175 } // namespace
176 
177 #include "OgreHeaderSuffix.h"
178 
179 #endif
180