[cosmetics] update date in GPL header
[vuplus_xbmc] / xbmc / cores / paplayer / CodecFactory.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://www.xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #include "system.h"
22 #include "CodecFactory.h"
23 #include "MP3codec.h"
24 #include "CDDAcodec.h"
25 #include "OGGcodec.h"
26 #include "FLACcodec.h"
27 #include "WAVcodec.h"
28 #include "ModplugCodec.h"
29 #include "NSFCodec.h"
30 #ifdef HAS_SPC_CODEC
31 #include "SPCCodec.h"
32 #endif
33 #include "SIDCodec.h"
34 #include "VGMCodec.h"
35 #include "YMCodec.h"
36 #include "ADPCMCodec.h"
37 #include "TimidityCodec.h"
38 #ifdef HAS_ASAP_CODEC
39 #include "ASAPCodec.h"
40 #endif
41 #include "URL.h"
42 #include "DVDPlayerCodec.h"
43 #include "PCMCodec.h"
44
45 ICodec* CodecFactory::CreateCodec(const CStdString& strFileType)
46 {
47   if (strFileType.Equals("mp3") || strFileType.Equals("mp2"))
48     return new MP3Codec();
49   else if (strFileType.Equals("pcm") || strFileType.Equals("l16"))
50     return new PCMCodec();
51   else if (strFileType.Equals("ape") || strFileType.Equals("mac"))
52     return new DVDPlayerCodec();
53   else if (strFileType.Equals("cdda"))
54     return new CDDACodec();
55   else if (strFileType.Equals("mpc") || strFileType.Equals("mp+") || strFileType.Equals("mpp"))
56     return new DVDPlayerCodec();
57   else if (strFileType.Equals("shn"))
58     return new DVDPlayerCodec();
59   else if (strFileType.Equals("mka"))
60     return new DVDPlayerCodec();
61   else if (strFileType.Equals("flac"))
62     return new FLACCodec();
63   else if (strFileType.Equals("wav"))
64     return new DVDPlayerCodec();
65   else if (strFileType.Equals("dts") || strFileType.Equals("ac3") ||
66            strFileType.Equals("m4a") || strFileType.Equals("aac") ||
67            strFileType.Equals("pvr"))
68     return new DVDPlayerCodec();
69   else if (strFileType.Equals("wv"))
70     return new DVDPlayerCodec();
71   else if (strFileType.Equals("669")  ||  strFileType.Equals("abc") ||
72            strFileType.Equals("amf")  ||  strFileType.Equals("ams") ||
73            strFileType.Equals("dbm")  ||  strFileType.Equals("dmf") ||
74            strFileType.Equals("dsm")  ||  strFileType.Equals("far") ||
75            strFileType.Equals("it")   ||  strFileType.Equals("j2b") ||
76            strFileType.Equals("mdl")  ||  strFileType.Equals("med") ||
77            strFileType.Equals("mod")  ||  strFileType.Equals("itgz")||
78            strFileType.Equals("mt2")  ||  strFileType.Equals("mtm") ||
79            strFileType.Equals("okt")  ||  strFileType.Equals("pat") ||
80            strFileType.Equals("psm")  ||  strFileType.Equals("ptm") ||
81            strFileType.Equals("s3m")  ||  strFileType.Equals("stm") ||
82            strFileType.Equals("ult")  ||  strFileType.Equals("umx") ||
83            strFileType.Equals("xm")   || strFileType.Equals("mdgz") ||
84            strFileType.Equals("s3gz") || strFileType.Equals("xmgz"))
85     return new ModplugCodec();
86   else if (strFileType.Equals("nsf") || strFileType.Equals("nsfstream"))
87     return new NSFCodec();
88 #ifdef HAS_SPC_CODEC
89   else if (strFileType.Equals("spc"))
90     return new SPCCodec();
91 #endif
92   else if (strFileType.Equals("sid") || strFileType.Equals("sidstream"))
93     return new SIDCodec();
94   else if (VGMCodec::IsSupportedFormat(strFileType))
95     return new VGMCodec();
96   else if (strFileType.Equals("ym"))
97     return new YMCodec();
98   else if (strFileType.Equals("wma"))
99     return new DVDPlayerCodec();
100   else if (strFileType.Equals("aiff") || strFileType.Equals("aif"))
101     return new DVDPlayerCodec();
102   else if (strFileType.Equals("xwav"))
103     return new ADPCMCodec();
104   else if (TimidityCodec::IsSupportedFormat(strFileType))
105     return new TimidityCodec();
106 #ifdef HAS_ASAP_CODEC
107   else if (ASAPCodec::IsSupportedFormat(strFileType) || strFileType.Equals("asapstream"))
108     return new ASAPCodec();
109 #endif
110   else if (strFileType.Equals("tta"))
111     return new DVDPlayerCodec();
112
113   return NULL;
114 }
115
116 ICodec* CodecFactory::CreateCodecDemux(const CStdString& strFile, const CStdString& strContent, unsigned int filecache)
117 {
118   CURL urlFile(strFile);
119   if( strContent.Equals("audio/mpeg")
120   ||  strContent.Equals("audio/mp3") )
121     return new MP3Codec();
122   else if (strContent.Left(9).Equals("audio/l16"))
123   {
124     PCMCodec * pcm_codec = new PCMCodec();
125     pcm_codec->SetMimeParams(strContent);
126     return pcm_codec;
127   }
128   else if( strContent.Equals("audio/aac")
129     || strContent.Equals("audio/aacp") )
130   {
131     DVDPlayerCodec *pCodec = new DVDPlayerCodec;
132     if (urlFile.GetProtocol() == "shout" )
133       pCodec->SetContentType(strContent);
134     return pCodec;
135   }
136   else if( strContent.Equals("audio/x-ms-wma") )
137     return new DVDPlayerCodec();
138   else if( strContent.Equals("audio/x-ape") || strContent.Equals("audio/ape") )
139     return new DVDPlayerCodec();
140   else if( strContent.Equals("application/ogg") || strContent.Equals("audio/ogg"))
141     return CreateOGGCodec(strFile,filecache);
142   else if (strContent.Equals("audio/x-xbmc-pcm"))
143   {
144     // audio/x-xbmc-pcm this is the used codec for AirTunes
145     // (apples audio only streaming)
146     DVDPlayerCodec *dvdcodec = new DVDPlayerCodec();
147     dvdcodec->SetContentType(strContent);
148     return dvdcodec;
149   }
150   else if (strContent.Equals("audio/flac") || strContent.Equals("audio/x-flac") || strContent.Equals("application/x-flac"))
151     return new FLACCodec();
152
153   if (urlFile.GetProtocol() == "lastfm" || urlFile.GetProtocol() == "shout")
154   {
155     return new MP3Codec(); // if we got this far with internet radio - content-type was wrong. gamble on mp3.
156   }
157
158   if (urlFile.GetFileType().Equals("wav") || strContent.Equals("audio/wav") || strContent.Equals("audio/x-wav"))
159   {
160     ICodec* codec;
161     //lets see what it contains...
162     //this kinda sucks 'cause if it's a plain wav file the file
163     //will be opened, sniffed and closed 2 times before it is opened *again* for wav
164     //would be better if the papcodecs could work with bitstreams instead of filenames.
165     DVDPlayerCodec *dvdcodec = new DVDPlayerCodec();
166     dvdcodec->SetContentType("audio/x-spdif-compressed");
167     if (dvdcodec->Init(strFile, filecache))
168     {
169       return dvdcodec;
170     }
171     delete dvdcodec;
172     codec = new ADPCMCodec();
173     if (codec->Init(strFile, filecache))
174     {
175       return codec;
176     }
177     delete codec;
178
179     codec = new WAVCodec();
180     if (codec->Init(strFile, filecache))
181     {
182       return codec;
183     }
184     delete codec;
185   }
186   if (urlFile.GetFileType().Equals("cdda"))
187   {
188     //lets see what it contains...
189     //this kinda sucks 'cause if it's plain cdda the file
190     //will be opened, sniffed and closed before it is opened *again* for cdda
191     //would be better if the papcodecs could work with bitstreams instead of filenames.
192     DVDPlayerCodec *dvdcodec = new DVDPlayerCodec();
193     dvdcodec->SetContentType("audio/x-spdif-compressed");
194     if (dvdcodec->Init(strFile, filecache))
195     {
196       return dvdcodec;
197     }
198     delete dvdcodec;
199   }
200   else if (urlFile.GetFileType().Equals("ogg") || urlFile.GetFileType().Equals("oggstream") || urlFile.GetFileType().Equals("oga"))
201     return CreateOGGCodec(strFile,filecache);
202
203   //default
204   return CreateCodec(urlFile.GetFileType());
205 }
206
207 ICodec* CodecFactory::CreateOGGCodec(const CStdString& strFile,
208                                      unsigned int filecache)
209 {
210   // oldnemesis: we want to use OGGCodec() for OGG music since unlike DVDCodec 
211   // it provides better timings for Karaoke. However OGGCodec() cannot handle 
212   // ogg-flac and ogg videos, that's why this block.
213   ICodec* codec = new OGGCodec();
214   try
215   {
216     if (codec->Init(strFile, filecache))
217       return codec;
218   }
219   catch( ... )
220   {
221   }
222   delete codec;
223   return new DVDPlayerCodec();
224 }
225